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 80df30ca2..4d11d2c22 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 @@ -627,6 +627,8 @@ public enum Captions implements Caption { 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_DESCRIPTION_GAMEMODE("Determines the gamemode in the plot.", "Flags"), + FLAG_DESCRIPTION_GUEST_GAMEMODE("Determines the guest gamemode in 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 c72b497ef..d0bef763d 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,8 +6,6 @@ import com.github.intellectualsites.plotsquared.plot.util.MathMan; public final class Flags { - public static final GameModeFlag GAMEMODE = new GameModeFlag("gamemode"); - public static final GameModeFlag GUEST_GAMEMODE = new GameModeFlag("guest-gamemode"); public static final LongFlag TIME = new LongFlag("time"); public static final StringListFlag BLOCKED_CMDS = new StringListFlag("blocked-cmds"); public static final BlockStateListFlag USE = new BlockStateListFlag("use"); 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 60e104355..ed1123c7a 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 @@ -19,8 +19,10 @@ import com.github.intellectualsites.plotsquared.plot.flags.implementations.Farew 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.GamemodeFlag; 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.GuestGamemodeFlag; 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; @@ -156,6 +158,10 @@ public final class GlobalFlagContainer extends FlagContainer { // Double flags this.addFlag(PriceFlag.PRICE_NOT_BUYABLE); + // Misc + this.addFlag(GamemodeFlag.GAMEMODE_FLAG_DEFAULT); + this.addFlag(GuestGamemodeFlag.GUEST_GAMEMODE_FLAG_DEFAULT); + // 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/GamemodeFlag.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/implementations/GamemodeFlag.java new file mode 100644 index 000000000..880d69ab0 --- /dev/null +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/implementations/GamemodeFlag.java @@ -0,0 +1,82 @@ +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.sk89q.worldedit.world.gamemode.GameMode; +import com.sk89q.worldedit.world.gamemode.GameModes; +import org.jetbrains.annotations.NotNull; + +public class GamemodeFlag extends PlotFlag { + + public static final GameMode DEFAULT = new GameMode("default"); + static { + GameModes.register(DEFAULT); + } + + public static final GamemodeFlag GAMEMODE_FLAG_CREATIVE = new GamemodeFlag(GameModes.CREATIVE); + public static final GamemodeFlag GAMEMODE_FLAG_ADVENTURE = new GamemodeFlag(GameModes.ADVENTURE); + public static final GamemodeFlag GAMEMODE_FLAG_SPECTATOR = new GamemodeFlag(GameModes.SPECTATOR); + public static final GamemodeFlag GAMEMODE_FLAG_SURVIVAL = new GamemodeFlag(GameModes.SURVIVAL); + public static final GamemodeFlag GAMEMODE_FLAG_DEFAULT = new GamemodeFlag(DEFAULT); + + /** + * Construct a new flag instance. + * + * @param value Flag value + */ + protected GamemodeFlag(@NotNull GameMode value) { + super(value, Captions.FLAG_CATEGORY_GAMEMODE, Captions.FLAG_DESCRIPTION_GAMEMODE); + } + + @Override public GamemodeFlag parse(@NotNull String input) throws FlagParseException { + switch (input) { + case "creative": + case "c": + case "1": + return flagOf(GameModes.CREATIVE); + case "adventure": + case "a": + case "2": + return flagOf(GameModes.ADVENTURE); + case "spectator": + case "sp": + case "3": + return flagOf(GameModes.SPECTATOR); + case "survival": + case "s": + case "0": + return flagOf(GameModes.SURVIVAL); + default: + return flagOf(DEFAULT); + } + } + + @Override public GamemodeFlag merge(@NotNull GameMode newValue) { + return flagOf(newValue); + } + + @Override public String toString() { + return getValue().getId(); + } + + @Override public String getExample() { + return "survival"; + } + + @Override protected GamemodeFlag flagOf(@NotNull GameMode value) { + switch (value.getId()) { + case "creative": + return GAMEMODE_FLAG_CREATIVE; + case "adventure": + return GAMEMODE_FLAG_ADVENTURE; + case "spectator": + return GAMEMODE_FLAG_SPECTATOR; + case "survival": + return GAMEMODE_FLAG_SURVIVAL; + default: + return GAMEMODE_FLAG_DEFAULT; + } + } + +} diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/implementations/GuestGamemodeFlag.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/implementations/GuestGamemodeFlag.java new file mode 100644 index 000000000..b299f05d1 --- /dev/null +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/implementations/GuestGamemodeFlag.java @@ -0,0 +1,77 @@ +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.sk89q.worldedit.world.gamemode.GameMode; +import com.sk89q.worldedit.world.gamemode.GameModes; +import org.jetbrains.annotations.NotNull; + +public class GuestGamemodeFlag extends PlotFlag { + + public static final GuestGamemodeFlag GUEST_GAMEMODE_FLAG_CREATIVE = new GuestGamemodeFlag(GameModes.CREATIVE); + public static final GuestGamemodeFlag GUEST_GAMEMODE_FLAG_ADVENTURE = new GuestGamemodeFlag(GameModes.ADVENTURE); + public static final GuestGamemodeFlag GUEST_GAMEMODE_FLAG_SPECTATOR = new GuestGamemodeFlag(GameModes.SPECTATOR); + public static final GuestGamemodeFlag GUEST_GAMEMODE_FLAG_SURVIVAL = new GuestGamemodeFlag(GameModes.SURVIVAL); + public static final GuestGamemodeFlag GUEST_GAMEMODE_FLAG_DEFAULT = new GuestGamemodeFlag(GamemodeFlag.DEFAULT); + + /** + * Construct a new flag instance. + * + * @param value Flag value + */ + protected GuestGamemodeFlag(@NotNull GameMode value) { + super(value, Captions.FLAG_CATEGORY_GAMEMODE, Captions.FLAG_DESCRIPTION_GUEST_GAMEMODE); + } + + @Override public GuestGamemodeFlag parse(@NotNull String input) throws FlagParseException { + switch (input) { + case "creative": + case "c": + case "1": + return flagOf(GameModes.CREATIVE); + case "adventure": + case "a": + case "2": + return flagOf(GameModes.ADVENTURE); + case "spectator": + case "sp": + case "3": + return flagOf(GameModes.SPECTATOR); + case "survival": + case "s": + case "0": + return flagOf(GameModes.SURVIVAL); + default: + return flagOf(GamemodeFlag.DEFAULT); + } + } + + @Override public GuestGamemodeFlag merge(@NotNull GameMode newValue) { + return flagOf(newValue); + } + + @Override public String toString() { + return getValue().getId(); + } + + @Override public String getExample() { + return "survival"; + } + + @Override protected GuestGamemodeFlag flagOf(@NotNull GameMode value) { + switch (value.getId()) { + case "creative": + return GUEST_GAMEMODE_FLAG_CREATIVE; + case "adventure": + return GUEST_GAMEMODE_FLAG_ADVENTURE; + case "spectator": + return GUEST_GAMEMODE_FLAG_SPECTATOR; + case "survival": + return GUEST_GAMEMODE_FLAG_SURVIVAL; + default: + return GUEST_GAMEMODE_FLAG_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 977aac951..995970a07 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 @@ -8,7 +8,9 @@ import com.github.intellectualsites.plotsquared.plot.flag.Flags; import com.github.intellectualsites.plotsquared.plot.flags.implementations.DenyExitFlag; import com.github.intellectualsites.plotsquared.plot.flags.implementations.FarewellFlag; import com.github.intellectualsites.plotsquared.plot.flags.implementations.FlightFlag; +import com.github.intellectualsites.plotsquared.plot.flags.implementations.GamemodeFlag; import com.github.intellectualsites.plotsquared.plot.flags.implementations.GreetingFlag; +import com.github.intellectualsites.plotsquared.plot.flags.implementations.GuestGamemodeFlag; import com.github.intellectualsites.plotsquared.plot.flags.implementations.MusicFlag; import com.github.intellectualsites.plotsquared.plot.flags.implementations.NotifyEnterFlag; import com.github.intellectualsites.plotsquared.plot.flags.implementations.NotifyLeaveFlag; @@ -29,7 +31,6 @@ import com.github.intellectualsites.plotsquared.plot.util.StringMan; import com.github.intellectualsites.plotsquared.plot.util.TaskManager; import com.github.intellectualsites.plotsquared.plot.util.UUIDHandler; import com.github.intellectualsites.plotsquared.plot.util.expiry.ExpireManager; -import com.github.intellectualsites.plotsquared.plot.util.world.ItemUtil; import com.sk89q.worldedit.world.gamemode.GameMode; import com.sk89q.worldedit.world.gamemode.GameModes; import com.sk89q.worldedit.world.item.ItemType; @@ -112,33 +113,35 @@ public class PlotListener { player.setFlight(true); } - Optional gamemodeFlag = plot.getFlag(Flags.GAMEMODE); - if (gamemodeFlag.isPresent()) { - if (player.getGameMode() != gamemodeFlag.get()) { + final GameMode gameMode = plot.getFlag(GamemodeFlag.class); + if (!gameMode.equals(GamemodeFlag.DEFAULT)) { + if (player.getGameMode() != gameMode) { if (!Permissions.hasPermission(player, "plots.gamemode.bypass")) { - player.setGameMode(gamemodeFlag.get()); + player.setGameMode(gameMode); } else { MainUtil.sendMessage(player, StringMan .replaceAll(Captions.GAMEMODE_WAS_BYPASSED.getTranslated(), "{plot}", - plot.getId(), "{gamemode}", gamemodeFlag.get())); + plot.getId(), "{gamemode}", gameMode)); } } } - Optional guestGamemodeFlag = plot.getFlag(Flags.GUEST_GAMEMODE); - if (guestGamemodeFlag.isPresent()) { - if (player.getGameMode() != guestGamemodeFlag.get() && !plot + + final GameMode guestGameMode = plot.getFlag(GuestGamemodeFlag.class); + if (!guestGameMode.equals(GamemodeFlag.DEFAULT)) { + if (player.getGameMode() != guestGameMode && !plot .isAdded(player.getUUID())) { if (!Permissions.hasPermission(player, "plots.gamemode.bypass")) { - player.setGameMode(guestGamemodeFlag.get()); + player.setGameMode(guestGameMode); } else { MainUtil.sendMessage(player, StringMan .replaceAll(Captions.GAMEMODE_WAS_BYPASSED.getTranslated(), "{plot}", - plot.getId(), "{gamemode}", guestGamemodeFlag.get())); + plot.getId(), "{gamemode}", guestGameMode)); } } } + Optional timeFlag = plot.getFlag(Flags.TIME); if (timeFlag.isPresent() && !player.getAttribute("disabletime")) { try { @@ -225,8 +228,8 @@ public class PlotListener { } return false; } - if (plot.getFlag(Flags.GAMEMODE).isPresent() || plot.getFlag(Flags.GUEST_GAMEMODE) - .isPresent()) { + if (!plot.getFlag(GamemodeFlag.class).equals(GamemodeFlag.DEFAULT) || + !plot.getFlag(GuestGamemodeFlag.class).equals(GamemodeFlag.DEFAULT)) { if (player.getGameMode() != pw.GAMEMODE) { if (!Permissions.hasPermission(player, "plots.gamemode.bypass")) { player.setGameMode(pw.GAMEMODE);