From 080e3d07883491cf8fef88796404af8c803dc0b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20S=C3=B6derberg?= Date: Sun, 16 Feb 2020 15:57:33 +0100 Subject: [PATCH] Port weather flag --- .../plotsquared/plot/commands/FlagCmd.java | 5 +- .../plotsquared/plot/config/Captions.java | 1 + .../plotsquared/plot/flag/Flags.java | 1 - .../plot/flags/GlobalFlagContainer.java | 2 + .../implementations/PlotWeatherFlag.java | 63 +++++++++++++++++++ .../plot/listener/PlotListener.java | 9 +-- 6 files changed, 74 insertions(+), 7 deletions(-) create mode 100644 Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/implementations/PlotWeatherFlag.java diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/FlagCmd.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/FlagCmd.java index 38cf40664..d0bae4fb1 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/FlagCmd.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/FlagCmd.java @@ -8,6 +8,7 @@ import com.github.intellectualsites.plotsquared.plot.database.DBFunc; import com.github.intellectualsites.plotsquared.plot.flag.BlockStateListFlag; import com.github.intellectualsites.plotsquared.plot.flag.FlagManager; import com.github.intellectualsites.plotsquared.plot.flag.Flags; +import com.github.intellectualsites.plotsquared.plot.flag.PlotWeatherFlag; import com.github.intellectualsites.plotsquared.plot.flags.GlobalFlagContainer; import com.github.intellectualsites.plotsquared.plot.flags.PlotFlag; import com.github.intellectualsites.plotsquared.plot.flags.types.BlockTypeListFlag; @@ -122,7 +123,7 @@ public class FlagCmd extends SubCommand { .sendMessage(player, Captions.NO_PERMISSION, Captions.PERMISSION_SET_FLAG_OTHER); return false; } - PlotFlag flag = null; + PlotFlag flag = null; if (args.length > 1) { flag = GlobalFlagContainer.getInstance().getFlagFromString(args[1]); if (flag == null || flag.isReserved()) { @@ -254,7 +255,7 @@ public class FlagCmd extends SubCommand { } if (flag == Flags.TIME) { player.setTime(Long.MAX_VALUE); - } else if (flag == Flags.WEATHER) { + } else if (flag.getClass().isInstance(PlotWeatherFlag.class)) { player.setWeather(PlotWeather.RESET); } MainUtil.sendMessage(player, Captions.FLAG_REMOVED); 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 86d39129a..98b69de27 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 @@ -555,6 +555,7 @@ public enum Captions implements Caption { FLAG_DESCRIPTION_DESCRIPTION("Plot description. Supports '&' color codes.", "Flags"), FLAG_DESCRIPTION_GREETING("Message sent to players on plot entry. Supports '&' color codes.", "Flags"), FLAG_DESCRIPTION_FAREWELL("Message sent to players when leaving the plot. Supports '&' color codes.", "Flags"), + FLAG_DESCRIPTION_WEATHER("Specifies the weather conditions inside of the plot.", "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 23913614f..46ce92ff5 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 @@ -20,7 +20,6 @@ public final class Flags { public static final BooleanFlag HIDE_INFO = new BooleanFlag("hide-info"); public static final BooleanFlag SERVER_PLOT = new BooleanFlag("server-plot"); public static final LongFlag TIME = new LongFlag("time"); - public static final PlotWeatherFlag WEATHER = new PlotWeatherFlag("weather"); public static final DoubleFlag PRICE = new DoubleFlag("price") { @Override public Double parseValue(String input) { Double value = super.parseValue(input); 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 1c105612c..f9a62eefa 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.Farew import com.github.intellectualsites.plotsquared.plot.flags.implementations.FlightFlag; import com.github.intellectualsites.plotsquared.plot.flags.implementations.GreetingFlag; import com.github.intellectualsites.plotsquared.plot.flags.implementations.MusicFlag; +import com.github.intellectualsites.plotsquared.plot.flags.implementations.PlotWeatherFlag; import com.github.intellectualsites.plotsquared.plot.flags.implementations.UntrustedVisitFlag; import lombok.Getter; @@ -35,6 +36,7 @@ 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); } @Override public PlotFlag getFlagErased(Class flagClass) { diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/implementations/PlotWeatherFlag.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/implementations/PlotWeatherFlag.java new file mode 100644 index 000000000..9a5588bfa --- /dev/null +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/implementations/PlotWeatherFlag.java @@ -0,0 +1,63 @@ +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 com.github.intellectualsites.plotsquared.plot.util.PlotWeather; +import org.jetbrains.annotations.NotNull; + +public class PlotWeatherFlag extends PlotFlag { + + public static final PlotWeatherFlag PLOT_WEATHER_FLAG_RAIN = new PlotWeatherFlag(PlotWeather.RAIN); + public static final PlotWeatherFlag PLOT_WEATHER_FLAG_CLEAR = new PlotWeatherFlag(PlotWeather.CLEAR); + public static final PlotWeatherFlag PLOT_WEATHER_FLAG_OFF = new PlotWeatherFlag(PlotWeather.RESET); + + /** + * Construct a new flag instance. + * + * @param value Flag value + */ + protected PlotWeatherFlag(@NotNull PlotWeather value) { + super(value, Captions.FLAG_CATEGORY_WEATHER, Captions.FLAG_DESCRIPTION_WEATHER); + } + + @Override public PlotWeatherFlag parse(@NotNull String input) { + switch (input.toLowerCase()) { + case "rain": + case "storm": + case "on": + case "lightning": + case "thunder": + return flagOf(PlotWeather.RAIN); + case "clear": + case "off": + case "sun": + return flagOf(PlotWeather.CLEAR); + default: + return flagOf(PlotWeather.RESET); + } + } + + @Override public PlotWeatherFlag merge(@NotNull PlotWeather newValue) { + return flagOf(newValue); + } + + @Override public String toString() { + return getValue().toString(); + } + + @Override public String getExample() { + return "storm"; + } + + @Override protected PlotWeatherFlag flagOf(@NotNull PlotWeather value) { + switch (value) { + case RAIN: + return PLOT_WEATHER_FLAG_RAIN; + case CLEAR: + return PLOT_WEATHER_FLAG_CLEAR; + default: + return PLOT_WEATHER_FLAG_OFF; + } + } + +} 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 041d78d43..2884739c3 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 @@ -10,6 +10,7 @@ import com.github.intellectualsites.plotsquared.plot.flags.implementations.Farew import com.github.intellectualsites.plotsquared.plot.flags.implementations.FlightFlag; import com.github.intellectualsites.plotsquared.plot.flags.implementations.GreetingFlag; import com.github.intellectualsites.plotsquared.plot.flags.implementations.MusicFlag; +import com.github.intellectualsites.plotsquared.plot.flags.implementations.PlotWeatherFlag; import com.github.intellectualsites.plotsquared.plot.object.Location; import com.github.intellectualsites.plotsquared.plot.object.Plot; import com.github.intellectualsites.plotsquared.plot.object.PlotArea; @@ -145,8 +146,7 @@ public class PlotListener { } } - Optional weatherFlag = plot.getFlag(Flags.WEATHER); - weatherFlag.ifPresent(player::setWeather); + player.setWeather(plot.getFlag(PlotWeatherFlag.class)); ItemType musicFlag = plot.getFlag(MusicFlag.class); if (musicFlag != null) { @@ -279,8 +279,9 @@ public class PlotListener { player.setTime(Long.MAX_VALUE); } - if (plot.getFlag(Flags.WEATHER).isPresent()) { - player.setWeather(PlotWeather.CLEAR); + final PlotWeather plotWeather = plot.getFlag(PlotWeatherFlag.class); + if (plotWeather != PlotWeather.RESET) { + player.setWeather(plotWeather); } Location lastLoc = player.getMeta("music");