I am working on a Survival Games part of my server, and I am fairly new to java+bukkit. What would be the best way of going about this: I want to make it so if a player has left the server for a certain amount of times, he resigns from the hunger games. (if he leaves the server for 2 minutes (for example), he will be removed from the tournament). Players would also be removed from the hunger games if they exit the server a certain amount of times (If a player leaves then rejoins 4 times (for example), he will resign from the tournament the 5th time he quits the server). Thanks if you can help! It's appreciated
Add a PlayerQuitEvent and add the current time to a hashmap. When the player joins get the current time and compare whether he was away for longer than 2 minutes. Code: private Map<String, Long> times = new HashMap<String, Long>(); @EventHandler public void onPlayerQuit(PlayerQuitEvent event) { times.put(event.getPlayer().getName(), System.currentTimeMillis()); } @EventHandler public void onPlayerJoin(PlayerJoinEvent event) { if(System.currentTimeMillis() - 120000 > times.get(event.getPlayer().getName())) { //remove player from game... } }
Are hashmaps like arrays, except instead of using an integer for an index, you use a string (in this case)?
Yes... HashMap<Player, List<Location>> That would be like an array containing Lists and with Players as the indexes...
oh, one more thing. How can i make it so once the person has been off the server for a certain amount of time, the server removes the player from the survival games? The player might log off, then not come back on for a few hours, and that would cause the games to not end until the player comes back on.
For that you have to schedule a SyncDelayedTask with the delay that you want. After that the delay, remove the player from the game. If he reconnects to the server, stop the task... (for that you have to save the taskID)
That was terrible advice! Don't store the Player object out side of the method scope... Instead use the players name. The reason for that is when you store the object, it can not be removed from memory after a player logs out. This in the end can cause duplicate objects, (if you're unlucky). And memory leaks are pretty much around the corner as well, seeing how the object remains as a floating one in memory until you and all other things that store it unload it.