当前位置: 动力学知识库 > 问答 > 编程问答 >

java - HashMap value and variable value match, but are not considered equal

问题描述:

I have a method that is called every time a player right-clicks a chest (Minecraft item), and if this block's location matches one of the values in the HashMap, it should check to see that the player's username matches the key in the HashMap. Simply put: is this location saved, and does this player who is trying to interact with it own it? My if statement to check if the player and the location match is not working correctly.

Problematic line:

if (DeathChestManager.deathChestLocation.get(playerName) == b.getLocation()) {}

HashMap and block (b) location values (what the `println()'s output):

HashMap value:

Location{world=CraftWorld{name=world},x=59.0,y=64.0,z=-30.0,pitch=0.0,yaw=0.0}

Block location value:

Location{world=CraftWorld{name=world},x=59.0,y=64.0,z=-30.0,pitch=0.0,yaw=0.0}

HashMap from the DeathChestManager class:

public static Map<String, Location> deathChestLocation = new HashMap<String, Location>();

PlayerInteract Class:

public void onPlayerInteract(PlayerInteractEvent e) {

Player p = e.getPlayer();

String playerName = p.getName();

Block b = e.getClickedBlock();

if (b != null && b.getType() == Material.CHEST) {

if (DeathChestManager.deathChestLocation.containsValue(b.getLocation())) {

e.setCancelled(true);

System.out.println(DeathChestManager.deathChestLocation.get(playerName));

System.out.println(b.getLocation());

if (DeathChestManager.deathChestLocation.get(playerName) == b.getLocation()) {

if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {

b.setType(Material.AIR);

Location loc = p.getLocation();

for (final ItemStack item : DeathChestManager.inventoryContents.get(p.getName())) {

int count = 0;

for (ItemStack i : p.getInventory().getContents()) {

if (i == null) {

count++;

} else if (i.getType() == Material.AIR) {

count++;

}

}

if (count == 0) {

if (item.getType() != Material.AIR) {

p.getWorld().dropItemNaturally(loc, item);

}

} else {

if (item != null) {

p.getInventory().addItem(item);

p.updateInventory();

}

}

}

DeathChestManager dcManager = new DeathChestManager();

dcManager.endTimer(p, p.getWorld().getName());

}

} else {

MessageManager.getInstance().severe(p, "You do not own that death-chest.");

}

}

}

}

网友答案:

Use .equals() as opposed to == when comparing objects. == will compare the objects' addresses in memory, whereas .equals() will check to see if they have the same values. Since two objects will very rarely have the same address you should never use == except for comparing primitive types (int, char, but String is not a primitive type!) where it doesn't matter.

So you want:

if (DeathChestManager.deathChestLocation.get(playerName).equals(b.getLocation())) {}
网友答案:

You are comparing object references. This will return true only if they both refer to same same instance. If you wish to compare the contents of these objects, use .equals().

网友答案:

You should use .equals(), not ==, when testing equality for anything other than primitives (int, boolean, etc.). This includes Strings.

分享给朋友:
您可能感兴趣的文章:
随机阅读: