Skip to content
This repository has been archived by the owner on Nov 5, 2019. It is now read-only.

Commit

Permalink
Improve player cleaning system
Browse files Browse the repository at this point in the history
  • Loading branch information
magnusulf committed Nov 16, 2017
1 parent 81ca676 commit 1d02942
Show file tree
Hide file tree
Showing 14 changed files with 194 additions and 132 deletions.
6 changes: 3 additions & 3 deletions plugin.yml
Expand Up @@ -18,7 +18,7 @@ permissions:
massivecore.store.stats: {description: show mstore statistics, default: false}
massivecore.store.listcolls: {description: list collections in a database, default: false}
massivecore.store.copydb: {description: copy database content, default: false}
massivecore.store.playerclean: {description: clean inactive players, default: false}
massivecore.store.clean: {description: clean database, default: false}
massivecore.usys: {description: use the usys command, default: false}
massivecore.usys.multiverse: {description: manage multiverses, default: false}
massivecore.usys.multiverse.list: {description: list multiverses, default: false}
Expand Down Expand Up @@ -63,7 +63,7 @@ permissions:
massivecore.store.stats: true
massivecore.store.listcolls: true
massivecore.store.copydb: true
massivecore.store.playerclean: true
massivecore.store.clean: true
massivecore.usys: true
massivecore.usys.multiverse: true
massivecore.usys.multiverse.list: true
Expand Down Expand Up @@ -103,7 +103,7 @@ permissions:
massivecore.kit.rank3:
default: false
children:
massivecore.store.playerclean: true
massivecore.store.clean: true
massivecore.kit.rank2: true
massivecore.kit.rank2:
default: false
Expand Down
8 changes: 4 additions & 4 deletions src/com/massivecraft/massivecore/MassiveCoreMConf.java
Expand Up @@ -128,25 +128,25 @@ public int getTpdelay(Permissible permissible)
public boolean warnOnLocalAlter = false;

// -------------------------------------------- //
// PLAYERCLEAN
// CLEAN
// -------------------------------------------- //

// How often should the task run?
// When set to 0 this feature is disabled. Meaning no cleaning will be done.
// Default: 1 day (Per default once a day.)
public long playercleanPeriodMillis = TimeUnit.MILLIS_PER_DAY;
public long cleanTaskPeriodMillis = TimeUnit.MILLIS_PER_DAY;

// This is used to decide at what time of the day the task will run.
// For Example: If the taskPeriodMillis is 24 hours:
// Set it to 0 for UTC midnight.
// Set it to 3600000 for UTC midnight + 1 hour.
public long playercleanOffsetMillis = 0;
public long cleanTaskOffsetMillis = 0;

// When did the task last run?
// This need not be modified by the server owner.
// It will be set for you automatically.
// 0 means it never ran before.
public long playercleanLastMillis = 0;
public long cleanTaskLastMillis = 0;

// -------------------------------------------- //
// MONGODB
Expand Down
2 changes: 1 addition & 1 deletion src/com/massivecraft/massivecore/MassiveCorePerm.java
Expand Up @@ -18,7 +18,7 @@ public enum MassiveCorePerm implements Identified
STORE_STATS,
STORE_LISTCOLLS,
STORE_COPYDB,
STORE_PLAYERCLEAN,
STORE_CLEAN,
USYS,
USYS_MULTIVERSE,
USYS_MULTIVERSE_LIST,
Expand Down
Expand Up @@ -20,6 +20,6 @@ public class CmdMassiveCoreStore extends MassiveCoreCommand
public CmdMassiveCoreStoreStats cmdMassiveCoreStoreStats = new CmdMassiveCoreStoreStats();
public CmdMassiveCoreStoreListcolls cmdMassiveCoreStoreListcolls = new CmdMassiveCoreStoreListcolls();
public CmdMassiveCoreStoreCopydb cmdMassiveCoreStoreCopydb = new CmdMassiveCoreStoreCopydb();
public CmdMassiveCoreStorePlayerclean cmdMassiveCoreStorePlayerclean = new CmdMassiveCoreStorePlayerclean();
public CmdMassiveCoreStoreClean cmdMassiveCoreStoreClean = new CmdMassiveCoreStoreClean();

}
Expand Up @@ -2,25 +2,26 @@

import com.massivecraft.massivecore.MassiveException;
import com.massivecraft.massivecore.command.type.container.TypeSet;
import com.massivecraft.massivecore.command.type.store.TypeSenderColl;
import com.massivecraft.massivecore.store.SenderColl;
import com.massivecraft.massivecore.store.inactive.InactiveUtil;
import com.massivecraft.massivecore.command.type.store.TypeColl;
import com.massivecraft.massivecore.store.Coll;
import com.massivecraft.massivecore.store.cleanable.Cleanable;
import com.massivecraft.massivecore.store.cleanable.CleaningUtil;
import com.massivecraft.massivecore.util.IdUtil;
import com.massivecraft.massivecore.util.MUtil;
import org.bukkit.command.CommandSender;

import java.util.Set;

public class CmdMassiveCoreStorePlayerclean extends MassiveCoreCommand
public class CmdMassiveCoreStoreClean extends MassiveCoreCommand
{
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //

public CmdMassiveCoreStorePlayerclean()
public CmdMassiveCoreStoreClean()
{
// Parameters
this.addParameter(TypeSet.get(TypeSenderColl.get()), "player coll", true).setDesc("the coll to clean inactive players from");
this.addParameter(TypeSet.get(TypeColl.get()), "coll", true).setDesc("the coll to clean");
}

// -------------------------------------------- //
Expand All @@ -30,13 +31,13 @@ public CmdMassiveCoreStorePlayerclean()
@Override
public void perform() throws MassiveException
{
Set<SenderColl<?>> colls = this.readArg();
Set<Coll<? extends Cleanable>> colls = this.readArg();

Set<CommandSender> receivers = MUtil.set(sender, IdUtil.getConsole());

for (SenderColl<?> coll : colls)
for (Coll<? extends Cleanable> coll : colls)
{
InactiveUtil.considerRemoveInactive(coll, receivers);
CleaningUtil.considerClean(coll, receivers);
}
}

Expand Down
Expand Up @@ -3,9 +3,9 @@
import com.massivecraft.massivecore.Engine;
import com.massivecraft.massivecore.MassiveCore;
import com.massivecraft.massivecore.MassiveCoreMConf;
import com.massivecraft.massivecore.event.EventMassiveCorePlayercleanToleranceMillis;
import com.massivecraft.massivecore.store.SenderColl;
import com.massivecraft.massivecore.store.inactive.InactiveUtil;
import com.massivecraft.massivecore.event.EventMassiveCorePlayerCleanInactivityToleranceMillis;
import com.massivecraft.massivecore.store.Coll;
import com.massivecraft.massivecore.store.cleanable.CleaningUtil;
import com.massivecraft.massivecore.util.IdUtil;
import org.bukkit.command.CommandSender;
import org.bukkit.event.EventHandler;
Expand All @@ -14,15 +14,15 @@
import java.util.Collections;
import java.util.List;

public class EngineMassiveCorePlayerclean extends Engine
public class EngineMassiveCoreClean extends Engine
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //

private static EngineMassiveCorePlayerclean i = new EngineMassiveCorePlayerclean();
public static EngineMassiveCorePlayerclean get() { return i; }
public EngineMassiveCorePlayerclean()
private static EngineMassiveCoreClean i = new EngineMassiveCoreClean();
public static EngineMassiveCoreClean get() { return i; }
public EngineMassiveCoreClean()
{
// Just check once a minute
this.setPeriod(60L * 20L);
Expand All @@ -44,7 +44,7 @@ public void run()
final long currentInvocation = getInvocationFromMillis(now);

// ... and the last invocation ...
final long lastInvocation = getInvocationFromMillis(MassiveCoreMConf.get().playercleanLastMillis);
final long lastInvocation = getInvocationFromMillis(MassiveCoreMConf.get().cleanTaskLastMillis);

// ... are different ...
if (currentInvocation == lastInvocation) return;
Expand All @@ -56,14 +56,14 @@ public void run()
public void invoke(long now)
{
// Update lastMillis
MassiveCoreMConf.get().playercleanLastMillis = now;
MassiveCoreMConf.get().cleanTaskLastMillis = now;
MassiveCoreMConf.get().changed();

List<CommandSender> recipients = Collections.<CommandSender>singletonList(IdUtil.getConsole());
for (SenderColl<?> coll : SenderColl.getSenderInstances())
for (Coll<?> coll : Coll.getInstances())
{
if (!coll.isPlayercleanTaskEnabled()) continue;
InactiveUtil.considerRemoveInactive(now, coll, recipients);
if (!coll.isCleanTaskEnabled()) continue;
CleaningUtil.considerClean(now, coll, recipients);
}
}

Expand All @@ -77,17 +77,17 @@ public void invoke(long now)
// Here we accept millis from inside the period by rounding down.
private static long getInvocationFromMillis(long millis)
{
return (millis - MassiveCoreMConf.get().playercleanOffsetMillis) / MassiveCoreMConf.get().playercleanPeriodMillis;
return (millis - MassiveCoreMConf.get().cleanTaskOffsetMillis) / MassiveCoreMConf.get().cleanTaskPeriodMillis;
}

// -------------------------------------------- //
// DEFAULT
// -------------------------------------------- //

@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void defaultMillis(EventMassiveCorePlayercleanToleranceMillis event)
public void defaultMillis(EventMassiveCorePlayerCleanInactivityToleranceMillis event)
{
event.getToleranceCauseMillis().put("Default", event.getColl().getPlayercleanToleranceMillis());
event.getToleranceCauseMillis().put("Default", event.getColl().getCleanInactivityToleranceMillis());
}

}
Expand Up @@ -2,13 +2,12 @@

import com.massivecraft.massivecore.store.SenderColl;
import com.massivecraft.massivecore.store.SenderEntity;
import com.massivecraft.massivecore.store.inactive.Inactive;
import org.bukkit.event.HandlerList;

import java.util.LinkedHashMap;
import java.util.Map;

public class EventMassiveCorePlayercleanToleranceMillis extends EventMassiveCore
public class EventMassiveCorePlayerCleanInactivityToleranceMillis extends EventMassiveCore
{
// -------------------------------------------- //
// REQUIRED EVENT CODE
Expand All @@ -22,6 +21,9 @@ public class EventMassiveCorePlayercleanToleranceMillis extends EventMassiveCore
// FIELD
// -------------------------------------------- //

private final long lastActivityMillis;
public long getLastActivityMillis() { return lastActivityMillis; }

private final long now;
public long getNow() { return now; }

Expand All @@ -37,13 +39,14 @@ public class EventMassiveCorePlayercleanToleranceMillis extends EventMassiveCore
// CONSTRUCT
// -------------------------------------------- //

public EventMassiveCorePlayercleanToleranceMillis(SenderEntity entity)
public EventMassiveCorePlayerCleanInactivityToleranceMillis(long lastActivityMillis, SenderEntity entity)
{
this(System.currentTimeMillis(), entity);
this(lastActivityMillis, System.currentTimeMillis(), entity);
}

public EventMassiveCorePlayercleanToleranceMillis(long now, SenderEntity entity)
public EventMassiveCorePlayerCleanInactivityToleranceMillis(long lastActivityMillis, long now, SenderEntity entity)
{
this.lastActivityMillis = lastActivityMillis;
this.now = now;
this.entity = entity;
}
Expand All @@ -64,12 +67,15 @@ public long getToleranceMillis()
return ret;
}

public boolean shouldBeRemoved()
public boolean shouldBeCleaned()
{
long toleranceMillis = getToleranceMillis();

long now = this.getNow();
long lastActivityMillis = ((Inactive)this.getEntity()).getLastActivityMillis();
long lastActivityMillis = this.getLastActivityMillis();

// If unknown don't remove
if (lastActivityMillis <= 0) return false;

long removeTime = lastActivityMillis + toleranceMillis;

Expand Down
7 changes: 7 additions & 0 deletions src/com/massivecraft/massivecore/store/Coll.java
Expand Up @@ -142,6 +142,13 @@ public String getDebugName()
protected final int entityTargetVersion;
@Override public int getEntityTargetVersion() { return this.entityTargetVersion; }

// This should be false under most circumstances.
// In some cases such as Factions we want it though.
// Especially so we don't change the years old way Factions does it.
private boolean cleanTaskEnabled = false;
public boolean isCleanTaskEnabled() { return this.cleanTaskEnabled; }
public void setCleanTaskEnabled(boolean cleanTaskEnabled) { this.cleanTaskEnabled = cleanTaskEnabled; }

// -------------------------------------------- //
// IDENTIFIED MODIFICATIONS
// -------------------------------------------- //
Expand Down
11 changes: 2 additions & 9 deletions src/com/massivecraft/massivecore/store/SenderColl.java
Expand Up @@ -16,13 +16,6 @@

public class SenderColl<E extends SenderEntity<E>> extends Coll<E> implements SenderIdSource
{
// This should be false under most circumstances.
// In some cases such as Factions we want it though.
// Especially so we don't change the years old way Factions does it.
private boolean playercleanTaskEnabled = false;
public boolean isPlayercleanTaskEnabled() { return this.playercleanTaskEnabled; }
public void setPlayercleanTaskEnabled(boolean playercleanTaskEnabled) { this.playercleanTaskEnabled = playercleanTaskEnabled; }

// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
Expand Down Expand Up @@ -203,11 +196,11 @@ public static void setSenderReferences(String senderId, CommandSender sender)
}

// -------------------------------------------- //
// ACTIVITY MILLIS
// CLEAN
// -------------------------------------------- //

// Must be overriden if they want to use it.
public long getPlayercleanToleranceMillis()
public long getCleanInactivityToleranceMillis()
{
return -1;
}
Expand Down
38 changes: 37 additions & 1 deletion src/com/massivecraft/massivecore/store/SenderEntity.java
Expand Up @@ -4,11 +4,13 @@
import com.massivecraft.massivecore.Named;
import com.massivecraft.massivecore.PlayerState;
import com.massivecraft.massivecore.event.EventMassiveCoreAknowledge;
import com.massivecraft.massivecore.event.EventMassiveCorePlayerCleanInactivityToleranceMillis;
import com.massivecraft.massivecore.mixin.MixinDisplayName;
import com.massivecraft.massivecore.mixin.MixinMessage;
import com.massivecraft.massivecore.mixin.MixinPlayed;
import com.massivecraft.massivecore.mixin.MixinVisibility;
import com.massivecraft.massivecore.mson.Mson;
import com.massivecraft.massivecore.store.cleanable.Cleanable;
import com.massivecraft.massivecore.util.IdUtil;
import com.massivecraft.massivecore.util.PermissionUtil;
import org.bukkit.GameMode;
Expand All @@ -19,7 +21,7 @@
import java.util.Collection;
import java.util.UUID;

public abstract class SenderEntity<E extends SenderEntity<E>> extends Entity<E> implements Named
public abstract class SenderEntity<E extends SenderEntity<E>> extends Entity<E> implements Named, Cleanable
{
// -------------------------------------------- //
// FIELDS
Expand Down Expand Up @@ -65,6 +67,40 @@ public SenderColl<E> getColl()
return (SenderColl<E>) super.getColl();
}

// -------------------------------------------- //
// OVERRIDE: CLEANABLE
// -------------------------------------------- //

@Override
public boolean shouldBeCleaned(long now)
{
// If unknown don't clean
Long lastActivityMillis = this.getLastPlayed();
if (lastActivityMillis == null) return false;

return this.shouldBeCleaned(now, lastActivityMillis);
}

protected boolean shouldBeCleaned(long now, long lastActivityMillis)
{
// This means it is disabled for this coll
if (this.getColl().getCleanInactivityToleranceMillis() < 0) return false;

EventMassiveCorePlayerCleanInactivityToleranceMillis event = new EventMassiveCorePlayerCleanInactivityToleranceMillis(lastActivityMillis, now, this);
event.run();
return event.shouldBeCleaned();
}

public void preClean()
{

}

public void postClean()
{

}

// -------------------------------------------- //
// CONVENIENCE: DATABASE
// -------------------------------------------- //
Expand Down
14 changes: 14 additions & 0 deletions src/com/massivecraft/massivecore/store/cleanable/Cleanable.java
@@ -0,0 +1,14 @@
package com.massivecraft.massivecore.store.cleanable;

import com.massivecraft.massivecore.store.Coll;

// This interface only really makes sense for SenderEntity's
// But by using this conversion to non-senders should be easier when that is done
public interface Cleanable
{
boolean shouldBeCleaned(long now);
Coll<?> getColl();

void preClean();
void postClean();
}

0 comments on commit 1d02942

Please sign in to comment.