Solved List/HashMap bugs? D:

Discussion in 'Plugin Development' started by thebigdolphin1, Jan 30, 2013.

Thread Status:
Not open for further replies.
  1. Code:
            Sign sign = (Sign) signGameLocs.get(gameNumber).getBlock().getState();
            Bukkit.broadcastMessage(signGameLocs.get(gameNumber).toString()); //DEBUG
            Location torchLoc = signGameLocs.get(gameNumber);
            Bukkit.broadcastMessage(signGameLocs.get(gameNumber).toString()); //DEBUG
            torchLoc.setY(signGameLocs.get(gameNumber).getY() - 5);
            Bukkit.broadcastMessage(signGameLocs.get(gameNumber).toString()); //DEBUG
            Block torch = torchLoc.getBlock();
            Bukkit.broadcastMessage(signGameLocs.get(gameNumber).toString()); //DEBUG
    Heres the DEBUG messages from the console (in same order as above):
    Code:
    17:55:12 [INFO] Location{world=CraftWorld{name=HG_Lobby},x=511.0,y=14.0,z=-408.0,pitch=0.0,yaw=0.0}
    17:55:12 [INFO] Location{world=CraftWorld{name=HG_Lobby},x=511.0,y=14.0,z=-408.0,pitch=0.0,yaw=0.0}
    17:55:12 [INFO] Location{world=CraftWorld{name=HG_Lobby},x=511.0,y=9.0,z=-408.0,pitch=0.0,yaw=0.0}
    17:55:12 [INFO] Location{world=CraftWorld{name=HG_Lobby},x=511.0,y=9.0,z=-408.0,pitch=0.0,yaw=0.0}
    The -5 seems to be taking it away from the actual List/HashMap.
     
  2. Offline

    ZachBora

    You need to use .clone()
    When you store a location, it does not copy it references it, so any changes will affect the object of this location.
     
  3. ZachBora
    Thankyou SO MUCH!
    :D
    Although, ive never come across this problem before... Is it just with Lists/HashMaps?
     
  4. Offline

    ZachBora

    I'm fairly certain you'd have the same problem with a Location[] or a Set<Location>
     
  5. Offline

    EnvisionRed

    When you're calling signGameLocs.get(gamenumber) you're getting the exact same location object as is stored in signGameLocs. That's why any changes you make are reflected in the list. You needed to do clone to obtain an exact copy of that object. The copy can be changed without affecting what's in the list. I know this was already solved, but I felt like it needed some explanation on top of the answer.
     
  6. EnvisionRed
    But usually, with a Variable (for example here, int LOLWTF = 5;), if you do some kind of thing, for example, bukkit.broadcastMessage(LOLWTF - 2);, it wouldnt change the variable. But with lists, it seems to.
     
  7. Offline

    EnvisionRed

    LOLWTF -2 doesn't change the value of LOLWTF. The Location.setY method changes internal fields belonging to the class. It's different.
     
  8. EnvisionRed
    Exactly, setY(#) should change it, not getting it and using it as a Double/int. Still, .clone() works.
    Must be a bug or something.
     
  9. Offline

    ZachBora

    You're still not understanding.

    When you do :
    Location torchLoc = signGameLocs.get(gameNumber);

    It puts in torchLoc a "pointer", not a copy.
    When you do :

    torchLoc.setY(signGameLocs.get(gameNumber).getY() - 5);

    It changes both torchLoc and signGameLocs.get(gameNumber).
     
  10. ZachBora
    Ok, I think I see what you mean!
    Thanks! :)
    Is there anything else that does this? Because strings, ints, doubles, eg, dont do this...
     
  11. Offline

    ZachBora

    Those are primitives types. Usually anything that is an object will do it. But I am not sure exactly. Just pretend that anything will do this aside from primitives types.
     
Thread Status:
Not open for further replies.

Share This Page