From 4904bb39c620e79d2b8156aa2e5c8bfe6dfe38ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magnus=20Ulf=20J=C3=B8rgensen?= Date: Tue, 25 Jul 2017 12:12:50 +0200 Subject: [PATCH] Fix cleaning bug --- .../factions/cmd/CmdFactionsPlayer.java | 6 ++-- ...rclean.java => EngineCleanInactivity.java} | 18 ++++++------ .../massivecraft/factions/entity/Faction.java | 2 +- .../massivecraft/factions/entity/MConf.java | 6 ++-- .../massivecraft/factions/entity/MPlayer.java | 28 +++++++++++++++---- .../factions/entity/MPlayerColl.java | 6 ++-- ...a => MigratorMConf002CleanInactivity.java} | 14 +++++----- 7 files changed, 48 insertions(+), 32 deletions(-) rename src/com/massivecraft/factions/engine/{EnginePlayerclean.java => EngineCleanInactivity.java} (74%) rename src/com/massivecraft/factions/entity/migrator/{MigratorMConf002Playerclean.java => MigratorMConf002CleanInactivity.java} (56%) diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsPlayer.java b/src/com/massivecraft/factions/cmd/CmdFactionsPlayer.java index 3be670c9..9d234367 100644 --- a/src/com/massivecraft/factions/cmd/CmdFactionsPlayer.java +++ b/src/com/massivecraft/factions/cmd/CmdFactionsPlayer.java @@ -5,7 +5,7 @@ import com.massivecraft.factions.entity.MConf; import com.massivecraft.factions.entity.MPlayer; import com.massivecraft.massivecore.MassiveException; import com.massivecraft.massivecore.Progressbar; -import com.massivecraft.massivecore.event.EventMassiveCorePlayercleanToleranceMillis; +import com.massivecraft.massivecore.event.EventMassiveCorePlayerCleanInactivityToleranceMillis; import com.massivecraft.massivecore.util.TimeDiffUtil; import com.massivecraft.massivecore.util.TimeUnit; import com.massivecraft.massivecore.util.Txt; @@ -80,9 +80,9 @@ public class CmdFactionsPlayer extends FactionsCommand msg("Power per Death: %.2f", mplayer.getPowerPerDeath()); // Display automatic kick / remove info if the system is in use - if (MConf.get().playercleanToleranceMillis <= 0) return; + if (MConf.get().cleanInactivityToleranceMillis <= 0) return; - EventMassiveCorePlayercleanToleranceMillis event = new EventMassiveCorePlayercleanToleranceMillis(mplayer); + EventMassiveCorePlayerCleanInactivityToleranceMillis event = new EventMassiveCorePlayerCleanInactivityToleranceMillis(mplayer.getLastActivityMillis(), mplayer); event.run(); msg("Automatic removal after %s of inactivity:", format(event.getToleranceMillis())); for (Entry causeMillis : event.getToleranceCauseMillis().entrySet()) diff --git a/src/com/massivecraft/factions/engine/EnginePlayerclean.java b/src/com/massivecraft/factions/engine/EngineCleanInactivity.java similarity index 74% rename from src/com/massivecraft/factions/engine/EnginePlayerclean.java rename to src/com/massivecraft/factions/engine/EngineCleanInactivity.java index 1b292964..8107c508 100644 --- a/src/com/massivecraft/factions/engine/EnginePlayerclean.java +++ b/src/com/massivecraft/factions/engine/EngineCleanInactivity.java @@ -5,28 +5,28 @@ import com.massivecraft.factions.entity.MConf; import com.massivecraft.factions.entity.MPlayer; import com.massivecraft.factions.entity.MPlayerColl; import com.massivecraft.massivecore.Engine; -import com.massivecraft.massivecore.event.EventMassiveCorePlayercleanToleranceMillis; +import com.massivecraft.massivecore.event.EventMassiveCorePlayerCleanInactivityToleranceMillis; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import java.util.Map; import java.util.Map.Entry; -public class EnginePlayerclean extends Engine +public class EngineCleanInactivity extends Engine { // -------------------------------------------- // // INSTANCE & CONSTRUCT // -------------------------------------------- // - private static EnginePlayerclean i = new EnginePlayerclean(); - public static EnginePlayerclean get() { return i; } + private static EngineCleanInactivity i = new EngineCleanInactivity(); + public static EngineCleanInactivity get() { return i; } // -------------------------------------------- // // REMOVE PLAYER MILLIS // -------------------------------------------- // @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) - public void ageBonus(EventMassiveCorePlayercleanToleranceMillis event) + public void ageBonus(EventMassiveCorePlayerCleanInactivityToleranceMillis event) { if (event.getColl() != MPlayerColl.get()) return; @@ -34,7 +34,7 @@ public class EnginePlayerclean extends Engine applyFactionAgeBonus(event); } - public void applyPlayerAgeBonus(EventMassiveCorePlayercleanToleranceMillis event) + public void applyPlayerAgeBonus(EventMassiveCorePlayerCleanInactivityToleranceMillis event) { // Calculate First Played Long firstPlayed = event.getEntity().getFirstPlayed(); @@ -45,14 +45,14 @@ public class EnginePlayerclean extends Engine } // Calculate the Bonus! - Long bonus = calculateBonus(age, MConf.get().playercleanToleranceMillisPlayerAgeToBonus); + Long bonus = calculateBonus(age, MConf.get().cleanInactivityToleranceMillisPlayerAgeToBonus); if (bonus == null) return; // Apply event.getToleranceCauseMillis().put("Player Age Bonus", bonus); } - public void applyFactionAgeBonus(EventMassiveCorePlayercleanToleranceMillis event) + public void applyFactionAgeBonus(EventMassiveCorePlayerCleanInactivityToleranceMillis event) { // Calculate Faction Age Faction faction = ((MPlayer)event.getEntity()).getFaction(); @@ -63,7 +63,7 @@ public class EnginePlayerclean extends Engine } // Calculate the Bonus! - Long bonus = calculateBonus(age, MConf.get().playercleanToleranceMillisFactionAgeToBonus); + Long bonus = calculateBonus(age, MConf.get().cleanInactivityToleranceMillisFactionAgeToBonus); if (bonus == null) return; // Apply diff --git a/src/com/massivecraft/factions/entity/Faction.java b/src/com/massivecraft/factions/entity/Faction.java index 48a3e5e4..b2833f09 100644 --- a/src/com/massivecraft/factions/entity/Faction.java +++ b/src/com/massivecraft/factions/entity/Faction.java @@ -88,7 +88,7 @@ public class Faction extends Entity implements FactionsParticipator if (Money.exists(this)) { // ... remove it. - Money.set(this, null, 0); + Money.set(this, null, 0, "Factions"); } } diff --git a/src/com/massivecraft/factions/entity/MConf.java b/src/com/massivecraft/factions/entity/MConf.java index a4ad12a4..ccadf83f 100644 --- a/src/com/massivecraft/factions/entity/MConf.java +++ b/src/com/massivecraft/factions/entity/MConf.java @@ -111,17 +111,17 @@ public class MConf extends Entity // The Default @EditorType(TypeMillisDiff.class) - public long playercleanToleranceMillis = 10 * TimeUnit.MILLIS_PER_DAY; // 10 days + public long cleanInactivityToleranceMillis = 10 * TimeUnit.MILLIS_PER_DAY; // 10 days // Player Age Bonus @EditorTypeInner({TypeMillisDiff.class, TypeMillisDiff.class}) - public Map playercleanToleranceMillisPlayerAgeToBonus = MUtil.map( + public Map cleanInactivityToleranceMillisPlayerAgeToBonus = MUtil.map( 2 * TimeUnit.MILLIS_PER_WEEK, 10 * TimeUnit.MILLIS_PER_DAY // +10 days after 2 weeks ); // Faction Age Bonus @EditorTypeInner({TypeMillisDiff.class, TypeMillisDiff.class}) - public Map playercleanToleranceMillisFactionAgeToBonus = MUtil.map( + public Map cleanInactivityToleranceMillisFactionAgeToBonus = MUtil.map( 4 * TimeUnit.MILLIS_PER_WEEK, 10 * TimeUnit.MILLIS_PER_DAY, // +10 days after 4 weeks 2 * TimeUnit.MILLIS_PER_WEEK, 5 * TimeUnit.MILLIS_PER_DAY // +5 days after 2 weeks ); diff --git a/src/com/massivecraft/factions/entity/MPlayer.java b/src/com/massivecraft/factions/entity/MPlayer.java index 07d26725..a63b7713 100644 --- a/src/com/massivecraft/factions/entity/MPlayer.java +++ b/src/com/massivecraft/factions/entity/MPlayer.java @@ -18,7 +18,6 @@ import com.massivecraft.massivecore.mixin.MixinTitle; import com.massivecraft.massivecore.ps.PS; import com.massivecraft.massivecore.ps.PSFormatHumanSpace; import com.massivecraft.massivecore.store.SenderEntity; -import com.massivecraft.massivecore.store.inactive.Inactive; import com.massivecraft.massivecore.util.IdUtil; import com.massivecraft.massivecore.util.MUtil; import com.massivecraft.massivecore.util.Txt; @@ -34,7 +33,7 @@ import java.util.Iterator; import java.util.Map.Entry; import java.util.Set; -public class MPlayer extends SenderEntity implements FactionsParticipator, Inactive +public class MPlayer extends SenderEntity implements FactionsParticipator { // -------------------------------------------- // // META @@ -78,7 +77,7 @@ public class MPlayer extends SenderEntity implements FactionsParticipat @Override public boolean isDefault() { - // Last activity millis is data we use for clearing out inactive players. So it does not in itself make the player data worth keeping. + // Last activity millis is data we use for clearing out cleanable players. So it does not in itself make the player data worth keeping. if (this.hasFaction()) return false; // Role means nothing without a faction. // Title means nothing without a faction. @@ -106,6 +105,17 @@ public class MPlayer extends SenderEntity implements FactionsParticipat { FactionsIndex.get().update(this); } + + @Override + public void preClean() + { + if (this.getRole() == Rel.LEADER) + { + this.getFaction().promoteNewLeader(); + } + + this.leave(); + } // -------------------------------------------- // // FIELDS: RAW @@ -114,9 +124,9 @@ public class MPlayer extends SenderEntity implements FactionsParticipat // Each field has it's own section further down since just the getter and setter logic takes up quite some place. // The last known time of explicit player activity, such as login or logout. - // This value is most importantly used for removing inactive players. + // This value is most importantly used for removing cleanable players. // For that reason it defaults to the current time. - // Really inactive players will be considered newly active when upgrading Factions from 2.6 --> 2.7. + // Really cleanable players will be considered newly active when upgrading Factions from 2.6 --> 2.7. // There is actually more than one reason we store this data ourselves and don't use the OfflinePlayer#getLastPlayed. // 1. I don't trust that method. It's been very buggy or even completely broken in previous Bukkit versions. // 2. The method depends on the player.dat files being present. @@ -229,7 +239,13 @@ public class MPlayer extends SenderEntity implements FactionsParticipat { this.setLastActivityMillis(System.currentTimeMillis()); } - + + @Override + public boolean shouldBeCleaned(long now) + { + return this.shouldBeCleaned(now, this.lastActivityMillis); + } + // -------------------------------------------- // // FIELD: factionId // -------------------------------------------- // diff --git a/src/com/massivecraft/factions/entity/MPlayerColl.java b/src/com/massivecraft/factions/entity/MPlayerColl.java index 12e3382d..83926b0b 100644 --- a/src/com/massivecraft/factions/entity/MPlayerColl.java +++ b/src/com/massivecraft/factions/entity/MPlayerColl.java @@ -12,7 +12,7 @@ public class MPlayerColl extends SenderColl public static MPlayerColl get() { return i; } public MPlayerColl() { - this.setPlayercleanTaskEnabled(true); + this.setCleanTaskEnabled(true); } // -------------------------------------------- // @@ -30,9 +30,9 @@ public class MPlayerColl extends SenderColl // -------------------------------------------- // @Override - public long getPlayercleanToleranceMillis() + public long getCleanInactivityToleranceMillis() { - return MConf.get().playercleanToleranceMillis; + return MConf.get().cleanInactivityToleranceMillis; } } diff --git a/src/com/massivecraft/factions/entity/migrator/MigratorMConf002Playerclean.java b/src/com/massivecraft/factions/entity/migrator/MigratorMConf002CleanInactivity.java similarity index 56% rename from src/com/massivecraft/factions/entity/migrator/MigratorMConf002Playerclean.java rename to src/com/massivecraft/factions/entity/migrator/MigratorMConf002CleanInactivity.java index ebc3bbe7..4dfa157a 100644 --- a/src/com/massivecraft/factions/entity/migrator/MigratorMConf002Playerclean.java +++ b/src/com/massivecraft/factions/entity/migrator/MigratorMConf002CleanInactivity.java @@ -4,20 +4,20 @@ import com.massivecraft.factions.entity.MConf; import com.massivecraft.massivecore.store.migrator.MigratorFieldRename; import com.massivecraft.massivecore.store.migrator.MigratorRoot; -public class MigratorMConf002Playerclean extends MigratorRoot +public class MigratorMConf002CleanInactivity extends MigratorRoot { // -------------------------------------------- // // INSTANCE & CONSTRUCT // -------------------------------------------- // - private static MigratorMConf002Playerclean i = new MigratorMConf002Playerclean(); - public static MigratorMConf002Playerclean get() { return i; } - private MigratorMConf002Playerclean() + private static MigratorMConf002CleanInactivity i = new MigratorMConf002CleanInactivity(); + public static MigratorMConf002CleanInactivity get() { return i; } + private MigratorMConf002CleanInactivity() { super(MConf.class); - this.addInnerMigrator(MigratorFieldRename.get("removePlayerMillisDefault", "playercleanToleranceMillis")); - this.addInnerMigrator(MigratorFieldRename.get("removePlayerMillisPlayerAgeToBonus", "playercleanToleranceMillisPlayerAgeToBonus")); - this.addInnerMigrator(MigratorFieldRename.get("removePlayerMillisFactionAgeToBonus", "playercleanToleranceMillisFactionAgeToBonus")); + this.addInnerMigrator(MigratorFieldRename.get("removePlayerMillisDefault", "cleanInactivityToleranceMillis")); + this.addInnerMigrator(MigratorFieldRename.get("removePlayerMillisPlayerAgeToBonus", "cleanInactivityToleranceMillisPlayerAgeToBonus")); + this.addInnerMigrator(MigratorFieldRename.get("removePlayerMillisFactionAgeToBonus", "cleanInactivityToleranceMillisFactionAgeToBonus")); } }