Using blocks as Keys

Discussion in 'Plugin Development' started by Lologarithm, Feb 2, 2011.

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

    Lologarithm

    Hello!

    In my mod people push a button and it turns on a stargate. To quickly check to see if a button they pressed was part of a stargate I have a hashmap mapping the buttons to the gates (HashMap<Block, Stargate> ). Previously everything worked fine, but with a recent update to Bukkit no longer is a block being hit the same block that is in my HashMap after a chunk is unloaded/reloaded.

    So my question is, if I can't rely on a Block object staying the same object, what is the most efficient way of storing this data? Should I wrap the Block class and write a custom .equals to be used as the key? Should I just create something like a 3d point class to just store where the button is? But either way I have to construct some class in order to compare...

    So when it gets more complicated and I have to check every move to see if the block a player is moving on is a block inside of an active gate. I don't want to have to construct an object every single time. If you have a bunch of players all moving that is a lot of 'key objects' being constructed all the time.

    What would you all suggest?
    --- merged: Feb 3, 2011 6:07 AM ---
    Would this work very well?

    Took this from the BlockCoordinate class...

    Code:
        public final int x;
        public final int y;
        public final int z;
    
        public BlockCoordinate(Block b)
        {
            x = b.getX();
            y = b.getY();
            z = b.getZ();
        }
    
        @Override
        public boolean equals(Object o)
        {
            if ( o == null )
            {
                return false;
            }
    
            if ( this.getClass() != o.getClass() )
            {
                return false;
            }
            BlockCoordinate bc = (BlockCoordinate) o;
            if  ( bc.x != this.x )
                return false;
            if  ( bc.y != this.y )
                return false;
            if  ( bc.z != this.z )
                return false;
    
            return true;
        }
     
  2. Offline

    fullwall

    Just change the HashMap from a Block to a Location, which is always constant, and it should work fine. Might be more memory efficient too :)
    Location constructor - Location(x,y,z,world(just use player.getWorld()))
     
  3. Offline

    Lologarithm

    I will try that out - thanks for the idea!
     
  4. Offline

    Raphfrk

    In ServerPort, I have an IntLocation class that is basically the block integer coordinates ... though that is legacy from hmod.
     
  5. Offline

    Lologarithm

    Ya, I think using location is in the end better: I don't have to construct any new objects each time, and there is no extra memory use (other than the pointer to the Location object).

    My first 'fix' was using that 'BlockCoordinate' class, which works fine, but there is a def increase in CPU/RAM due to needing to create so many new objects all the time.
     
Thread Status:
Not open for further replies.

Share This Page