Solved Can't pass an event

Discussion in 'Spigot Plugin Development' started by Waldxn, Apr 20, 2017.

  1. [​IMG]

    Code (Text):
       @EventHandler
        public void onSlay(EntityDeathEvent e) {

            String ent = e.getEntity().toString();
            Player killer = e.getEntity().getKiller();
            UUID killeruuid = killer.getUniqueId();

            if (ent == "CraftCreeper") {
                ItemStack creeper = this.plugin.getConfig().getItemStack("craftcreeper");
                ItemMeta creeperm = creeper.getItemMeta();
                Bukkit.broadcastMessage("Yuh it works");

            }
            else {


                Bukkit.broadcastMessage("Not yet " + ent);
            }
        }
    Any ideas?
     
  2. What's line 35 of slay.java


    Sent from my iPhone using Tapatalk
     
  3. Code (Text):
       ItemMeta creeperm = creeper.getItemMeta();
     
  4. Creeper is null.
    You can't get itemMeta from null thus the ItemStack isn't getting created


    Sent from my iPhone using Tapatalk
     
  5. But I have this in my config.yml?
    Code (Text):
    craftcreeper: diamond
     
  6. Not 100% sure on getting an item stack from a config but shouldn't you be doing

    New ItemStack(type,qty);?


    Sent from my iPhone using Tapatalk
     

  7. Hmm, I'm trying to grab an item from the config. So when Creeper is killed it adds a diamond to the inventory. What should I do for the config then?
    EDIT: I want the drop to be customizable in the config
     
  8. So getItemStack would work if you saved a serialized version of a diamond item stack.

    What you probably need to do is save it as a string, read that string in and translate it to a material type. Then pass it like I mentioned before as a new ItemStack(<material>,<qty>)

    Edit: use Material.valueOf(yourstring);

    You'll need to make sure they use legal names of the items in the config like DIAMOND not diamond

    Sent from my iPhone using Tapatalk
     
  9. Translating a string to material how?

    I tried this but won't work:
    Code (Text):
    Material m = (Material) creeper;
     
  10. Code (Text):


    Material m = Material.valueOf(creeper);

    And if(m == null) {
               System.out.printLn("invalid material specified for: " + creeper + " you must use valid names for minecraft items i.e. DIAMOND");
    Return;
    }

     
    Never assume your users will put in the right item types or even spell it right when reading from config you have to think of every possible way someone can screw it up and handle it.

    Edit.

    You could also use creeper.toUpperCase() to reduce not require them using uppercase item names.
    Sent from my iPhone using Tapatalk
     
  11. Could not pass event EntityDeathEvent to SlayRewards v1.0.0
    org.bukkit.event.EventException
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-1.11.2-R0.1-SNAPSHOT.jar:git-Spigot-54ec0b8-e04a179]
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.11.2-R0.1-SNAPSHOT.jar:git-Spigot-54ec0b8-e04a179]
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot-1.11.2-R0.1-SNAPSHOT.jar:git-Spigot-54ec0b8-e04a179]
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot-1.11.2-R0.1-SNAPSHOT.jar:git-Spigot-54ec0b8-e04a179]
    at org.bukkit.craftbukkit.v1_11_R1.event.CraftEventFactory.callEntityDeathEvent(CraftEventFactory.java:394) [spigot-1.11.2-R0.1-SNAPSHOT.jar:git-Spigot-54ec0b8-e04a179]
    at net.minecraft.server.v1_11_R1.EntityLiving.die(EntityLiving.java:1079) [spigot-1.11.2-R0.1-SNAPSHOT.jar:git-Spigot-54ec0b8-e04a179]
    at net.minecraft.server.v1_11_R1.EntityCreeper.die(EntityCreeper.java:145) [spigot-1.11.2-R0.1-SNAPSHOT.jar:git-Spigot-54ec0b8-e04a179]
    at net.minecraft.server.v1_11_R1.EntityLiving.damageEntity(EntityLiving.java:933) [spigot-1.11.2-R0.1-SNAPSHOT.jar:git-Spigot-54ec0b8-e04a179]
    at net.minecraft.server.v1_11_R1.EntityMonster.damageEntity(EntityMonster.java:44) [spigot-1.11.2-R0.1-SNAPSHOT.jar:git-Spigot-54ec0b8-e04a179]
    at net.minecraft.server.v1_11_R1.EntityHuman.attack(EntityHuman.java:1023) [spigot-1.11.2-R0.1-SNAPSHOT.jar:git-Spigot-54ec0b8-e04a179]
    at net.minecraft.server.v1_11_R1.EntityPlayer.attack(EntityPlayer.java:1157) [spigot-1.11.2-R0.1-SNAPSHOT.jar:git-Spigot-54ec0b8-e04a179]
    at net.minecraft.server.v1_11_R1.PlayerConnection.a(PlayerConnection.java:1577) [spigot-1.11.2-R0.1-SNAPSHOT.jar:git-Spigot-54ec0b8-e04a179]
    at net.minecraft.server.v1_11_R1.PacketPlayInUseEntity.a(SourceFile:69) [spigot-1.11.2-R0.1-SNAPSHOT.jar:git-Spigot-54ec0b8-e04a179]
    at net.minecraft.server.v1_11_R1.PacketPlayInUseEntity.a(SourceFile:13) [spigot-1.11.2-R0.1-SNAPSHOT.jar:git-Spigot-54ec0b8-e04a179]
    at net.minecraft.server.v1_11_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.11.2-R0.1-SNAPSHOT.jar:git-Spigot-54ec0b8-e04a179]
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_121]
    at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_121]
    at net.minecraft.server.v1_11_R1.SystemUtils.a(SourceFile:46) [spigot-1.11.2-R0.1-SNAPSHOT.jar:git-Spigot-54ec0b8-e04a179]
    at net.minecraft.server.v1_11_R1.MinecraftServer.D(MinecraftServer.java:747) [spigot-1.11.2-R0.1-SNAPSHOT.jar:git-Spigot-54ec0b8-e04a179]
    at net.minecraft.server.v1_11_R1.DedicatedServer.D(DedicatedServer.java:399) [spigot-1.11.2-R0.1-SNAPSHOT.jar:git-Spigot-54ec0b8-e04a179]
    at net.minecraft.server.v1_11_R1.MinecraftServer.C(MinecraftServer.java:678) [spigot-1.11.2-R0.1-SNAPSHOT.jar:git-Spigot-54ec0b8-e04a179]
    at net.minecraft.server.v1_11_R1.MinecraftServer.run(MinecraftServer.java:576) [spigot-1.11.2-R0.1-SNAPSHOT.jar:git-Spigot-54ec0b8-e04a179]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_121]
    Caused by: java.lang.IllegalArgumentException: No enum constant org.bukkit.Material.diamond
    at java.lang.Enum.valueOf(Unknown Source) ~[?:1.8.0_121]
    at org.bukkit.Material.valueOf(Material.java:1) ~[spigot-1.11.2-R0.1-SNAPSHOT.jar:git-Spigot-54ec0b8-e04a179]
    at com.waldxn.slayRewards.event.Slay.onSlay(Slay.java:33) ~[?:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_121]
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_121]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_121]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_121]
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[spigot-1.11.2-R0.1-SNAPSHOT.jar:git-Spigot-54ec0b8-e04a179]
    ... 22 more

    Code (Text):
    public class Slay implements Listener {

        private SlayRewards plugin;

        public Slay(SlayRewards pl) {
            this.plugin = pl;
        }


        @EventHandler
        public void onSlay(EntityDeathEvent e) {

            String ent = e.getEntity().toString();
            Player killer = e.getEntity().getKiller();
            UUID killeruuid = killer.getUniqueId();

            if (ent == "CraftCreeper") {
                String creeper = this.plugin.getConfig().getString("craftcreeper");
                int creeperq = this.plugin.getConfig().getInt("craftcreeper-quantity");
                Material m = Material.valueOf(creeper);
                ItemStack creepers = new ItemStack(m, creeperq);

                if (m == null) {
                    killer.sendMessage("invalid material specified for: " + creeper + " you must use valid names for minecraft items i.e. DIAMOND");
                }

            } else {
               
                Bukkit.broadcastMessage("Not yet " + ent);
            }
        }

    }
    I have no clue how to fix this
     
  12. Try changing it in the config to DIAMOND

    Getvalueof is failing because diamond does not exist as a legal Material name

    Or force it to uppercase with getValueOf(creeper.toUppercase());

    You'll still need a way to handle it if they put in diamondslol as a material name. Probably a try/catch block

    Edit also your m==null check needs to be before you try to make an ItemStack out of it otherwise if the material is invalid you'll still get a null pointer because you can't make an ItemStack out of null


    Edit also you shouldn't send the message to the killer about an invalid config material name, as likely they can't fix it errors like that should go to the console.
    Sent from my iPhone using Tapatalk
     
    • Agree Agree x 1
    • Winner Winner x 1
  13. No errors anymore and it works! For some reason the config didn't update from diamond to DIAMOND when I first fixed it. I had to delete config.yml in my plugins folder and regenerate the default config file.
     
  14. Yeah study your stacktraces they usually will tell you exactly where an error is. At first they're kinda daunting because of their sheer size. But you can usually ignore all the lines in the stack trace until one mentions a file in YOUR plugin. Ie the slay.java. It let me know the line to look at and how to diagnose the error.

    It takes time but the stacktrace is your best friend!


    Sent from my iPhone using Tapatalk
     
  15. ^^

    Adding onto that, usually you can skip the whole first block of random information, then look for the "Caused by: <something, typically a NullPointerException>" and two or three lines down, you should see your class name and line number! :)
     
    • Like Like x 1
  16. Ok cool I'll start doing that. What method should I call in my onEnable() to save the config correctly. Like if someone had it already, then downloaded a new update with an updated config.yml
     
  17. I wouldn't recommend force deleting someone's config, just because they won't like you overwriting all of their settings after they set it up. If there was a change that needed a config rewrite you should let them know when it starts or save a completely new one so they can update from the old one.


    Sent from my iPhone using Tapatalk