Solved please help can anyone help?

Discussion in 'Spigot Help' started by ZachThePwn, Jun 14, 2018.

  1. I am trying to make it so that if a sword is named something then it will send the message that they have done damage. Im really lost as im new to this and I cant figure out how to go about this please help.

    Code (Text):
        @EventHandler
        public boolean onDamagingHit(EntityDamageByEntityEvent e) {      
            if (e.getDamager() instanceof Player && e.getCause() == EntityDamageEvent.DamageCause.ENTITY_ATTACK) {
                return true;
            }
            return false;
        }
       
       
        @EventHandler
        public void onDamageByPlayer(PlayerInteractEvent e) {
            Action action = e.getAction();
            Player p = e.getPlayer();
            ItemStack woodensword = new ItemStack(Material.WOOD_SWORD);
            ItemMeta woodmeta = woodensword.getItemMeta();
           
            if (action.equals(Action.LEFT_CLICK_AIR)) {
                    if (onDamagingHit(null)) {
                        if (woodmeta.getItemMeta().getDisplayName().equals(ChatColor.WHITE + "Basic Wooden Sword")) {
                            p.sendMessage(ChatColor.RED + "Damage");
                        }
                       
                    }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
               
            }
        }
     
  2. error when typing it and error in console if I take out the "if ( wood meta" what is wrong!!! :(

    Code (Text):
    [20:22:35 ERROR]: Could not pass event PlayerInteractEvent to BasicMinecraft v1.0
    org.bukkit.event.EventException: null
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-latest.jar:git-Spigot-2086bb0-8cc5a7e]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-latest.jar:git-Spigot-2086bb0-8cc5a7e]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:500) [spigot-latest.jar:git-Spigot-2086bb0-8cc5a7e]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:485) [spigot-latest.jar:git-Spigot-2086bb0-8cc5a7e]
            at org.bukkit.craftbukkit.v1_12_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:235) [spigot-latest.jar:git-Spigot-2086bb0-8cc5a7e]
            at org.bukkit.craftbukkit.v1_12_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:202) [spigot-latest.jar:git-Spigot-2086bb0-8cc5a7e]
            at org.bukkit.craftbukkit.v1_12_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:198) [spigot-latest.jar:git-Spigot-2086bb0-8cc5a7e]
            at net.minecraft.server.v1_12_R1.PlayerConnection.a(PlayerConnection.java:1435) [spigot-latest.jar:git-Spigot-2086bb0-8cc5a7e]
            at net.minecraft.server.v1_12_R1.PacketPlayInArmAnimation.a(PacketPlayInArmAnimation.java:24) [spigot-latest.jar:git-Spigot-2086bb0-8cc5a7e]
            at net.minecraft.server.v1_12_R1.PacketPlayInArmAnimation.a(PacketPlayInArmAnimation.java:1) [spigot-latest.jar:git-Spigot-2086bb0-8cc5a7e]
            at net.minecraft.server.v1_12_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-latest.jar:git-Spigot-2086bb0-8cc5a7e]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:?]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:?]
            at net.minecraft.server.v1_12_R1.SystemUtils.a(SourceFile:46) [spigot-latest.jar:git-Spigot-2086bb0-8cc5a7e]
            at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:748) [spigot-latest.jar:git-Spigot-2086bb0-8cc5a7e]
            at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:406) [spigot-latest.jar:git-Spigot-2086bb0-8cc5a7e]
            at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:679) [spigot-latest.jar:git-Spigot-2086bb0-8cc5a7e]
            at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:577) [spigot-latest.jar:git-Spigot-2086bb0-8cc5a7e]
            at java.lang.Thread.run(Unknown Source) [?:?]
    Caused by: java.lang.NullPointerException
            at dev.zach.basicminecraft.Main.onDamagingHit(Main.java:260) ~[?:?]
            at dev.zach.basicminecraft.Main.onDamageByPlayer(Main.java:279) ~[?:?]
            at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?]
            at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:?]
            at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:?]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:?]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[spigot-latest.jar:git-Spigot-2086bb0-8cc5a7e]
            ... 18 more
     
  3. Just looking at this I can tell you're very inexperienced. Basically, these onDamagingHIt and onDamageByPlayer methods you have get called when the client communicates with the server that the event in the parameters (parenthesis) happened. The code in onDamagingHit is called for an entity damaging another entity. Pretty self-explanatory. Knowing this now, and if I understand what you want to do completely, you'd only want to use your first method.

    Delete the onDamageByPlayer as that has nothing to do with this (thats for left & right clicking stuff). Now you need to check what the player is holding in his hand and then check if its a sword and has the meta you want. You did part of this (kind of) in your second method. So try moving that to the first event (onDamagingHit). Here is the player Javadoc that you'll need for getting what the player is holding in their hand.
     
    • Like Like x 1
    • Informative Informative x 1
  4. ok i tried to make this and it says no errors but then has this error in console when i damage something :(

    @Perotin

    Code (Text):
    @EventHandler
        public void onDamagingHit(EntityDamageByEntityEvent e) {  
            if (e.getDamager() instanceof Player && e.getCause() == EntityDamageEvent.DamageCause.ENTITY_ATTACK) {
                Player p = (Player) e.getDamager();
                ItemStack woodensword = new ItemStack(Material.WOOD_SWORD);
                ItemMeta woodmeta = woodensword.getItemMeta();
                if (((ItemStack) woodmeta).getItemMeta().getDisplayName().equals(ChatColor.WHITE + "Basic Wooden Sword")) {
                    p.sendMessage(ChatColor.RED + "Damage");
                }
            }
        }
    Code (Text):
     Could not pass event EntityDamageByEntityEvent to BasicMinecraft v1.0
    org.bukkit.event.EventException: null
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-latest.jar:git-Spigot-2086bb0-8cc5a7e]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-latest.jar:git-Spigot-2086bb0-8cc5a7e]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:500) [spigot-latest.jar:git-Spigot-2086bb0-8cc5a7e]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:485) [spigot-latest.jar:git-Spigot-2086bb0-8cc5a7e]
            at org.bukkit.craftbukkit.v1_12_R1.event.CraftEventFactory.callEvent(CraftEventFactory.java:93) [spigot-latest.jar:git-Spigot-2086bb0-8cc5a7e]
            at org.bukkit.craftbukkit.v1_12_R1.event.CraftEventFactory.callEntityDamageEvent(CraftEventFactory.java:580) [spigot-latest.jar:git-Spigot-2086bb0-8cc5a7e]
            at org.bukkit.craftbukkit.v1_12_R1.event.CraftEventFactory.handleEntityDamageEvent(CraftEventFactory.java:483) [spigot-latest.jar:git-Spigot-2086bb0-8cc5a7e]
            at org.bukkit.craftbukkit.v1_12_R1.event.CraftEventFactory.handleLivingEntityDamageEvent(CraftEventFactory.java:612) [spigot-latest.jar:git-Spigot-2086bb0-8cc5a7e]
            at net.minecraft.server.v1_12_R1.EntityLiving.damageEntity0(EntityLiving.java:1346) [spigot-latest.jar:git-Spigot-2086bb0-8cc5a7e]
            at net.minecraft.server.v1_12_R1.EntityLiving.damageEntity(EntityLiving.java:868) [spigot-latest.jar:git-Spigot-2086bb0-8cc5a7e]
            at net.minecraft.server.v1_12_R1.EntityMonster.damageEntity(EntityMonster.java:44) [spigot-latest.jar:git-Spigot-2086bb0-8cc5a7e]
            at net.minecraft.server.v1_12_R1.EntityZombie.damageEntity(EntityZombie.java:163) [spigot-latest.jar:git-Spigot-2086bb0-8cc5a7e]
            at net.minecraft.server.v1_12_R1.EntityHuman.attack(EntityHuman.java:1005) [spigot-latest.jar:git-Spigot-2086bb0-8cc5a7e]
            at net.minecraft.server.v1_12_R1.EntityPlayer.attack(EntityPlayer.java:1311) [spigot-latest.jar:git-Spigot-2086bb0-8cc5a7e]
            at net.minecraft.server.v1_12_R1.PlayerConnection.a(PlayerConnection.java:1621) [spigot-latest.jar:git-Spigot-2086bb0-8cc5a7e]
            at net.minecraft.server.v1_12_R1.PacketPlayInUseEntity.a(SourceFile:69) [spigot-latest.jar:git-Spigot-2086bb0-8cc5a7e]
            at net.minecraft.server.v1_12_R1.PacketPlayInUseEntity.a(SourceFile:13) [spigot-latest.jar:git-Spigot-2086bb0-8cc5a7e]
            at net.minecraft.server.v1_12_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-latest.jar:git-Spigot-2086bb0-8cc5a7e]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:?]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:?]
            at net.minecraft.server.v1_12_R1.SystemUtils.a(SourceFile:46) [spigot-latest.jar:git-Spigot-2086bb0-8cc5a7e]
            at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:748) [spigot-latest.jar:git-Spigot-2086bb0-8cc5a7e]
            at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:406) [spigot-latest.jar:git-Spigot-2086bb0-8cc5a7e]
            at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:679) [spigot-latest.jar:git-Spigot-2086bb0-8cc5a7e]
            at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:577) [spigot-latest.jar:git-Spigot-2086bb0-8cc5a7e]
            at java.lang.Thread.run(Unknown Source) [?:?]
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_12_R1.inventory.CraftMetaItem cannot be cast to org.bukkit.inventory.ItemStack
            at dev.zach.basicminecraft.Main.onDamagingHit(Main.java:264) ~[?:?]
            at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?]
            at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:?]
            at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:?]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:?]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[spigot-latest.jar:git-Spigot-2086bb0-8cc5a7e]
            ... 25 more
     
  5. this sends the message but it does it on any damaging hit not only when it is a hit by the sword

    Code (Text):
        public void onDamagingHit(EntityDamageByEntityEvent e) {  
            if (e.getDamager() instanceof Player && e.getCause() == EntityDamageEvent.DamageCause.ENTITY_ATTACK) {
                Entity ep = e.getDamager();
                ItemStack woodensword = new ItemStack(Material.WOOD_SWORD);
               
                ItemMeta woodmeta = woodensword.getItemMeta();
                if (woodensword.getType().equals(Material.WOOD_SWORD)) {
                    ep.sendMessage(ChatColor.RED + "Damage");
                    //if (woodmeta.getDisplayName().equals(ChatColor.WHITE + "Basic Wooden Sword")) {
                        //ep.sendMessage(ChatColor.RED + "Damage");
                       
                    //} else {
                       
                    //}
                } else {
                   
                }
               
            } else {
               
            }
        }
     
  6. now the error is back when i check for the main hand what is wrong guys?? :(((( still sends the message normal damage though, no matter what he holding

    Code (Text):
    public void onDamagingHit(EntityDamageByEntityEvent e) {  
            if (e.getDamager() instanceof Player && e.getCause() == EntityDamageEvent.DamageCause.ENTITY_ATTACK) {
                Entity ep = e.getDamager();
                HumanEntity he = (HumanEntity) e.getEntity();
                ItemStack woodensword = new ItemStack(Material.WOOD_SWORD);
               
                ItemMeta woodmeta = woodensword.getItemMeta();
                if (woodensword.getType().equals(Material.WOOD_SWORD)) {
                    ep.sendMessage(ChatColor.RED + "Damage");
                    if (he.getMainHand().equals(Material.WOOD_SWORD) && woodmeta.getDisplayName().equals(ChatColor.WHITE + "Basic Wooden Sword")) {
                        ep.sendMessage(ChatColor.RED + "Damage special");
                       
                    } else {
                       
                    }
                } else {
                   
                }
               
            } else {
               
            }
        }
           
     
  7. Code (Text):
    ItemMeta woodmeta = woodensword.getItemMeta();
    if (woodmeta.getDisplayName().equals(ChatColor.WHITE + "Basic Wooden Sword"))
    You aren't checking if the item has a ItemMeta or a Display name.

    Code (Text):
    ItemMeta woodMeta;
    if (woodensword.hasItemMeta()) {
      woodMeta = woodenSword.getItemMeta;
    }

    String name;
    if (woodMeta.hasDisplayName()) {
       name = woodMeta.getDisplayName();
    }

    if (itemMeta != null && !name.equals(null)) {
    // do stuff
    }
     
    Try something like this
     
    • Agree Agree x 1
  8. Strahan

    Benefactor

    Not to mention he is grabbing meta for an ItemStack he just created; it will never have meta.

    I'd do it as such:
    Code (Text):
    @EventHandler
    public void onDamagingHit(EntityDamageByEntityEvent e) {  
        if (e.getDamager() instanceof Player && e.getCause() == DamageCause.ENTITY_ATTACK) {
            Player p = (Player)e.getDamager();
            if (p.getInventory().getItemInMainHand().getType() != Material.WOOD_SWORD) return;
            ItemStack woodensword = p.getInventory().getItemInMainHand();
           
            if (!woodensword.hasItemMeta()) return;
            if (!woodensword.getItemMeta().hasDisplayName()) return;
           
            if (ChatColor.stripColor(woodensword.getItemMeta().getDisplayName()).equalsIgnoreCase("basic wooden sword")) {
              p.sendMessage(ChatColor.RED + "Damage special");
            }
        }
    }
     
    #8 Strahan, Jun 15, 2018
    Last edited: Jun 15, 2018
    • Like Like x 1