What's wrong with this code?

Discussion in 'Plugin Development' started by Liam Allan, Aug 19, 2011.

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

    Liam Allan

    Here are the errors:

    Code:
    [18/08/2011 23:59:19] Liam Allan: 23:58:44 [SEVERE] Could not pass event PLAYER_INTERACT to BlockLock
    java.lang.NullPointerException
            at me.liamallan.BlockLock.BlockLockPlayerListener.onPlayerInteract(BlockLockPlayerListener.java:32)
            at org.bukkit.plugin.java.JavaPluginLoader$10.execute(JavaPluginLoader.java:307)
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:58)
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:332)
            at org.bukkit.craftbukkit.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:168)
            at net.minecraft.server.ItemInWorldManager.interact(ItemInWorldManager.java:210)
            at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:553)
            at net.minecraft.server.Packet15Place.a(SourceFile:57)
            at net.minecraft.server.NetworkManager.b(NetworkManager.java:226)
            at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:85)
            at net.minecraft.server.NetworkListenThread.a(SourceFile:105)
            at net.minecraft.server.MinecraftServer.h(MinecraftServer.java:451)
            at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:361)
            at net.minecraft.server.ThreadServerApplication.run(SourceFile:422)
    23:58:52 [SEVERE] Could not pass event BLOCK_BREAK to BlockLock
    java.lang.NullPointerException
            at me.liamallan.BlockLock.BlockLockBlockListener.onBlockBreak(BlockLockBlockListener.java:46)
            at org.bukkit.plugin.java.JavaPluginLoader$36.execute(JavaPluginLoader.java:490)
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:58)
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:332)
            at net.minecraft.server.ItemInWorldManager.c(ItemInWorldManager.java:157)
            at net.minecraft.server.ItemInWorldManager.a(ItemInWorldManager.java:121)
            at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:461)
            at net.minecraft.server.Packet14BlockDig.a(SourceFile:42)
            at net.minecraft.server.NetworkManager.b(NetworkManager.java:226)
            at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:85)
            at net.minecraft.server.NetworkListenThread.a(SourceFile:105)
            at net.minecraft.server.MinecraftServer.h(MinecraftServer.java:451)
            at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:361)
            at net.minecraft.server.ThreadServerApplication.run(SourceFile:422)
    >

    Here's the onPlayerInteract:

    Code:
          public void onPlayerInteract(PlayerInteractEvent event){
            Player player = event.getPlayer();
            Block block = event.getClickedBlock();
            Location loc = event.getClickedBlock().getLocation();
            String blockloc = loc.getBlockX() + "," + loc.getBlockY() + "," + loc.getBlockZ();
     
              if(event.getAction() == Action.RIGHT_CLICK_BLOCK){
                ItemStack item_in_hand = player.getItemInHand();
    
                    if(block.getType() == Material.CHEST){
                        if(plugin.config.getString(blockloc).equals(player.getName() !=null)) {
                            player.sendMessage("You have opened your chest");
                        } else {
                            player.sendMessage("You can't look in this chest!");
                            event.setCancelled(true);
                        }
                        }
              }
            }
    And here's onBlockBreak

    Code:
      public void onBlockBreak(BlockBreakEvent event) {
            //loading events the blocklistener needs
            Player player = event.getPlayer();
            Block block = event.getBlock();
            Location loc = event.getBlock().getLocation();
            String blockloc = loc.getBlockX() + "," + loc.getBlockY() + "," + loc.getBlockZ();
    
            if(block.getType() == Material.CHEST) {
                if(plugin.config.getNode(blockloc).equals(player.getName()!=null))  {
                    player.sendMessage("You removed your protected chest!");
                    plugin.config.removeProperty(blockloc);
                } else {
                    player.sendMessage("You can't break this chest!");
                    event.setCancelled(true);
                }
            }
          }
    And it does write 'blockloc: playername' to a file, Check and it does work when they place the chest :D There are no problems with this code . . .
    Code:
      public void onBlockPlace(BlockPlaceEvent event) {
        //loading events the blocklistener needs
        Player player = event.getPlayer();
        Block block = event.getBlockPlaced();
        Location loc = event.getBlockPlaced().getLocation();
        String blockloc = loc.getBlockX() + "," + loc.getBlockY() + "," + loc.getBlockZ();
    
        if(block.getType() == Material.CHEST) {
            player.sendMessage("This chest has been protected under your name!");
            plugin.writeToConfig(blockloc + ": " + player.getName());
        }
      }

    Please help!
     
  2. Offline

    bassfader

    I'd try to remove the "!= null" from these statements "if(plugin.config.getString(blockloc).equals(player.getName() !=null))", don't really know what you are trying to achive with that xD

    Otherwise if that doesn't help please tell us where the lines are where the erros occur (BlockLockPlayerListener.java:32 and BlockLockBlockListener.java:46 - in other words: what is line 32 in your Player listener and what is line 46 in your BlockListener?)
     
  3. Offline

    Liam Allan

    Playerlistener line32: if(plugin.config.getString(blockloc).equals(player.getName() !=null)) {
    Blocklistener line46: if(plugin.config.getString(blockloc).equals(player.getName()!=null)) {

    :D

    I removed the !=null and I still get the errors!
    Code:
    10:57:39 [SEVERE] Could not pass event PLAYER_INTERACT to BlockLock
    java.lang.NullPointerException
            at me.liamallan.BlockLock.BlockLockPlayerListener.onPlayerInteract(BlockLockPlayerListener.java:32)
            at org.bukkit.plugin.java.JavaPluginLoader$10.execute(JavaPluginLoader.java:307)
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:58)
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:332)
            at org.bukkit.craftbukkit.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:168)
            at net.minecraft.server.ItemInWorldManager.interact(ItemInWorldManager.java:210)
            at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:553)
            at net.minecraft.server.Packet15Place.a(SourceFile:57)
            at net.minecraft.server.NetworkManager.b(NetworkManager.java:226)
            at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:85)
            at net.minecraft.server.NetworkListenThread.a(SourceFile:105)
            at net.minecraft.server.MinecraftServer.h(MinecraftServer.java:451)
            at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:361)
            at net.minecraft.server.ThreadServerApplication.run(SourceFile:422)
    10:57:45 [SEVERE] Could not pass event BLOCK_BREAK to BlockLock
    java.lang.NullPointerException
            at me.liamallan.BlockLock.BlockLockBlockListener.onBlockBreak(BlockLockBlockListener.java:46)
            at org.bukkit.plugin.java.JavaPluginLoader$36.execute(JavaPluginLoader.java:490)
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:58)
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:332)
            at net.minecraft.server.ItemInWorldManager.c(ItemInWorldManager.java:157)
            at net.minecraft.server.ItemInWorldManager.a(ItemInWorldManager.java:121)
            at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:461)
            at net.minecraft.server.Packet14BlockDig.a(SourceFile:42)
            at net.minecraft.server.NetworkManager.b(NetworkManager.java:226)
            at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:85)
            at net.minecraft.server.NetworkListenThread.a(SourceFile:105)
            at net.minecraft.server.MinecraftServer.h(MinecraftServer.java:451)
            at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:361)
            at net.minecraft.server.ThreadServerApplication.run(SourceFile:422)
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 18, 2016
  4. Offline

    bassfader

    Well config.getString() can return null if it doesn't find the node (blockloc in your case), so equals may throw the null pointer exceptions since equals cant be called on null. Try checking the result before doing an equals on it.

    Also I still dont understand what you are trying to achive with the "!= null" within the equals command. Basically you will check if getString(blocklic) equals to true or false since you have another comparison within the equals (player.getName() is NOT null) which will return either true or false, but not the player name as its supposed to (at least if I understand what you are trying to do).
     
  5. Offline

    Liam Allan

    So? How do I check the result?
     
  6. Offline

    Samkio

    if(plugin.config.getString(blockloc)!=null){
    //Do the rest of teh stuff.
    }
     
  7. Offline

    Liam Allan

    Does this check for the name to? They can't access it if it's not there name :D

    it will check that string for there name:
    File example:
    X,Y,Z: name
    If the name is someone else's, it won't let them open it: event.setcancelled

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 18, 2016
  8. Offline

    Samkio

    Code:
    if(block.getType() == Material.CHEST) {
    if(plugin.config.getString(blockloc)!=null){
                if(plugin.config.getNode(blockloc).equals(player.getName()!=null))  {
                    player.sendMessage("You removed your protected chest!");
                    plugin.config.removeProperty(blockloc);
                } else {
                    player.sendMessage("You can't break this chest!");
                    event.setCancelled(true);
                }
    }
            }
     
  9. Offline

    Liam Allan

    it removes the errors. But still allows anyone to enter the chest . . .
     
  10. Offline

    Samkio

    @Liam Allan
    Maybe:
    plugin.config.getString(blockloc)

    Is always null.

    Thus everychest is deemed a normal chest.
    Check that works.
     
  11. Offline

    Liam Allan

    So? Would I put what the code is instead of the string?
    E.G. loc.getBlockX() + "," + loc.getBlockY() + "," + loc.getBlockZ()
     
  12. Offline

    Samkio

    @Liam Allan
    Can i see the entire code please? xD
    Zip etc. Will be much quicker.
     
  13. Offline

    Liam Allan

    here :D
     

    Attached Files:

  14. Offline

    dsmyth1915

    I know i'm a novice at coding but, I'd like to know something. in blocklocblocklistener.class there's this code.
    Code:
    if ((block.getType() == Material.CHEST) &&
          (plugin.config.getString(blockloc) != null))
          if (plugin.config.getNode(blockloc).equals(Boolean.valueOf(player.getName() != null))) {
            player.sendMessage("You removed your protected chest!");
            plugin.config.removeProperty(blockloc);
          } else {
            player.sendMessage("You can't break this chest!");
            event.setCancelled(true);
    the if (plugin.config.getNode(blockloc).equals(Boolean.valueOf(player.getName() != null))) line, it seems to my knowledge it checks the node blockloc is the value of the playername? or am I reading it wrong.
     
  15. Offline

    Liam Allan

    It's still letting other people into the chest :( :D

    It's meant to check if it's the players name :D

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 18, 2016
  16. Offline

    bassfader

    Looking into it, gimme a second :p
     
  17. Offline

    Liam Allan

    <3
     
  18. Offline

    Samkio

    Instead of writing the string to the file.
    Try using:
    config.setProperty(loc,name);
     
  19. Offline

    Liam Allan

    I'm not setting a property anywhere, So why would I need this?
     
  20. Offline

    Samkio

    @Liam Allan
    When people place a chest it autolocks for them.
    And writes a propety to be read later no?
     
  21. Offline

    bassfader

    Change this line:
    Code:
    if(plugin.config.getNode(blockloc).equals(player.getName()!=null))
    To this:
    Code:
    if(plugin.config.getNode(blockloc).equalsIgnoreCase(player.getName()))
    Like I said above, the !=null stuff you have in there just does not make any sense (in that case).

     
  22. Offline

    Liam Allan

    Yes, But thats different to: plugin.writeToConfig("Blah");
    Why would I want: config.setProperty(loc,name); if the property does not exist
     
  23. Offline

    bassfader

    Also you'd be much better of using a Hashmap by the way, instead of this Configuration sh... (sorry but really).

    You can have a Hashmap with Player as Key and a List of blocks as Value: Map<Player, List<Block>> yourMap = new HashMap<Player, List<Block>>()

    If you want to save this one to a file you'd need some more code which is not that easy but it will work much better, maybe just google for "save a hashmap to file" or something like that.
     
  24. Offline

    Liam Allan

    Fu . . . . I know nothing of HashMaps!

    Don't worry about it everybody! I give up :D

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 18, 2016
  25. Offline

    dsmyth1915

    I knew there was something wrong with that.
     
  26. Offline

    feildmaster

    He needs to use the config function I gave him. -_-

    He was calling from a config he wasn't saving to. (That's his problem)
    He was saving directly to the file, which doesn't set the properties in runtime. (Because he's not using the built in functions...)
     
Thread Status:
Not open for further replies.

Share This Page