Fix cleaning bug

This commit is contained in:
Magnus Ulf Jørgensen 2017-07-25 12:12:50 +02:00
parent 8090acc04a
commit 4904bb39c6
7 changed files with 48 additions and 32 deletions

View File

@ -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("<a>Power per Death: <v>%.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("<i>Automatic removal after %s <i>of inactivity:", format(event.getToleranceMillis()));
for (Entry<String, Long> causeMillis : event.getToleranceCauseMillis().entrySet())

View File

@ -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

View File

@ -88,7 +88,7 @@ public class Faction extends Entity<Faction> implements FactionsParticipator
if (Money.exists(this))
{
// ... remove it.
Money.set(this, null, 0);
Money.set(this, null, 0, "Factions");
}
}

View File

@ -111,17 +111,17 @@ public class MConf extends Entity<MConf>
// 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<Long, Long> playercleanToleranceMillisPlayerAgeToBonus = MUtil.map(
public Map<Long, Long> 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<Long, Long> playercleanToleranceMillisFactionAgeToBonus = MUtil.map(
public Map<Long, Long> 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
);

View File

@ -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<MPlayer> implements FactionsParticipator, Inactive
public class MPlayer extends SenderEntity<MPlayer> implements FactionsParticipator
{
// -------------------------------------------- //
// META
@ -78,7 +77,7 @@ public class MPlayer extends SenderEntity<MPlayer> 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.
@ -107,6 +106,17 @@ public class MPlayer extends SenderEntity<MPlayer> 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<MPlayer> 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.
@ -230,6 +240,12 @@ public class MPlayer extends SenderEntity<MPlayer> implements FactionsParticipat
this.setLastActivityMillis(System.currentTimeMillis());
}
@Override
public boolean shouldBeCleaned(long now)
{
return this.shouldBeCleaned(now, this.lastActivityMillis);
}
// -------------------------------------------- //
// FIELD: factionId
// -------------------------------------------- //

View File

@ -12,7 +12,7 @@ public class MPlayerColl extends SenderColl<MPlayer>
public static MPlayerColl get() { return i; }
public MPlayerColl()
{
this.setPlayercleanTaskEnabled(true);
this.setCleanTaskEnabled(true);
}
// -------------------------------------------- //
@ -30,9 +30,9 @@ public class MPlayerColl extends SenderColl<MPlayer>
// -------------------------------------------- //
@Override
public long getPlayercleanToleranceMillis()
public long getCleanInactivityToleranceMillis()
{
return MConf.get().playercleanToleranceMillis;
return MConf.get().cleanInactivityToleranceMillis;
}
}

View File

@ -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"));
}
}