Add option to only count online jailed time (#3705)

Adds `jail-online-time` config option to only count a player's online time to the jail cap.

This also fixes memory leak in `EssentialsTimer` which I found in the process of testing.

Closes #429.
This commit is contained in:
Josh Roy 2021-01-08 15:43:32 -05:00 committed by GitHub
parent 8177893e28
commit 026d279556
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 78 additions and 20 deletions

View File

@ -44,6 +44,7 @@ public class EssentialsTimer implements Runnable {
}
lastPoll = startTime;
int count = 0;
onlineUsers.clear();
for (final Player player : ess.getOnlinePlayers()) {
count++;
if (skip1 > 0) {

View File

@ -355,6 +355,8 @@ public interface ISettings extends IConf {
boolean isTeleportBackWhenFreedFromJail();
boolean isJailOnlineTime();
boolean isCompassTowardsHomePerm();
boolean isAllowWorldInBroadcastworld();

View File

@ -1610,6 +1610,11 @@ public class Settings implements net.ess3.api.ISettings {
return teleportBackWhenFreedFromJail;
}
@Override
public boolean isJailOnlineTime() {
return config.getBoolean("jail-online-time", false);
}
private boolean _isCompassTowardsHomePerm() {
return config.getBoolean("compass-towards-home-perm", false);
}

View File

@ -6,6 +6,7 @@ import com.earth2me.essentials.messaging.SimpleMessageRecipient;
import com.earth2me.essentials.register.payment.Method;
import com.earth2me.essentials.register.payment.Methods;
import com.earth2me.essentials.utils.DateUtil;
import com.earth2me.essentials.utils.EnumUtil;
import com.earth2me.essentials.utils.FormatUtil;
import com.earth2me.essentials.utils.NumberUtil;
import com.earth2me.essentials.utils.VersionUtil;
@ -18,6 +19,7 @@ import net.ess3.api.events.UserBalanceUpdateEvent;
import net.essentialsx.api.v2.events.TransactionEvent;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Statistic;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
@ -41,6 +43,7 @@ import java.util.logging.Logger;
import static com.earth2me.essentials.I18n.tl;
public class User extends UserData implements Comparable<User>, IMessageRecipient, net.ess3.api.IUser {
private static final Statistic PLAY_ONE_TICK = EnumUtil.getStatistic("PLAY_ONE_MINUTE", "PLAY_ONE_TICK");
private static final Logger logger = Logger.getLogger("Essentials");
private final IMessageRecipient messageRecipient;
private transient final AsyncTeleport teleport;
@ -595,24 +598,34 @@ public class User extends UserData implements Comparable<User>, IMessageRecipien
//Returns true if status expired during this check
public boolean checkJailTimeout(final long currentTime) {
if (getJailTimeout() > 0 && getJailTimeout() < currentTime && isJailed()) {
final JailStatusChangeEvent event = new JailStatusChangeEvent(this, null, false);
ess.getServer().getPluginManager().callEvent(event);
if (getJailTimeout() > 0) {
if (!event.isCancelled()) {
setJailTimeout(0);
setJailed(false);
sendMessage(tl("haveBeenReleased"));
setJail(null);
if (ess.getSettings().isTeleportBackWhenFreedFromJail()) {
final CompletableFuture<Boolean> future = new CompletableFuture<>();
getAsyncTeleport().back(future);
future.exceptionally(e -> {
getAsyncTeleport().respawn(null, TeleportCause.PLUGIN, new CompletableFuture<>());
return false;
});
if (getOnlineJailedTime() > 0) {
if (getOnlineJailedTime() > getBase().getStatistic(PLAY_ONE_TICK)) {
return false;
}
}
if (getJailTimeout() < currentTime && isJailed() ) {
final JailStatusChangeEvent event = new JailStatusChangeEvent(this, null, false);
ess.getServer().getPluginManager().callEvent(event);
if (!event.isCancelled()) {
setJailTimeout(0);
setOnlineJailedTime(0);
setJailed(false);
sendMessage(tl("haveBeenReleased"));
setJail(null);
if (ess.getSettings().isTeleportBackWhenFreedFromJail()) {
final CompletableFuture<Boolean> future = new CompletableFuture<>();
getAsyncTeleport().back(future);
future.exceptionally(e -> {
getAsyncTeleport().respawn(null, TeleportCause.PLUGIN, new CompletableFuture<>());
return false;
});
}
return true;
}
return true;
}
}
return false;

View File

@ -122,6 +122,7 @@ public abstract class UserData extends PlayerExtension implements IConf {
muteReason = _getMuteReason();
jailed = _getJailed();
jailTimeout = _getJailTimeout();
onlineJailed = _getOnlineJailedTime();
lastLogin = _getLastLogin();
lastLogout = _getLastLogout();
lastLoginAddress = _getLastLoginAddress();
@ -660,6 +661,22 @@ public abstract class UserData extends PlayerExtension implements IConf {
config.save();
}
private long onlineJailed;
private long _getOnlineJailedTime() {
return config.getLong("timestamps.onlinejail", 0);
}
public long getOnlineJailedTime() {
return onlineJailed;
}
public void setOnlineJailedTime(long onlineJailed) {
this.onlineJailed = onlineJailed;
config.setProperty("timestamps.onlinejail", onlineJailed);
config.save();
}
private long _getLastLogin() {
return config.getLong("timestamps.login", 0);
}

View File

@ -3,8 +3,10 @@ package com.earth2me.essentials.commands;
import com.earth2me.essentials.CommandSource;
import com.earth2me.essentials.User;
import com.earth2me.essentials.utils.DateUtil;
import com.earth2me.essentials.utils.EnumUtil;
import net.ess3.api.events.JailStatusChangeEvent;
import org.bukkit.Server;
import org.bukkit.Statistic;
import org.bukkit.event.player.PlayerTeleportEvent;
import java.util.ArrayList;
@ -15,6 +17,8 @@ import java.util.concurrent.CompletableFuture;
import static com.earth2me.essentials.I18n.tl;
public class Commandtogglejail extends EssentialsCommand {
private static final Statistic PLAY_ONE_TICK = EnumUtil.getStatistic("PLAY_ONE_MINUTE", "PLAY_ONE_TICK");
public Commandtogglejail() {
super("togglejail");
}
@ -44,13 +48,15 @@ public class Commandtogglejail extends EssentialsCommand {
ess.getServer().getPluginManager().callEvent(event);
if (!event.isCancelled()) {
long displayTime = 0;
long preTimeDiff = 0;
if (args.length > 2) {
final String time = getFinalArg(args, 2);
preTimeDiff = DateUtil.parseDateDiff(time, true);
displayTime = DateUtil.parseDateDiff(time, true);
preTimeDiff = DateUtil.parseDateDiff(time, true, ess.getSettings().isJailOnlineTime());
}
final long timeDiff = preTimeDiff;
final long finalDisplayTime = displayTime;
final CompletableFuture<Boolean> future = getNewExceptionFuture(sender, commandLabel);
future.thenAccept(success -> {
if (success) {
@ -60,8 +66,9 @@ public class Commandtogglejail extends EssentialsCommand {
player.setJail(args[1]);
if (args.length > 2) {
player.setJailTimeout(timeDiff);
player.setOnlineJailedTime(ess.getSettings().isJailOnlineTime() ? ((player.getBase().getStatistic(PLAY_ONE_TICK)) + (timeDiff / 50)) : 0);
}
sender.sendMessage(timeDiff > 0 ? tl("playerJailedFor", player.getName(), DateUtil.formatDateDiff(timeDiff)) : tl("playerJailed", player.getName()));
sender.sendMessage(timeDiff > 0 ? tl("playerJailedFor", player.getName(), DateUtil.formatDateDiff(finalDisplayTime)) : tl("playerJailed", player.getName()));
}
});
if (player.getBase().isOnline()) {

View File

@ -18,7 +18,11 @@ public final class DateUtil {
return timePattern.matcher(input).replaceFirst("").trim();
}
public static long parseDateDiff(final String time, final boolean future) throws Exception {
public static long parseDateDiff(String time, boolean future) throws Exception {
return parseDateDiff(time, future, false);
}
public static long parseDateDiff(String time, boolean future, boolean emptyEpoch) throws Exception {
final Matcher m = timePattern.matcher(time);
int years = 0;
int months = 0;
@ -67,6 +71,11 @@ public final class DateUtil {
throw new Exception(tl("illegalDate"));
}
final Calendar c = new GregorianCalendar();
if (emptyEpoch) {
c.setTimeInMillis(0);
}
if (years > 0) {
if (years > maxYears) {
years = maxYears;

View File

@ -628,6 +628,10 @@ default-enabled-confirm-commands:
# Whether or not to teleport a player back to their previous position after they have been freed from jail.
teleport-back-when-freed-from-jail: true
# Whether or not jail time should only be counted while the user is online.
# If true, a jailed player's time will only decrement when they are online.
jail-online-time: false
# Set the timeout, in seconds for players to accept a tpa before the request is cancelled.
# Set to 0 for no timeout.
tpa-accept-cancellation: 120