From 74d96ce6249e0c53d25ec7e03197e999d4dcc938 Mon Sep 17 00:00:00 2001 From: Johnny Cao <5037004+AgentTroll@users.noreply.github.com> Date: Mon, 17 Jun 2019 03:28:12 -0700 Subject: [PATCH] Fixes #2121 - Allow players to teleport into a location with water if configured (#2520) # Description of #2520 This is a continuation of #2457, accidentally hit rebase and recommitted a load of commits from the 2.x log into my repo... I've reverted the code and added the configuration option, modifying the `LocationUtil#HOLLOW_MATERIALS` as necessary when the config is loaded. New demo: streamable.com/pm50r ``` [16:01:00 INFO]: Server version: 1.13.2-R0.1-SNAPSHOT git-Spigot-3cb9dcb-77ca7ca (MC: 1.13.2) [16:01:00 INFO]: EssentialsX version: 2.16.1.154 [16:01:00 INFO]: Vault is not installed. Chat and permissions may not work. ``` # Description of #2457 Fixes #2121. Prior to the addition of this patch, teleporting from another world through commands such as `/spawn` and `/home` would cause players to be teleported to the surface of the water. After this patch, using the same command will correctly teleport them to the original location. In seeing that the addition of water would cause the `HOLLOW_MATERIALS` set to be identical to the `TRANSPARENT_MATERIALS`, I have removed the latter's usage and simply added water to the former. I'm not exactly sure if adding water to `HOLLOW_MATERIALS` is the right decision, but it fixes the issue, and I personally don't really see any point in not having water in the list. I imagine some people might use this as a way to drop players on the surface of the water, but they can fix that issue quite easily by actually going to the surface and setting the location there. I also can see that water is not necessarily a "safe" location because players can drown, but I really see no other alternative. The only reason it works like normal in the same world is because the safe location method exempts locations in the same world as the teleporting player, and thus this check is never even performed in the first place for those players anyway. **Demo** ``` [16:22:49 INFO]: CONSOLE issued server command: /ess version [16:22:49 INFO]: Server version: 1.13.2-R0.1-SNAPSHOT git-Paper-"16db0e6a" (MC: 1.13.2) [16:22:49 INFO]: EssentialsX version: 2.16.1.9 [16:22:49 INFO]: LuckPerms version: 4.3.73 [16:22:49 INFO]: Vault is not installed. Chat and permissions may not work. ``` https://streamable.com/71072 --- .../com/earth2me/essentials/ISettings.java | 2 ++ .../src/com/earth2me/essentials/Settings.java | 32 ++++++++++++++++--- .../essentials/utils/LocationUtil.java | 17 +++++++--- Essentials/src/config.yml | 5 +++ 4 files changed, 48 insertions(+), 8 deletions(-) diff --git a/Essentials/src/com/earth2me/essentials/ISettings.java b/Essentials/src/com/earth2me/essentials/ISettings.java index 9ab04b4cb..95d1404c0 100644 --- a/Essentials/src/com/earth2me/essentials/ISettings.java +++ b/Essentials/src/com/earth2me/essentials/ISettings.java @@ -327,5 +327,7 @@ public interface ISettings extends IConf { boolean allowOldIdSigns(); + boolean isWaterSafe(); + boolean isSafeUsermap(); } diff --git a/Essentials/src/com/earth2me/essentials/Settings.java b/Essentials/src/com/earth2me/essentials/Settings.java index 325fbb662..edc834f66 100644 --- a/Essentials/src/com/earth2me/essentials/Settings.java +++ b/Essentials/src/com/earth2me/essentials/Settings.java @@ -8,8 +8,8 @@ import com.earth2me.essentials.textreader.IText; import com.earth2me.essentials.textreader.SimpleTextInput; import com.earth2me.essentials.utils.EnumUtil; import com.earth2me.essentials.utils.FormatUtil; +import com.earth2me.essentials.utils.LocationUtil; import com.earth2me.essentials.utils.NumberUtil; - import net.ess3.api.IEssentials; import org.bukkit.ChatColor; import org.bukkit.Material; @@ -24,15 +24,24 @@ import java.math.RoundingMode; import java.text.DecimalFormat; import java.text.DecimalFormatSymbols; import java.text.NumberFormat; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Locale; +import java.util.Map; import java.util.Map.Entry; +import java.util.Set; import java.util.logging.Level; import java.util.logging.Logger; - -import static com.earth2me.essentials.I18n.tl; import java.util.regex.Pattern; import java.util.regex.PatternSyntaxException; +import static com.earth2me.essentials.I18n.tl; + public class Settings implements net.ess3.api.ISettings { private final transient EssentialsConf config; @@ -543,6 +552,7 @@ public class Settings implements net.ess3.api.ISettings { itemDbType = _getItemDbType(); forceEnableRecipe = _isForceEnableRecipe(); allowOldIdSigns = _allowOldIdSigns(); + isWaterSafe = _isWaterSafe(); isSafeUsermap = _isSafeUsermap(); } @@ -1542,6 +1552,20 @@ public class Settings implements net.ess3.api.ISettings { return allowOldIdSigns; } + private boolean isWaterSafe; + + private boolean _isWaterSafe() { + boolean _isWaterSafe = config.getBoolean("is-water-safe", false); + LocationUtil.setIsWaterSafe(_isWaterSafe); + + return _isWaterSafe; + } + + @Override + public boolean isWaterSafe() { + return isWaterSafe; + } + private boolean isSafeUsermap; private boolean _isSafeUsermap() { diff --git a/Essentials/src/com/earth2me/essentials/utils/LocationUtil.java b/Essentials/src/com/earth2me/essentials/utils/LocationUtil.java index d8a54781c..2683e1e7d 100644 --- a/Essentials/src/com/earth2me/essentials/utils/LocationUtil.java +++ b/Essentials/src/com/earth2me/essentials/utils/LocationUtil.java @@ -16,6 +16,10 @@ import static com.earth2me.essentials.I18n.tl; public class LocationUtil { + // Water types used for TRANSPARENT_MATERIALS and is-water-safe config option + private static final Set WATER_TYPES = + EnumUtil.getAllMatching(Material.class, "WATER", "FLOWING_WATER"); + // The player can stand inside these materials private static final Set HOLLOW_MATERIALS = new HashSet<>(); private static final Set TRANSPARENT_MATERIALS = new HashSet<>(); @@ -29,10 +33,15 @@ public class LocationUtil { } TRANSPARENT_MATERIALS.addAll(HOLLOW_MATERIALS); - TRANSPARENT_MATERIALS.add(Material.WATER); - try { - TRANSPARENT_MATERIALS.add(Material.valueOf("FLOWING_WATER")); - } catch (Exception ignored) {} // 1.13 WATER uses Levelled + TRANSPARENT_MATERIALS.addAll(WATER_TYPES); + } + + public static void setIsWaterSafe(boolean isWaterSafe) { + if (isWaterSafe) { + HOLLOW_MATERIALS.addAll(WATER_TYPES); + } else { + HOLLOW_MATERIALS.removeAll(WATER_TYPES); + } } public static final int RADIUS = 3; diff --git a/Essentials/src/config.yml b/Essentials/src/config.yml index 678375225..2ce72d16f 100644 --- a/Essentials/src/config.yml +++ b/Essentials/src/config.yml @@ -559,6 +559,11 @@ allow-direct-hat: true # This doesn't affect running the command from the console, where a world is always required. allow-world-in-broadcastworld: true +# Consider water blocks as "safe," therefore allowing players to teleport +# using commands such as /home or /spawn to a location that is occupied +# by water blocks +is-water-safe: false + # Should the usermap try to sanitise usernames before saving them? # You should only change this to false if you use Minecraft China. safe-usermap-names: true