Help with EntityDamageByEntity

Discussion in 'Plugin Development' started by Rotten_Lemons, Oct 14, 2020.

  1. Offline

    Rotten_Lemons

    Sorry for disturbing you guys, but was writing a plugin where when player attacks a mob, they will take the same damage as the mob. When I used to run it, when I hit a mob, it worked and the command responded but not it does not. Here are my classes:

    Code:
    package me.rottenlemons.damage;
    
    
    import org.bukkit.plugin.java.JavaPlugin;
    
    import me.rottenlemons.damage.Events.EventsClass;
    import net.md_5.bungee.api.ChatColor;
    
    public class damage extends JavaPlugin{
    
        public void onEnable(){
            getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "\n\ndamage has been Enabled\n\n");
            getServer().getPluginManager().registerEvents(new EventsClass(), this);
    
    
        }
    
    }
    Code:
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    
    public class EventsClass implements Listener {
    
        @EventHandler
        public void onDamage(EntityDamageByEntityEvent event) {
            damageprotect frame = new damageprotect();
            int p = frame.pro;
            if(event.getDamager() instanceof Player) {
                Player player = (Player) event.getDamager();
                if (player.getHealth()-(event.getDamage()*p)<1) {
                    player.setHealth(0);
                }
                else {
                    player.setHealth(player.getHealth()-(event.getDamage()*p));
    
    
                }
            }
    
        }
    
    }
    
    Code:
    package me.rottenlemons.damage.Events;
    
    import org.bukkit.event.Listener;
    
    import org.bukkit.entity.Player;
    
    import org.bukkit.event.EventHandler;
    import org.bukkit.Material;
    import org.bukkit.inventory.ItemStack;
    public class damageprotect implements Listener {
    
    
        private int protect;
    
        @EventHandler
        public void onEquip(ArmorEquipEvent e) {
            protect = 1;
            Player p = e.getPlayer();
            p.sendMessage("3");
    
            ItemStack helmet = p.getInventory().getHelmet();
            ItemStack chestplate = p.getInventory().getChestplate();
            ItemStack leggings = p.getInventory().getLeggings();
            ItemStack boots = p.getInventory().getBoots();
    
            if (helmet != null && helmet.getType() == Material.DIAMOND_HELMET) {
                protect = protect - 12/100;
            }
            else if (helmet != null && helmet.getType() == Material.IRON_HELMET) {
                protect = protect - 8/100;
            }
    
            else if (helmet != null && helmet.getType() == Material.LEATHER_HELMET) {
                protect = protect - 4/100;
            }
    
            else if (helmet != null && helmet.getType() == Material.CHAINMAIL_HELMET) {
                protect = protect - 4/100;
            }
            else if (helmet != null && helmet.getType() == Material.GOLDEN_HELMET) {
                protect = protect - 4/100;
            }
    
            if (chestplate != null && chestplate.getType() == Material.DIAMOND_CHESTPLATE) {
                protect = protect - 32/100;
            }
            else if (chestplate != null && chestplate.getType() == Material.IRON_CHESTPLATE) {
                protect = protect - 24/100;
            }
            else if (chestplate != null && chestplate.getType() == Material.GOLDEN_CHESTPLATE) {
                protect = protect - 20/100;
            }
            else if (chestplate != null && chestplate.getType() == Material.LEATHER_CHESTPLATE) {
                protect = protect - 12/100;
            }
    
            else if (chestplate != null && chestplate.getType() == Material.CHAINMAIL_CHESTPLATE) {
                protect = protect - 20/100;
            }
    
    
    
            if (leggings != null && leggings.getType() == Material.DIAMOND_LEGGINGS) {
                protect = protect - 24/100;
            }
            else if (leggings != null && leggings.getType() == Material.GOLDEN_LEGGINGS) {
                protect = protect - 12/100;
            }
            else if (leggings != null && leggings.getType() == Material.IRON_LEGGINGS) {
                protect = protect - 28/100;
            }
            else if (leggings != null && leggings.getType() == Material.LEATHER_LEGGINGS) {
                protect = protect - 8/100;
            }
            else if (leggings != null && leggings.getType() == Material.NETHERITE_LEGGINGS) {
                protect = protect - 24/100;
            }
            else if (leggings != null && leggings.getType() == Material.CHAINMAIL_LEGGINGS) {
                protect = protect - 16/100;
            }
    
            if (boots != null && leggings.getType() == Material.DIAMOND_BOOTS) {
                protect = protect - 12/100;
            }
            else if (boots != null && leggings.getType() == Material.GOLDEN_BOOTS) {
                protect = protect - 4/100;
            }
            else if (boots != null && leggings.getType() == Material.IRON_BOOTS) {
                protect = protect - 8/100;
            }
            else if (boots != null && leggings.getType() == Material.LEATHER_BOOTS) {
                protect = protect - 4/100;
            }
            else if (boots != null && leggings.getType() == Material.NETHERITE_BOOTS) {
                protect = protect - 12/100;
            }
            else if (boots != null && leggings.getType() == Material.CHAINMAIL_BOOTS) {
                protect = protect - 4/100;
            }
        }
    
    
        public int pro = protect;
    
    
    }
    
    Code:
    package me.rottenlemons.damage.Events;
    
    import org.bukkit.entity.Player;
    import org.bukkit.event.Cancellable;
    import org.bukkit.event.HandlerList;
    import org.bukkit.event.player.PlayerEvent;
    import org.bukkit.inventory.ItemStack;
    
    
    public final class ArmorEquipEvent extends PlayerEvent implements Cancellable{
    
        private static final HandlerList handlers = new HandlerList();
        private boolean cancel = false;
        private final EquipMethod equipType;
        private final ArmorType type;
        private ItemStack oldArmorPiece, newArmorPiece;
    
        /**
         * @param player The player who put on / removed the armor.
         * @param type The ArmorType of the armor added
         * @param oldArmorPiece The ItemStack of the armor removed.
         * @param newArmorPiece The ItemStack of the armor added.
         */
        public ArmorEquipEvent(final Player player, final EquipMethod equipType, final ArmorType type, final ItemStack oldArmorPiece, final ItemStack newArmorPiece){
            super(player);
            this.equipType = equipType;
            this.type = type;
            this.oldArmorPiece = oldArmorPiece;
            this.newArmorPiece = newArmorPiece;
        }
    
        /**
         * Gets a list of handlers handling this event.
         *
         * @return A list of handlers handling this event.
         */
        public static HandlerList getHandlerList(){
            return handlers;
        }
    
        /**
         * Gets a list of handlers handling this event.
         *
         * @return A list of handlers handling this event.
         */
        @Override
        public final HandlerList getHandlers(){
            return handlers;
        }
    
        /**
         * Sets if this event should be cancelled.
         *
         * @param cancel If this event should be cancelled.
         */
        public final void setCancelled(final boolean cancel){
            this.cancel = cancel;
        }
    
        /**
         * Gets if this event is cancelled.
         *
         * @return If this event is cancelled
         */
        public final boolean isCancelled(){
            return cancel;
        }
    
        public final ArmorType getType(){
            return type;
        }
    
        /**
         * Returns the last equipped armor piece, could be a piece of armor, or null
         */
        public final ItemStack getOldArmorPiece(){
            return oldArmorPiece;
        }
    
        public final void setOldArmorPiece(final ItemStack oldArmorPiece){
            this.oldArmorPiece = oldArmorPiece;
        }
    
        /**
         * Returns the newly equipped armor, could be a piece of armor, or null
         */
        public final ItemStack getNewArmorPiece(){
            return newArmorPiece;
        }
    
        public final void setNewArmorPiece(final ItemStack newArmorPiece){
            this.newArmorPiece = newArmorPiece;
        }
    
        /**
         * Gets the method used to either equip or unequip an armor piece.
         */
        public EquipMethod getMethod(){
            return equipType;
        }
    
        public enum EquipMethod{// These have got to be the worst documentations ever.
            /**
             * When you shift click an armor piece to equip or unequip
             */
            SHIFT_CLICK,
            /**
             * When you drag and drop the item to equip or unequip
             */
            DRAG,
            /**
             * When you manually equip or unequip the item. Use to be DRAG
             */
            PICK_DROP,
            /**
             * When you right click an armor piece in the hotbar without the inventory open to equip.
             */
            HOTBAR,
            /**
             * When you press the hotbar slot number while hovering over the armor slot to equip or unequip
             */
            HOTBAR_SWAP,
            /**
             * When in range of a dispenser that shoots an armor piece to equip.<br>
             * Requires the spigot version to have {@link org.bukkit.event.block.BlockDispenseArmorEvent} implemented.
             */
            DISPENSER,
            /**
             * When an armor piece is removed due to it losing all durability.
             */
            BROKE,
            /**
             * When you die causing all armor to unequip
             */
            DEATH,
            ;
        }
    }
    
    Code:
    package me.rottenlemons.damage.Events;
    
    import org.bukkit.inventory.ItemStack;
    
    public enum ArmorType{
        HELMET(5), CHESTPLATE(6), LEGGINGS(7), BOOTS(8);
    
        private final int slot;
    
        ArmorType(int slot){
            this.slot = slot;
        }
    
    
        public static ArmorType matchType(final ItemStack itemStack){
            if(ArmorListener.isAirOrNull(itemStack)) return null;
            String type = itemStack.getType().name();
            if(type.endsWith("_HELMET") || type.endsWith("_SKULL") || type.endsWith("_HEAD")) return HELMET;
            else if(type.endsWith("_CHESTPLATE") || type.equals("ELYTRA")) return CHESTPLATE;
            else if(type.endsWith("_LEGGINGS")) return LEGGINGS;
            else if(type.endsWith("_BOOTS")) return BOOTS;
            else return null;
        }
    
        public int getSlot(){
            return slot;
        }
    }
    Code:
    import org.bukkit.inventory.ItemStack;
    
    import me.rottenlemons.damage.Events.ArmorEquipEvent.EquipMethod;
    
    /**
    * @author Arnah
    * @since Jul 30, 2015
    */
    public class ArmorListener implements Listener{
    
        private final List<String> blockedMaterials;
    
        public ArmorListener(List<String> blockedMaterials){
            this.blockedMaterials = blockedMaterials;
        }
        //Event Priority is highest because other plugins might cancel the events before we check.
    
        @EventHandler(priority =  EventPriority.HIGHEST, ignoreCancelled = true)
        public final void inventoryClick(final InventoryClickEvent e){
            boolean shift = false, numberkey = false;
            if(e.isCancelled()) return;
            if(e.getAction() == InventoryAction.NOTHING) return;// Why does this get called if nothing happens??
            if(e.getClick().equals(ClickType.SHIFT_LEFT) || e.getClick().equals(ClickType.SHIFT_RIGHT)){
                shift = true;
            }
            if(e.getClick().equals(ClickType.NUMBER_KEY)){
                numberkey = true;
            }
            if(e.getSlotType() != SlotType.ARMOR && e.getSlotType() != SlotType.QUICKBAR && e.getSlotType() != SlotType.CONTAINER) return;
            if(e.getClickedInventory() != null && !e.getClickedInventory().getType().equals(InventoryType.PLAYER)) return;
            if (!e.getInventory().getType().equals(InventoryType.CRAFTING) && !e.getInventory().getType().equals(InventoryType.PLAYER)) return;
            if(!(e.getWhoClicked() instanceof Player)) return;
            ArmorType newArmorType = ArmorType.matchType(shift ? e.getCurrentItem() : e.getCursor());
            if(!shift && newArmorType != null && e.getRawSlot() != newArmorType.getSlot()){
                // Used for drag and drop checking to make sure you aren't trying to place a helmet in the boots slot.
                return;
            }
            if(shift){
                newArmorType = ArmorType.matchType(e.getCurrentItem());
                if(newArmorType != null){
                    boolean equipping = true;
                    if(e.getRawSlot() == newArmorType.getSlot()){
                        equipping = false;
                    }
                    if(newArmorType.equals(ArmorType.HELMET) && (equipping ? isAirOrNull(e.getWhoClicked().getInventory().getHelmet()) : !isAirOrNull(e.getWhoClicked().getInventory().getHelmet())) || newArmorType.equals(ArmorType.CHESTPLATE) && (equipping ? isAirOrNull(e.getWhoClicked().getInventory().getChestplate()) : !isAirOrNull(e.getWhoClicked().getInventory().getChestplate())) || newArmorType.equals(ArmorType.LEGGINGS) && (equipping ? isAirOrNull(e.getWhoClicked().getInventory().getLeggings()) : !isAirOrNull(e.getWhoClicked().getInventory().getLeggings())) || newArmorType.equals(ArmorType.BOOTS) && (equipping ? isAirOrNull(e.getWhoClicked().getInventory().getBoots()) : !isAirOrNull(e.getWhoClicked().getInventory().getBoots()))){
                        ArmorEquipEvent armorEquipEvent = new ArmorEquipEvent((Player) e.getWhoClicked(), EquipMethod.SHIFT_CLICK, newArmorType, equipping ? null : e.getCurrentItem(), equipping ? e.getCurrentItem() : null);
                        Bukkit.getServer().getPluginManager().callEvent(armorEquipEvent);
                        if(armorEquipEvent.isCancelled()){
                            e.setCancelled(true);
                        }
                    }
                }
            }else{
                ItemStack newArmorPiece = e.getCursor();
                ItemStack oldArmorPiece = e.getCurrentItem();
                if(numberkey){
                    if(e.getClickedInventory().getType().equals(InventoryType.PLAYER)){// Prevents shit in the 2by2 crafting
                        // e.getClickedInventory() == The players inventory
                        // e.getHotBarButton() == key people are pressing to equip or unequip the item to or from.
                        // e.getRawSlot() == The slot the item is going to.
                        // e.getSlot() == Armor slot, can't use e.getRawSlot() as that gives a hotbar slot ;-;
                        ItemStack hotbarItem = e.getClickedInventory().getItem(e.getHotbarButton());
                        if(!isAirOrNull(hotbarItem)){// Equipping
                            newArmorType = ArmorType.matchType(hotbarItem);
                            newArmorPiece = hotbarItem;
                            oldArmorPiece = e.getClickedInventory().getItem(e.getSlot());
                        }else{// Unequipping
                            newArmorType = ArmorType.matchType(!isAirOrNull(e.getCurrentItem()) ? e.getCurrentItem() : e.getCursor());
                        }
                    }
                }else{
                    if(isAirOrNull(e.getCursor()) && !isAirOrNull(e.getCurrentItem())){// unequip with no new item going into the slot.
                        newArmorType = ArmorType.matchType(e.getCurrentItem());
                    }
                    // e.getCurrentItem() == Unequip
                    // e.getCursor() == Equip
                    // newArmorType = ArmorType.matchType(!isAirOrNull(e.getCurrentItem()) ? e.getCurrentItem() : e.getCursor());
                }
                if(newArmorType != null && e.getRawSlot() == newArmorType.getSlot()){
                    EquipMethod method = EquipMethod.PICK_DROP;
                    if(e.getAction().equals(InventoryAction.HOTBAR_SWAP) || numberkey) method = EquipMethod.HOTBAR_SWAP;
                    ArmorEquipEvent armorEquipEvent = new ArmorEquipEvent((Player) e.getWhoClicked(), method, newArmorType, oldArmorPiece, newArmorPiece);
                    Bukkit.getServer().getPluginManager().callEvent(armorEquipEvent);
                    if(armorEquipEvent.isCancelled()){
                        e.setCancelled(true);
                    }
                }
            }
        }
    
        @EventHandler(priority =  EventPriority.HIGHEST)
        public void playerInteractEvent(PlayerInteractEvent e){
            if(e.useItemInHand().equals(Result.DENY))return;
            //
            if(e.getAction() == Action.PHYSICAL) return;
            if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK){
                Player player = e.getPlayer();
                if(!e.useInteractedBlock().equals(Result.DENY)){
                    if(e.getClickedBlock() != null && e.getAction() == Action.RIGHT_CLICK_BLOCK && !player.isSneaking()){// Having both of these checks is useless, might as well do it though.
                        // Some blocks have actions when you right click them which stops the client from equipping the armor in hand.
                        Material mat = e.getClickedBlock().getType();
                        for(String s : blockedMaterials){
                            if(mat.name().equalsIgnoreCase(s)) return;
                        }
                    }
                }
                ArmorType newArmorType = ArmorType.matchType(e.getItem());
                if(newArmorType != null){
                    if(newArmorType.equals(ArmorType.HELMET) && isAirOrNull(e.getPlayer().getInventory().getHelmet()) || newArmorType.equals(ArmorType.CHESTPLATE) && isAirOrNull(e.getPlayer().getInventory().getChestplate()) || newArmorType.equals(ArmorType.LEGGINGS) && isAirOrNull(e.getPlayer().getInventory().getLeggings()) || newArmorType.equals(ArmorType.BOOTS) && isAirOrNull(e.getPlayer().getInventory().getBoots())){
                        ArmorEquipEvent armorEquipEvent = new ArmorEquipEvent(e.getPlayer(), EquipMethod.HOTBAR, ArmorType.matchType(e.getItem()), null, e.getItem());
                        Bukkit.getServer().getPluginManager().callEvent(armorEquipEvent);
                        if(armorEquipEvent.isCancelled()){
                            e.setCancelled(true);
                            player.updateInventory();
                        }
                    }
                }
            }
        }
    
        @EventHandler(priority =  EventPriority.HIGHEST, ignoreCancelled = true)
        public void inventoryDrag(InventoryDragEvent event){
            // getType() seems to always be even.
            // Old Cursor gives the item you are equipping
            // Raw slot is the ArmorType slot
            // Can't replace armor using this method making getCursor() useless.
            ArmorType type = ArmorType.matchType(event.getOldCursor());
            if(event.getRawSlots().isEmpty()) return;// Idk if this will ever happen
            if(type != null && type.getSlot() == event.getRawSlots().stream().findFirst().orElse(0)){
                ArmorEquipEvent armorEquipEvent = new ArmorEquipEvent((Player) event.getWhoClicked(), EquipMethod.DRAG, type, null, event.getOldCursor());
                Bukkit.getServer().getPluginManager().callEvent(armorEquipEvent);
                if(armorEquipEvent.isCancelled()){
                    event.setResult(Result.DENY);
                    event.setCancelled(true);
                }
            }
            // Debug shit
            /*System.out.println("Slots: " + event.getInventorySlots().toString());
            System.out.println("Raw Slots: " + event.getRawSlots().toString());
            if(event.getCursor() != null){
                System.out.println("Cursor: " + event.getCursor().getType().name());
            }
            if(event.getOldCursor() != null){
                System.out.println("OldCursor: " + event.getOldCursor().getType().name());
            }
            System.out.println("Type: " + event.getType().name());*/
        }
    
        @SuppressWarnings("deprecation")
        @EventHandler
        public void itemBreakEvent(PlayerItemBreakEvent e){
            ArmorType type = ArmorType.matchType(e.getBrokenItem());
            if(type != null){
                Player p = e.getPlayer();
                ArmorEquipEvent armorEquipEvent = new ArmorEquipEvent(p, EquipMethod.BROKE, type, e.getBrokenItem(), null);
                Bukkit.getServer().getPluginManager().callEvent(armorEquipEvent);
                if(armorEquipEvent.isCancelled()){
                    ItemStack i = e.getBrokenItem().clone();
                    i.setAmount(1);
                    i.setDurability((short) (i.getDurability() - 1));
                    if(type.equals(ArmorType.HELMET)){
                        p.getInventory().setHelmet(i);
                    }else if(type.equals(ArmorType.CHESTPLATE)){
                        p.getInventory().setChestplate(i);
                    }else if(type.equals(ArmorType.LEGGINGS)){
                        p.getInventory().setLeggings(i);
                    }else if(type.equals(ArmorType.BOOTS)){
                        p.getInventory().setBoots(i);
                    }
                }
            }
        }
    
        @EventHandler
        public void playerDeathEvent(PlayerDeathEvent e){
            Player p = e.getEntity();
            if(e.getKeepInventory()) return;
            for(ItemStack i : p.getInventory().getArmorContents()){
                if(!isAirOrNull(i)){
                    Bukkit.getServer().getPluginManager().callEvent(new ArmorEquipEvent(p, EquipMethod.DEATH, ArmorType.matchType(i), i, null));
                    // No way to cancel a death event.
                }
            }
        }
    
        /**
         * A utility method to support versions that use null or air ItemStacks.
         */
        public static boolean isAirOrNull(ItemStack item){
            return item == null || item.getType().equals(Material.AIR);
        }
    }
    
    
    The ArmorEquipEvent, ArmorType and ArmorListener are not my code and I have not edited them in any way.
    Here is my plugin.yml:
    Code:
    name: damage
    main: me.rottenlemons.damage.damage
    version: 1.0
    author: rottenlemons
    api-version: 1.16
    
    I have learnt the basics of java and hope you guys can help.
    There are some errors in intellij such as Method 'onDamage(org.bukkit.event.entity.EntityDamageByEntityEvent)' is never use
     
    Last edited: Oct 14, 2020
  2. Offline

    Yochran

    I have no idea why you needed this
    damageprotect frame = new damageprotect();
    in your events class. Second of all, can we see your stack trace? As well as that, we don't need ever single one of your classes. We just need your class that you're having an error with, and your main class.
     
  3. Offline

    Rotten_Lemons

    Sorry. The problem I found was that before, register Events would work when I hit an entity but without tinkering anything in the main class, It stopped registering Events and sending it in command
     

Share This Page