[SOLVED]Still learning new event system, need help please

Discussion in 'Plugin Development' started by CRAZYxMUNK3Y, Jan 25, 2012.

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

    CRAZYxMUNK3Y

    I am attempting update my plugin, but when i load it up, it shows this error.
    Code:
    2012-01-25 21:27:33 [SEVERE] Could not load 'plugins\GlassDrops.jar' in folder 'plugins':
    java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
        at java.lang.reflect.Constructor.newInstance(Unknown Source)
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:136)
        at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:285)
        at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:200)
        at org.bukkit.craftbukkit.CraftServer.loadPlugins(CraftServer.java:156)
        at org.bukkit.craftbukkit.CraftServer.reload(CraftServer.java:442)
        at org.bukkit.Bukkit.reload(Bukkit.java:188)
        at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:22)
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:168)
        at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:386)
        at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:382)
        at net.minecraft.server.MinecraftServer.b(MinecraftServer.java:573)
        at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:550)
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:434)
        at net.minecraft.server.ThreadServerApplication.run(SourceFile:465)
    Caused by: java.lang.NullPointerException
        at org.bukkit.plugin.SimplePluginManager.registerEvents(SimplePluginManager.java:563)
        at me.ben.GlassDrops.GlassDropListener.<init>(GlassDropListener.java:17)
        at me.ben.GlassDrops.GlassDrops.<init>(GlassDrops.java:19)
        ... 18 more
    My main class (excluding imports and commands):
    Code:java
    1.  
    2. public class GlassDrops extends JavaPlugin {
    3.  
    4. Logger log = Logger.getLogger("Minecraft");
    5.  
    6. private final GlassDropListener BlockListener = new GlassDropListener(this);
    7. private final GlassDropPaneListener PaneListsner = new GlassDropPaneListener(this);
    8.  
    9. public ArrayList<String> GlassDrops = new ArrayList<String>();
    10.  
    11. public void onEnable() {
    12. PluginDescriptionFile pdfFile = this.getDescription();
    13. PluginManager pm = Bukkit.getServer().getPluginManager();
    14. pm.registerEvents(this.BlockListener, this);
    15. pm.registerEvents(this.PaneListsner, this);
    16. log.info(pdfFile.getName() + " has been enabled with version: " + pdfFile.getVersion());
    17. }
    18.  
    19. public void onDisable() {
    20. PluginDescriptionFile pdfFile = this.getDescription();
    21. log.info(pdfFile.getName() + " has been disabled!");
    22. }
    23.  


    And one of the BlockListener classes (all are the same)(again, excluding the imports);
    Code:java
    1.  
    2. public class GlassDropListener implements Listener {
    3.  
    4. public static GlassDrops plugin;
    5.  
    6. public GlassDropListener (GlassDrops instance) {
    7. Bukkit.getServer().getPluginManager().registerEvents(this, plugin);
    8. }
    9.  
    10.  
    11. @EventHandler(priority = EventPriority.NORMAL)
    12. public void onBlockBreak(final BlockBreakEvent even) {
    13.  
    14. Block block = even.getBlock();
    15.  
    16. if (block.getType() == Material.GLASS){
    17. if (plugin.GlassDrops.contains(even.getPlayer().getName())){
    18. block.setType(Material.AIR);
    19. block.getWorld().dropItemNaturally(block.getLocation(), new ItemStack(Material.GLASS, 1));
    20. }
    21. }
    22. }
    23. }
    24.  


    Neither have any errors and i am stumped on what is wrong :S

    Thanks
     
  2. Offline

    desht

    Your listener objects are being created in your class constructor, which happens before onEnable(). That's not necessarily a problem, except for the fact that your listener object constructors are calling registerEvents(). So registerEvents() is getting called before your plugin is enabled - not good.

    There's absolutely no need to call registerEvents() in your listener constructors, since you're calling it anyway in onEnable(). Your listener constructor is also wrong in that it passes plugin to registerEvents() even though it's null (you're ignoring the instance parameter that you passed in).

    So just get rid of that constructor in your listener (and the unused private static plugin field) - it's not necessary.

    Finally, it's very poor practice to start your variable names with an uppercase letter - that makes them look like class names, which will just confuse you and everybody else later on. Variable names should lookLikeThis, class names should LookLikeThis.
     
  3. Here is how new events system works :

    Code:java
    1.  
    2. public class MyPlugin extends JavaPlugin {
    3. private final MyListenerClass myListener ;
    4.  
    5. public void onEnable() {
    6. myListener = new MyListenerClass(this);
    7.  
    8. PluginManager pm = Bukkit.getServer().getPluginManager();
    9. pm.registerEvents(this.myListener, this);
    10. }
    11. }
    12.  

    Code:java
    1.  
    2. public class MyListenerClass implements Listener {
    3.  
    4. public static MyPlugin plugin;
    5.  
    6. public MyListenerClass (MyPlugin instance) {
    7. this.plugin = instance;
    8. }
    9.  
    10.  
    11. @EventHandler(priority = EventPriority.NORMAL)
    12. public void onBlockBreak(final BlockBreakEvent even) {}
    13. }
    14.  


    It's what desht wrote. Again, please think about class names and variables names.
     
  4. Offline

    CRAZYxMUNK3Y

    My new listener
    Code:java
    1.  
    2. public class GlassDropListener implements Listener {
    3.  
    4. public static GlassDrops plugin;
    5.  
    6. @SuppressWarnings("static-access")
    7. public GlassDropListener (GlassDrops instance) {
    8. this.plugin = instance;
    9. }
    10.  
    11.  
    12. @EventHandler(priority = EventPriority.NORMAL)
    13. public void onBlockBreak(final BlockBreakEvent even){
    14.  
    15. Block block = even.getBlock();
    16.  

    New main
    Code:java
    1.  
    2. public class GlassDrops extends JavaPlugin {
    3.  
    4.  
    5. Logger log = Logger.getLogger("Minecraft");
    6.  
    7. private final GlassDropListener myGlassListener ;
    8. private final GlassDropPaneListener myPaneListener ;
    9.  
    10. public ArrayList<String> GlassDrops = new ArrayList<String>();
    11.  
    12. public void onEnable() {
    13. PluginDescriptionFile pdfFile = this.getDescription();
    14. PluginManager pm = Bukkit.getServer().getPluginManager();
    15. pm.registerEvents(this.myGlassListener, this);
    16. pm.registerEvents(this.myPaneListener, this);
    17. log.info(pdfFile.getName() + " has been enabled with version: " + pdfFile.getVersion());
    18. }
    19.  
    20.  
    21. public void onDisable() {
    22. PluginDescriptionFile pdfFile = this.getDescription();
    23. log.info(pdfFile.getName() + " has been disabled!");
    24.  
    25.  
    26. }
    27.  


    But i get an error in the main line with GlassDrops
    Code:java
    1.  
    2. public class GlassDrops extends JavaPlugin {
    3.  

    Error is:
    The blank final field myPaneListener may not have been initialized

    I have never seen this error before :S
     
  5. Offline

    uruhax

    Code:
    private final GlassDropListener myGlassListener = new GlassDropListener(this);
    private final GlassDropPaneListener myPaneListener = new GlassDropPaneListener(this);
    instead of just

    Code:
    private final GlassDropListener myGlassListener ;
    private final GlassDropPaneListener myPaneListener ;
    You just forgot to initialize the listeners you use in the .registerEvents() methods
     
  6. Offline

    CRAZYxMUNK3Y

    I did change it to the;
    private final GlassDropListener myGlassListener ;
    private final GlassDropPaneListener myPaneListener ;

    How excatly do i imitialize it sorry? :S

    Never really learnt that.
     
  7. Offline

    RROD


    Exactly how he said:
    Code:
    private final GlassDropListener myGlassListener = new GlassDropListener(this);
    private final GlassDropPaneListener myPaneListener = new GlassDropPaneListener(this);
     
  8. Offline

    uruhax

    No no, you have to change
    private final GlassDropListener myGlassListener ;
    private final GlassDropPaneListener myPaneListener ;

    to

    private final GlassDropListener myGlassListener = new GlassDropListener(this);
    private final GlassDropPaneListener myPaneListener = new GlassDropPaneListener(this);

    initializing is the part where you use the key word "new" along with a classes constructor (example: new GlassDropListener(this)).

    If you didn't understand then you should learn a bit more about java. I recommend watching thenewboston's java programming tutorials on youtube.
    link: http://www.youtube.com/user/thenewboston#grid/user/FE2CE09D83EE3E28

    Good luck :D
     
  9. Offline

    RROD

    GlassDrops (Main)
    Code:
    public class GlassDrops extends JavaPlugin {
        private Logger log = Logger.getLogger("Minecraft");
     
        //You only need one Listener Class now.
        //Rename your MyGlassListener to GlassListener.
     
        //Commented out un-used code. Plus, it shares the same name as the class.
        //public ArrayList<String> GlassDrops = new ArrayList<String>();
        public void onEnable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            PluginManager pm = Bukkit.getServer().getPluginManager();
            pm.registerEvents(new GlassListener(this), this);
            log.info(pdfFile.getName() + " has been enabled with version: " + pdfFile.getVersion());
        }
        public void onDisable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            log.info(pdfFile.getName() + " has been disabled!");
        }
    }
    
    GlassListener Class
    Code:
    public class GlassListener implements Listener {
        private GlassDrops plugin;
        public void GlassListener (GlassDrops plugin) {
              this.plugin = plugin;
        }
     
        @EventHandler(priority = EventPriority.NORMAL)
        public void onBlockBreak(BlockBreakEvent event) {
              Block block = event.getBlock();
        }
    }
    
    That should work for you.
    I've edited this quite a few times. Any other code you want to put in the onBlockBreak should be fine.
     
  10. Offline

    CRAZYxMUNK3Y

    I have watched them all (basics lot anyway).

    And sorry, i misunderstood...

    All works now.

    Thanks guys
     
Thread Status:
Not open for further replies.

Share This Page