Why is the ArrayList.contains false (Minecraft, Java)?

Sp
7

Source code:

Why does the query (line 9) become false when I query the content of the list there's CraftPlayer: Playername, so the player is still in the list

Ul

Error message?

Sp

No

Ba

Did the Player class override the equals () method from class Object?

If not, the result is clear. Contains () can't check two objects for the same content.

Ul

Why does the query (line 9) become false when I query the content of the list there's CraftPlayer: Playername, so the player is still in the list

Then work with the UUID in the list and rewrite your list, problem solved

Sp

And how exactly do I solve the problem?

Ba

In the Object class, look at the equals () method. You write such a method in class Player with the same name, return value and parameters.

First, check if the argument is from the Player class. If not, return false ;. Check with instanceof.

Then make the argument as a player using a cast (player). After all, you need to know when two instances are supposed to be the same. According to return true; or return false ;.

While you're at it: check out the hashCode () method. Otherwise you will soon stumble across it.

Lo

Good question