Add config option to respawn jailed players to spawn (#4132)

This commit is contained in:
Josh Roy 2021-06-08 21:04:30 -04:00 committed by GitHub
parent 3abddd6f84
commit 54c70581a2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 46 additions and 17 deletions

View File

@ -345,7 +345,7 @@ public interface ISettings extends IConf {
boolean isConfirmCommandEnabledByDefault(String commandName);
boolean isTeleportBackWhenFreedFromJail();
TeleportWhenFreePolicy getTeleportWhenFreePolicy();
boolean isJailOnlineTime();
@ -385,4 +385,10 @@ public interface ISettings extends IConf {
DROP
}
enum TeleportWhenFreePolicy {
SPAWN,
BACK,
OFF
}
}

View File

@ -122,7 +122,7 @@ public class Settings implements net.ess3.api.ISettings {
private NumberFormat currencyFormat;
private List<EssentialsSign> unprotectedSigns = Collections.emptyList();
private List<String> defaultEnabledConfirmCommands;
private boolean teleportBackWhenFreedFromJail;
private TeleportWhenFreePolicy teleportWhenFreePolicy;
private boolean isCompassTowardsHomePerm;
private boolean isAllowWorldInBroadcastworld;
private String itemDbType; // #EasterEgg - admins can manually switch items provider if they want
@ -724,7 +724,7 @@ public class Settings implements net.ess3.api.ISettings {
currencyFormat = _getCurrencyFormat();
unprotectedSigns = _getUnprotectedSign();
defaultEnabledConfirmCommands = _getDefaultEnabledConfirmCommands();
teleportBackWhenFreedFromJail = _isTeleportBackWhenFreedFromJail();
teleportWhenFreePolicy = _getTeleportWhenFreePolicy();
isCompassTowardsHomePerm = _isCompassTowardsHomePerm();
isAllowWorldInBroadcastworld = _isAllowWorldInBroadcastworld();
itemDbType = _getItemDbType();
@ -1703,13 +1703,27 @@ public class Settings implements net.ess3.api.ISettings {
return getDefaultEnabledConfirmCommands().contains(commandName.toLowerCase());
}
private boolean _isTeleportBackWhenFreedFromJail() {
return config.getBoolean("teleport-back-when-freed-from-jail", true);
private TeleportWhenFreePolicy _getTeleportWhenFreePolicy() {
if (config.hasProperty("teleport-back-when-freed-from-jail")) {
return config.getBoolean("teleport-back-when-freed-from-jail", true) ? TeleportWhenFreePolicy.BACK : TeleportWhenFreePolicy.OFF;
}
if (config.hasProperty("teleport-when-freed")) {
// snakeyaml more like cursedyaml
final String value = config.getString("teleport-when-freed", "back").replace("false", "off");
try {
return TeleportWhenFreePolicy.valueOf(value.toUpperCase(Locale.ROOT));
} catch (IllegalArgumentException e) {
throw new RuntimeException("Invalid value \"" + value + "\" for config option \"teleport-when-freed\"!", e);
}
}
return TeleportWhenFreePolicy.BACK;
}
@Override
public boolean isTeleportBackWhenFreedFromJail() {
return teleportBackWhenFreedFromJail;
public TeleportWhenFreePolicy getTeleportWhenFreePolicy() {
return teleportWhenFreePolicy;
}
@Override

View File

@ -649,13 +649,15 @@ public class User extends UserData implements Comparable<User>, IMessageRecipien
setJailed(false);
sendMessage(tl("haveBeenReleased"));
setJail(null);
if (ess.getSettings().isTeleportBackWhenFreedFromJail()) {
if (ess.getSettings().getTeleportWhenFreePolicy() == ISettings.TeleportWhenFreePolicy.BACK) {
final CompletableFuture<Boolean> future = new CompletableFuture<>();
getAsyncTeleport().back(future);
future.exceptionally(e -> {
getAsyncTeleport().respawn(null, TeleportCause.PLUGIN, new CompletableFuture<>());
return false;
});
} else if (ess.getSettings().getTeleportWhenFreePolicy() == ISettings.TeleportWhenFreePolicy.SPAWN) {
getAsyncTeleport().respawn(null, TeleportCause.PLUGIN, new CompletableFuture<>());
}
return true;
}

View File

@ -1,6 +1,7 @@
package com.earth2me.essentials.commands;
import com.earth2me.essentials.CommandSource;
import com.earth2me.essentials.ISettings;
import com.earth2me.essentials.User;
import com.earth2me.essentials.utils.DateUtil;
import com.earth2me.essentials.utils.EnumUtil;
@ -122,19 +123,23 @@ public class Commandtogglejail extends EssentialsCommand {
player.setJailTimeout(0);
player.sendMessage(tl("jailReleasedPlayerNotify"));
player.setJail(null);
if (player.getBase().isOnline() && ess.getSettings().isTeleportBackWhenFreedFromJail()) {
if (player.getBase().isOnline()) {
final CompletableFuture<Boolean> future = getNewExceptionFuture(sender, commandLabel);
player.getAsyncTeleport().back(future);
future.thenAccept(success -> {
if (success) {
sender.sendMessage(tl("jailReleased", player.getName()));
}
});
future.exceptionally(e -> {
player.getAsyncTeleport().respawn(null, PlayerTeleportEvent.TeleportCause.PLUGIN, new CompletableFuture<>());
sender.sendMessage(tl("jailReleased", player.getName()));
return false;
});
if (ess.getSettings().getTeleportWhenFreePolicy() == ISettings.TeleportWhenFreePolicy.BACK) {
player.getAsyncTeleport().back(future);
future.exceptionally(e -> {
player.getAsyncTeleport().respawn(null, PlayerTeleportEvent.TeleportCause.PLUGIN, new CompletableFuture<>());
sender.sendMessage(tl("jailReleased", player.getName()));
return false;
});
} else if (ess.getSettings().getTeleportWhenFreePolicy() == ISettings.TeleportWhenFreePolicy.SPAWN) {
player.getAsyncTeleport().respawn(null, PlayerTeleportEvent.TeleportCause.PLUGIN, future);
}
return;
}
sender.sendMessage(tl("jailReleased", player.getName()));

View File

@ -644,8 +644,10 @@ default-enabled-confirm-commands:
#- pay
#- clearinventory
# 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
# Where should Essentials teleport players when they are freed from jail?
# You can set to "back" to have them teleported to where they were before they were jailed, "spawn" to have them
# teleport to spawn, or "off" to not have them teleport.
teleport-when-freed: back
# 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.