Development Assistance Right Click Block Location...

Discussion in 'Plugin Help/Development/Requests' started by AppleMC, Jun 28, 2015.

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

    AppleMC

    I've set the X, Y, and Z value in my config after using /chest create... but when I right click the chest, it gives me a bunch of errors

    Code:
    package com.ChestLoot.Listeners;
    
    import java.util.ArrayList;
    
    import org.bukkit.block.Block;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    
    import com.ChestLoot.ChestLoot.ChestLoot;
    
    public class MyListener implements Listener {
       
        private ChestLoot plugin;
       
        private static ChestLoot instance;
       
        public ArrayList<String> string = new ArrayList<String>();
    
        public static ChestLoot getInstance() {
           
            return instance;
           
        }
       
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent e) {
           
            Action a = e.getAction();
            Player p = e.getPlayer();
           
            if (e.getAction().equals(a.RIGHT_CLICK_BLOCK)) {
               
                if (e.getClickedBlock().getTypeId() == 54) {
                   
                    if (e.getClickedBlock().getX() == plugin.getConfig().getInt("Chest.X")
                            && e.getClickedBlock().getY() == plugin.getConfig().getInt("Chest.Y")
                            && e.getClickedBlock().getZ() == plugin.getConfig().getInt("Chest.Z")) {
                       
                        e.setCancelled(true);
                       
                        if (p.getItemInHand().getTypeId() == 7) {
                           
                            p.giveExp(50);
                            p.sendMessage("§7[§bValyrian§7] &a" + "Congratulations! You have used your key for 50 EXP!");
                           
                        }
                       
                    }
                   
                }
               
            }
           
        }
       
    }
    Errors (open)

    6:07:06 PM [SEVERE] Could not pass event PlayerInteractEvent to ChestLoot v1
    6:07:06 PM org.bukkit.event.EventException
    6:07:06 PM at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:305) ~[spigot-1.8.3.jar:git-Spigot-dbe012b-61ef214]
    6:07:06 PM at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.8.3.jar:git-Spigot-dbe012b-61ef214]
    6:07:06 PM at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot-1.8.3.jar:git-Spigot-dbe012b-61ef214]
    6:07:06 PM at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot-1.8.3.jar:git-Spigot-dbe012b-61ef214]
    6:07:06 PM at org.bukkit.craftbukkit.v1_8_R2.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:226) [spigot-1.8.3.jar:git-Spigot-dbe012b-61ef214]
    6:07:06 PM at net.minecraft.server.v1_8_R2.PlayerInteractManager.interact(PlayerInteractManager.java:463) [spigot-1.8.3.jar:git-Spigot-dbe012b-61ef214]
    6:07:06 PM at net.minecraft.server.v1_8_R2.PlayerConnection.a(PlayerConnection.java:734) [spigot-1.8.3.jar:git-Spigot-dbe012b-61ef214]
    6:07:06 PM at net.minecraft.server.v1_8_R2.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:52) [spigot-1.8.3.jar:git-Spigot-dbe012b-61ef214]
    6:07:06 PM at net.minecraft.server.v1_8_R2.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:1) [spigot-1.8.3.jar:git-Spigot-dbe012b-61ef214]
    6:07:06 PM at net.minecraft.server.v1_8_R2.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.8.3.jar:git-Spigot-dbe012b-61ef214]
    6:07:06 PM at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_31]
    6:07:06 PM at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_31]
    6:07:06 PM at net.minecraft.server.v1_8_R2.SystemUtils.a(SourceFile:60) [spigot-1.8.3.jar:git-Spigot-dbe012b-61ef214]
    6:07:06 PM at net.minecraft.server.v1_8_R2.MinecraftServer.A(MinecraftServer.java:710) [spigot-1.8.3.jar:git-Spigot-dbe012b-61ef214]
    6:07:06 PM at net.minecraft.server.v1_8_R2.DedicatedServer.A(DedicatedServer.java:368) [spigot-1.8.3.jar:git-Spigot-dbe012b-61ef214]
    6:07:06 PM at net.minecraft.server.v1_8_R2.MinecraftServer.z(MinecraftServer.java:651) [spigot-1.8.3.jar:git-Spigot-dbe012b-61ef214]
    6:07:06 PM at net.minecraft.server.v1_8_R2.MinecraftServer.run(MinecraftServer.java:554) [spigot-1.8.3.jar:git-Spigot-dbe012b-61ef214]
    6:07:06 PM at java.lang.Thread.run(Unknown Source) [?:1.8.0_31]
    6:07:06 PM Caused by: java.lang.NullPointerException
    6:07:06 PM at com.ChestLoot.Listeners.MyListener.onPlayerInteract(MyListener.java:38) ~[?:?]
    6:07:06 PM at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_31]
    6:07:06 PM at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_31]
    6:07:06 PM at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_31]
    6:07:06 PM at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_31]
    6:07:06 PM at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:301) ~[spigot-1.8.3.jar:git-Spigot-dbe012b-61ef214]
    6:07:06 PM ... 17 more
     
  2. Offline

    nverdier

  3. Offline

    AppleMC

    @nverdier I've narrowed it down to plugin being null at plugin.getConfig()... but I haven't got the slightest idea how to fix this...
     
  4. Offline

    nverdier

    @AppleMC You never set plugin or instance to anything.

    Create a constructor which takes a ChestLoot object and set 'plugin' to that. When you instantiate the Listener class, (assuming from the ChestLoot class), simply add 'this' as a parameter.
     
  5. Offline

    AppleMC

    Code:
    public class MyListener implements Listener {
       
        private ChestLoot plugin;
       
        public void MyListener(ChestLoot instance) {
           
            plugin = instance;
           
        }
       
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent e) {
           
            Action a = e.getAction();
            Player p = e.getPlayer();
           
            if (e.getAction().equals(a.RIGHT_CLICK_BLOCK)) {
               
                if (e.getClickedBlock().getTypeId() == 54) {
                   
                    if (e.getClickedBlock().getX() == plugin.getConfig().getInt("Chest.X")
                            && e.getClickedBlock().getY() == plugin.getConfig().getInt("Chest.Y")
                            && e.getClickedBlock().getZ() == plugin.getConfig().getInt("Chest.Z")) {
                       
                        e.setCancelled(true);
                       
                        if (p.getItemInHand().getTypeId() == 7) {
                           
                            p.giveExp(50);
                            p.sendMessage("§7[§bValyrian§7] &a" + "Congratulations! You have used your key for 50 EXP!");
                           
                        }
                       
                    }
                   
                }
               
            }
           
        }
       
    }
    This is my current code, I have never really needed to refer back to plugin before, so I don't really know if I'm doing this right :/
     
  6. Offline

    nverdier

    @AppleMC Yeah that's correct. Where are you instantiating the class? Please post all relevant code.
     
  7. Offline

    AppleMC

    @nverdier
    Main Class:
    Code:
    package com.ChestLoot.ChestLoot;
    
    import java.util.logging.Logger;
    
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import com.ChestLoot.Commands.ChestLootCommand;
    import com.ChestLoot.Listeners.MyListener;
    
    public class ChestLoot extends JavaPlugin {
      
        public static ChestLoot plugin;
      
        private static ChestLoot instance;
      
        public final Logger logger = Logger.getLogger("Minecraft");
      
        public final MyListener pl = new MyListener();
      
        @Override
        public void onEnable() {
          
            PluginDescriptionFile p = this.getDescription();
          
            this.logger.info(p.getName() + " Version " + p.getVersion() + " has been enabled!");
          
            this.getCommand("chestloot").setExecutor(new ChestLootCommand(this));
          
            getConfig().options().copyDefaults(true);
            saveConfig();
          
            PluginManager pm = getServer().getPluginManager();
            pm.registerEvents(this.pl, this);
          
            instance = this;
          
        }
      
        @Override
        public void onDisable() {
          
            PluginDescriptionFile p = this.getDescription();
          
            this.logger.info(p.getName() + " Version " + p.getVersion() + " has been disabled!");
          
            instance = this;
          
        }
    
    }
     
  8. Offline

    nverdier

    You have to pass the instance of the main class as a parameter.
     
  9. Offline

    AppleMC

    @nverdier Lol I don't really know how to do this, as I never had to? :/ Is there some reference that I can look to?
     
  10. Offline

    ArmyArmy

    Change public final MyListener pl = new MyListener();
    to public final MyListener pl;

    and to onEnable() at the end add
    pl = new MyListener(this);
    pm.registerEvents(this.pl, this);
     
  11. Code:
    public void MyListener(ChestLoot instance) {
      plugin = instance;
    }
    This is a method, not a constructor >.<. Remove the void and in the main class call new MyListener(this) instead of new MyListener();
     
  12. Offline

    timtower Administrator Administrator Moderator

    Moved to Bukkit alternatives
     
Thread Status:
Not open for further replies.

Share This Page