Comparing two chunks doesn't work properly

Discussion in 'Plugin Development' started by Schwarzer Zylinder, Jan 22, 2012.

Thread Status:
Not open for further replies.
  1. Hey,
    I have to handle the events ChunkLoad and ChunkUnload. I have to check, whether a specified location is in this new (un)loaded chunks. The java method .equals() doesn't work, always returning false. The == operator is always returning false, too.
    I tried with chunk.getX() == otherChunk.getX() and this is returning a correct value.
    But there is one problem: I am in this chunk and it says me the chunk gets loaded or unloaded every 20 seconds or so. Is this because the mehod doesn't work or is it normal chunk updating?
     
  2. Offline

    Raphfrk

    I assume you check both x and y values ?

    You could submit a pull request. Something like

    Code:
        @Override
        public boolean equals(Object obj) {
            if (obj == null) {
                return false;
            }
            if (obj == this) {
                return true;
            }
            if (!(obj instanceof Chunk) {
                return false;
            }
    
            final Chunk otherChunk = (Chunk)obj;
    
            return otherChunk.getWorld().equals(getWorld()) && getX() == otherChunk.getX() && getZ() == otherChunk.getZ();
        }
    
     
  3. I have this code: The chunkMatch() method seems to work correctly, I tested it.
    I set up a guard and teleported directly behind him. If I used my own player location, it was detected, if I used the guards location, not...

    The two methods are called from onChunkLoad() and onChunkUnload() and uses event.getChunk()

    Code:
    public void deleteGuardsInChunk(Chunk chunk) {
            for(Guard guard : guardsList) {
                if(chunkMatch(guard.getLocation().getChunk(), chunk)) {guard.removeCreature();System.out.println("b");}
            }
        }
     
        public void reloadGuardsInChunk(Chunk chunk) {
            for(Guard guard : guardsList) {
                //If a guard is in the chunk
                if(chunkMatch(guard.getLocation().getChunk(), chunk)) {guard.reloadCreature();System.out.println("c");}
            }
        }
     
        private boolean chunkMatch(Chunk chunk1, Chunk chunk2) {
            return chunk1.getX() == chunk2.getX() && chunk1.getZ() == chunk2.getZ();
        }
     
Thread Status:
Not open for further replies.

Share This Page