Solved EntityTargetEvent Problem.

Discussion in 'Spigot Plugin Development' started by Nosmakos, May 25, 2016.

Thread Status:
Not open for further replies.
  1. Hey guys i have made a same post months ago but i haven't tried how to do that yet so now i'm testing it and i have a bit problems.
    This is my full code of the entity target meaning i want when a player is wearing a golden_chestplate to cancel the event so zombies cannot see him but when i made the first i thought that they can have unlimited time to kill zombies without even see the players so i thought i could make a task but seems to not be working. I just getting an error (EntityTargetEvent it's 100% working) the problem is in the InventoryClickEvent. Task is not running. What i want is when someone for example is wearing it for 10 minutes to destroy the golden chestplate this is what i wanna do.
    The error is this:

    Code (Text):
    if (p.getInventory().getItem(315) == null){
    Events for the target:

    Code (Text):
        @EventHandler
        public void onGutsTarget(EntityTargetEvent e){
            Entity z = e.getEntity();
            Player p = (Player) e.getTarget();
            if (z instanceof Zombie && p instanceof Player){
                if (p.getInventory().getChestplate() != null){
                    if (p.getInventory().getChestplate().getType() == Material.GOLD_CHESTPLATE){
                        e.setCancelled(true);
                    }
                }
            }
        }
     
        @EventHandler
        public void onGutsClick(InventoryClickEvent e){
            BukkitScheduler Scheduler = Bukkit.getServer().getScheduler();
            Scheduler.scheduleSyncRepeatingTask(plugin, new Runnable() {
                @Override
                public void run() {
                    for (Player p : Bukkit.getOnlinePlayers()) {
                        if (p.getInventory().getItem(315) == null){
                            return;
                        }
                        if (p.getInventory().getItem(315).getType() == Material.GOLD_CHESTPLATE){
                            String Guts = p.getInventory().getItem(315).getItemMeta().getDisplayName();
                            if (Guts.contains("Golden Chestplate")) {
                                Guts = Guts.replaceAll("Golden Chestplate", "");
                                Integer timer = Integer.valueOf(Guts);
                                if (timer >= 1) {
                                    timer--;
                                    Guts = "Golden Chestplate" + timer;
                                    ItemMeta cPlate = p.getInventory().getChestplate().getItemMeta();
                                    cPlate.setDisplayName(Guts);
                                    p.getInventory().getChestplate().setItemMeta(cPlate);
                                    return;
                                }
                                if (timer == 0) {
                                    p.getPlayer().getInventory().setChestplate(new ItemStack(Material.AIR));
                                }
                            }
                        }
                    }
                }
            }, 0, 20L);
        }
    }
     
  2. First use proper java conventions. You should change the Guts variable to guts. And I don't quite get what you are trying to do.
     
  3. Oh so you are trying to make a timer? Do a runTaskLater.
     
  4. im trying when someone for example is wearing it for 10 minutes to destroy the golden chestplate this is what i wanna do :/
     
  5. ohh
     
  6. MiniDigger

    Supporter

    this is not a save call. you don't know if that items exists, if it has a item meta and if it has a display name. 3 potential npes in a single line ^^
     
  7. so what's the better way to destroy the item if the player is wearing it for 10 minutes? but i want if he don't wear it for example take it off then to stop and if he wear it to contineue the time to where it was before
     
  8. Try this?

    Code (Text):
    @EventHandler
        public void onGutsTarget(EntityTargetEvent e) {
            Entity z = e.getEntity();
            Player p = (Player) e.getTarget();
            if (z instanceof Zombie && p instanceof Player) {
                if (p.getInventory().getChestplate() != null) {
                    if (p.getInventory().getChestplate().getType() == Material.GOLD_CHESTPLATE) {
                        e.setCancelled(true);
                    }
                }
            }
        }

        @EventHandler
        public void onGutsClick(InventoryClickEvent e) {
            int i = 900 * 20;

            new BukkitRunnable() {

                @Override
                public void run() {
                    for (Player p : Bukkit.getOnlinePlayers()) {
                        if (p.getInventory().getItem(315) == null) {
                            return;
                        }
                        if (p.getInventory().getItem(315).getType() == Material.GOLD_CHESTPLATE) {
                            String Guts = p.getInventory().getItem(315).getItemMeta().getDisplayName();
                            if (Guts.contains("Golden Chestplate")) {
                                Guts = Guts.replaceAll("Golden Chestplate", "");
                                Integer timer = Integer.valueOf(Guts);
                                if (timer >= 1) {
                                    timer--;
                                    Guts = "Golden Chestplate" + timer;
                                    ItemMeta cPlate = p.getInventory().getChestplate().getItemMeta();
                                    cPlate.setDisplayName(Guts);
                                    p.getInventory().getChestplate().setItemMeta(cPlate);
                                    return;
                                }
                                if (i == 0) {
                                    p.getPlayer().getInventory().setChestplate(new ItemStack(Material.AIR));
                                }
                            }
                        }
                    }
                }
            }.runTaskLater(plugin, i);
        }
     
  9. it has no error now but the task still not working :/ armor is still full
    upload_2016-5-25_17-20-56.png
     
  10. Instead of p.getInventory.getChestplate.set thing, do
    Code (Text):
                        p.getInventory().clear(102);
     
     
  11. yeah its not removing the durability of the chestplate this is so weird..the chestplate stays full
     
  12. Try this?
    Code (Text):
                        p.getInventory().getChestplate().setDurability((short) 0);
     
    or this
    Code (Text):
    p.getInventory().getChestplate().setAmount(0);
                       
    or this
    Code (Text):
    p.getInventory().getChestplate().setType(Material.AIR);
     
  13. i replace one of those with the p.getInventory().getChestplate().setItemMeta(cPlate); ?
     
  14. No, you replace p.getInventory.setChestplate(new ItemStack(Material.AIR))
     
  15. Could you show the code you have right now?
     
  16. Code (Text):
        @EventHandler
        public void onGutsTarget(EntityTargetEvent e){
            Entity z = e.getEntity();
            Player p = (Player) e.getTarget();
            if (z instanceof Zombie && p instanceof Player){
                if (p.getInventory().getChestplate() != null){
                    if (p.getInventory().getChestplate().getType() == Material.GOLD_CHESTPLATE){
                        e.setCancelled(true);
                    }
                }
            }
        }
       
        int i = 900 * 20;
       
        @EventHandler
        public void onGutsClick(InventoryClickEvent e) {

            new BukkitRunnable() {

                @Override
                public void run() {
                    for (Player p : Bukkit.getOnlinePlayers()) {
                        if (p.getInventory().getItem(315) == null) {
                            return;
                        }
                        if (p.getInventory().getItem(315).getType() == Material.GOLD_CHESTPLATE) {
                            String guts = p.getInventory().getItem(315).getItemMeta().getDisplayName();
                            if (guts.contains("Golden Chestplate")) {
                                guts = guts.replaceAll("Golden Chestplate", "");
                                Integer timer = Integer.valueOf(guts);
                                if (timer >= 1) {
                                    timer--;
                                    guts = "Golden Chestplate" + timer;
                                    ItemMeta cPlate = p.getInventory().getChestplate().getItemMeta();
                                    cPlate.setDisplayName(guts);
                                    p.getInventory().getChestplate().setItemMeta(cPlate);
                                    return;
                                }
                                if (i == 0) {
                                    p.getInventory().getChestplate().setDurability((short) 0);
                                }
                            }
                        }
                    }
                }
            }.runTaskLater(plugin, i);
        }
    }
    this is what i have but its like the event its not even working lol because the task is not even running
     
  17. What is item 315? I think that's the problem
    EDIT: If that is the chestplate slot, that is wrong. The chestplate slot is 102.
     
  18. 315 its the golden chestplate ID
     
  19. OH
     
  20. yeah but this is weird because the task is not even running ;/
     
Thread Status:
Not open for further replies.