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
This commit is contained in:
Johnny Cao 2019-06-17 03:28:12 -07:00 committed by md678685
parent 8b7bcbb880
commit 74d96ce624
4 changed files with 48 additions and 8 deletions

View File

@ -327,5 +327,7 @@ public interface ISettings extends IConf {
boolean allowOldIdSigns(); boolean allowOldIdSigns();
boolean isWaterSafe();
boolean isSafeUsermap(); boolean isSafeUsermap();
} }

View File

@ -8,8 +8,8 @@ import com.earth2me.essentials.textreader.IText;
import com.earth2me.essentials.textreader.SimpleTextInput; import com.earth2me.essentials.textreader.SimpleTextInput;
import com.earth2me.essentials.utils.EnumUtil; import com.earth2me.essentials.utils.EnumUtil;
import com.earth2me.essentials.utils.FormatUtil; import com.earth2me.essentials.utils.FormatUtil;
import com.earth2me.essentials.utils.LocationUtil;
import com.earth2me.essentials.utils.NumberUtil; import com.earth2me.essentials.utils.NumberUtil;
import net.ess3.api.IEssentials; import net.ess3.api.IEssentials;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Material; import org.bukkit.Material;
@ -24,15 +24,24 @@ import java.math.RoundingMode;
import java.text.DecimalFormat; import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols; import java.text.DecimalFormatSymbols;
import java.text.NumberFormat; 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.Map.Entry;
import java.util.Set;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import static com.earth2me.essentials.I18n.tl;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException; import java.util.regex.PatternSyntaxException;
import static com.earth2me.essentials.I18n.tl;
public class Settings implements net.ess3.api.ISettings { public class Settings implements net.ess3.api.ISettings {
private final transient EssentialsConf config; private final transient EssentialsConf config;
@ -543,6 +552,7 @@ public class Settings implements net.ess3.api.ISettings {
itemDbType = _getItemDbType(); itemDbType = _getItemDbType();
forceEnableRecipe = _isForceEnableRecipe(); forceEnableRecipe = _isForceEnableRecipe();
allowOldIdSigns = _allowOldIdSigns(); allowOldIdSigns = _allowOldIdSigns();
isWaterSafe = _isWaterSafe();
isSafeUsermap = _isSafeUsermap(); isSafeUsermap = _isSafeUsermap();
} }
@ -1542,6 +1552,20 @@ public class Settings implements net.ess3.api.ISettings {
return allowOldIdSigns; 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;
private boolean _isSafeUsermap() { private boolean _isSafeUsermap() {

View File

@ -16,6 +16,10 @@ import static com.earth2me.essentials.I18n.tl;
public class LocationUtil { public class LocationUtil {
// Water types used for TRANSPARENT_MATERIALS and is-water-safe config option
private static final Set<Material> WATER_TYPES =
EnumUtil.getAllMatching(Material.class, "WATER", "FLOWING_WATER");
// The player can stand inside these materials // The player can stand inside these materials
private static final Set<Material> HOLLOW_MATERIALS = new HashSet<>(); private static final Set<Material> HOLLOW_MATERIALS = new HashSet<>();
private static final Set<Material> TRANSPARENT_MATERIALS = new HashSet<>(); private static final Set<Material> TRANSPARENT_MATERIALS = new HashSet<>();
@ -29,10 +33,15 @@ public class LocationUtil {
} }
TRANSPARENT_MATERIALS.addAll(HOLLOW_MATERIALS); TRANSPARENT_MATERIALS.addAll(HOLLOW_MATERIALS);
TRANSPARENT_MATERIALS.add(Material.WATER); TRANSPARENT_MATERIALS.addAll(WATER_TYPES);
try { }
TRANSPARENT_MATERIALS.add(Material.valueOf("FLOWING_WATER"));
} catch (Exception ignored) {} // 1.13 WATER uses Levelled 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; public static final int RADIUS = 3;

View File

@ -559,6 +559,11 @@ allow-direct-hat: true
# This doesn't affect running the command from the console, where a world is always required. # This doesn't affect running the command from the console, where a world is always required.
allow-world-in-broadcastworld: true 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? # Should the usermap try to sanitise usernames before saving them?
# You should only change this to false if you use Minecraft China. # You should only change this to false if you use Minecraft China.
safe-usermap-names: true safe-usermap-names: true