if(List.contains(new Object(int x, int y, int z))) Help!

Discussion in 'Plugin Development' started by Prgr, Jul 3, 2012.

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

    Prgr

    Ok I need to make sure I don't have duplicate blocks in my list so I was using this method of adding blocks to a linkedList
    PHP:
      protected void addBlock(int x,int yint z){
        if(!
    blocks.contains(new pBlock(x,y,z)))
          
    blocks.add(new pBlock(x,y,z));
      }
    Trying to not have duplicate block coordinates.
    Does any one know what I am trying to do?

    public class pBlock {
    public int x;
    public int y;
    public int z;

    public pBlock (int x, int y, int z) {
    this.x = x;
    this.y = y;
    this.z = z;
    }
    }
     
  2. Offline

    LucasEmanuel

    Okey let me explain what your if-statement will do:

    if(!blocks.contains(new pBlock(x,y,z)))

    "Hey i just created a new object! Does the list contain this new object?"

    The answer will always be false since that object was just created and obviously wasnt added prior the if-statement to the list.

    In your adding statement below you are creating yet another pBlock instance. You are not storing a reference to an existing object. Instead, try to store a reference to the actual object like: "blocks.add(world.getBlockAt(x,y,z))"

    Then you can call the "blocks.contains(world.getBlockAt(x,y,z))" to see if you have stored the block.

    But BEWERE! Dont do this with alot of blocks! That way youll have a memory leak and a laggy server. Try to get it down to primitive data values and store those instead.
     
  3. Offline

    Prgr

    Thanks for the comment! I realized why this doesn't work but I was curious if there was a way to do this without using World.getBlockAt() because of overhead memory, that is why I am asking. Any other suggestions?
     
  4. Offline

    tomjw64

    Except that would always return false because blocks doesn't contain Blocks, it contains pBlocks. I do agree with the new creation of an object thing though. :p

    Prgr
    I think your best bet is to check each int for equality in a for loop.
     
  5. Offline

    nisovin

    Add an equals() method to your pBlock object. Something like this:

    Code:
    @Override
    public boolean equals(Object o) {
        if (o instanceof pBlock) {
            pBlock b = (pBlock)o;
            return b.x == this.x && b.y == this.y && b.z == this.z;
        }
        return false;
    }
    For efficiency, you should also only create a new object once:

    Code:
      protected void addBlock(int x,int y, int z){
        pBlock b = new pBlock(x,y,z);
        if(!blocks.contains(b))
          blocks.add(b);
      }
    
     
    tomjw64 likes this.
  6. Offline

    desht

    Prgr you said "linked list" at the top of your post. Seriously, don't use a linked list for this, unless you really enjoy having your server telling you it can't keep up. contains() on any kind of List with a non-trivial number of elements isn't going to be efficient. Use a HashSet to hold your objects, and ensure you have a valid hashCode() method in your pBlock class. Eclipse can generate one for you (Source -> Generate hashCode() and equals()) and I'm sure other IDE's can too.
     
Thread Status:
Not open for further replies.

Share This Page