[Solved]Shot Arrow and Make Arena

Discussion in 'Spigot Plugin Development' started by cadox8, May 18, 2015.

  1. Hi, I'm new in game developing and I don't know how make this two things:
    1) Shot an arrow by clicking in an Anvil.
    2) Make the Arena code with two teams ands Kits.

    Can someone help me with this things, please? I'm trying make a new game based in one TV show that I love.
     
  2. What have you tried so far?
     
    • Agree Agree x 1
  3. Use the PlayerInteractEvent and if the player right click the block, just throw an arrow.
    There are a lot of tutorials about how create an arena with temas, just search :D
     
  4. I tried a lot! And I try with the PlayerInteractEvent in an especial class (Items) and in Main, but It doesn't work! xD
     
  5. konsolas

    Supporter

    Create an arena around the location returned by ProjectileHitEvent.
     
  6. Code (Text):
    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent e) {
    if (e.getAction() == Action.RIGHT_CLICK_BLOCK || e.getAction() == Action.RIGHT_CLICK_AIR) {
    if (e.getPlayer().getItemInHand() == <Your ItemStack>) {
    e.setCancelled(true);

    //Your code goes here!
    }
    }
    }
    It's easy!
     
  7. The second if-statement will fail because you are comparing references which clearly won't point to the same object.
     
    • Agree Agree x 1
  8. Yeah xD, sorry
    PHP:
    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent e) {
    if (e.getAction() == Action.RIGHT_CLICK_BLOCK || e.getAction() == Action.RIGHT_CLICK_AIR) {
    if (e.getItem() != null) {
    if (e.getItem().equals(<Your Item>)) {
    e.setCancelled(true);

    //Code
    }
    }
    }
    }
     
    • Like Like x 1
  9. I've tried it but don't work. My code:
    PHP:
        public void onPlayerInteract(PlayerInteractEvent e){
            Action a = e.getAction();
            Player p = e.getPlayer();

            if (e.getAction() == Action.RIGHT_CLICK_BLOCK || e.getAction() == Action.RIGHT_CLICK_AIR) {
                if (e.getItem() != null) {
                    if (e.getItem().equals("Laser Arrow")) {
                        shotArrow(p);
                    }
                }
            } else if (e.getAction() == Action.RIGHT_CLICK_BLOCK || e.getAction() == Action.RIGHT_CLICK_AIR) {
                if (e.getItem() != null) {
                    if (e.getItem().equals("Vehicle")) {
                        p.sendMessage("Work");
                    }
                }
            } else if (e.getAction() == Action.RIGHT_CLICK_BLOCK || e.getAction() == Action.RIGHT_CLICK_AIR) {
                if (e.getItem() != null) {
                    if (e.getItem().equals("Soon...")) {
                        p.sendMessage("Work");
                    }
                }
            }
        }
    PHP:
    package com.gmail.cadox8.cl.kits;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.Sound;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Arrow;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;

    import com.gmail.cadox8.cl.Main;

    public class Odd implements CommandExecutor, Listener{

        private Main plugin;

        public Odd(Main Main)
        {
            this.plugin = Main;
        }

        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
            if ((cmd.getName().equalsIgnoreCase("odd")) && ((sender instanceof Player))){
                Player p = (Player)sender;
                if ((args.length == 0) && (!this.plugin.odd.contains(p.getName()))){

                    //Items
                    ItemStack fl = new ItemStack(Material.ANVIL);
                    ItemMeta wfl = fl.getItemMeta();
                    wfl.setDisplayName(ChatColor.BOLD.toString() + ChatColor.GREEN + "Laser Arrow");
                    fl.setItemMeta(wfl);

                    ItemStack v = new ItemStack(Material.WOOD_HOE);
                    ItemMeta wv = v.getItemMeta();
                    wv.setDisplayName(ChatColor.BOLD.toString() + ChatColor.GRAY + "Vehicle");
                    v.setItemMeta(wv);

                    ItemStack a = new ItemStack(Material.COAL);
                    ItemMeta wa = a.getItemMeta();
                    wa.setDisplayName(ChatColor.BOLD.toString() + ChatColor.AQUA + "Soon...");
                    a.setItemMeta(wa);

                    //Add Kit
                    this.plugin.clearKits(p);
                    if (!this.plugin.odd.contains(p.getName()))
                    {
                        this.plugin.odd.add(p.getName());
                        this.plugin.exitGame(p);


                        //Give Items
                        p.getInventory().setItem(0, new ItemStack(fl));
                        p.getInventory().setItem(3, new ItemStack(v));
                        p.getInventory().setItem(5, new ItemStack(a));

                        //Skills
                        p.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 9999999, 0));

                        //Other Funny Things :D
                        p.setHealthScale(20.0D);
                        p.playSound(p.getLocation(), Sound.LEVEL_UP, 3.0F, 2.0F);
                        p.updateInventory();
                        p.sendMessage(this.plugin.prefix + ChatColor.DARK_PURPLE + " You're Odd");
                    }
                }
            }
            return false;
        }

        @SuppressWarnings("unused")
        public void onPlayerInteract(PlayerInteractEvent e){
            Action a = e.getAction();
            Player p = e.getPlayer();

            if (e.getAction() == Action.RIGHT_CLICK_BLOCK || e.getAction() == Action.RIGHT_CLICK_AIR) {
                if (e.getItem() != null) {
                    if (e.getItem().equals("Laser Arrow")) {
                        shotArrow(p);
                    }
                }
            } else if (e.getAction() == Action.RIGHT_CLICK_BLOCK || e.getAction() == Action.RIGHT_CLICK_AIR) {
                if (e.getItem() != null) {
                    if (e.getItem().equals("Vehicle")) {
                        p.sendMessage("Work");
                    }
                }
            } else if (e.getAction() == Action.RIGHT_CLICK_BLOCK || e.getAction() == Action.RIGHT_CLICK_AIR) {
                if (e.getItem() != null) {
                    if (e.getItem().equals("Soon...")) {
                        p.sendMessage("Work");
                    }
                }
            }
        }

        public void shotArrow(final Player p){
            Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable(){
                public void run() {
                    p.launchProjectile(Arrow.class);
                }
            }, 60L);
        }
    }

    Anyone can help me, please?
     
  10. It doesn't work says nothing. Debug your code and find the line were it doesn't work. If there are errors post them.
     
  11. I can't post errors because there aren't. If you check the console, you don't see nothing, but in-game, the event don't work. And if you put debug lines (p.send message("Code Work")), on Right click in an item, don't show nothing
     
  12. Have you registered the event?
     
  13. Code (Text):
     if (e.getItem().equals("Laser Arrow")) {

    }
    .. e.getItem() returns an ItemStack object, and "Laser Arrow" is a String object. Those will never ever be equal.
    Instead you will have to check if the item has ItemMeta, if that ItemMeta has an displayName, and then check if that displayName equalsIgnoreCase's the String you want to check.
    Also you don't have to triple check the action of the event, one time is enough..

    I really don't want to be mean, but it looks like you have no idea what you are doing. My advise is to follow some regular Java tutorials before trying anything like this.
     
  14. You probably forget to
    * register your event
    * use the EventHandler annotation

    I suggest reading bukkit's begging tutorials.
     
  15. Did you make sure to put in the register event method at the top of your code? I tried getting that to work with my plugin to read player uuids and paste them into a config. I WAS MISSING JUST THAT.. the PlayerJoinEvent was never read because of it xD.
     
  16. The method 'e.getItem()' returns a ItemStack, not a String... If you want to do the action if the display name equals to a specific String, just take the ItemMeta and then compare.
     
  17. Like this? And the best way to learn is ask and test :D
    PHP:
    public class IOdd implements Listener{

        private Main plugin;

        public IOdd(Main Main)
        {
            this.plugin = Main;
            this.plugin.getServer().getPluginManager().registerEvents(this, this.plugin);
        }

        public void onPlayerInteract(PlayerInteractEvent e){
            Player p = e.getPlayer();

            if (e.getAction() == Action.RIGHT_CLICK_BLOCK || e.getAction() == Action.RIGHT_CLICK_AIR) {
                if (e.getItem().equals(Material.ANVIL)) {
                    shotArrow(p);
                }
            }
            if (e.getAction() == Action.RIGHT_CLICK_BLOCK || e.getAction() == Action.RIGHT_CLICK_AIR) {
                if (e.getItem().equals(Material.WOOD_HOE)) {
                    p.sendMessage("Work");
                }
            }
            if (e.getAction() == Action.RIGHT_CLICK_BLOCK || e.getAction() == Action.RIGHT_CLICK_AIR) {
                if (e.getItem().equals(Material.COAL)) {
                    p.sendMessage("Work");
                }
            }
        }

        public void shotArrow(Player p){
            p.launchProjectile(Arrow.class);
            //        Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable(){
            //            public void run() {
            //                p.launchProjectile(Arrow.class);
            //            }
            //        }, 60L);
        }
    }
     
     
  18. Getting close, but still not quite there. Here are some pointers:
    - Don't forget the @EventHandler annotation above your interact method, otherwise CraftBukkit won't know it should call that method
    - Your equals() useage is not correct. You are comparing an ItemStack object with an Material instance. Those will never be equal.
    ItemStack#getType() returns it's Material, which you can then use to compare.
    In this case, because Material is an enum you can use == to check for equality instead of equals(). If you want to know why do a google search for the difference between equals and ==.
    Code (Text):
    if(e.getItem().getType() == Material.COAL) {
      // code
    }
    - You do not have to check the action three times, instead check it once, and have all the other (material) checks inside that if-block
    Code (Text):
    if(e.getAction() == myAction) {
        if(materialcheck) {

        }
        else if(other materialcheck) {

        }
    }
    Hope this helps
     
  19. It's preferred to do the action check separately and return when it's not valid
    Code (Java):
    if (event.getAction() != myAction)
    {
        return;
    }
    // Here, the action is always myAction
    if (materialcheck)
    {
        foo();
    }
    else if (other materialcheck)
    {
        bar();
    }
     
  20. The Event it's working! Thanks!