From 9f384c71f3ffc66e171029fe0b7efd37c66ac4c5 Mon Sep 17 00:00:00 2001 From: pop4959 Date: Wed, 1 Jul 2020 14:03:22 -0700 Subject: [PATCH] Confirm home replacement when overwriting existing homes (#3338) Co-authored-by: Josh Roy <10731363+JRoy@users.noreply.github.com> Co-authored-by: MD <1917406+md678685@users.noreply.github.com> Warns the player that they are trying to replace a home, if they try to set one with the same name, when `confirm-home-overwrite` is set to `true` in the config. https://user-images.githubusercontent.com/17698576/83004206-3633eb00-9fc4-11ea-9317-fe245fed9cbb.png Fixes #2038 Closes #2847 --- .../src/com/earth2me/essentials/ISettings.java | 2 ++ .../src/com/earth2me/essentials/Settings.java | 7 ++++++- .../src/com/earth2me/essentials/User.java | 18 ++++++++++++++++++ .../src/com/earth2me/essentials/UserData.java | 4 ++++ .../essentials/commands/Commandsethome.java | 9 +++++++++ Essentials/src/config.yml | 3 +++ Essentials/src/messages.properties | 1 + 7 files changed, 43 insertions(+), 1 deletion(-) diff --git a/Essentials/src/com/earth2me/essentials/ISettings.java b/Essentials/src/com/earth2me/essentials/ISettings.java index 96c1e24cd..962b26165 100644 --- a/Essentials/src/com/earth2me/essentials/ISettings.java +++ b/Essentials/src/com/earth2me/essentials/ISettings.java @@ -372,6 +372,8 @@ public interface ISettings extends IConf { boolean isSpawnIfNoHome(); + boolean isConfirmHomeOverwrite(); + boolean infoAfterDeath(); } diff --git a/Essentials/src/com/earth2me/essentials/Settings.java b/Essentials/src/com/earth2me/essentials/Settings.java index 6046fc3e4..b8900e979 100644 --- a/Essentials/src/com/earth2me/essentials/Settings.java +++ b/Essentials/src/com/earth2me/essentials/Settings.java @@ -1742,7 +1742,12 @@ public class Settings implements net.ess3.api.ISettings { public boolean isSpawnIfNoHome() { return config.getBoolean("spawn-if-no-home", true); } - + + @Override + public boolean isConfirmHomeOverwrite() { + return config.getBoolean("confirm-home-overwrite", false); + } + @Override public boolean infoAfterDeath() { return config.getBoolean("send-info-after-death", false); diff --git a/Essentials/src/com/earth2me/essentials/User.java b/Essentials/src/com/earth2me/essentials/User.java index dbc6fc464..574849f93 100644 --- a/Essentials/src/com/earth2me/essentials/User.java +++ b/Essentials/src/com/earth2me/essentials/User.java @@ -66,6 +66,8 @@ public class User extends UserData implements Comparable, IMessageRecipien private final Map confirmingPayments = new WeakHashMap<>(); private String confirmingClearCommand; private long lastNotifiedAboutMailsMs; + private String lastHomeConfirmation; + private long lastHomeConfirmationTimestamp; public User(final Player base, final IEssentials ess) { super(base, ess); @@ -979,4 +981,20 @@ public class User extends UserData implements Comparable, IMessageRecipien } } } + + public String getLastHomeConfirmation() { + return lastHomeConfirmation; + } + + public void setLastHomeConfirmation(String lastHomeConfirmation) { + this.lastHomeConfirmation = lastHomeConfirmation; + } + + public long getLastHomeConfirmationTimestamp() { + return lastHomeConfirmationTimestamp; + } + + public void setLastHomeConfirmationTimestamp() { + this.lastHomeConfirmationTimestamp = System.currentTimeMillis(); + } } diff --git a/Essentials/src/com/earth2me/essentials/UserData.java b/Essentials/src/com/earth2me/essentials/UserData.java index 86abc4ec1..163537264 100644 --- a/Essentials/src/com/earth2me/essentials/UserData.java +++ b/Essentials/src/com/earth2me/essentials/UserData.java @@ -217,6 +217,10 @@ public abstract class UserData extends PlayerExtension implements IConf { return config.hasProperty("home"); } + public boolean hasHome(String name) { + return config.hasProperty("homes." + name); + } + private String nickname; public String _getNickname() { diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandsethome.java b/Essentials/src/com/earth2me/essentials/commands/Commandsethome.java index 28c7b2b48..99b81406e 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandsethome.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandsethome.java @@ -10,6 +10,7 @@ import org.bukkit.Server; import java.util.Collections; import java.util.List; import java.util.Locale; +import java.util.concurrent.TimeUnit; import static com.earth2me.essentials.I18n.tl; @@ -55,8 +56,16 @@ public class Commandsethome extends EssentialsCommand { throw new Exception(tl("unsafeTeleportDestination", location.getWorld().getName(), location.getBlockX(), location.getBlockY(), location.getBlockZ())); } + if (ess.getSettings().isConfirmHomeOverwrite() && usersHome.hasHome(name) && (!name.equals(usersHome.getLastHomeConfirmation()) || name.equals(usersHome.getLastHomeConfirmation()) && System.currentTimeMillis() - usersHome.getLastHomeConfirmationTimestamp() > TimeUnit.MINUTES.toMillis(2))) { + usersHome.setLastHomeConfirmation(name); + usersHome.setLastHomeConfirmationTimestamp(); + user.sendMessage(tl("homeConfirmation", name)); + return; + } + usersHome.setHome(name, location); user.sendMessage(tl("homeSet", user.getLocation().getWorld().getName(), user.getLocation().getBlockX(), user.getLocation().getBlockY(), user.getLocation().getBlockZ(), name)); + usersHome.setLastHomeConfirmation(null); } diff --git a/Essentials/src/config.yml b/Essentials/src/config.yml index 3cca03c2a..9ead6e8e0 100644 --- a/Essentials/src/config.yml +++ b/Essentials/src/config.yml @@ -676,6 +676,9 @@ compass-towards-home-perm: false # If set to false, players will not be teleported when they run /home without setting a home first. spawn-if-no-home: true +# Should players be asked to provide confirmation for homes which they attempt to overwrite? +confirm-home-overwrite: false + ############################################################ # +------------------------------------------------------+ # # | Economy | # diff --git a/Essentials/src/messages.properties b/Essentials/src/messages.properties index b80a6eb8b..0a0336820 100644 --- a/Essentials/src/messages.properties +++ b/Essentials/src/messages.properties @@ -280,6 +280,7 @@ holeInFloor=\u00a74Hole in floor\! homeCommandDescription=Teleport to your home. homeCommandUsage=/ [player:][name] homes=\u00a76Homes\:\u00a7r {0} +homeConfirmation=\u00a76You already have a home named \u00a7c{0}\u00a76!\nTo overwrite your existing home, type the command again. homeSet=\u00a76Home set to current location. hour=hour hours=hours