HashMaps not reconising blocks ?

Discussion in 'Plugin Development' started by Slipswhitley, Nov 25, 2012.

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

    Slipswhitley

    Hey,

    As far as I'm concerned this is fine but I'm not sure why it thinks the block in the hashmap is different to the clicked block (note. i trimmed down the code to only key factors)

    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(cmd.getName().equalsIgnoreCase("setbutton")) {
                Player player = (Player) sender;
                Block targetblock = player.getTargetBlock(null, 5);
                if(targetblock.getType() == Material.STONE_BUTTON) {
                    map.put("MyButton", block);
                }
                return true;
            }
            return false;
        }
       
        public void onInteract(PlayerInteractEvent Event) {
            if(Event.getClickedBlock() == map.get("MyButton")) {
                System.out.println("This is my button");
            }
        }
     
  2. Offline

    ZeusAllMighty11

    Why not store the blocktype and location, instead of using strings that are failing?

    Benefits:
    - Multiple buttons in different locations
    - Simple
     
  3. Offline

    Slipswhitley

    BlockTypes would not work because i need a specific block and Location have the same problem

    Ugh solved by i have no idea this didn't work

    Code:
    if(loc == block.getLocation())
    And for some retarded reason this did
    if(loc.getX() == block.getLocation().getX() && loc.getZ() == block.getLocation().getZ() && loc.getY() == block.getLocation().getY())

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  4. You really shouldn't save Blocks or Locations over time. The problem is that they have a reference to the chunk, so when the chunk unloads you prevent it from being garbage collected. Also they have a reference to the world (from the chunk), so if the world unloads you prevent it from being garbage collected, too. Both is a memory leak.

    Use some form of abstraction instead, like this: https://github.com/V10lator/PortalStick/blob/master/src/de/V10lator/PortalStick/V10Location.java

    //EDIT: Also == can't work like you want, cause bukkit gives you a new instance of Block/Location every time, but == compares instances. The right way would have been to use equals().
     
  5. Offline

    fireblast709

    why not just save a toString() from the location, as it has the world, x, y and z (which makes it kind of unique) and it does not leave any memory leaks afaik
     
Thread Status:
Not open for further replies.

Share This Page