Various sender reference improvements. Fixes MassiveCraft/Factions#726.

This commit is contained in:
Olof Larsson 2014-12-10 10:28:10 +01:00
parent 1eaf1cbfe5
commit c1db14eebe
2 changed files with 51 additions and 15 deletions

View File

@ -16,7 +16,6 @@ import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
import org.bukkit.event.player.AsyncPlayerChatEvent; import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerKickEvent; import org.bukkit.event.player.PlayerKickEvent;
import org.bukkit.event.player.PlayerLoginEvent; import org.bukkit.event.player.PlayerLoginEvent;
import org.bukkit.event.player.PlayerLoginEvent.Result;
import org.bukkit.event.player.PlayerChatTabCompleteEvent; import org.bukkit.event.player.PlayerChatTabCompleteEvent;
import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.player.PlayerRespawnEvent; import org.bukkit.event.player.PlayerRespawnEvent;
@ -174,6 +173,7 @@ public class MassiveCoreEngineMain extends EngineAbstract
// Note: For now we update both names and ids. // Note: For now we update both names and ids.
// That way collections in plugins that haven't yet undergone update will still work. // That way collections in plugins that haven't yet undergone update will still work.
// This method sets the sender reference to what you decide.
public static void setSenderReferences(CommandSender sender, CommandSender reference) public static void setSenderReferences(CommandSender sender, CommandSender reference)
{ {
String id = IdUtil.getId(sender); String id = IdUtil.getId(sender);
@ -189,35 +189,67 @@ public class MassiveCoreEngineMain extends EngineAbstract
} }
} }
// This method sets the sender reference based on it's online state.
public static void setSenderReferences(CommandSender sender)
{
CommandSender reference = sender;
if (sender instanceof Player)
{
Player player = (Player)sender;
if ( ! player.isOnline())
{
reference = null;
}
}
setSenderReferences(sender, reference);
}
// Same as above but next tick.
public static void setSenderReferencesSoon(final CommandSender sender)
{
Bukkit.getScheduler().scheduleSyncDelayedTask(MassiveCore.get(), new Runnable()
{
@Override
public void run()
{
setSenderReferences(sender);
}
});
}
@EventHandler(priority = EventPriority.LOWEST) @EventHandler(priority = EventPriority.LOWEST)
public void setSenderReferencesLoginLowest(PlayerLoginEvent event) public void setSenderReferencesLoginLowest(PlayerLoginEvent event)
{ {
setSenderReferences(event.getPlayer(), event.getPlayer()); final Player player = event.getPlayer();
}
@EventHandler(priority = EventPriority.MONITOR) // We set the reference at LOWEST so that it's present during this event.
public void setSenderReferencesLoginMonitor(PlayerLoginEvent event) setSenderReferences(player, player);
{
if (event.getResult() == Result.ALLOWED) return;
setSenderReferences(event.getPlayer(), null); // And the next tick we update the reference based on it's online state.
// Not all players succeed in logging in. They may for example be banned.
setSenderReferencesSoon(player);
} }
@EventHandler(priority = EventPriority.MONITOR) @EventHandler(priority = EventPriority.MONITOR)
public void setSenderReferencesQuitMonitor(PlayerQuitEvent event) public void setSenderReferencesQuitMonitor(PlayerQuitEvent event)
{ {
setSenderReferences(event.getPlayer(), null); // PlayerQuitEvents are /probably/ trustworthy.
// We check ourselves the next tick just to be on the safe side.
setSenderReferencesSoon(event.getPlayer());
} }
@EventHandler(priority = EventPriority.MONITOR) @EventHandler(priority = EventPriority.MONITOR)
public void setSenderReferencesRegisterMonitor(EventMassiveCoreSenderRegister event) public void setSenderReferencesRegisterMonitor(EventMassiveCoreSenderRegister event)
{ {
// This one we can however trust.
setSenderReferences(event.getSender(), event.getSender()); setSenderReferences(event.getSender(), event.getSender());
} }
@EventHandler(priority = EventPriority.MONITOR) @EventHandler(priority = EventPriority.MONITOR)
public void setSenderReferencesUnregisterMonitor(EventMassiveCoreSenderUnregister event) public void setSenderReferencesUnregisterMonitor(EventMassiveCoreSenderUnregister event)
{ {
// This one we can however trust.
setSenderReferences(event.getSender(), null); setSenderReferences(event.getSender(), null);
} }

View File

@ -25,14 +25,18 @@ public abstract class SenderEntity<E extends SenderEntity<E>> extends Entity<E>
protected transient boolean senderInitiated = false; protected transient boolean senderInitiated = false;
public CommandSender getSender() public CommandSender getSender()
{ {
if ( ! this.senderInitiated) if ( ! this.senderInitiated) this.initiateSender();
{
this.sender = IdUtil.getSender(this.getId());
this.senderInitiated = true;
}
return this.sender; return this.sender;
} }
public void initiateSender()
{
String id = this.getId();
if (id == null) return;
this.sender = IdUtil.getSender(id);
this.senderInitiated = true;
}
public String getName() public String getName()
{ {
return IdUtil.getName(this.getId()); return IdUtil.getName(this.getId());