Use new inactivity cleanup system

This commit is contained in:
Magnus Ulf Jørgensen
2017-05-19 08:47:42 +02:00
parent 9ed9ad4f82
commit b681ee01a4
9 changed files with 150 additions and 266 deletions

View File

@ -351,6 +351,16 @@ public class Faction extends Entity<Faction> implements FactionsParticipator
this.changed();
}
public long getAge()
{
return this.getAge(System.currentTimeMillis());
}
public long getAge(long now)
{
return now - this.getCreatedAtMillis();
}
// -------------------------------------------- //
// FIELD: home
// -------------------------------------------- //

View File

@ -60,7 +60,7 @@ public class MConf extends Entity<MConf>
// VERSION
// -------------------------------------------- //
public int version = 1;
public int version = 2;
// -------------------------------------------- //
// COMMAND ALIASES
@ -111,17 +111,17 @@ public class MConf extends Entity<MConf>
// The Default
@EditorType(TypeMillisDiff.class)
public long removePlayerMillisDefault = 10 * TimeUnit.MILLIS_PER_DAY; // 10 days
public long playercleanToleranceMillis = 10 * TimeUnit.MILLIS_PER_DAY; // 10 days
// Player Age Bonus
@EditorTypeInner({TypeMillisDiff.class, TypeMillisDiff.class})
public Map<Long, Long> removePlayerMillisPlayerAgeToBonus = MUtil.map(
public Map<Long, Long> playercleanToleranceMillisPlayerAgeToBonus = 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> removePlayerMillisFactionAgeToBonus = MUtil.map(
public Map<Long, Long> playercleanToleranceMillisFactionAgeToBonus = 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

@ -11,7 +11,6 @@ import com.massivecraft.factions.event.EventFactionsChunksChange;
import com.massivecraft.factions.event.EventFactionsDisband;
import com.massivecraft.factions.event.EventFactionsMembershipChange;
import com.massivecraft.factions.event.EventFactionsMembershipChange.MembershipChangeReason;
import com.massivecraft.factions.event.EventFactionsRemovePlayerMillis;
import com.massivecraft.factions.mixin.PowerMixin;
import com.massivecraft.factions.util.RelationUtil;
import com.massivecraft.massivecore.mixin.MixinSenderPs;
@ -19,6 +18,7 @@ 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 +34,7 @@ import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;
public class MPlayer extends SenderEntity<MPlayer> implements FactionsParticipator
public class MPlayer extends SenderEntity<MPlayer> implements FactionsParticipator, Inactive
{
// -------------------------------------------- //
// META
@ -701,57 +701,7 @@ public class MPlayer extends SenderEntity<MPlayer> implements FactionsParticipat
if (ps == null) return false;
return BoardColl.get().getFactionAt(ps).getRelationTo(this) == Rel.ENEMY;
}
// -------------------------------------------- //
// INACTIVITY TIMEOUT
// -------------------------------------------- //
public long getRemovePlayerMillis(boolean async)
{
EventFactionsRemovePlayerMillis event = new EventFactionsRemovePlayerMillis(async, this);
event.run();
return event.getMillis();
}
public boolean considerRemovePlayerMillis(boolean async)
{
// This may or may not be required.
// Some users have been reporting a loop issue with the same player
// detaching over and over again.
// Maybe skipping ahead if the player is detached will solve the issue.
if (this.detached()) return false;
// Get the last activity millis.
long lastActivityMillis = this.getLastActivityMillis();
// Consider
long toleranceMillis = this.getRemovePlayerMillis(async);
if (System.currentTimeMillis() - lastActivityMillis <= toleranceMillis) return false;
// Inform
if (MConf.get().logFactionLeave || MConf.get().logFactionKick)
{
Factions.get().log("Player " + this.getName() + " was auto-removed due to inactivity.");
}
// Apply
// Promote a new leader if required.
if (this.getRole() == Rel.LEADER)
{
Faction faction = this.getFaction();
if (faction != null)
{
this.getFaction().promoteNewLeader();
}
}
this.leave();
this.detach();
return true;
}
// -------------------------------------------- //
// ACTIONS
// -------------------------------------------- //

View File

@ -1,10 +1,6 @@
package com.massivecraft.factions.entity;
import com.massivecraft.factions.Factions;
import com.massivecraft.massivecore.store.SenderColl;
import org.bukkit.Bukkit;
import java.util.Collection;
public class MPlayerColl extends SenderColl<MPlayer>
{
@ -14,6 +10,10 @@ public class MPlayerColl extends SenderColl<MPlayer>
private static MPlayerColl i = new MPlayerColl();
public static MPlayerColl get() { return i; }
public MPlayerColl()
{
this.setPlayercleanTaskEnabled(true);
}
// -------------------------------------------- //
// STACK TRACEABILITY
@ -29,29 +29,10 @@ public class MPlayerColl extends SenderColl<MPlayer>
// EXTRAS
// -------------------------------------------- //
public void considerRemovePlayerMillis()
@Override
public long getPlayercleanToleranceMillis()
{
// If the config option is 0 or below that means the server owner want it disabled.
if (MConf.get().removePlayerMillisDefault <= 0.0) return;
// For each of the offline players...
// NOTE: If the player is currently online it's most definitely not inactive.
// NOTE: This check catches some important special cases like the @console "player".
final Collection<MPlayer> mplayersOffline = this.getAllOffline();
Bukkit.getScheduler().runTaskAsynchronously(Factions.get(), new Runnable()
{
@Override
public void run()
{
// For each offline player ...
for (MPlayer mplayer : mplayersOffline)
{
// ... see if they should be removed.
mplayer.considerRemovePlayerMillis(true);
}
}
});
return MConf.get().playercleanToleranceMillis;
}
}

View File

@ -0,0 +1,23 @@
package com.massivecraft.factions.entity.migrator;
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
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static MigratorMConf002Playerclean i = new MigratorMConf002Playerclean();
public static MigratorMConf002Playerclean get() { return i; }
private MigratorMConf002Playerclean()
{
super(MConf.class);
this.addInnerMigrator(MigratorFieldRename.get("removePlayerMillisDefault", "playercleanToleranceMillis"));
this.addInnerMigrator(MigratorFieldRename.get("removePlayerMillisPlayerAgeToBonus", "playercleanToleranceMillisPlayerAgeToBonus"));
this.addInnerMigrator(MigratorFieldRename.get("removePlayerMillisFactionAgeToBonus", "playercleanToleranceMillisFactionAgeToBonus"));
}
}