Problem with Events!

Discussion in 'Plugin Development' started by tizrain, Jan 21, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    tizrain

    Hello! I'm trying to make a plugin where you can toggle item pickup and godmode in one command! I have the Godmode event listener in a separate class and so is the item pickup event. I just can't figure out what's going on. I checked the error log and it said this:

    For the godmode event:​
    Code:
    [02:39:54] [Server thread/ERROR]: Could not pass event EntityDamageEvent to HexaMine v1.0
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:427) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:481) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:466) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at org.bukkit.craftbukkit.v1_7_R1.event.CraftEventFactory.callEvent(CraftEventFactory.java:94) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at org.bukkit.craftbukkit.v1_7_R1.event.CraftEventFactory.callEntityDamageEvent(CraftEventFactory.java:382) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.EntityLiving.b(EntityLiving.java:857) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.EntityHuman.b(EntityHuman.java:1337) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.Entity.a(Entity.java:791) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.EntityLiving.a(EntityLiving.java:143) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.EntityPlayer.b(EntityPlayer.java:555) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.PlayerConnection.a(PlayerConnection.java:417) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.PacketPlayInFlying.a(SourceFile:137) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.PacketPlayInPositionLook.handle(SourceFile:20) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:146) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:655) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:250) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:545) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:457) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    Caused by: java.lang.NullPointerException
        at me.theepicexcalibur.hexamine.GodMode.onPlayerDamageEvent(GodMode.java:21) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_45]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_45]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_45]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_45]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        ... 20 more
    For the loot blocker event:​
    Code:
    [02:39:04] [Server thread/ERROR]: Could not pass event PlayerPickupItemEvent to HexaMine v1.0
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:427) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:481) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:466) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.EntityItem.b_(EntityItem.java:250) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.EntityHuman.r(EntityHuman.java:445) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.EntityHuman.e(EntityHuman.java:437) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.EntityLiving.h(EntityLiving.java:1299) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.EntityHuman.h(EntityHuman.java:162) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.EntityPlayer.i(EntityPlayer.java:240) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.PlayerConnection.a(PlayerConnection.java:342) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.PacketPlayInFlying.a(SourceFile:137) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.PacketPlayInFlying.handle(SourceFile:8) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:146) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:655) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:250) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:545) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:457) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    Caused by: java.lang.NullPointerException
        at me.theepicexcalibur.hexamine.LootBlocker.onPlayerPickupItemEvent(LootBlocker.java:16) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_45]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_45]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_45]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_45]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        ... 19 more
    I saw that the error occured at line 21 in the godmode class and line 16 in the loot blocker class, but I don't know why... Here's my classes:
    Main Class:​
    Code:java
    1. package me.theepicexcalibur.hexamine;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.configuration.file.FileConfiguration;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.entity.EntityDamageEvent;
    10. import org.bukkit.plugin.PluginManager;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13. public class Main extends JavaPlugin {
    14. public static Main plugin;
    15.  
    16. public EntityDamageEvent ede;
    17.  
    18. public boolean lootBlocker = false;
    19. public boolean isGodmode = false;
    20.  
    21. public void onEnable() {
    22. loadConfig();
    23. PluginManager plm = getServer().getPluginManager();
    24. plm.registerEvents(new GodMode(plugin), this);
    25. plm.registerEvents(new LootBlocker(plugin), this);
    26. }
    27.  
    28. public void onDisable() {
    29. saveConfig();
    30. }
    31.  
    32. public boolean onCommand(CommandSender sender, Command cmd, String cmdLab, String[] args) {
    33. Player p = (Player) sender;
    34. if (p.hasPermission("hexamine.command.clearchat")) {
    35. if (cmdLab.equalsIgnoreCase("clearchat") || cmdLab.equalsIgnoreCase("cc") || cmdLab.equalsIgnoreCase("clearc")) {
    36. for (int i = 0; i < 100; i++) {
    37. Bukkit.broadcastMessage(" ");
    38. }
    39. p.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("messages.chatcleared")));
    40. }
    41. }
    42. if (p.hasPermission("hexamine.command.reload")) {
    43. if (cmdLab.equalsIgnoreCase("hreload") || cmdLab.equalsIgnoreCase("hr") || cmdLab.equalsIgnoreCase("reloadh") || cmdLab.equalsIgnoreCase("rh")) {
    44. reloadConfig();
    45. p.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("messages.configreloaded")));
    46. }
    47. }
    48. if (cmdLab.equalsIgnoreCase("vanish") || cmdLab.equalsIgnoreCase("v") || cmdLab.equalsIgnoreCase("ev") || cmdLab.equalsIgnoreCase("evanish")) {
    49.  
    50. if (!lootBlocker && !isGodmode) {
    51. lootBlocker = true;
    52. isGodmode = true;
    53. p.sendMessage("Du slog på blockering av loot och godmode!");
    54. } else if (lootBlocker && isGodmode) {
    55. lootBlocker = false;
    56. isGodmode = false;
    57. p.sendMessage("Du slog av blockering av loot och godmode!");
    58. }
    59. }
    60. return false;
    61. }
    62.  
    63. public void loadConfig() {
    64. FileConfiguration cfg = getConfig();
    65. cfg.options().copyDefaults(true);
    66. saveConfig();
    67. }
    68. }
    69.  

    Godmode Class:​
    Code:java
    1. package me.theepicexcalibur.hexamine;
    2.  
    3. import org.bukkit.entity.Entity;
    4. import org.bukkit.entity.Player;
    5. import org.bukkit.event.EventHandler;
    6. import org.bukkit.event.Listener;
    7. import org.bukkit.event.entity.EntityDamageEvent;
    8.  
    9. public class GodMode implements Listener {
    10. public static Main plugin;
    11.  
    12. public GodMode(Main instance) {
    13. plugin = instance;
    14. }
    15.  
    16. @EventHandler
    17. public void onPlayerDamageEvent (EntityDamageEvent e) {
    18. Entity entity = e.getEntity();
    19.  
    20. if (entity instanceof Player) {
    21. if (plugin.isGodmode) {
    22. e.setCancelled(true);
    23. } else if (!plugin.isGodmode) {
    24. e.setCancelled(false);
    25. }
    26. }
    27. }
    28. }

    Loot Blocker Class:​
    Code:java
    1. package me.theepicexcalibur.hexamine;
    2.  
    3. import org.bukkit.event.EventHandler;
    4. import org.bukkit.event.Listener;
    5. import org.bukkit.event.player.PlayerPickupItemEvent;
    6.  
    7. public class LootBlocker implements Listener {
    8. public static Main plugin;
    9.  
    10. public LootBlocker (Main instance) {
    11. plugin = instance;
    12. }
    13.  
    14. @EventHandler
    15. public void onPlayerPickupItemEvent (PlayerPickupItemEvent e) {
    16. if (plugin.lootBlocker) {
    17. if(e.getItem().isOnGround()){
    18. e.setCancelled(true);
    19. }
    20. } else if (!plugin.lootBlocker) {
    21. if(e.getItem().isOnGround()){
    22. e.setCancelled(false);
    23. }
    24. }
    25. }
    26. }
    27.  


    Could it be with the "public static Main plugin" part to do?
     
  2. Offline

    Warreo

    tizrain you need to override the onEnable method like so:

    Code:
    @Override
    public void onEnable(){
    //Do stuff
    }
    Note: you will also need to do this for you onDisable() method too. And there is no reason to have the plugin variable in GodMode and Loot Blocker static since you've already passed it. :)
     
  3. Offline

    tizrain

    Ah I get it, thanks. :) But if I don't have the plugin variable in the other classes, I will not be able to access the booleans from the Main class?
     
  4. Offline

    Gopaintman


    Code:java
    1. private Main plugin;
    2. public GodMode(Main instance){
    3. plugin = instance;
    4.  
    5. }
    6. public void method(){
    7. if(plugin.<name of variable>){
    8.  
    9. }
    10. }


    However if you make a variable private in the Main class it will not be accessible.
     
  5. Offline

    beastman3226

    You never initialize the plugin variable you are using to initialize the two classes. I would just use 'this' when initializing your listeners. But here is a diagram of what is going wrong
    Main Class: plugin = null
    Main.onEnable().LootBlock(plugin <- this is null)<init>
    LootBlocker.onPlayerPickupItem() calls:
    plugin(null).getConfig().
     
  6. Offline

    tizrain

    Thank you man! I found the error you talked about. I removed the public void Main plugin in the main class and changed to "this" instead, and now everything works perfectly ^^
     
Thread Status:
Not open for further replies.

Share This Page