From 2b11bc104295f6e477163c2fd44565283b65960e Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Thu, 18 Apr 2013 11:02:58 +0200 Subject: [PATCH] Working on the player listener slightly and trying to figure out how to handle the lastLoginMillis through Mixin instead of locally stored. --- src/com/massivecraft/factions/FPlayer.java | 14 ++- .../listeners/FactionsEntityListener.java | 2 +- .../listeners/FactionsPlayerListener.java | 95 +++++++++---------- 3 files changed, 54 insertions(+), 57 deletions(-) diff --git a/src/com/massivecraft/factions/FPlayer.java b/src/com/massivecraft/factions/FPlayer.java index da2bf3f7..647bee63 100644 --- a/src/com/massivecraft/factions/FPlayer.java +++ b/src/com/massivecraft/factions/FPlayer.java @@ -353,7 +353,7 @@ public class FPlayer extends SenderEntity implements EconomyParticipato public double getPower() { - this.updatePower(); + this.recalculatePower(); return this.power; } @@ -385,14 +385,13 @@ public class FPlayer extends SenderEntity implements EconomyParticipato return ConfServer.powerMin + this.powerBoost; } - public void updatePower() + public void recalculatePower() { - this.updatePower(this.isOnline()); + this.recalculatePower(this.isOnline()); } private static final transient long POWER_RECALCULATION_MINIMUM_WAIT_MILLIS = 10 * TimeUnit.MILLIS_PER_SECOND; - - public void updatePower(boolean online) + public void recalculatePower(boolean online) { // Is the player really on this server? // We use the sender ps mixin to fetch the current player location. @@ -498,17 +497,16 @@ public class FPlayer extends SenderEntity implements EconomyParticipato // -------------------------------------------- // // TODO + @Deprecated public void setLastLoginTime(long lastLoginTime) { - losePowerFromBeingOffline(); - //this.lastLoginTime = lastLoginTime; this.lastPowerUpdateTime = lastLoginTime; if (ConfServer.noPVPDamageToOthersForXSecondsAfterLogin > 0) { this.loginPvpDisabled = true; } } - + public boolean hasLoginPvpDisabled() { if (!loginPvpDisabled) diff --git a/src/com/massivecraft/factions/listeners/FactionsEntityListener.java b/src/com/massivecraft/factions/listeners/FactionsEntityListener.java index 53b4acdb..431f7689 100644 --- a/src/com/massivecraft/factions/listeners/FactionsEntityListener.java +++ b/src/com/massivecraft/factions/listeners/FactionsEntityListener.java @@ -81,7 +81,7 @@ public class FactionsEntityListener implements Listener // Call player onDeath if the event is not cancelled if ( ! powerLossEvent.isCancelled()) { - fplayer.onDeath(); + fplayer.setPower(fplayer.getPower() + ConfServer.powerPerDeath); } // Send the message from the powerLossEvent diff --git a/src/com/massivecraft/factions/listeners/FactionsPlayerListener.java b/src/com/massivecraft/factions/listeners/FactionsPlayerListener.java index 087e1ce1..c991d7ec 100644 --- a/src/com/massivecraft/factions/listeners/FactionsPlayerListener.java +++ b/src/com/massivecraft/factions/listeners/FactionsPlayerListener.java @@ -20,7 +20,6 @@ import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerKickEvent; import org.bukkit.event.player.PlayerMoveEvent; -import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.util.NumberConversions; import com.massivecraft.factions.BoardColl; @@ -34,7 +33,9 @@ import com.massivecraft.factions.Rel; import com.massivecraft.factions.TerritoryAccess; import com.massivecraft.factions.integration.SpoutFeatures; import com.massivecraft.factions.util.VisualizeUtil; +import com.massivecraft.mcore.event.MCorePlayerLeaveEvent; import com.massivecraft.mcore.ps.PS; +import com.massivecraft.mcore.util.MUtil; public class FactionsPlayerListener implements Listener @@ -42,91 +43,89 @@ public class FactionsPlayerListener implements Listener @EventHandler(priority = EventPriority.NORMAL) public void onPlayerJoin(PlayerJoinEvent event) { - // Make sure that all online players do have a fplayer. - final FPlayer me = FPlayerColl.get().get(event.getPlayer()); + // If a player is joining the server ... + Player player = event.getPlayer(); + FPlayer fplayer = FPlayerColl.get().get(player); + + // ... recalculate their power as if they were offline since last recalculation ... + fplayer.recalculatePower(false); + + // ... update the current chunk ... + fplayer.setCurrentChunk(PS.valueOf(event.getPlayer())); + + // ... notify the player about where they are ... + if ( ! SpoutFeatures.updateTerritoryDisplay(fplayer)) + { + fplayer.sendFactionHereMessage(); + } // Update the lastLoginTime for this fplayer - me.setLastLoginTime(System.currentTimeMillis()); - - // Store player's current Chunk and notify them where they are - me.setCurrentChunk(PS.valueOf(event.getPlayer())); - - if ( ! SpoutFeatures.updateTerritoryDisplay(me)) - { - me.sendFactionHereMessage(); - } + fplayer.setLastLoginTime(System.currentTimeMillis()); } @EventHandler(priority = EventPriority.NORMAL) - public void onPlayerQuit(PlayerQuitEvent event) + public void onPlayerLeave(MCorePlayerLeaveEvent event) { - FPlayer me = FPlayerColl.get().get(event.getPlayer()); + Player player = event.getPlayer(); + FPlayer fplayer = FPlayerColl.get().get(player); - // Make sure player's power is up to date when they log off. - me.getPower(); + // Recalculate the power before the player leaves. + // This is required since we recalculate as if the player were offline when they log back in. + // TODO: When I setup universes I must do this for all universe instance of the player that logs off! + fplayer.recalculatePower(true); + // and update their last login time to point to when the logged off, for auto-remove routine - me.setLastLoginTime(System.currentTimeMillis()); + fplayer.setLastLoginTime(System.currentTimeMillis()); - SpoutFeatures.playerDisconnect(me); + SpoutFeatures.playerDisconnect(fplayer); } - @EventHandler(priority = EventPriority.MONITOR) + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void onPlayerMove(PlayerMoveEvent event) { - if (event.isCancelled()) return; - - // quick check to make sure player is moving between chunks; good performance boost - if - ( - event.getFrom().getBlockX() >> 4 == event.getTo().getBlockX() >> 4 - && - event.getFrom().getBlockZ() >> 4 == event.getTo().getBlockZ() >> 4 - && - event.getFrom().getWorld() == event.getTo().getWorld() - ) - return; + // If the player is moving from one chunk to another ... + if (MUtil.isSameChunk(event)) return; + // ... update the stored current chunk ... Player player = event.getPlayer(); - FPlayer me = FPlayerColl.get().get(player); + FPlayer fplayer = FPlayerColl.get().get(player); - // Did we change coord? - PS chunkFrom = me.getCurrentChunk(); + PS chunkFrom = fplayer.getCurrentChunk(); PS chunkTo = PS.valueOf(event.getTo()).getChunk(true); - if (chunkFrom.equals(chunkTo)) return; + fplayer.setCurrentChunk(chunkTo); - // Yes we did change coord (: + // ... TODO: assorted and uncleaned code below ... - me.setCurrentChunk(chunkTo); TerritoryAccess access = BoardColl.get().getTerritoryAccessAt(chunkTo); // Did we change "host"(faction)? boolean changedFaction = (BoardColl.get().getFactionAt(chunkFrom) != access.getHostFaction()); // let Spout handle most of this if it's available - boolean handledBySpout = changedFaction && SpoutFeatures.updateTerritoryDisplay(me); + boolean handledBySpout = changedFaction && SpoutFeatures.updateTerritoryDisplay(fplayer); - if (me.isMapAutoUpdating()) + if (fplayer.isMapAutoUpdating()) { - me.sendMessage(BoardColl.get().getMap(me.getFaction(), chunkTo, player.getLocation().getYaw())); + fplayer.sendMessage(BoardColl.get().getMap(fplayer.getFaction(), chunkTo, player.getLocation().getYaw())); } else if (changedFaction && ! handledBySpout) { - me.sendFactionHereMessage(); + fplayer.sendFactionHereMessage(); } // show access info message if needed - if ( ! handledBySpout && ! SpoutFeatures.updateAccessInfo(me) && ! access.isDefault()) + if ( ! handledBySpout && ! SpoutFeatures.updateAccessInfo(fplayer) && ! access.isDefault()) { - if (access.subjectHasAccess(me)) - me.msg("You have access to this area."); - else if (access.subjectAccessIsRestricted(me)) - me.msg("This area has restricted access."); + if (access.subjectHasAccess(fplayer)) + fplayer.msg("You have access to this area."); + else if (access.subjectAccessIsRestricted(fplayer)) + fplayer.msg("This area has restricted access."); } - if (me.getAutoClaimFor() != null) + if (fplayer.getAutoClaimFor() != null) { - me.attemptClaim(me.getAutoClaimFor(), event.getTo(), true); + fplayer.attemptClaim(fplayer.getAutoClaimFor(), event.getTo(), true); } }