Finalize PlotListener

This commit is contained in:
Alexander Söderberg 2020-02-18 14:52:44 +01:00
parent 2fe00ef284
commit e4915407e7
4 changed files with 167 additions and 130 deletions

View File

@ -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_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_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_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_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_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"), FLAG_DESCRIPTION_VEHICLE_CAP("Set to an integer value to limit the amount of vehicles on the plot.", "Flags"),

View File

@ -136,7 +136,6 @@ public final class GlobalFlagContainer extends FlagContainer {
this.addFlag(SoilDryFlag.SOIL_DRY_FALSE); this.addFlag(SoilDryFlag.SOIL_DRY_FALSE);
this.addFlag(TamedAttackFlag.TAMED_ATTACK_FALSE); this.addFlag(TamedAttackFlag.TAMED_ATTACK_FALSE);
this.addFlag(TamedInteractFlag.TAMED_INTERACT_FALSE); this.addFlag(TamedInteractFlag.TAMED_INTERACT_FALSE);
this.addFlag(TitlesFlag.TITLES_TRUE);
this.addFlag(VehicleBreakFlag.VEHICLE_BREAK_FALSE); this.addFlag(VehicleBreakFlag.VEHICLE_BREAK_FALSE);
this.addFlag(VehicleUseFlag.VEHICLE_USE_FALSE); this.addFlag(VehicleUseFlag.VEHICLE_USE_FALSE);
this.addFlag(VillagerInteractFlag.VILLAGER_INTERACT_FALSE); this.addFlag(VillagerInteractFlag.VILLAGER_INTERACT_FALSE);
@ -148,6 +147,7 @@ public final class GlobalFlagContainer extends FlagContainer {
// Enum Flags // Enum Flags
this.addFlag(PlotWeatherFlag.PLOT_WEATHER_FLAG_OFF); this.addFlag(PlotWeatherFlag.PLOT_WEATHER_FLAG_OFF);
this.addFlag(DenyTeleportFlag.DENY_TELEPORT_FLAG_NONE); this.addFlag(DenyTeleportFlag.DENY_TELEPORT_FLAG_NONE);
this.addFlag(TitlesFlag.TITLES_NONE);
// Integer flags // Integer flags
this.addFlag(AnimalCapFlag.ANIMAL_CAP_UNLIMITED); this.addFlag(AnimalCapFlag.ANIMAL_CAP_UNLIMITED);

View File

@ -1,20 +1,72 @@
package com.github.intellectualsites.plotsquared.plot.flags.implementations; package com.github.intellectualsites.plotsquared.plot.flags.implementations;
import com.github.intellectualsites.plotsquared.plot.config.Captions; 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.NotNull;
import org.jetbrains.annotations.Nullable;
public class TitlesFlag extends BooleanFlag<TitlesFlag> { import java.util.Arrays;
import java.util.Locale;
public static final TitlesFlag TITLES_TRUE = new TitlesFlag(true); public class TitlesFlag extends PlotFlag<TitlesFlag.TitlesFlagValue, TitlesFlag> {
public static final TitlesFlag TITLES_FALSE = new TitlesFlag(false);
private TitlesFlag(boolean value) { public static final TitlesFlag TITLES_NONE = new TitlesFlag(TitlesFlagValue.NONE);
super(value, Captions.FLAG_DESCRIPTION_TITLES); 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) { @Override public TitlesFlag parse(@NotNull final String input) throws FlagParseException {
return value ? TITLES_TRUE : TITLES_FALSE; 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;
}
} }
} }

View File

@ -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.Captions;
import com.github.intellectualsites.plotsquared.plot.config.Settings; 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.DenyExitFlag;
import com.github.intellectualsites.plotsquared.plot.flags.implementations.FarewellFlag; 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.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.ItemType;
import com.sk89q.worldedit.world.item.ItemTypes; import com.sk89q.worldedit.world.item.ItemTypes;
import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
@ -59,128 +55,117 @@ public class PlotListener {
player.setMeta(PlotPlayer.META_LAST_PLOT, plot); player.setMeta(PlotPlayer.META_LAST_PLOT, plot);
EventUtil.manager.callEntry(player, plot); EventUtil.manager.callEntry(player, plot);
if (plot.hasOwner()) { if (plot.hasOwner()) {
// This will inherit values from PlotArea
final Collection<PlotFlag<?, ?>> plotFlags = plot.getApplicableFlags(false); final TitlesFlag.TitlesFlagValue titleFlag = plot.getFlag(TitlesFlag.class);
final boolean titles;
boolean titles; if (titleFlag == TitlesFlag.TitlesFlagValue.NONE) {
if (!plot.getArea().DEFAULT_FLAGS.isEmpty()) {
Boolean value = (Boolean) plot.getArea().DEFAULT_FLAGS.get(Flags.TITLES);
titles = value != null ? value : Settings.TITLES;
} else {
titles = Settings.TITLES; titles = Settings.TITLES;
} else {
titles = titleFlag == TitlesFlag.TitlesFlagValue.TRUE;
} }
String greeting;
if (flags.isEmpty()) { final String greeting = plot.getFlag(GreetingFlag.class);
if (titles) { if (!greeting.isEmpty()) {
greeting = ""; MainUtil
} else { .format(Captions.PREFIX_GREETING.getTranslated() + greeting, plot, player,
return true; false, new RunnableVal<String>() {
@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 { } else {
titles = titles && plot.getFlag(TitlesFlag.class); Location lastLoc = player.getMeta("music");
if (lastLoc != null) {
greeting = plot.getFlag(GreetingFlag.class); player.deleteMeta("music");
if (!greeting.isEmpty()) { player.playMusic(lastLoc, ItemTypes.AIR);
MainUtil
.format(Captions.PREFIX_GREETING.getTranslated() + greeting, plot, player,
false, new RunnableVal<String>() {
@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 {
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 (titles && !player.getAttribute("disabletitles")) {
if (!Captions.TITLE_ENTERED_PLOT.getTranslated().isEmpty() if (!Captions.TITLE_ENTERED_PLOT.getTranslated().isEmpty()
|| !Captions.TITLE_ENTERED_PLOT_SUB.getTranslated().isEmpty()) { || !Captions.TITLE_ENTERED_PLOT_SUB.getTranslated().isEmpty()) {