From ae2867136aa809c35912cc9eca53cc896e7adfb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20S=C3=B6derberg?= Date: Wed, 8 Apr 2020 16:39:19 +0200 Subject: [PATCH] Make the fly flag an enum flag instead. It now accepts: `true`, `false` and `default`, where `default` is the default value, and does not change the flight status at all. --- .../plotsquared/plot/config/Captions.java | 4 +- .../plot/flags/GlobalFlagContainer.java | 6 +- .../flags/implementations/FlightFlag.java | 19 ------ .../plot/flags/implementations/FlyFlag.java | 64 +++++++++++++++++++ .../plot/listener/PlotListener.java | 8 ++- 5 files changed, 75 insertions(+), 26 deletions(-) delete mode 100644 Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/implementations/FlightFlag.java create mode 100644 Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/implementations/FlyFlag.java 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 9e7fa737b..366b33e4c 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 @@ -567,13 +567,15 @@ public enum Captions implements Caption { FLAG_CATEGORY_ENUM("Generic Enum Flags", "Flags"), FLAG_CATEGORY_DECIMAL("Decimal Flags", "Flags"), FLAG_CATEGORY_BOOLEAN("Boolean Flags", "Flags"), + FLAG_CATEGORY_FLY("Flight Flags", "Flags"), FLAG_CATEGORY_MIXED("Mixed Value Flags", "Flags"), // // FLAG_DESCRIPTION_ENTITY_CAP("Set to an integer value to limit the amount of entities on the plot.", "Flags"), FLAG_DESCRIPTION_EXPLOSION("Set to `true` to enable explosions in the plot, and `false` to disable them.", "Flags"), FLAG_DESCRIPTION_MUSIC("Set to a music disk ID (item name) to play the music disc inside of the plot.", "Flags"), - FLAG_DESCRIPTION_FLIGHT("Set to `true` to enable flight within the plot when in survival or adventure mode.", "Flags"), + FLAG_DESCRIPTION_FLIGHT("Set to `true` to enable flight within the plot when in survival or adventure mode," + + " set to `default` to use the gamemode default, and `false` to disable flight entirely.", "Flags"), FLAG_DESCRIPTION_UNTRUSTED("Set to `false` to disallow untrusted players from visiting the plot.", "Flags"), FLAG_DESCRIPTION_DENY_EXIT("Set to `true` to disallow players from exiting the plot.", "Flags"), FLAG_DESCRIPTION_DESCRIPTION("Plot description. Supports '&' color codes.", "Flags"), 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 5a9223a61..3aa7759ba 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 @@ -8,6 +8,7 @@ import com.github.intellectualsites.plotsquared.plot.flags.implementations.Block import com.github.intellectualsites.plotsquared.plot.flags.implementations.BlockIgnitionFlag; import com.github.intellectualsites.plotsquared.plot.flags.implementations.BlockedCmdsFlag; import com.github.intellectualsites.plotsquared.plot.flags.implementations.BreakFlag; +import com.github.intellectualsites.plotsquared.plot.flags.implementations.CoralDryFlag; 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; @@ -19,7 +20,7 @@ import com.github.intellectualsites.plotsquared.plot.flags.implementations.Entit import com.github.intellectualsites.plotsquared.plot.flags.implementations.ExplosionFlag; import com.github.intellectualsites.plotsquared.plot.flags.implementations.FarewellFlag; import com.github.intellectualsites.plotsquared.plot.flags.implementations.FeedFlag; -import com.github.intellectualsites.plotsquared.plot.flags.implementations.FlightFlag; +import com.github.intellectualsites.plotsquared.plot.flags.implementations.FlyFlag; import com.github.intellectualsites.plotsquared.plot.flags.implementations.ForcefieldFlag; import com.github.intellectualsites.plotsquared.plot.flags.implementations.GamemodeFlag; import com.github.intellectualsites.plotsquared.plot.flags.implementations.GrassGrowFlag; @@ -60,7 +61,6 @@ import com.github.intellectualsites.plotsquared.plot.flags.implementations.Serve import com.github.intellectualsites.plotsquared.plot.flags.implementations.SnowFormFlag; import com.github.intellectualsites.plotsquared.plot.flags.implementations.SnowMeltFlag; import com.github.intellectualsites.plotsquared.plot.flags.implementations.SoilDryFlag; -import com.github.intellectualsites.plotsquared.plot.flags.implementations.CoralDryFlag; import com.github.intellectualsites.plotsquared.plot.flags.implementations.TamedAttackFlag; import com.github.intellectualsites.plotsquared.plot.flags.implementations.TamedInteractFlag; import com.github.intellectualsites.plotsquared.plot.flags.implementations.TimeFlag; @@ -96,7 +96,6 @@ public final class GlobalFlagContainer extends FlagContainer { // Register all default flags here // Boolean flags this.addFlag(ExplosionFlag.EXPLOSION_FALSE); - this.addFlag(FlightFlag.FLIGHT_FLAG_FALSE); this.addFlag(UntrustedVisitFlag.UNTRUSTED_VISIT_FLAG_TRUE); this.addFlag(DenyExitFlag.DENY_EXIT_FLAG_FALSE); this.addFlag(DescriptionFlag.DESCRIPTION_FLAG_EMPTY); @@ -150,6 +149,7 @@ public final class GlobalFlagContainer extends FlagContainer { this.addFlag(WeatherFlag.PLOT_WEATHER_FLAG_OFF); this.addFlag(DenyTeleportFlag.DENY_TELEPORT_FLAG_NONE); this.addFlag(TitlesFlag.TITLES_NONE); + this.addFlag(FlyFlag.FLIGHT_FLAG_DEFAULT); // Integer flags this.addFlag(AnimalCapFlag.ANIMAL_CAP_UNLIMITED); diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/implementations/FlightFlag.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/implementations/FlightFlag.java deleted file mode 100644 index ffe9df063..000000000 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/implementations/FlightFlag.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.github.intellectualsites.plotsquared.plot.flags.implementations; - -import com.github.intellectualsites.plotsquared.plot.config.Captions; -import com.github.intellectualsites.plotsquared.plot.flags.types.BooleanFlag; -import org.jetbrains.annotations.NotNull; - -public class FlightFlag extends BooleanFlag { - - public static final FlightFlag FLIGHT_FLAG_FALSE = new FlightFlag(false); - - protected FlightFlag(final boolean value) { - super(value, Captions.FLAG_DESCRIPTION_FLIGHT); - } - - @Override protected FlightFlag flagOf(@NotNull Boolean value) { - return new FlightFlag(value); - } - -} diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/implementations/FlyFlag.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/implementations/FlyFlag.java new file mode 100644 index 000000000..2da869b1e --- /dev/null +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/implementations/FlyFlag.java @@ -0,0 +1,64 @@ +package com.github.intellectualsites.plotsquared.plot.flags.implementations; + +import com.github.intellectualsites.plotsquared.plot.config.Captions; +import com.github.intellectualsites.plotsquared.plot.flags.PlotFlag; +import org.jetbrains.annotations.NotNull; + +public class FlyFlag extends PlotFlag { + + public static final FlyFlag FLIGHT_FLAG_DISABLED = new FlyFlag(FlyStatus.DISABLED); + public static final FlyFlag FLIGHT_FLAG_ENABLED = new FlyFlag(FlyStatus.ENABLED); + public static final FlyFlag FLIGHT_FLAG_DEFAULT = new FlyFlag(FlyStatus.DEFAULT); + + protected FlyFlag(final FlyStatus value) { + super(value, Captions.FLAG_CATEGORY_BOOLEAN, Captions.FLAG_DESCRIPTION_FLIGHT); + } + + @Override public FlyFlag parse(@NotNull final String input) { + switch (input.toLowerCase()) { + case "true": + case "enabled": + case "allow": + return FLIGHT_FLAG_ENABLED; + case "false": + case "disabled": + case "disallow": + return FLIGHT_FLAG_DISABLED; + default: + return FLIGHT_FLAG_DEFAULT; + } + } + + @Override public FlyFlag merge(@NotNull final FlyStatus newValue) { + if (newValue == FlyStatus.ENABLED || this.getValue() == FlyStatus.ENABLED) { + return FLIGHT_FLAG_ENABLED; + } + return flagOf(newValue); + } + + @Override public String toString() { + return this.getValue().name().toLowerCase(); + } + + @Override public String getExample() { + return "true"; + } + + @Override protected FlyFlag flagOf(@NotNull final FlyStatus value) { + switch (value) { + case ENABLED: + return FLIGHT_FLAG_ENABLED; + case DISABLED: + return FLIGHT_FLAG_DISABLED; + default: + return FLIGHT_FLAG_DEFAULT; + } + } + + public enum FlyStatus { + ENABLED, + DISABLED, + DEFAULT + } + +} diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/listener/PlotListener.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/listener/PlotListener.java index 12f70a40e..942c5e101 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/listener/PlotListener.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/listener/PlotListener.java @@ -124,7 +124,8 @@ public class PlotListener { } } - if (plot.getFlag(FlightFlag.class)) { + final FlyFlag.FlyStatus flyStatus = plot.getFlag(FlyFlag.class); + if (flyStatus != FlyFlag.FlyStatus.DEFAULT) { boolean flight = player.getFlight(); GameMode gamemode = player.getGameMode(); if (flight != (gamemode == GameModes.CREATIVE @@ -132,7 +133,7 @@ public class PlotListener { player.setPersistentMeta("flight", ByteArrayUtilities.booleanToBytes(player.getFlight())); } - player.setFlight(true); + player.setFlight(flyStatus == FlyFlag.FlyStatus.ENABLED); } final GameMode gameMode = plot.getFlag(GamemodeFlag.class); @@ -299,7 +300,8 @@ public class PlotListener { } } - if (plot.getFlag(FlightFlag.class)) { + final FlyFlag.FlyStatus flyStatus = plot.getFlag(FlyFlag.class); + if (flyStatus != FlyFlag.FlyStatus.DEFAULT) { if (player.hasPersistentMeta("flight")) { player.setFlight( ByteArrayUtilities.bytesToBoolean(player.getPersistentMeta("flight")));