From baeb9aa2af52937c4f2dac42c55d1a052ec5d3db Mon Sep 17 00:00:00 2001 From: Hannes Greule Date: Tue, 18 Feb 2020 00:46:21 +0100 Subject: [PATCH] Port TimeFlag --- .../plotsquared/plot/config/Captions.java | 1 + .../plotsquared/plot/flag/Flags.java | 1 - .../plot/flags/GlobalFlagContainer.java | 2 + .../plot/flags/implementations/TimeFlag.java | 17 +++++++++ .../plot/flags/types/LongFlag.java | 37 +++++++++++++++++++ .../plot/listener/PlotListener.java | 10 ++--- 6 files changed, 62 insertions(+), 6 deletions(-) create mode 100644 Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/implementations/TimeFlag.java create mode 100644 Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/types/LongFlag.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 cebdfa470..c25eab306 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 @@ -621,6 +621,7 @@ public enum Captions implements Caption { FLAG_DESCRIPTION_SOIL_DRY("Set to `true` to allow soil to dry within the plot.", "Flags"), FLAG_DESCRIPTION_TAMED_ATTACK("Set to `true` to allow guests to attack tamed animals in the plot.", "Flags"), FLAG_DESCRIPTION_TAMED_INTERACT("Set to `true` to allow guests to interact with tamed animals in the plot.", "Flags"), + FLAG_DESCRIPTION_TIME("Set the time in the plot to a fixed value.", "Flags"), FLAG_DESCRIPTION_TITLES("Set to `false` to disable plot titles.", "Flags"), FLAG_DESCRIPTION_USE("Define a list of materials players should be able to interact with in the plot.", "Flags"), FLAG_DESCRIPTION_VEHICLE_BREAK("Set to `true` to allow guests to break vehicles in the plot.", "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 4684c1776..379e984e9 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 @@ -6,7 +6,6 @@ import com.github.intellectualsites.plotsquared.plot.util.MathMan; public final class Flags { - public static final LongFlag TIME = new LongFlag("time"); public static final Flag KEEP = new Flag(Captions.FLAG_CATEGORY_MIXED, "keep") { @Override public String valueToString(Object value) { return value.toString(); 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 031207757..a709f357a 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 @@ -62,6 +62,7 @@ import com.github.intellectualsites.plotsquared.plot.flags.implementations.SnowM import com.github.intellectualsites.plotsquared.plot.flags.implementations.SoilDryFlag; 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; import com.github.intellectualsites.plotsquared.plot.flags.implementations.TitlesFlag; import com.github.intellectualsites.plotsquared.plot.flags.implementations.UntrustedVisitFlag; import com.github.intellectualsites.plotsquared.plot.flags.implementations.UseFlag; @@ -153,6 +154,7 @@ public final class GlobalFlagContainer extends FlagContainer { this.addFlag(HostileCapFlag.HOSTILE_CAP_UNLIMITED); this.addFlag(MiscCapFlag.MISC_CAP_UNLIMITED); this.addFlag(MobCapFlag.MOB_CAP_UNLIMITED); + this.addFlag(TimeFlag.TIME_DISABLED); this.addFlag(VehicleCapFlag.VEHICLE_CAP_UNLIMITED); // Timed flags diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/implementations/TimeFlag.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/implementations/TimeFlag.java new file mode 100644 index 000000000..bf6d1068d --- /dev/null +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/implementations/TimeFlag.java @@ -0,0 +1,17 @@ +package com.github.intellectualsites.plotsquared.plot.flags.implementations; + +import com.github.intellectualsites.plotsquared.plot.config.Captions; +import com.github.intellectualsites.plotsquared.plot.flags.types.LongFlag; +import org.jetbrains.annotations.NotNull; + +public class TimeFlag extends LongFlag { + public static final TimeFlag TIME_DISABLED = new TimeFlag(Long.MIN_VALUE); + + protected TimeFlag(@NotNull Long value) { + super(value, Captions.FLAG_DESCRIPTION_TIME); + } + + @Override protected TimeFlag flagOf(@NotNull Long value) { + return new TimeFlag(value); + } +} diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/types/LongFlag.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/types/LongFlag.java new file mode 100644 index 000000000..b83cdad08 --- /dev/null +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/types/LongFlag.java @@ -0,0 +1,37 @@ +package com.github.intellectualsites.plotsquared.plot.flags.types; + +import com.github.intellectualsites.plotsquared.plot.config.Caption; +import com.github.intellectualsites.plotsquared.plot.config.Captions; +import com.github.intellectualsites.plotsquared.plot.flags.FlagParseException; +import org.jetbrains.annotations.NotNull; + +public abstract class LongFlag> extends NumberFlag { + + protected LongFlag(@NotNull Long value, Long minimum, Long maximum, @NotNull Caption flagDescription) { + super(value, minimum, maximum, Captions.FLAG_CATEGORY_INTEGERS, flagDescription); + } + + protected LongFlag(@NotNull Long value,@NotNull Caption flagDescription) { + this(value, Long.MIN_VALUE, Long.MAX_VALUE, flagDescription); + } + + @Override public F merge(@NotNull Long newValue) { + return flagOf(getValue() + newValue); + } + + @Override public String toString() { + return getValue().toString(); + } + + @Override public String getExample() { + return "123456789"; + } + + @NotNull @Override protected Long parseNumber(String input) throws FlagParseException { + try { + return Long.parseLong(input); + } catch (Throwable throwable) { + throw new FlagParseException(this, input, Captions.NOT_A_NUMBER, input); + } + } +} 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 995970a07..144984128 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 @@ -15,6 +15,7 @@ import com.github.intellectualsites.plotsquared.plot.flags.implementations.Music import com.github.intellectualsites.plotsquared.plot.flags.implementations.NotifyEnterFlag; import com.github.intellectualsites.plotsquared.plot.flags.implementations.NotifyLeaveFlag; import com.github.intellectualsites.plotsquared.plot.flags.implementations.PlotWeatherFlag; +import com.github.intellectualsites.plotsquared.plot.flags.implementations.TimeFlag; import com.github.intellectualsites.plotsquared.plot.flags.implementations.TitlesFlag; import com.github.intellectualsites.plotsquared.plot.object.Location; import com.github.intellectualsites.plotsquared.plot.object.Plot; @@ -142,13 +143,12 @@ public class PlotListener { } } - Optional timeFlag = plot.getFlag(Flags.TIME); - if (timeFlag.isPresent() && !player.getAttribute("disabletime")) { + long time = plot.getFlag(TimeFlag.class); + if (time != TimeFlag.TIME_DISABLED.getValue() && !player.getAttribute("disabletime")) { try { - long time = timeFlag.get(); player.setTime(time); } catch (Exception ignored) { - FlagManager.removePlotFlag(plot, Flags.TIME); + plot.removeFlag(TimeFlag.class); } } @@ -280,7 +280,7 @@ public class PlotListener { } } - if (plot.getFlag(Flags.TIME).isPresent()) { + if (plot.getFlag(TimeFlag.class) != TimeFlag.TIME_DISABLED.getValue().longValue()) { player.setTime(Long.MAX_VALUE); }