FatBoy-DOTC
5 months ago
2 changed files with 45 additions and 1 deletions
@ -0,0 +1,43 @@ |
|||||||
|
package engine.util; |
||||||
|
|
||||||
|
import engine.objects.PlayerCharacter; |
||||||
|
|
||||||
|
import java.util.ArrayList; |
||||||
|
import java.util.HashMap; |
||||||
|
|
||||||
|
public class BoxTracker { |
||||||
|
private static final HashMap<String, ArrayList<PlayerCharacter>> players_by_machine = new HashMap<>(); |
||||||
|
|
||||||
|
public static void addPlayer(String machineId, PlayerCharacter player) { |
||||||
|
synchronized (players_by_machine) { |
||||||
|
ArrayList<PlayerCharacter> players = players_by_machine.get(machineId); |
||||||
|
if (players != null) { |
||||||
|
player.isBoxed = true; |
||||||
|
players.add(player); |
||||||
|
} else { |
||||||
|
players = new ArrayList<>(); |
||||||
|
players.add(player); |
||||||
|
players_by_machine.put(machineId, players); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public static ArrayList<PlayerCharacter> getPlayers(String machineId) { |
||||||
|
synchronized (players_by_machine) { |
||||||
|
return new ArrayList<>(players_by_machine.getOrDefault(machineId, new ArrayList<>())); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public static void removePlayer(String machineId, PlayerCharacter player) { |
||||||
|
synchronized (players_by_machine) { |
||||||
|
ArrayList<PlayerCharacter> players = players_by_machine.get(machineId); |
||||||
|
if (players != null) { |
||||||
|
players.remove(player); |
||||||
|
if (players.isEmpty()) { |
||||||
|
players_by_machine.remove(machineId); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
} |
Loading…
Reference in new issue