From 3df7d3cde9d5ea80c5f1dbf262d38d1c843f9d90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20S=C3=B6derberg?= Date: Mon, 17 Feb 2020 18:45:50 +0100 Subject: [PATCH] Port deny-teleport --- .../bukkit/listeners/PlayerEvents.java | 4 +- .../plotsquared/plot/config/Captions.java | 1 + .../plotsquared/plot/flag/Flags.java | 2 - .../plot/flag/TeleportDenyFlag.java | 32 +----- .../plot/flags/GlobalFlagContainer.java | 6 +- .../implementations/DenyTeleportFlag.java | 99 +++++++++++++++++++ 6 files changed, 109 insertions(+), 35 deletions(-) create mode 100644 Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/implementations/DenyTeleportFlag.java diff --git a/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/listeners/PlayerEvents.java b/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/listeners/PlayerEvents.java index 85a6bcdd4..97ab143f5 100644 --- a/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/listeners/PlayerEvents.java +++ b/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/listeners/PlayerEvents.java @@ -8,10 +8,12 @@ import com.github.intellectualsites.plotsquared.plot.PlotSquared; import com.github.intellectualsites.plotsquared.plot.config.Captions; import com.github.intellectualsites.plotsquared.plot.config.Settings; import com.github.intellectualsites.plotsquared.plot.flag.Flags; +import com.github.intellectualsites.plotsquared.plot.flag.TeleportDenyFlag; import com.github.intellectualsites.plotsquared.plot.flags.implementations.AnimalAttackFlag; import com.github.intellectualsites.plotsquared.plot.flags.implementations.AnimalInteractFlag; import com.github.intellectualsites.plotsquared.plot.flags.implementations.BlockBurnFlag; import com.github.intellectualsites.plotsquared.plot.flags.implementations.BlockIgnitionFlag; +import com.github.intellectualsites.plotsquared.plot.flags.implementations.DenyTeleportFlag; import com.github.intellectualsites.plotsquared.plot.flags.implementations.DisablePhysicsFlag; import com.github.intellectualsites.plotsquared.plot.flags.implementations.DoneFlag; import com.github.intellectualsites.plotsquared.plot.flags.implementations.ExplosionFlag; @@ -818,7 +820,7 @@ import java.util.regex.Pattern; } Plot plot = area.getPlot(location); if (plot != null) { - final boolean result = Flags.DENY_TELEPORT.allowsTeleport(pp, plot); + final boolean result = DenyTeleportFlag.allowsTeleport(pp, plot); // there is one possibility to still allow teleportation: // to is identical to the plot's home location, and untrusted-visit is true // i.e. untrusted-visit can override deny-teleport diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/config/Captions.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/config/Captions.java index e393bad70..80df30ca2 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/config/Captions.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/config/Captions.java @@ -626,6 +626,7 @@ public enum Captions implements Caption { FLAG_DESCRIPTION_VEHICLE_USE("Set to `true` to allow guests to use vehicles in the plot.", "Flags"), FLAG_DESCRIPTION_VILLAGER_INTERACT("Set to `true` to allow guests to interact with villagers in the plot.", "Flags"), FLAG_DESCRIPTION_VINE_GROW("Set to `true` to allow vines to grow within the plot.", "Flags"), + FLAG_DESCRIPTION_DENY_TELEPORT("Deny a certain group from teleporting to the plot. Available groups: members, nonmembers, trusted, nontrusted, nonowners", "Flags"), // // FLAG_ERROR_BOOLEAN("Flag value must be a boolean (true|false)", "Flags"), diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flag/Flags.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flag/Flags.java index c25aa8436..c72b497ef 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flag/Flags.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flag/Flags.java @@ -37,6 +37,4 @@ public final class Flags { } }; - public static final TeleportDenyFlag DENY_TELEPORT = new TeleportDenyFlag("deny-teleport"); - } diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flag/TeleportDenyFlag.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flag/TeleportDenyFlag.java index 6c02516fd..1a8e7c694 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flag/TeleportDenyFlag.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flag/TeleportDenyFlag.java @@ -1,40 +1,10 @@ package com.github.intellectualsites.plotsquared.plot.flag; -import com.github.intellectualsites.plotsquared.plot.object.Plot; -import com.github.intellectualsites.plotsquared.plot.object.PlotPlayer; - public class TeleportDenyFlag extends EnumFlag { public TeleportDenyFlag(String name) { super(name, "trusted", "members", "nonmembers", "nontrusted", "nonowners"); } - public boolean allowsTeleport(PlotPlayer player, Plot plot) { - String value = plot.getFlag(this, null); - if (value == null || !plot.hasOwner()) { - return true; - } - boolean result; - switch (plot.getFlag(this, null)) { - case "trusted": - result = !plot.getTrusted().contains(player.getUUID()); - break; - case "members": - result = !plot.getMembers().contains(player.getUUID()); - break; - case "nonmembers": - result = plot.isAdded(player.getUUID()); - break; - case "nontrusted": - result = plot.getTrusted().contains(player.getUUID()) || plot - .isOwner(player.getUUID()); - break; - case "nonowners": - result = plot.isOwner(player.getUUID()); - break; - default: - return true; - } - return result || player.hasPermission("plots.admin.entry.denied"); - } + } diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/GlobalFlagContainer.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/GlobalFlagContainer.java index a2b98b396..60e104355 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/GlobalFlagContainer.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/GlobalFlagContainer.java @@ -7,6 +7,7 @@ import com.github.intellectualsites.plotsquared.plot.flags.implementations.Anima import com.github.intellectualsites.plotsquared.plot.flags.implementations.BlockBurnFlag; import com.github.intellectualsites.plotsquared.plot.flags.implementations.BlockIgnitionFlag; import com.github.intellectualsites.plotsquared.plot.flags.implementations.DenyExitFlag; +import com.github.intellectualsites.plotsquared.plot.flags.implementations.DenyTeleportFlag; import com.github.intellectualsites.plotsquared.plot.flags.implementations.DescriptionFlag; import com.github.intellectualsites.plotsquared.plot.flags.implementations.DeviceInteractFlag; import com.github.intellectualsites.plotsquared.plot.flags.implementations.DisablePhysicsFlag; @@ -92,7 +93,6 @@ public final class GlobalFlagContainer extends FlagContainer { this.addFlag(DescriptionFlag.DESCRIPTION_FLAG_EMPTY); this.addFlag(GreetingFlag.GREETING_FLAG_EMPTY); this.addFlag(FarewellFlag.FAREWELL_FLAG_EMPTY); - this.addFlag(PlotWeatherFlag.PLOT_WEATHER_FLAG_OFF); this.addFlag(AnimalAttackFlag.ANIMAL_ATTACK_FALSE); this.addFlag(AnimalInteractFlag.ANIMAL_INTERACT_FALSE); this.addFlag(BlockBurnFlag.BLOCK_BURN_FALSE); @@ -137,6 +137,10 @@ public final class GlobalFlagContainer extends FlagContainer { this.addFlag(InstabreakFlag.INSTABREAK_FALSE); this.addFlag(InvincibleFlag.INVINCIBLE_FALSE); + // Enum Flags + this.addFlag(PlotWeatherFlag.PLOT_WEATHER_FLAG_OFF); + this.addFlag(DenyTeleportFlag.DENY_TELEPORT_FLAG_NONE); + // Integer flags this.addFlag(AnimalCapFlag.ANIMAL_CAP_UNLIMITED); this.addFlag(EntityCapFlag.ENTITY_CAP_UNLIMITED); diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/implementations/DenyTeleportFlag.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/implementations/DenyTeleportFlag.java new file mode 100644 index 000000000..4ea8f8a59 --- /dev/null +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/implementations/DenyTeleportFlag.java @@ -0,0 +1,99 @@ +package com.github.intellectualsites.plotsquared.plot.flags.implementations; + +import com.github.intellectualsites.plotsquared.plot.config.Captions; +import com.github.intellectualsites.plotsquared.plot.flags.FlagParseException; +import com.github.intellectualsites.plotsquared.plot.flags.PlotFlag; +import com.github.intellectualsites.plotsquared.plot.object.Plot; +import com.github.intellectualsites.plotsquared.plot.object.PlotPlayer; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +public class DenyTeleportFlag extends PlotFlag { + + public static final DenyTeleportFlag DENY_TELEPORT_FLAG_NONE = new DenyTeleportFlag(DeniedGroup.NONE); + + /** + * Construct a new flag instance. + * + * @param value Flag value + */ + protected DenyTeleportFlag(@NotNull DeniedGroup value) { + super(value, Captions.FLAG_CATEGORY_ENUM, Captions.FLAG_DESCRIPTION_DENY_TELEPORT); + } + + @Override public DenyTeleportFlag parse(@NotNull String input) throws FlagParseException { + final DeniedGroup group = DeniedGroup.fromString(input); + if (group == null) { + throw new FlagParseException(this, input, Captions.FLAG_ERROR_ENUM, "members, nonmembers," + + " trusted, nontrusted, nonowners"); + } + return flagOf(group); + } + + @Override public DenyTeleportFlag merge(@NotNull DeniedGroup newValue) { + if (getValue().ordinal() < newValue.ordinal()) { + return flagOf(newValue); + } + return this; + } + + @Override public String toString() { + return this.getValue().name(); + } + + @Override public String getExample() { + return "trusted"; + } + + @Override protected DenyTeleportFlag flagOf(@NotNull DeniedGroup value) { + return new DenyTeleportFlag(value); + } + + public static boolean allowsTeleport(PlotPlayer player, Plot plot) { + final DeniedGroup value = plot.getFlag(DenyTeleportFlag.class); + if (value == DeniedGroup.NONE) { + return true; + } + final boolean result; + switch (value) { + case TRUSTED: + result = !plot.getTrusted().contains(player.getUUID()); + break; + case MEMBERS: + result = !plot.getMembers().contains(player.getUUID()); + break; + case NONMEMBERS: + result = plot.isAdded(player.getUUID()); + break; + case NONTRUSTED: + result = plot.getTrusted().contains(player.getUUID()) || plot + .isOwner(player.getUUID()); + break; + case NONOWNERS: + result = plot.isOwner(player.getUUID()); + break; + default: + return true; + } + return result || player.hasPermission("plots.admin.entry.denied"); + } + + public enum DeniedGroup { + NONE, + MEMBERS, + TRUSTED, + NONMEMBERS, + NONTRUSTED, + NONOWNERS; + + @Nullable public static DeniedGroup fromString(@NotNull final String string) { + for (final DeniedGroup group : values()) { + if (group.name().equalsIgnoreCase(string)) { + return group; + } + } + return null; + } + } + +}