0

I am creating a minigame engine for Minecraft with Bukkit, but it seems like there is a problem with adding a player to the game. After a player is being successfully added, he has the ability to join again, as a result, participating more than once.

It is really odd, because in my if-statement I do check if the HashMap with the participants contains the player key. If it does, it should not allow him to join the game again.

Here is my method.

public void addPlayer(Client client) {

    // Checks if the game is waiting, and the slots are sufficient.

    if (game.isWaiting() && !game.isFull() && !game.getClients().containsKey(client)) {

        Random random = new Random();

        int teamChance = random.nextInt(game.getTeams().size());

        // Adding the player to the match and assigning the default kit.

        game.getClients().put(client, game.getDefaultKit());

        // Adding the player to a team.

        game.getTeams().get(teamChance).add(client);

        // Checking if the slots are now maximum, prepare the game.

        if (game.isFull()) {
            prepareGame();
        }

        // If the player was added.

        if (game.getClients().containsKey(client))
        {
            Main.log("Added " + client.getName() + " (" + game.getCurrentSlots() + "/" + game.getSlots() + ")" + " to " + game.getType().getName() + " (" + game.getWorld().getName() + ").", Level.INFO);

            game.broadcast("Join", client.getName() + " (" + game.getCurrentSlots() + "/" + game.getSlots() + ")");

            client.getPlayer().teleport(game.getWorld().getSpawnLocation());

            client.reset();

            // Adding lobby items to the player's inventory.

            new LobbyManager(game);
        }
    }

From the Game class.

private HashMap<Client, Kit> clients;

// ...

public HashMap<Client, Kit> getClients() {
    return this.clients;
}

How do I modify this method in order to prevent a player being added to the game more than once?

Your help is much appreciated.


Client: http://pastebin.com/Fmgc0T3C

Unihedron
  • 10,902
  • 13
  • 62
  • 72
Thanos Paravantis
  • 7,393
  • 3
  • 17
  • 24
  • Have you implemented `equals()` and `hashCode()` in your `Client` class? – Craig Otis Oct 12 '14 at 13:20
  • Hmm, since I am a fairly new to Java, it seems like the player is being added, but twice because Client works like a wrapper class for the Player (in order to implement my own methods). No, I haven't. – Thanos Paravantis Oct 12 '14 at 13:22
  • are you working with threads? – conFusl Oct 12 '14 at 13:22
  • Yes, because since I am working with Bukkit, there are more 'Plugins' loaded to the server. But I am using only my own. – Thanos Paravantis Oct 12 '14 at 13:23
  • I would be extremely surprised if this was a thread race issue. My money is on equals and hashcode, 100%. – Craig Otis Oct 12 '14 at 13:30
  • Working on it, however I do not implement any interfaces, because I am only storing a Player from the constructor to a variable, and then wrapping it with other methods. So there must be the problem (?). http://pastebin.com/Fmgc0T3C - Client – Thanos Paravantis Oct 12 '14 at 13:32

1 Answers1

1

If it's possible that two threads are adding the same client, you should lock the operations where you add them to the games! When you are creating a new client object every time a player wants to join, you have to override equals() and hashCode(), because conainsKey will return false when you are comparing the same client in different client objects.

Community
  • 1
  • 1
conFusl
  • 919
  • 6
  • 12