Add in a system to detect if a PlayerDeathEvent is an invalid duplicate event.

This commit is contained in:
Olof Larsson 2013-08-27 10:56:11 +02:00
parent d21858d5c7
commit 296d0cbdb2
2 changed files with 65 additions and 11 deletions

View File

@ -135,7 +135,6 @@ public class MCore extends MPlugin
//db = MStore.getDb(ConfServer.dburi);
// Setup PlayerUtil and it's events
new PlayerUtil(this);
SenderIdMixinDefault.get().setup();
// Register events
@ -144,6 +143,7 @@ public class MCore extends MPlugin
TeleportMixinCauseEngine.get().setup();
EngineWorldNameSet.get().setup();
EngineOfflineCase.get().setup();
PlayerUtil.get().setup();
// Schedule the collection ticker.
Bukkit.getScheduler().scheduleSyncRepeatingTask(this, this.collTickTask, 1, 1);

View File

@ -1,5 +1,7 @@
package com.massivecraft.mcore.util;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentSkipListSet;
@ -12,35 +14,87 @@ import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.plugin.Plugin;
import com.massivecraft.mcore.MCore;
public class PlayerUtil implements Listener
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static PlayerUtil i = new PlayerUtil();
public static PlayerUtil get() { return i; }
// -------------------------------------------- //
// FIELDS
// -------------------------------------------- //
protected static Set<String> joinedPlayerNames = new ConcurrentSkipListSet<String>(String.CASE_INSENSITIVE_ORDER);
private static Set<String> joinedPlayerNames = new ConcurrentSkipListSet<String>(String.CASE_INSENSITIVE_ORDER);
private static Map<String, PlayerDeathEvent> lowercaseToDeath = new HashMap<String, PlayerDeathEvent>();
// -------------------------------------------- //
// CONSTRUCTOR AND EVENT LISTENER
// SETUP
// -------------------------------------------- //
public PlayerUtil(Plugin plugin)
public void setup()
{
Bukkit.getPluginManager().registerEvents(this, plugin);
joinedPlayerNames.clear();
for (Player player : Bukkit.getOnlinePlayers())
{
joinedPlayerNames.add(player.getName());
}
Bukkit.getPluginManager().registerEvents(this, MCore.get());
}
// -------------------------------------------- //
// IS DUPLICATE DEATH EVENT
// -------------------------------------------- //
// Some times when players die the PlayerDeathEvent is fired twice.
// We want to ignore the extra calls.
public static boolean isDuplicateDeathEvent(PlayerDeathEvent event)
{
// Prepare the lowercase name ...
final String lowercase = event.getEntity().getName().toLowerCase();
// ... take a look at the currently stored event ...
PlayerDeathEvent current = lowercaseToDeath.get(lowercase);
if (current != null) return !current.equals(event);
// ... otherwise store ...
lowercaseToDeath.put(lowercase, event);
// ... schedule removal ...
Bukkit.getScheduler().scheduleSyncDelayedTask(MCore.get(), new Runnable()
{
@Override
public void run()
{
lowercaseToDeath.remove(lowercase);
}
});
// ... and return the fact that it was not a duplicate.
return false;
}
@EventHandler(priority = EventPriority.LOWEST)
public void isDuplicateDeathEventLowest(PlayerDeathEvent event)
{
isDuplicateDeathEvent(event);
}
// -------------------------------------------- //
// IS JOINED
// -------------------------------------------- //
@EventHandler(priority = EventPriority.MONITOR)
public void joinMonitor(PlayerJoinEvent event)
{
@ -62,16 +116,16 @@ public class PlayerUtil implements Listener
joinedPlayerNames.remove(playerName);
}
// -------------------------------------------- //
// PUBLIC METHODS
// -------------------------------------------- //
public static boolean isJoined(Player player)
{
if (player == null) throw new NullPointerException("player was null");
return joinedPlayerNames.contains(player.getName());
}
// -------------------------------------------- //
// PACKET
// -------------------------------------------- //
/**
* Updates the players food and health information.
*/