diff --git a/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/listeners/PlotPlusListener.java b/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/listeners/PlotPlusListener.java index 52d4b4539..05cb01894 100644 --- a/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/listeners/PlotPlusListener.java +++ b/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/listeners/PlotPlusListener.java @@ -3,12 +3,13 @@ package com.github.intellectualsites.plotsquared.bukkit.listeners; import com.github.intellectualsites.plotsquared.bukkit.events.PlayerEnterPlotEvent; import com.github.intellectualsites.plotsquared.bukkit.events.PlayerLeavePlotEvent; import com.github.intellectualsites.plotsquared.bukkit.util.BukkitUtil; -import com.github.intellectualsites.plotsquared.plot.flag.Flags; -import com.github.intellectualsites.plotsquared.plot.flag.IntervalFlag; import com.github.intellectualsites.plotsquared.plot.flags.implementations.DropProtectionFlag; +import com.github.intellectualsites.plotsquared.plot.flags.implementations.FeedFlag; +import com.github.intellectualsites.plotsquared.plot.flags.implementations.HealFlag; import com.github.intellectualsites.plotsquared.plot.flags.implementations.InstabreakFlag; import com.github.intellectualsites.plotsquared.plot.flags.implementations.InvincibleFlag; import com.github.intellectualsites.plotsquared.plot.flags.implementations.ItemDropFlag; +import com.github.intellectualsites.plotsquared.plot.flags.types.TimedFlag; import com.github.intellectualsites.plotsquared.plot.listener.PlotListener; import com.github.intellectualsites.plotsquared.plot.object.Plot; import com.github.intellectualsites.plotsquared.plot.object.PlotPlayer; @@ -32,7 +33,6 @@ import org.bukkit.plugin.java.JavaPlugin; import java.util.HashMap; import java.util.Iterator; import java.util.Map.Entry; -import java.util.Optional; import java.util.UUID; @SuppressWarnings("unused") public class PlotPlusListener extends PlotListener implements Listener { @@ -135,12 +135,14 @@ import java.util.UUID; @EventHandler public void onPlotEnter(PlayerEnterPlotEvent event) { Player player = event.getPlayer(); Plot plot = event.getPlot(); - Optional feed = plot.getFlag(Flags.FEED); - feed.ifPresent(value -> feedRunnable - .put(player.getUniqueId(), new Interval(value.getVal1(), value.getVal2(), 20))); - Optional heal = plot.getFlag(Flags.HEAL); - heal.ifPresent(value -> healRunnable - .put(player.getUniqueId(), new Interval(value.getVal1(), value.getVal2(), 20))); + TimedFlag.Timed feed = plot.getFlag(FeedFlag.class); + if (feed.getInterval() != 0 && feed.getValue() != 0) { + feedRunnable.put(player.getUniqueId(), new Interval(feed.getInterval(), feed.getValue(), 20)); + } + TimedFlag.Timed heal = plot.getFlag(HealFlag.class); + if (heal.getInterval() != 0 && heal.getValue() != 0) { + healRunnable.put(player.getUniqueId(), new Interval(heal.getInterval(), heal.getValue(), 20)); + } } @EventHandler public void onPlayerQuit(PlayerQuitEvent event) { 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 4f4596a78..36d8593e7 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 @@ -444,6 +444,9 @@ public enum Captions implements Caption { NOT_VALID_NUMBER("$2That's not a valid number within the range: %s", "Invalid"), NOT_VALID_PLOT_ID("$2That's not a valid plot id.", "Invalid"), FOUND_NO_PLOTS("$2Found no plots with your search query", "Invalid"), + NUMBER_NOT_IN_RANGE("That's not a valid number within the range: (%s, %s)", "Invalid"), + NUMBER_NOT_POSITIVE("That's not a positive number: %s", "Invalid"), + NOT_A_NUMBER("%s is not a valid number.", "Invalid"), // // NEED_BLOCK("$2You've got to specify a block", "Need"), @@ -577,10 +580,12 @@ public enum Captions implements Caption { FLAG_DESCRIPTION_DEVICE_INTERACT("Set to `true` to allow devices to be interacted with in the plot.", "Flags"), FLAG_DESCRIPTION_DISABLE_PHYSICS("Set to `true` to disable block physics in the plot.", "Flags"), FLAG_DESCRIPTION_DROP_PROTECTION("Set to `true` to prevent dropped items from being picked up by non-members of the plot.", "Flags"), + FLAG_DESCRIPTION_FEED("Specify an interval in seconds and an optional amount by which the players will be fed (amount is 1 by default).", "Flags"), FLAG_DESCRIPTION_FORCEFIELD("Set to `true` to enable member forcefield in the plot.", "Flags"), FLAG_DESCRIPTION_GRASS_GROW("Set to `false` to disable grass to grow within the plot.", "Flags"), FLAG_DESCRIPTION_HANGING_BREAK("Set to `true` to allow guests to break hanging objects in the plot.", "Flags"), FLAG_DESCRIPTION_HANGING_PLACE("Set to `true` to allow guests to hang objects in the plot.", "Flags"), + FLAG_DESCRIPTION_HEAL("Specify an interval in seconds and an optional amount by which the players will be healed (amount is 1 by default).", "Flags"), FLAG_DESCRIPTION_HIDE_INFO("Set to `true` to hide plot information.", "Flags"), FLAG_DESCRIPTION_HOSTILE_ATTACK("Set to `true` to enable players to attack hostile mobs in the plot.", "Flags"), FLAG_DESCRIPTION_HOSTILE_CAP("Set to an integer value to limit the amount of hostile entities on the plot.", "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 d613ff856..1c81cdbb4 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 @@ -15,12 +15,14 @@ import com.github.intellectualsites.plotsquared.plot.flags.implementations.DropP import com.github.intellectualsites.plotsquared.plot.flags.implementations.EntityCapFlag; 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.ForcefieldFlag; import com.github.intellectualsites.plotsquared.plot.flags.implementations.GrassGrowFlag; import com.github.intellectualsites.plotsquared.plot.flags.implementations.GreetingFlag; import com.github.intellectualsites.plotsquared.plot.flags.implementations.HangingBreakFlag; import com.github.intellectualsites.plotsquared.plot.flags.implementations.HangingPlaceFlag; +import com.github.intellectualsites.plotsquared.plot.flags.implementations.HealFlag; import com.github.intellectualsites.plotsquared.plot.flags.implementations.HideInfoFlag; import com.github.intellectualsites.plotsquared.plot.flags.implementations.HostileAttackFlag; import com.github.intellectualsites.plotsquared.plot.flags.implementations.HostileCapFlag; @@ -134,7 +136,7 @@ public final class GlobalFlagContainer extends FlagContainer { this.addFlag(InstabreakFlag.INSTABREAK_FALSE); this.addFlag(InvincibleFlag.INVINCIBLE_FALSE); - //Integer flags + // Integer flags this.addFlag(AnimalCapFlag.ANIMAL_CAP_UNLIMITED); this.addFlag(EntityCapFlag.ENTITY_CAP_UNLIMITED); this.addFlag(HostileCapFlag.HOSTILE_CAP_UNLIMITED); @@ -142,6 +144,10 @@ public final class GlobalFlagContainer extends FlagContainer { this.addFlag(MobCapFlag.MOB_CAP_UNLIMITED); this.addFlag(VehicleCapFlag.VEHICLE_CAP_UNLIMITED); + // Timed flags + this.addFlag(FeedFlag.FEED_NOTHING); + this.addFlag(HealFlag.HEAL_NOTHING); + // Internal flags this.addFlag(new AnalysisFlag(Collections.emptyList())); this.addFlag(new DoneFlag("")); diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/implementations/FeedFlag.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/implementations/FeedFlag.java new file mode 100644 index 000000000..00fb6e7a9 --- /dev/null +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/implementations/FeedFlag.java @@ -0,0 +1,39 @@ +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.types.TimedFlag; +import org.jetbrains.annotations.NotNull; + +public class FeedFlag extends TimedFlag { + public static final FeedFlag FEED_NOTHING = new FeedFlag(new Timed<>(0, 0)); + + public FeedFlag(@NotNull Timed value) { + super(value, 1, Captions.FLAG_DESCRIPTION_FEED); + } + + @Override protected Integer parseValue(String input) throws FlagParseException { + int parsed; + try { + parsed = Integer.parseInt(input); + } catch (Throwable throwable) { + throw new FlagParseException(this, input, Captions.NOT_A_NUMBER, input); + } + if (parsed < 1) { + throw new FlagParseException(this, input, Captions.NUMBER_NOT_POSITIVE, parsed); + } + return parsed; + } + + @Override protected Integer mergeValue(Integer other) { + return this.getValue().getValue() + other; + } + + @Override public String getExample() { + return "10 5"; + } + + @Override protected FeedFlag flagOf(@NotNull Timed value) { + return new FeedFlag(value); + } +} diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/implementations/HealFlag.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/implementations/HealFlag.java new file mode 100644 index 000000000..7f0d14b20 --- /dev/null +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/implementations/HealFlag.java @@ -0,0 +1,38 @@ +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.types.TimedFlag; +import org.jetbrains.annotations.NotNull; + +public class HealFlag extends TimedFlag { + public static final HealFlag HEAL_NOTHING = new HealFlag(new Timed<>(0, 0)); + protected HealFlag(@NotNull Timed value) { + super(value, 1, Captions.FLAG_DESCRIPTION_HEAL); + } + + @Override protected Integer parseValue(String input) throws FlagParseException { + int parsed; + try { + parsed = Integer.parseInt(input); + } catch (Throwable throwable) { + throw new FlagParseException(this, input, Captions.NOT_A_NUMBER, input); + } + if (parsed < 1) { + throw new FlagParseException(this, input, Captions.NUMBER_NOT_POSITIVE, parsed); + } + return parsed; + } + + @Override protected Integer mergeValue(Integer other) { + return this.getValue().getValue() + other; + } + + @Override public String getExample() { + return "20 2"; + } + + @Override protected HealFlag flagOf(@NotNull Timed value) { + return new HealFlag(value); + } +} diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/types/NumberFlag.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/types/NumberFlag.java index fd79cf773..2bc2543f7 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/types/NumberFlag.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/types/NumberFlag.java @@ -24,7 +24,7 @@ public abstract class NumberFlag, F extends Plo final N parsed = parseNumber(input); if (parsed.compareTo(minimum) < 0 || parsed.compareTo(maximum) > 0) { throw new FlagParseException(this, input, - Captions.NOT_VALID_NUMBER); // TODO format Caption, provide valid range + Captions.NUMBER_NOT_IN_RANGE, minimum, maximum); } return flagOf(parsed); diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/types/TimedFlag.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/types/TimedFlag.java new file mode 100644 index 000000000..5d6ed2e45 --- /dev/null +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/types/TimedFlag.java @@ -0,0 +1,68 @@ +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 com.github.intellectualsites.plotsquared.plot.flags.PlotFlag; +import org.jetbrains.annotations.NotNull; + +public abstract class TimedFlag, F>> extends PlotFlag, F> { + private final T defaultValue; + + protected TimedFlag(@NotNull Timed value, T defaultValue, @NotNull Caption flagDescription) { + super(value, Captions.FLAG_CATEGORY_INTERVALS, flagDescription); + this.defaultValue = defaultValue; + } + + @Override public F parse(@NotNull String input) throws FlagParseException { + String[] split = input.split(" ", 2); + int interval; + try { + interval = Integer.parseInt(split[0]); + } catch (Throwable throwable) { + throw new FlagParseException(this, input, Captions.NOT_A_NUMBER, split[0]); + } + if (interval < 1) { + throw new FlagParseException(this, input, Captions.NUMBER_NOT_POSITIVE, split[0]); + } + if (split.length == 1) { + return flagOf(new Timed<>(interval, defaultValue)); + } + final T parsedValue = parseValue(split[1]); + return flagOf(new Timed<>(interval, parsedValue)); + } + + @Override public F merge(@NotNull Timed newValue) { + return flagOf(new Timed<>(getValue().interval + newValue.interval, mergeValue(newValue.value))); + } + + protected abstract T parseValue(String input) throws FlagParseException; + + protected abstract T mergeValue(T other); + + @Override public String toString() { + return getValue().toString(); + } + + public static final class Timed { + private final int interval; + private final T value; + + public Timed(int interval, T value) { + this.interval = interval; + this.value = value; + } + + public int getInterval() { + return interval; + } + + public T getValue() { + return value; + } + + @Override public String toString() { + return String.format("%d %s", interval, value); + } + } +}