From 4fa65012c28c15899bbacc9c559ca595e78a1ffe Mon Sep 17 00:00:00 2001 From: Gabriele C Date: Mon, 26 Nov 2018 20:11:05 +0100 Subject: [PATCH] New flag system WIP --- ...ent.java => WrappedDamageEntityEvent.java} | 2 +- ...nt.java => WrappedDisallowedPVPEvent.java} | 2 +- ...ckEvent.java => WrappedUseBlockEvent.java} | 2 +- ...yEvent.java => WrappedUseEntityEvent.java} | 2 +- .../flag/AbstractWrappedFlag.java | 17 +++ .../flag/WrappedBooleanFlag.java | 11 ++ .../flag/WrappedDoubleFlag.java | 11 ++ .../flag/WrappedEnumFlag.java | 15 +++ .../flag/WrappedIntegerFlag.java | 15 +++ .../flag/WrappedLocationFlag.java | 17 +++ .../flag/WrappedSetFlag.java | 31 ++++++ .../flag/WrappedStringFlag.java | 15 +++ .../flags/AbstractWrappedFlag.java | 71 ------------- .../worldguardwrapper/flags/BooleanFlag.java | 43 -------- .../worldguardwrapper/flags/DoubleFlag.java | 37 ------- .../worldguardwrapper/flags/EnumFlag.java | 47 -------- .../worldguardwrapper/flags/IntegerFlag.java | 37 ------- .../worldguardwrapper/flags/LocationFlag.java | 100 ------------------ .../worldguardwrapper/flags/SetFlag.java | 77 -------------- .../worldguardwrapper/flags/StringFlag.java | 33 ------ .../{PlayerDomain.java => WrappedDomain.java} | 2 +- .../region/WrappedRegion.java | 7 +- .../IWorldGuardImplementation.java | 23 +--- .../wrapper/WrapperAdapter.java | 25 +++++ .../wrapper/WrapperAdapterRegister.java | 34 ++++++ .../implementation/v6/EventListener.java | 10 +- .../v6/WorldGuardImplementation.java | 89 ++++++---------- .../implementation/v7/EventListener.java | 10 +- .../v7/WorldGuardImplementation.java | 12 +-- 29 files changed, 253 insertions(+), 544 deletions(-) rename api/src/main/java/org/codemc/worldguardwrapper/event/{DamageEntityEvent.java => WrappedDamageEntityEvent.java} (90%) rename api/src/main/java/org/codemc/worldguardwrapper/event/{DisallowedPVPEvent.java => WrappedDisallowedPVPEvent.java} (89%) rename api/src/main/java/org/codemc/worldguardwrapper/event/{UseBlockEvent.java => WrappedUseBlockEvent.java} (92%) rename api/src/main/java/org/codemc/worldguardwrapper/event/{UseEntityEvent.java => WrappedUseEntityEvent.java} (91%) create mode 100644 api/src/main/java/org/codemc/worldguardwrapper/flag/AbstractWrappedFlag.java create mode 100644 api/src/main/java/org/codemc/worldguardwrapper/flag/WrappedBooleanFlag.java create mode 100644 api/src/main/java/org/codemc/worldguardwrapper/flag/WrappedDoubleFlag.java create mode 100644 api/src/main/java/org/codemc/worldguardwrapper/flag/WrappedEnumFlag.java create mode 100644 api/src/main/java/org/codemc/worldguardwrapper/flag/WrappedIntegerFlag.java create mode 100644 api/src/main/java/org/codemc/worldguardwrapper/flag/WrappedLocationFlag.java create mode 100644 api/src/main/java/org/codemc/worldguardwrapper/flag/WrappedSetFlag.java create mode 100644 api/src/main/java/org/codemc/worldguardwrapper/flag/WrappedStringFlag.java delete mode 100644 api/src/main/java/org/codemc/worldguardwrapper/flags/AbstractWrappedFlag.java delete mode 100644 api/src/main/java/org/codemc/worldguardwrapper/flags/BooleanFlag.java delete mode 100644 api/src/main/java/org/codemc/worldguardwrapper/flags/DoubleFlag.java delete mode 100644 api/src/main/java/org/codemc/worldguardwrapper/flags/EnumFlag.java delete mode 100644 api/src/main/java/org/codemc/worldguardwrapper/flags/IntegerFlag.java delete mode 100644 api/src/main/java/org/codemc/worldguardwrapper/flags/LocationFlag.java delete mode 100644 api/src/main/java/org/codemc/worldguardwrapper/flags/SetFlag.java delete mode 100644 api/src/main/java/org/codemc/worldguardwrapper/flags/StringFlag.java rename api/src/main/java/org/codemc/worldguardwrapper/region/{PlayerDomain.java => WrappedDomain.java} (89%) create mode 100644 implementation/interface/src/main/java/org/codemc/worldguardwrapper/implementation/wrapper/WrapperAdapter.java create mode 100644 implementation/interface/src/main/java/org/codemc/worldguardwrapper/implementation/wrapper/WrapperAdapterRegister.java diff --git a/api/src/main/java/org/codemc/worldguardwrapper/event/DamageEntityEvent.java b/api/src/main/java/org/codemc/worldguardwrapper/event/WrappedDamageEntityEvent.java similarity index 90% rename from api/src/main/java/org/codemc/worldguardwrapper/event/DamageEntityEvent.java rename to api/src/main/java/org/codemc/worldguardwrapper/event/WrappedDamageEntityEvent.java index f0c2ccf..de980e3 100644 --- a/api/src/main/java/org/codemc/worldguardwrapper/event/DamageEntityEvent.java +++ b/api/src/main/java/org/codemc/worldguardwrapper/event/WrappedDamageEntityEvent.java @@ -10,7 +10,7 @@ import org.bukkit.event.HandlerList; @RequiredArgsConstructor @Getter -public class DamageEntityEvent extends AbstractWrappedEvent { +public class WrappedDamageEntityEvent extends AbstractWrappedEvent { private static final HandlerList handlers = new HandlerList(); diff --git a/api/src/main/java/org/codemc/worldguardwrapper/event/DisallowedPVPEvent.java b/api/src/main/java/org/codemc/worldguardwrapper/event/WrappedDisallowedPVPEvent.java similarity index 89% rename from api/src/main/java/org/codemc/worldguardwrapper/event/DisallowedPVPEvent.java rename to api/src/main/java/org/codemc/worldguardwrapper/event/WrappedDisallowedPVPEvent.java index 3a150fd..8903173 100644 --- a/api/src/main/java/org/codemc/worldguardwrapper/event/DisallowedPVPEvent.java +++ b/api/src/main/java/org/codemc/worldguardwrapper/event/WrappedDisallowedPVPEvent.java @@ -8,7 +8,7 @@ import org.bukkit.event.HandlerList; @RequiredArgsConstructor @Getter -public class DisallowedPVPEvent extends AbstractWrappedEvent { +public class WrappedDisallowedPVPEvent extends AbstractWrappedEvent { private static final HandlerList handlers = new HandlerList(); diff --git a/api/src/main/java/org/codemc/worldguardwrapper/event/UseBlockEvent.java b/api/src/main/java/org/codemc/worldguardwrapper/event/WrappedUseBlockEvent.java similarity index 92% rename from api/src/main/java/org/codemc/worldguardwrapper/event/UseBlockEvent.java rename to api/src/main/java/org/codemc/worldguardwrapper/event/WrappedUseBlockEvent.java index 363ad76..9edc76d 100644 --- a/api/src/main/java/org/codemc/worldguardwrapper/event/UseBlockEvent.java +++ b/api/src/main/java/org/codemc/worldguardwrapper/event/WrappedUseBlockEvent.java @@ -13,7 +13,7 @@ import java.util.List; @RequiredArgsConstructor @Getter -public class UseBlockEvent extends AbstractWrappedEvent { +public class WrappedUseBlockEvent extends AbstractWrappedEvent { private static final HandlerList handlers = new HandlerList(); diff --git a/api/src/main/java/org/codemc/worldguardwrapper/event/UseEntityEvent.java b/api/src/main/java/org/codemc/worldguardwrapper/event/WrappedUseEntityEvent.java similarity index 91% rename from api/src/main/java/org/codemc/worldguardwrapper/event/UseEntityEvent.java rename to api/src/main/java/org/codemc/worldguardwrapper/event/WrappedUseEntityEvent.java index 7f903ea..03bb067 100644 --- a/api/src/main/java/org/codemc/worldguardwrapper/event/UseEntityEvent.java +++ b/api/src/main/java/org/codemc/worldguardwrapper/event/WrappedUseEntityEvent.java @@ -10,7 +10,7 @@ import org.bukkit.event.HandlerList; @RequiredArgsConstructor @Getter -public class UseEntityEvent extends AbstractWrappedEvent { +public class WrappedUseEntityEvent extends AbstractWrappedEvent { private static final HandlerList handlers = new HandlerList(); diff --git a/api/src/main/java/org/codemc/worldguardwrapper/flag/AbstractWrappedFlag.java b/api/src/main/java/org/codemc/worldguardwrapper/flag/AbstractWrappedFlag.java new file mode 100644 index 0000000..eb2b910 --- /dev/null +++ b/api/src/main/java/org/codemc/worldguardwrapper/flag/AbstractWrappedFlag.java @@ -0,0 +1,17 @@ +package org.codemc.worldguardwrapper.flag; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NonNull; +import lombok.RequiredArgsConstructor; + +@AllArgsConstructor +@RequiredArgsConstructor +@Getter +public abstract class AbstractWrappedFlag { + @NonNull + private String name; + @NonNull + private Class type; + private T defaultValue; +} diff --git a/api/src/main/java/org/codemc/worldguardwrapper/flag/WrappedBooleanFlag.java b/api/src/main/java/org/codemc/worldguardwrapper/flag/WrappedBooleanFlag.java new file mode 100644 index 0000000..f4bc53f --- /dev/null +++ b/api/src/main/java/org/codemc/worldguardwrapper/flag/WrappedBooleanFlag.java @@ -0,0 +1,11 @@ +package org.codemc.worldguardwrapper.flag; + +/** + * A flag that stores a boolean. + */ +public class WrappedBooleanFlag extends AbstractWrappedFlag { + + public WrappedBooleanFlag(String name) { + super(name, boolean.class, null); + } +} diff --git a/api/src/main/java/org/codemc/worldguardwrapper/flag/WrappedDoubleFlag.java b/api/src/main/java/org/codemc/worldguardwrapper/flag/WrappedDoubleFlag.java new file mode 100644 index 0000000..a307477 --- /dev/null +++ b/api/src/main/java/org/codemc/worldguardwrapper/flag/WrappedDoubleFlag.java @@ -0,0 +1,11 @@ +package org.codemc.worldguardwrapper.flag; + +/** + * A flag that stores a double. + */ +public class WrappedDoubleFlag extends AbstractWrappedFlag { + + public WrappedDoubleFlag(String name) { + super(name, double.class, null); + } +} diff --git a/api/src/main/java/org/codemc/worldguardwrapper/flag/WrappedEnumFlag.java b/api/src/main/java/org/codemc/worldguardwrapper/flag/WrappedEnumFlag.java new file mode 100644 index 0000000..2c8d99a --- /dev/null +++ b/api/src/main/java/org/codemc/worldguardwrapper/flag/WrappedEnumFlag.java @@ -0,0 +1,15 @@ +package org.codemc.worldguardwrapper.flag; + +/** + * A flag that stores an enum value. + */ +public class WrappedEnumFlag> extends AbstractWrappedFlag { + + public WrappedEnumFlag(String name, Class enumClass) { + this(name, enumClass, null); + } + + public WrappedEnumFlag(String name, Class enumClass, T defaultValue) { + super(name, enumClass, defaultValue); + } +} diff --git a/api/src/main/java/org/codemc/worldguardwrapper/flag/WrappedIntegerFlag.java b/api/src/main/java/org/codemc/worldguardwrapper/flag/WrappedIntegerFlag.java new file mode 100644 index 0000000..4851e4c --- /dev/null +++ b/api/src/main/java/org/codemc/worldguardwrapper/flag/WrappedIntegerFlag.java @@ -0,0 +1,15 @@ +package org.codemc.worldguardwrapper.flag; + +/** + * A flag that stores an integer. + */ +public class WrappedIntegerFlag extends AbstractWrappedFlag { + + public WrappedIntegerFlag(String name) { + this(name, 0); + } + + public WrappedIntegerFlag(String name, int defaultValue) { + super(name, int.class, defaultValue); + } +} diff --git a/api/src/main/java/org/codemc/worldguardwrapper/flag/WrappedLocationFlag.java b/api/src/main/java/org/codemc/worldguardwrapper/flag/WrappedLocationFlag.java new file mode 100644 index 0000000..0068503 --- /dev/null +++ b/api/src/main/java/org/codemc/worldguardwrapper/flag/WrappedLocationFlag.java @@ -0,0 +1,17 @@ +package org.codemc.worldguardwrapper.flag; + +import org.bukkit.Location; + +/** + * A flag that stores a bukkit location. + */ +public class WrappedLocationFlag extends AbstractWrappedFlag { + + public WrappedLocationFlag(String name) { + this(name, null); + } + + public WrappedLocationFlag(String name, Location defaultValue) { + super(name, Location.class, defaultValue); + } +} diff --git a/api/src/main/java/org/codemc/worldguardwrapper/flag/WrappedSetFlag.java b/api/src/main/java/org/codemc/worldguardwrapper/flag/WrappedSetFlag.java new file mode 100644 index 0000000..da6fd99 --- /dev/null +++ b/api/src/main/java/org/codemc/worldguardwrapper/flag/WrappedSetFlag.java @@ -0,0 +1,31 @@ +package org.codemc.worldguardwrapper.flag; + +import java.util.HashSet; +import java.util.Set; + +/** + * A flag that stores a set of values of the sub flag's type. + */ +public class WrappedSetFlag extends AbstractWrappedFlag> { + + private AbstractWrappedFlag subFlag; + + public WrappedSetFlag(String name, AbstractWrappedFlag subFlag) { + this(name, new HashSet<>(), subFlag); + } + + @SuppressWarnings("unchecked") + public WrappedSetFlag(String name, Set defaultValue, AbstractWrappedFlag subFlag) { + super(name, (Class>) defaultValue.getClass(), defaultValue); + this.subFlag = subFlag; + } + + /** + * Get the type of values stored in this flag. + * + * @return The stored flag type. + */ + public AbstractWrappedFlag getSubFlag() { + return subFlag; + } +} diff --git a/api/src/main/java/org/codemc/worldguardwrapper/flag/WrappedStringFlag.java b/api/src/main/java/org/codemc/worldguardwrapper/flag/WrappedStringFlag.java new file mode 100644 index 0000000..a9b296f --- /dev/null +++ b/api/src/main/java/org/codemc/worldguardwrapper/flag/WrappedStringFlag.java @@ -0,0 +1,15 @@ +package org.codemc.worldguardwrapper.flag; + +/** + * A flag that stores a string. + */ +public class WrappedStringFlag extends AbstractWrappedFlag { + + public WrappedStringFlag(String name) { + this(name, ""); + } + + public WrappedStringFlag(String name, String defaultValue) { + super(name, String.class, defaultValue); + } +} diff --git a/api/src/main/java/org/codemc/worldguardwrapper/flags/AbstractWrappedFlag.java b/api/src/main/java/org/codemc/worldguardwrapper/flags/AbstractWrappedFlag.java deleted file mode 100644 index 6a4febf..0000000 --- a/api/src/main/java/org/codemc/worldguardwrapper/flags/AbstractWrappedFlag.java +++ /dev/null @@ -1,71 +0,0 @@ -package org.codemc.worldguardwrapper.flags; - -import lombok.AllArgsConstructor; -import lombok.NonNull; -import lombok.RequiredArgsConstructor; -import org.bukkit.entity.Player; - -@AllArgsConstructor -@RequiredArgsConstructor -public abstract class AbstractWrappedFlag { - @NonNull - private String name; - @NonNull - private Class type; - private T defaultValue; - - /** - * Get the name of this flag. - * - * @return The name - */ - public String getName() { - return name; - } - - /** - * Get the type of this flag's value. - * - * @return The type - */ - public Class getType() { - return type; - } - - /** - * Get the default value of this flag. - * - * @return The default value (may be {@code null}) - */ - public T getDefaultValue() { - return defaultValue; - } - - /** - * Convert the value stored in this flag into a type that can be - * serialized into YAML. - * - * @param value The value - * @return The serialized type - */ - public abstract Object serialize(T value); - - /** - * Convert a raw object that was loaded (from a YAML file, for example) into the - * type that this flag uses. - * - * @param serialized The raw object - * @return The deserialized type - */ - public abstract T deserialize(Object serialized); - - /** - * Parse a given input to force it to a type compatible with the flag. - * - * @param player Player who entered the string. - * @param userInput Input string (e.g. a player input) - * @return A type compatible with the flag - */ - public abstract T parse(Player player, String userInput); - -} diff --git a/api/src/main/java/org/codemc/worldguardwrapper/flags/BooleanFlag.java b/api/src/main/java/org/codemc/worldguardwrapper/flags/BooleanFlag.java deleted file mode 100644 index cf96232..0000000 --- a/api/src/main/java/org/codemc/worldguardwrapper/flags/BooleanFlag.java +++ /dev/null @@ -1,43 +0,0 @@ -package org.codemc.worldguardwrapper.flags; - -import org.bukkit.entity.Player; - -/** - * A flag that stores a boolean. - */ -public class BooleanFlag extends AbstractWrappedFlag { - - public BooleanFlag(String name) { - this(name, false); - } - - public BooleanFlag(String name, boolean defaultValue) { - super(name, boolean.class, defaultValue); - } - - @Override - public Object serialize(Boolean value) { - return value; - } - - @Override - public Boolean deserialize(Object serialized) { - return (Boolean) serialized; - } - - @Override - public Boolean parse(Player player, String userInput) { - if (userInput.equalsIgnoreCase("true") || userInput.equalsIgnoreCase("yes") - || userInput.equalsIgnoreCase("on") - || userInput.equalsIgnoreCase("1")) { - return true; - } else if (userInput.equalsIgnoreCase("false") || userInput.equalsIgnoreCase("no") - || userInput.equalsIgnoreCase("off") - || userInput.equalsIgnoreCase("0")) { - return false; - } else { - return null; - } - } - -} diff --git a/api/src/main/java/org/codemc/worldguardwrapper/flags/DoubleFlag.java b/api/src/main/java/org/codemc/worldguardwrapper/flags/DoubleFlag.java deleted file mode 100644 index e2b182e..0000000 --- a/api/src/main/java/org/codemc/worldguardwrapper/flags/DoubleFlag.java +++ /dev/null @@ -1,37 +0,0 @@ -package org.codemc.worldguardwrapper.flags; - -import org.bukkit.entity.Player; - -/** - * A flag that stores a double. - */ -public class DoubleFlag extends AbstractWrappedFlag { - - public DoubleFlag(String name) { - this(name, 0d); - } - - public DoubleFlag(String name, double defaultValue) { - super(name, double.class, defaultValue); - } - - @Override - public Object serialize(Double value) { - return value; - } - - @Override - public Double deserialize(Object serialized) { - if (serialized instanceof Number) { - return ((Number) serialized).doubleValue(); - } else { - return null; - } - } - - @Override - public Double parse(Player player, String userInput) { - return Double.parseDouble(userInput); - } - -} diff --git a/api/src/main/java/org/codemc/worldguardwrapper/flags/EnumFlag.java b/api/src/main/java/org/codemc/worldguardwrapper/flags/EnumFlag.java deleted file mode 100644 index 454f49c..0000000 --- a/api/src/main/java/org/codemc/worldguardwrapper/flags/EnumFlag.java +++ /dev/null @@ -1,47 +0,0 @@ -package org.codemc.worldguardwrapper.flags; - -import org.bukkit.entity.Player; - -/** - * A flag that stores an enum value. - */ -public class EnumFlag> extends AbstractWrappedFlag { - - public EnumFlag(String name, Class enumClass) { - this(name, enumClass, null); - } - - public EnumFlag(String name, Class enumClass, T defaultValue) { - super(name, enumClass, defaultValue); - } - - /** - * Get the enum class. - * TODO: really needed? we already have getType() -Gab - * - * @return The enum class - */ - public Class getEnumClass() { - return getType(); - } - - @Override - public Object serialize(T value) { - return value.name(); - } - - @Override - public T deserialize(Object serialized) { - if (serialized instanceof String) { - return Enum.valueOf(getEnumClass(), (String) serialized); - } else { - return null; - } - } - - @Override - public T parse(Player player, String userInput) { - return Enum.valueOf(getEnumClass(), userInput); - } - -} diff --git a/api/src/main/java/org/codemc/worldguardwrapper/flags/IntegerFlag.java b/api/src/main/java/org/codemc/worldguardwrapper/flags/IntegerFlag.java deleted file mode 100644 index 23c467f..0000000 --- a/api/src/main/java/org/codemc/worldguardwrapper/flags/IntegerFlag.java +++ /dev/null @@ -1,37 +0,0 @@ -package org.codemc.worldguardwrapper.flags; - -import org.bukkit.entity.Player; - -/** - * A flag that stores an integer. - */ -public class IntegerFlag extends AbstractWrappedFlag { - - public IntegerFlag(String name) { - this(name, 0); - } - - public IntegerFlag(String name, int defaultValue) { - super(name, int.class, defaultValue); - } - - @Override - public Object serialize(Integer value) { - return value; - } - - @Override - public Integer deserialize(Object serialized) { - if (serialized instanceof Number) { - return ((Number) serialized).intValue(); - } else { - return null; - } - } - - @Override - public Integer parse(Player player, String userInput) { - return Integer.parseInt(userInput); - } - -} diff --git a/api/src/main/java/org/codemc/worldguardwrapper/flags/LocationFlag.java b/api/src/main/java/org/codemc/worldguardwrapper/flags/LocationFlag.java deleted file mode 100644 index 371a68e..0000000 --- a/api/src/main/java/org/codemc/worldguardwrapper/flags/LocationFlag.java +++ /dev/null @@ -1,100 +0,0 @@ -package org.codemc.worldguardwrapper.flags; - -import org.bukkit.Bukkit; -import org.bukkit.Location; -import org.bukkit.World; -import org.bukkit.entity.Player; - -import java.util.HashMap; -import java.util.Map; - -/** - * A flag that stores a bukkit location. - */ -public class LocationFlag extends AbstractWrappedFlag { - - public LocationFlag(String name) { - this(name, null); - } - - public LocationFlag(String name, Location defaultValue) { - super(name, Location.class, defaultValue); - } - - @Override - public Object serialize(Location value) { - Map map = new HashMap<>(); - - map.put("world", value.getWorld().getName()); - map.put("x", value.getX()); - map.put("y", value.getY()); - map.put("z", value.getZ()); - map.put("yaw", value.getYaw()); - map.put("pitch", value.getPitch()); - - return map; - } - - @Override - public Location deserialize(Object serialized) { - if (serialized instanceof Map) { - Map map = (Map) serialized; - - Object worldName = map.get("world"); - if (worldName == null) return null; - - Object x = map.get("x"); - if (x == null) return null; - - Object y = map.get("y"); - if (y == null) return null; - - Object z = map.get("z"); - if (z == null) return null; - - Object yaw = map.get("yaw"); - if (yaw == null) return null; - - Object pitch = map.get("pitch"); - if (pitch == null) return null; - - World world = Bukkit.getWorld(String.valueOf(worldName)); - if (world == null) return null; - - return new Location(world, toNumber(x), toNumber(y), toNumber(z), - (float) toNumber(yaw), (float) toNumber(pitch)); - } - return null; - } - - @Override - public Location parse(Player player, String userInput) { - if ("here".equalsIgnoreCase(userInput)) { - return player.getLocation(); - } else if ("none".equalsIgnoreCase(userInput)) { - return null; - } else { - String[] split = userInput.split(","); - if (split.length >= 3) { - final World world = player.getWorld(); - final double x = Double.parseDouble(split[0]); - final double y = Double.parseDouble(split[1]); - final double z = Double.parseDouble(split[2]); - final float yaw = split.length < 4 ? 0 : Float.parseFloat(split[3]); - final float pitch = split.length < 5 ? 0 : Float.parseFloat(split[4]); - - return new Location(world, x, y, z, yaw, pitch); - } - } - return null; - } - - private double toNumber(Object o) { - if (o instanceof Number) { - return ((Number) o).doubleValue(); - } else { - return 0; - } - } - -} diff --git a/api/src/main/java/org/codemc/worldguardwrapper/flags/SetFlag.java b/api/src/main/java/org/codemc/worldguardwrapper/flags/SetFlag.java deleted file mode 100644 index d9abae6..0000000 --- a/api/src/main/java/org/codemc/worldguardwrapper/flags/SetFlag.java +++ /dev/null @@ -1,77 +0,0 @@ -package org.codemc.worldguardwrapper.flags; - -import org.bukkit.entity.Player; - -import java.util.*; - -/** - * A flag that stores a set of values of the sub flag's type. - */ -public class SetFlag extends AbstractWrappedFlag> { - - private AbstractWrappedFlag subFlag; - - public SetFlag(String name, AbstractWrappedFlag subFlag) { - this(name, new HashSet<>(), subFlag); - } - - @SuppressWarnings("unchecked") - public SetFlag(String name, Set defaultValue, AbstractWrappedFlag subFlag) { - super(name, (Class>) defaultValue.getClass(), defaultValue); - this.subFlag = subFlag; - } - - /** - * Get the type of values stored in this flag. - * - * @return The stored flag type. - */ - public AbstractWrappedFlag getSubType() { - return subFlag; - } - - @Override - public Set deserialize(Object o) { - if (o instanceof Collection) { - Collection collection = (Collection) o; - Set items = new HashSet(); - - for (Object sub : collection) { - T item = subFlag.deserialize(sub); - if (item != null) { - items.add(item); - } - } - - return items; - } else { - return null; - } - } - - @Override - public Object serialize(Set o) { - List list = new ArrayList(); - for (T item : o) { - list.add(subFlag.serialize(item)); - } - - return list; - } - - @Override - public Set parse(Player player, String userInput) { - if (userInput.isEmpty()) { - return new HashSet<>(); - } else { - Set items = new HashSet<>(); - - for (String str : userInput.split(",")) { - items.add(subFlag.parse(player, str)); - } - - return items; - } - } - -} diff --git a/api/src/main/java/org/codemc/worldguardwrapper/flags/StringFlag.java b/api/src/main/java/org/codemc/worldguardwrapper/flags/StringFlag.java deleted file mode 100644 index 7871931..0000000 --- a/api/src/main/java/org/codemc/worldguardwrapper/flags/StringFlag.java +++ /dev/null @@ -1,33 +0,0 @@ -package org.codemc.worldguardwrapper.flags; - -import org.bukkit.entity.Player; - -/** - * A flag that stores a string. - */ -public class StringFlag extends AbstractWrappedFlag { - - public StringFlag(String name) { - this(name, ""); - } - - public StringFlag(String name, String defaultValue) { - super(name, String.class, defaultValue); - } - - @Override - public Object serialize(String value) { - return value; - } - - @Override - public String deserialize(Object serialized) { - return (String) serialized; - } - - @Override - public String parse(Player player, String userInput) { - return userInput; - } - -} diff --git a/api/src/main/java/org/codemc/worldguardwrapper/region/PlayerDomain.java b/api/src/main/java/org/codemc/worldguardwrapper/region/WrappedDomain.java similarity index 89% rename from api/src/main/java/org/codemc/worldguardwrapper/region/PlayerDomain.java rename to api/src/main/java/org/codemc/worldguardwrapper/region/WrappedDomain.java index 6707682..b878f87 100644 --- a/api/src/main/java/org/codemc/worldguardwrapper/region/PlayerDomain.java +++ b/api/src/main/java/org/codemc/worldguardwrapper/region/WrappedDomain.java @@ -3,7 +3,7 @@ package org.codemc.worldguardwrapper.region; import java.util.Set; import java.util.UUID; -public interface PlayerDomain { +public interface WrappedDomain { Set getPlayers(); diff --git a/api/src/main/java/org/codemc/worldguardwrapper/region/WrappedRegion.java b/api/src/main/java/org/codemc/worldguardwrapper/region/WrappedRegion.java index ffe96dc..041515d 100644 --- a/api/src/main/java/org/codemc/worldguardwrapper/region/WrappedRegion.java +++ b/api/src/main/java/org/codemc/worldguardwrapper/region/WrappedRegion.java @@ -1,6 +1,7 @@ package org.codemc.worldguardwrapper.region; import org.bukkit.Location; +import org.codemc.worldguardwrapper.flag.AbstractWrappedFlag; import org.codemc.worldguardwrapper.selection.Selection; import java.util.Map; @@ -16,11 +17,13 @@ public interface WrappedRegion { Map getFlags(); + Map, Object> getWrappedFlags(); + int getPriority(); - PlayerDomain getOwners(); + WrappedDomain getOwners(); - PlayerDomain getMembers(); + WrappedDomain getMembers(); boolean contains(Location location); diff --git a/implementation/interface/src/main/java/org/codemc/worldguardwrapper/implementation/IWorldGuardImplementation.java b/implementation/interface/src/main/java/org/codemc/worldguardwrapper/implementation/IWorldGuardImplementation.java index 3fbecf0..8939f4f 100644 --- a/implementation/interface/src/main/java/org/codemc/worldguardwrapper/implementation/IWorldGuardImplementation.java +++ b/implementation/interface/src/main/java/org/codemc/worldguardwrapper/implementation/IWorldGuardImplementation.java @@ -5,7 +5,7 @@ import org.bukkit.Location; import org.bukkit.World; import org.bukkit.entity.Player; import org.bukkit.plugin.java.JavaPlugin; -import org.codemc.worldguardwrapper.flags.AbstractWrappedFlag; +import org.codemc.worldguardwrapper.flag.AbstractWrappedFlag; import org.codemc.worldguardwrapper.region.WrappedRegion; import java.util.*; @@ -26,25 +26,6 @@ public interface IWorldGuardImplementation { */ int getApiVersion(); - /** - * Query a StateFlag's value for a given player at a given location. - * - * @param player The player - * @param location The location - * @param flagName The flag's name - * @return The flag's value - */ - Optional queryStateFlag(Player player, @NonNull Location location, @NonNull String flagName); - - /** - * Register a {@code StateFlag}. - * - * @param flagName The name of the flag - * @param defaultValue The flag's default value - * @return Whether the flag has been registered - */ - boolean registerStateFlag(@NonNull String flagName, @NonNull Boolean defaultValue); - /** * Query a flag's value for a given player at a given location. * @@ -74,7 +55,7 @@ public interface IWorldGuardImplementation { * @param flag The flag to register * @return Whether the flag has been registered */ - boolean registerFlag(@NonNull AbstractWrappedFlag flag); + boolean registerFlag(@NonNull AbstractWrappedFlag flag); /** * Get a region by its ID. diff --git a/implementation/interface/src/main/java/org/codemc/worldguardwrapper/implementation/wrapper/WrapperAdapter.java b/implementation/interface/src/main/java/org/codemc/worldguardwrapper/implementation/wrapper/WrapperAdapter.java new file mode 100644 index 0000000..b1af2b6 --- /dev/null +++ b/implementation/interface/src/main/java/org/codemc/worldguardwrapper/implementation/wrapper/WrapperAdapter.java @@ -0,0 +1,25 @@ +package org.codemc.worldguardwrapper.implementation.wrapper; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +import java.util.function.Function; + +@AllArgsConstructor +public class WrapperAdapter { + + @Getter + private Class wrappedFlag; + @Getter + private Class unwrappedFlag; + private Function wrapper; + private Function unwrapper; + + public W wrap(U flag) { + return wrapper.apply(flag); + } + + public U unwrap(W flag) { + return unwrapper.apply(flag); + } +} diff --git a/implementation/interface/src/main/java/org/codemc/worldguardwrapper/implementation/wrapper/WrapperAdapterRegister.java b/implementation/interface/src/main/java/org/codemc/worldguardwrapper/implementation/wrapper/WrapperAdapterRegister.java new file mode 100644 index 0000000..f722836 --- /dev/null +++ b/implementation/interface/src/main/java/org/codemc/worldguardwrapper/implementation/wrapper/WrapperAdapterRegister.java @@ -0,0 +1,34 @@ +package org.codemc.worldguardwrapper.implementation.wrapper; + +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; + +public class WrapperAdapterRegister { + + private Map, WrapperAdapter> wrappedToAdapter = new HashMap<>(); + private Map, WrapperAdapter> unwrappedToAdapter = new HashMap<>(); + + public void register(WrapperAdapter adapter) { + wrappedToAdapter.put(adapter.getWrappedFlag(), adapter); + unwrappedToAdapter.put(adapter.getUnwrappedFlag(), adapter); + } + + @SuppressWarnings("unchecked") + public Optional> fromWrapped(W wrapped) { + return Optional.ofNullable(wrappedToAdapter.get(wrapped.getClass())); + } + + @SuppressWarnings("unchecked") + public Optional> fromUnwrapped(U unwrapped) { + return Optional.ofNullable(unwrappedToAdapter.get(unwrapped.getClass())); + } + + public Optional unwrap(W wrapped) { + return fromWrapped(wrapped).map(adapter -> adapter.unwrap(wrapped)); + } + + public Optional wrap(U unwrapped) { + return fromUnwrapped(unwrapped).map(adapter -> adapter.wrap(unwrapped)); + } +} diff --git a/implementation/v6/src/main/java/org/codemc/worldguardwrapper/implementation/v6/EventListener.java b/implementation/v6/src/main/java/org/codemc/worldguardwrapper/implementation/v6/EventListener.java index 40aa4f9..9f668b9 100644 --- a/implementation/v6/src/main/java/org/codemc/worldguardwrapper/implementation/v6/EventListener.java +++ b/implementation/v6/src/main/java/org/codemc/worldguardwrapper/implementation/v6/EventListener.java @@ -11,7 +11,7 @@ import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.Event.Result; -import org.codemc.worldguardwrapper.event.AbstractWrappedEvent; +import org.codemc.worldguardwrapper.event.*; @NoArgsConstructor public class EventListener implements Listener { @@ -24,7 +24,7 @@ public class EventListener implements Listener { return; } - AbstractWrappedEvent event = new org.codemc.worldguardwrapper.event.UseBlockEvent( + AbstractWrappedEvent event = new WrappedUseBlockEvent( worldGuardEvent.getOriginalEvent(), player, worldGuardEvent.getWorld(), worldGuardEvent.getBlocks(), @@ -46,7 +46,7 @@ public class EventListener implements Listener { return; } - AbstractWrappedEvent event = new org.codemc.worldguardwrapper.event.UseEntityEvent( + AbstractWrappedEvent event = new WrappedUseEntityEvent( worldGuardEvent.getOriginalEvent(), player, worldGuardEvent.getTarget(), @@ -68,7 +68,7 @@ public class EventListener implements Listener { return; } - AbstractWrappedEvent event = new org.codemc.worldguardwrapper.event.DamageEntityEvent( + AbstractWrappedEvent event = new WrappedDamageEntityEvent( worldGuardEvent.getOriginalEvent(), player, worldGuardEvent.getTarget(), @@ -84,7 +84,7 @@ public class EventListener implements Listener { @EventHandler(priority = EventPriority.LOW) public void onDisallowedPVP(DisallowedPVPEvent worldGuardEvent) { - AbstractWrappedEvent event = new org.codemc.worldguardwrapper.event.DisallowedPVPEvent( + AbstractWrappedEvent event = new WrappedDisallowedPVPEvent( worldGuardEvent.getAttacker(), worldGuardEvent.getDefender(), worldGuardEvent.getCause()); diff --git a/implementation/v6/src/main/java/org/codemc/worldguardwrapper/implementation/v6/WorldGuardImplementation.java b/implementation/v6/src/main/java/org/codemc/worldguardwrapper/implementation/v6/WorldGuardImplementation.java index 0f66287..5472d71 100644 --- a/implementation/v6/src/main/java/org/codemc/worldguardwrapper/implementation/v6/WorldGuardImplementation.java +++ b/implementation/v6/src/main/java/org/codemc/worldguardwrapper/implementation/v6/WorldGuardImplementation.java @@ -5,9 +5,8 @@ import com.sk89q.worldedit.BlockVector2D; import com.sk89q.worldguard.LocalPlayer; import com.sk89q.worldguard.bukkit.WorldGuardPlugin; import com.sk89q.worldguard.protection.ApplicableRegionSet; +import com.sk89q.worldguard.protection.flags.BooleanFlag; import com.sk89q.worldguard.protection.flags.Flag; -import com.sk89q.worldguard.protection.flags.FlagContext; -import com.sk89q.worldguard.protection.flags.InvalidFlagFormat; import com.sk89q.worldguard.protection.flags.StateFlag; import com.sk89q.worldguard.protection.flags.registry.FlagConflictException; import com.sk89q.worldguard.protection.flags.registry.FlagRegistry; @@ -15,15 +14,17 @@ import com.sk89q.worldguard.protection.managers.RegionManager; import com.sk89q.worldguard.protection.regions.ProtectedCuboidRegion; import com.sk89q.worldguard.protection.regions.ProtectedPolygonalRegion; import com.sk89q.worldguard.protection.regions.ProtectedRegion; -import lombok.NoArgsConstructor; import lombok.NonNull; import org.bukkit.Location; import org.bukkit.World; import org.bukkit.entity.Player; import org.bukkit.plugin.java.JavaPlugin; -import org.codemc.worldguardwrapper.flags.AbstractWrappedFlag; +import org.codemc.worldguardwrapper.flag.AbstractWrappedFlag; +import org.codemc.worldguardwrapper.flag.WrappedBooleanFlag; import org.codemc.worldguardwrapper.implementation.IWorldGuardImplementation; -import org.codemc.worldguardwrapper.region.PlayerDomain; +import org.codemc.worldguardwrapper.implementation.wrapper.WrapperAdapter; +import org.codemc.worldguardwrapper.implementation.wrapper.WrapperAdapterRegister; +import org.codemc.worldguardwrapper.region.WrappedDomain; import org.codemc.worldguardwrapper.region.WrappedRegion; import org.codemc.worldguardwrapper.selection.CuboidSelection; import org.codemc.worldguardwrapper.selection.PolygonalSelection; @@ -32,12 +33,20 @@ import org.codemc.worldguardwrapper.selection.Selection; import java.util.*; import java.util.stream.Collectors; -@NoArgsConstructor public class WorldGuardImplementation implements IWorldGuardImplementation { private final WorldGuardPlugin plugin = WorldGuardPlugin.inst(); private final FlagRegistry flagRegistry = plugin.getFlagRegistry(); + private final WrapperAdapterRegister, Flag> flagAdapter = new WrapperAdapterRegister<>(); + + @SuppressWarnings("unchecked") + public WorldGuardImplementation() { + // Register the flag adapters + flagAdapter.register(new WrapperAdapter<>(WrappedBooleanFlag.class, BooleanFlag.class, + flag -> new WrappedBooleanFlag(flag.getName()), flag -> new BooleanFlag(flag.getName()))); + } + private Optional wrapPlayer(Player player) { return Optional.ofNullable(player).map(bukkitPlayer -> plugin.wrapPlayer(player)); } @@ -130,6 +139,14 @@ public class WorldGuardImplementation implements IWorldGuardImplementation { return map; } + @Override + public Map, Object> getWrappedFlags() { + Map, Object> map = new HashMap<>(); + region.getFlags().forEach((flag, value) -> + flagAdapter.wrap(flag).ifPresent(wrapped -> map.put(wrapped, value))); + return map; + } + @Override public Optional getFlag(String name) { return Optional.ofNullable(flagRegistry.get(name)) @@ -142,8 +159,8 @@ public class WorldGuardImplementation implements IWorldGuardImplementation { } @Override - public PlayerDomain getOwners() { - return new PlayerDomain() { + public WrappedDomain getOwners() { + return new WrappedDomain() { @Override public Set getPlayers() { return region.getOwners().getUniqueIds(); @@ -177,8 +194,8 @@ public class WorldGuardImplementation implements IWorldGuardImplementation { } @Override - public PlayerDomain getMembers() { - return new PlayerDomain() { + public WrappedDomain getMembers() { + return new WrappedDomain() { @Override public Set getPlayers() { return region.getMembers().getUniqueIds(); @@ -228,25 +245,6 @@ public class WorldGuardImplementation implements IWorldGuardImplementation { return 6; } - @Override - public Optional queryStateFlag(Player player, @NonNull Location location, @NonNull String flagId) { - Flag flag = flagRegistry.get(flagId); - if (!(flag instanceof StateFlag)) { - return Optional.empty(); - } - return queryState(player, location, (StateFlag) flag).map(state -> state == StateFlag.State.ALLOW); - } - - @Override - public boolean registerStateFlag(@NonNull String flagId, @NonNull Boolean defaultValue) { - try { - flagRegistry.register(new StateFlag(flagId, defaultValue)); - return true; - } catch (FlagConflictException ignored) { - } - return false; - } - @Override @SuppressWarnings("unchecked") public Optional queryFlag(Player player, Location location, String flagName, Class type) { @@ -259,33 +257,14 @@ public class WorldGuardImplementation implements IWorldGuardImplementation { } @Override - public boolean registerFlag(AbstractWrappedFlag flag) { - Flag wgFlag = new Flag(flag.getName()) { - @Override - public T getDefault() { - return flag.getDefaultValue(); + public boolean registerFlag(AbstractWrappedFlag flag) { + Optional> unwrapped = flagAdapter.unwrap(flag); + if (unwrapped.isPresent()) { + try { + flagRegistry.register(unwrapped.get()); + return true; + } catch (FlagConflictException ignored) { } - - @Override - public Object marshal(T o) { - return flag.serialize(o); - } - - @Override - public T unmarshal(Object o) { - return flag.deserialize(o); - } - - @Override - public T parseInput(FlagContext context) throws InvalidFlagFormat { - return flag.parse(context.getPlayerSender(), context.getUserInput()); - } - }; - - try { - flagRegistry.register(wgFlag); - return true; - } catch (FlagConflictException ignored) { } return false; } diff --git a/implementation/v7/src/main/java/org/codemc/worldguardwrapper/implementation/v7/EventListener.java b/implementation/v7/src/main/java/org/codemc/worldguardwrapper/implementation/v7/EventListener.java index 9da9cba..6fa0b45 100644 --- a/implementation/v7/src/main/java/org/codemc/worldguardwrapper/implementation/v7/EventListener.java +++ b/implementation/v7/src/main/java/org/codemc/worldguardwrapper/implementation/v7/EventListener.java @@ -11,7 +11,7 @@ import org.bukkit.event.Event.Result; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; -import org.codemc.worldguardwrapper.event.AbstractWrappedEvent; +import org.codemc.worldguardwrapper.event.*; @NoArgsConstructor public class EventListener implements Listener { @@ -24,7 +24,7 @@ public class EventListener implements Listener { return; } - AbstractWrappedEvent event = new org.codemc.worldguardwrapper.event.UseBlockEvent( + AbstractWrappedEvent event = new WrappedUseBlockEvent( worldGuardEvent.getOriginalEvent(), player, worldGuardEvent.getWorld(), worldGuardEvent.getBlocks(), @@ -46,7 +46,7 @@ public class EventListener implements Listener { return; } - AbstractWrappedEvent event = new org.codemc.worldguardwrapper.event.UseEntityEvent( + AbstractWrappedEvent event = new WrappedUseEntityEvent( worldGuardEvent.getOriginalEvent(), player, worldGuardEvent.getTarget(), @@ -68,7 +68,7 @@ public class EventListener implements Listener { return; } - AbstractWrappedEvent event = new org.codemc.worldguardwrapper.event.DamageEntityEvent( + AbstractWrappedEvent event = new WrappedDamageEntityEvent( worldGuardEvent.getOriginalEvent(), player, worldGuardEvent.getTarget(), @@ -84,7 +84,7 @@ public class EventListener implements Listener { @EventHandler(priority = EventPriority.LOW) public void onDisallowedPVP(DisallowedPVPEvent worldGuardEvent) { - AbstractWrappedEvent event = new org.codemc.worldguardwrapper.event.DisallowedPVPEvent( + AbstractWrappedEvent event = new WrappedDisallowedPVPEvent( worldGuardEvent.getAttacker(), worldGuardEvent.getDefender(), worldGuardEvent.getCause()); diff --git a/implementation/v7/src/main/java/org/codemc/worldguardwrapper/implementation/v7/WorldGuardImplementation.java b/implementation/v7/src/main/java/org/codemc/worldguardwrapper/implementation/v7/WorldGuardImplementation.java index 98a26a7..228e891 100644 --- a/implementation/v7/src/main/java/org/codemc/worldguardwrapper/implementation/v7/WorldGuardImplementation.java +++ b/implementation/v7/src/main/java/org/codemc/worldguardwrapper/implementation/v7/WorldGuardImplementation.java @@ -23,9 +23,9 @@ import org.bukkit.Location; import org.bukkit.World; import org.bukkit.entity.Player; import org.bukkit.plugin.java.JavaPlugin; -import org.codemc.worldguardwrapper.flags.AbstractWrappedFlag; +import org.codemc.worldguardwrapper.flag.AbstractWrappedFlag; import org.codemc.worldguardwrapper.implementation.IWorldGuardImplementation; -import org.codemc.worldguardwrapper.region.PlayerDomain; +import org.codemc.worldguardwrapper.region.WrappedDomain; import org.codemc.worldguardwrapper.region.WrappedRegion; import org.codemc.worldguardwrapper.selection.CuboidSelection; import org.codemc.worldguardwrapper.selection.PolygonalSelection; @@ -142,8 +142,8 @@ public class WorldGuardImplementation implements IWorldGuardImplementation { } @Override - public PlayerDomain getOwners() { - return new PlayerDomain() { + public WrappedDomain getOwners() { + return new WrappedDomain() { @Override public Set getPlayers() { return region.getOwners().getUniqueIds(); @@ -177,8 +177,8 @@ public class WorldGuardImplementation implements IWorldGuardImplementation { } @Override - public PlayerDomain getMembers() { - return new PlayerDomain() { + public WrappedDomain getMembers() { + return new WrappedDomain() { @Override public Set getPlayers() { return region.getMembers().getUniqueIds();