From e4915407e7bfcd2aa91cec5d34cf120a5e4d40e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20S=C3=B6derberg?= Date: Tue, 18 Feb 2020 14:52:44 +0100 Subject: [PATCH] Finalize PlotListener --- .../plotsquared/plot/config/Captions.java | 2 +- .../plot/flags/GlobalFlagContainer.java | 2 +- .../flags/implementations/TitlesFlag.java | 68 +++++- .../plot/listener/PlotListener.java | 225 ++++++++---------- 4 files changed, 167 insertions(+), 130 deletions(-) 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 37b21a4bc..2d06f5724 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 @@ -624,7 +624,7 @@ public enum Captions implements Caption { 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_TITLES("Set to `false` to disable plot titles. Can be set to: 'none' (to inherit world settings), 'true', or 'false'", "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"), FLAG_DESCRIPTION_VEHICLE_CAP("Set to an integer value to limit the amount of vehicles 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 35f7d538a..9427df11c 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 @@ -136,7 +136,6 @@ public final class GlobalFlagContainer extends FlagContainer { this.addFlag(SoilDryFlag.SOIL_DRY_FALSE); this.addFlag(TamedAttackFlag.TAMED_ATTACK_FALSE); this.addFlag(TamedInteractFlag.TAMED_INTERACT_FALSE); - this.addFlag(TitlesFlag.TITLES_TRUE); this.addFlag(VehicleBreakFlag.VEHICLE_BREAK_FALSE); this.addFlag(VehicleUseFlag.VEHICLE_USE_FALSE); this.addFlag(VillagerInteractFlag.VILLAGER_INTERACT_FALSE); @@ -148,6 +147,7 @@ public final class GlobalFlagContainer extends FlagContainer { // Enum Flags this.addFlag(PlotWeatherFlag.PLOT_WEATHER_FLAG_OFF); this.addFlag(DenyTeleportFlag.DENY_TELEPORT_FLAG_NONE); + this.addFlag(TitlesFlag.TITLES_NONE); // Integer flags this.addFlag(AnimalCapFlag.ANIMAL_CAP_UNLIMITED); diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/implementations/TitlesFlag.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/implementations/TitlesFlag.java index db71a2cc8..0f52cf5de 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/implementations/TitlesFlag.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/implementations/TitlesFlag.java @@ -1,20 +1,72 @@ 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 com.github.intellectualsites.plotsquared.plot.flags.FlagParseException; +import com.github.intellectualsites.plotsquared.plot.flags.PlotFlag; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; -public class TitlesFlag extends BooleanFlag { +import java.util.Arrays; +import java.util.Locale; - public static final TitlesFlag TITLES_TRUE = new TitlesFlag(true); - public static final TitlesFlag TITLES_FALSE = new TitlesFlag(false); +public class TitlesFlag extends PlotFlag { - private TitlesFlag(boolean value) { - super(value, Captions.FLAG_DESCRIPTION_TITLES); + public static final TitlesFlag TITLES_NONE = new TitlesFlag(TitlesFlagValue.NONE); + public static final TitlesFlag TITLES_TRUE = new TitlesFlag(TitlesFlagValue.TRUE); + public static final TitlesFlag TITLES_FALSE = new TitlesFlag(TitlesFlagValue.FALSE); + + private TitlesFlag(final TitlesFlagValue value) { + super(value, Captions.FLAG_CATEGORY_ENUM, Captions.FLAG_DESCRIPTION_TITLES); } - @Override protected TitlesFlag flagOf(@NotNull Boolean value) { - return value ? TITLES_TRUE : TITLES_FALSE; + @Override public TitlesFlag parse(@NotNull final String input) throws FlagParseException { + final TitlesFlagValue titlesFlagValue = TitlesFlagValue.fromString(input); + if (titlesFlagValue == null) { + throw new FlagParseException(this, input, Captions.FLAG_ERROR_ENUM, + Arrays.asList("none", "true", "false")); + } + return flagOf(titlesFlagValue); + } + + @Override public TitlesFlag merge(@NotNull TitlesFlagValue newValue) { + if (newValue == TitlesFlagValue.TRUE || newValue == TitlesFlagValue.FALSE) { + return flagOf(newValue); + } + return this; + } + + @Override public String toString() { + return getValue().name().toLowerCase(Locale.ENGLISH); + } + + @Override public String getExample() { + return "true"; + } + + @Override protected TitlesFlag flagOf(@NotNull TitlesFlagValue value) { + if (value == TitlesFlagValue.TRUE) { + return TITLES_TRUE; + } else if (value == TitlesFlagValue.FALSE) { + return TITLES_FALSE; + } + return TITLES_NONE; + } + + public enum TitlesFlagValue { + NONE, + TRUE, + FALSE; + + @Nullable public static TitlesFlagValue fromString(final String value) { + if (value.equalsIgnoreCase("true")) { + return TRUE; + } else if (value.equalsIgnoreCase("false")) { + return FALSE; + } else if (value.equalsIgnoreCase("none")) { + return NONE; + } + return null; + } } } 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 12ae55ece..e524f8388 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 @@ -2,9 +2,6 @@ package com.github.intellectualsites.plotsquared.plot.listener; import com.github.intellectualsites.plotsquared.plot.config.Captions; import com.github.intellectualsites.plotsquared.plot.config.Settings; -import com.github.intellectualsites.plotsquared.plot.flag.Flag; -import com.github.intellectualsites.plotsquared.plot.flag.FlagManager; -import com.github.intellectualsites.plotsquared.plot.flags.PlotFlag; 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; @@ -37,7 +34,6 @@ import com.sk89q.worldedit.world.gamemode.GameModes; import com.sk89q.worldedit.world.item.ItemType; import com.sk89q.worldedit.world.item.ItemTypes; -import java.util.Collection; import java.util.HashMap; import java.util.Map; import java.util.UUID; @@ -59,128 +55,117 @@ public class PlotListener { player.setMeta(PlotPlayer.META_LAST_PLOT, plot); EventUtil.manager.callEntry(player, plot); if (plot.hasOwner()) { - - final Collection> plotFlags = plot.getApplicableFlags(false); - - boolean titles; - if (!plot.getArea().DEFAULT_FLAGS.isEmpty()) { - Boolean value = (Boolean) plot.getArea().DEFAULT_FLAGS.get(Flags.TITLES); - titles = value != null ? value : Settings.TITLES; - } else { + // This will inherit values from PlotArea + final TitlesFlag.TitlesFlagValue titleFlag = plot.getFlag(TitlesFlag.class); + final boolean titles; + if (titleFlag == TitlesFlag.TitlesFlagValue.NONE) { titles = Settings.TITLES; + } else { + titles = titleFlag == TitlesFlag.TitlesFlagValue.TRUE; } - String greeting; - if (flags.isEmpty()) { - if (titles) { - greeting = ""; - } else { - return true; + + final String greeting = plot.getFlag(GreetingFlag.class); + if (!greeting.isEmpty()) { + MainUtil + .format(Captions.PREFIX_GREETING.getTranslated() + greeting, plot, player, + false, new RunnableVal() { + @Override public void run(String value) { + MainUtil.sendMessage(player, value); + } + }); + } + + if (plot.getFlag(NotifyEnterFlag.class)) { + if (!Permissions.hasPermission(player, "plots.flag.notify-enter.bypass")) { + for (UUID uuid : plot.getOwners()) { + PlotPlayer owner = UUIDHandler.getPlayer(uuid); + if (owner != null && !owner.getUUID().equals(player.getUUID())) { + MainUtil.sendMessage(owner, Captions.NOTIFY_ENTER.getTranslated() + .replace("%player", player.getName()) + .replace("%plot", plot.getId().toString())); + } + } + } + } + + if (plot.getFlag(FlightFlag.class)) { + boolean flight = player.getFlight(); + GameMode gamemode = player.getGameMode(); + if (flight != (gamemode == GameModes.CREATIVE + || gamemode == GameModes.SPECTATOR)) { + player.setPersistentMeta("flight", + ByteArrayUtilities.booleanToBytes(player.getFlight())); + } + player.setFlight(true); + } + + 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(gameMode); + } else { + MainUtil.sendMessage(player, StringMan + .replaceAll(Captions.GAMEMODE_WAS_BYPASSED.getTranslated(), + "{plot}", plot.getId(), "{gamemode}", gameMode)); + } + } + } + + 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(guestGameMode); + } else { + MainUtil.sendMessage(player, StringMan + .replaceAll(Captions.GAMEMODE_WAS_BYPASSED.getTranslated(), + "{plot}", plot.getId(), "{gamemode}", guestGameMode)); + } + } + } + + long time = plot.getFlag(TimeFlag.class); + if (time != TimeFlag.TIME_DISABLED.getValue() && !player + .getAttribute("disabletime")) { + try { + player.setTime(time); + } catch (Exception ignored) { + plot.removeFlag(TimeFlag.class); + } + } + + player.setWeather(plot.getFlag(PlotWeatherFlag.class)); + + ItemType musicFlag = plot.getFlag(MusicFlag.class); + if (musicFlag != null) { + final String rawId = musicFlag.getId(); + if (rawId.contains("disc") || musicFlag == ItemTypes.AIR) { + Location location = player.getLocation(); + Location lastLocation = player.getMeta("music"); + if (lastLocation != null) { + player.playMusic(lastLocation, musicFlag); + if (musicFlag == ItemTypes.AIR) { + player.deleteMeta("music"); + } + } + if (musicFlag != ItemTypes.AIR) { + try { + player.setMeta("music", location); + player.playMusic(location, musicFlag); + } catch (Exception ignored) { + } + } } } else { - titles = titles && plot.getFlag(TitlesFlag.class); - - greeting = plot.getFlag(GreetingFlag.class); - if (!greeting.isEmpty()) { - MainUtil - .format(Captions.PREFIX_GREETING.getTranslated() + greeting, plot, player, - false, new RunnableVal() { - @Override public void run(String value) { - MainUtil.sendMessage(player, value); - } - }); + Location lastLoc = player.getMeta("music"); + if (lastLoc != null) { + player.deleteMeta("music"); + player.playMusic(lastLoc, ItemTypes.AIR); } - - if (plot.getFlag(NotifyEnterFlag.class)) { - if (!Permissions.hasPermission(player, "plots.flag.notify-enter.bypass")) { - for (UUID uuid : plot.getOwners()) { - PlotPlayer owner = UUIDHandler.getPlayer(uuid); - if (owner != null && !owner.getUUID().equals(player.getUUID())) { - MainUtil.sendMessage(owner, Captions.NOTIFY_ENTER.getTranslated() - .replace("%player", player.getName()) - .replace("%plot", plot.getId().toString())); - } - } - } - } - - if (plot.getFlag(FlightFlag.class)) { - boolean flight = player.getFlight(); - GameMode gamemode = player.getGameMode(); - if (flight != (gamemode == GameModes.CREATIVE - || gamemode == GameModes.SPECTATOR)) { - player.setPersistentMeta("flight", - ByteArrayUtilities.booleanToBytes(player.getFlight())); - } - player.setFlight(true); - } - - 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(gameMode); - } else { - MainUtil.sendMessage(player, StringMan - .replaceAll(Captions.GAMEMODE_WAS_BYPASSED.getTranslated(), - "{plot}", plot.getId(), "{gamemode}", gameMode)); - } - } - } - - 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(guestGameMode); - } else { - MainUtil.sendMessage(player, StringMan - .replaceAll(Captions.GAMEMODE_WAS_BYPASSED.getTranslated(), - "{plot}", plot.getId(), "{gamemode}", guestGameMode)); - } - } - } - - long time = plot.getFlag(TimeFlag.class); - if (time != TimeFlag.TIME_DISABLED.getValue() && !player - .getAttribute("disabletime")) { - try { - player.setTime(time); - } catch (Exception ignored) { - plot.removeFlag(TimeFlag.class); - } - } - - player.setWeather(plot.getFlag(PlotWeatherFlag.class)); - - ItemType musicFlag = plot.getFlag(MusicFlag.class); - if (musicFlag != null) { - final String rawId = musicFlag.getId(); - if (rawId.contains("disc") || musicFlag == ItemTypes.AIR) { - Location location = player.getLocation(); - Location lastLocation = player.getMeta("music"); - if (lastLocation != null) { - player.playMusic(lastLocation, musicFlag); - if (musicFlag == ItemTypes.AIR) { - player.deleteMeta("music"); - } - } - if (musicFlag != ItemTypes.AIR) { - try { - player.setMeta("music", location); - player.playMusic(location, musicFlag); - } catch (Exception ignored) { - } - } - } - } else { - Location lastLoc = player.getMeta("music"); - if (lastLoc != null) { - player.deleteMeta("music"); - player.playMusic(lastLoc, ItemTypes.AIR); - } - } - CommentManager.sendTitle(player, plot); } + CommentManager.sendTitle(player, plot); + if (titles && !player.getAttribute("disabletitles")) { if (!Captions.TITLE_ENTERED_PLOT.getTranslated().isEmpty() || !Captions.TITLE_ENTERED_PLOT_SUB.getTranslated().isEmpty()) {