New flag system WIP

This commit is contained in:
Gabriele C 2018-11-26 20:11:05 +01:00
parent c15c99d06b
commit 4fa65012c2
29 changed files with 253 additions and 544 deletions

View File

@ -10,7 +10,7 @@ import org.bukkit.event.HandlerList;
@RequiredArgsConstructor @RequiredArgsConstructor
@Getter @Getter
public class DamageEntityEvent extends AbstractWrappedEvent { public class WrappedDamageEntityEvent extends AbstractWrappedEvent {
private static final HandlerList handlers = new HandlerList(); private static final HandlerList handlers = new HandlerList();

View File

@ -8,7 +8,7 @@ import org.bukkit.event.HandlerList;
@RequiredArgsConstructor @RequiredArgsConstructor
@Getter @Getter
public class DisallowedPVPEvent extends AbstractWrappedEvent { public class WrappedDisallowedPVPEvent extends AbstractWrappedEvent {
private static final HandlerList handlers = new HandlerList(); private static final HandlerList handlers = new HandlerList();

View File

@ -13,7 +13,7 @@ import java.util.List;
@RequiredArgsConstructor @RequiredArgsConstructor
@Getter @Getter
public class UseBlockEvent extends AbstractWrappedEvent { public class WrappedUseBlockEvent extends AbstractWrappedEvent {
private static final HandlerList handlers = new HandlerList(); private static final HandlerList handlers = new HandlerList();

View File

@ -10,7 +10,7 @@ import org.bukkit.event.HandlerList;
@RequiredArgsConstructor @RequiredArgsConstructor
@Getter @Getter
public class UseEntityEvent extends AbstractWrappedEvent { public class WrappedUseEntityEvent extends AbstractWrappedEvent {
private static final HandlerList handlers = new HandlerList(); private static final HandlerList handlers = new HandlerList();

View File

@ -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<T> {
@NonNull
private String name;
@NonNull
private Class<T> type;
private T defaultValue;
}

View File

@ -0,0 +1,11 @@
package org.codemc.worldguardwrapper.flag;
/**
* A flag that stores a boolean.
*/
public class WrappedBooleanFlag extends AbstractWrappedFlag<Boolean> {
public WrappedBooleanFlag(String name) {
super(name, boolean.class, null);
}
}

View File

@ -0,0 +1,11 @@
package org.codemc.worldguardwrapper.flag;
/**
* A flag that stores a double.
*/
public class WrappedDoubleFlag extends AbstractWrappedFlag<Double> {
public WrappedDoubleFlag(String name) {
super(name, double.class, null);
}
}

View File

@ -0,0 +1,15 @@
package org.codemc.worldguardwrapper.flag;
/**
* A flag that stores an enum value.
*/
public class WrappedEnumFlag<T extends Enum<T>> extends AbstractWrappedFlag<T> {
public WrappedEnumFlag(String name, Class<T> enumClass) {
this(name, enumClass, null);
}
public WrappedEnumFlag(String name, Class<T> enumClass, T defaultValue) {
super(name, enumClass, defaultValue);
}
}

View File

@ -0,0 +1,15 @@
package org.codemc.worldguardwrapper.flag;
/**
* A flag that stores an integer.
*/
public class WrappedIntegerFlag extends AbstractWrappedFlag<Integer> {
public WrappedIntegerFlag(String name) {
this(name, 0);
}
public WrappedIntegerFlag(String name, int defaultValue) {
super(name, int.class, defaultValue);
}
}

View File

@ -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<Location> {
public WrappedLocationFlag(String name) {
this(name, null);
}
public WrappedLocationFlag(String name, Location defaultValue) {
super(name, Location.class, defaultValue);
}
}

View File

@ -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<T> extends AbstractWrappedFlag<Set<T>> {
private AbstractWrappedFlag<T> subFlag;
public WrappedSetFlag(String name, AbstractWrappedFlag<T> subFlag) {
this(name, new HashSet<>(), subFlag);
}
@SuppressWarnings("unchecked")
public WrappedSetFlag(String name, Set<T> defaultValue, AbstractWrappedFlag<T> subFlag) {
super(name, (Class<Set<T>>) defaultValue.getClass(), defaultValue);
this.subFlag = subFlag;
}
/**
* Get the type of values stored in this flag.
*
* @return The stored flag type.
*/
public AbstractWrappedFlag<T> getSubFlag() {
return subFlag;
}
}

View File

@ -0,0 +1,15 @@
package org.codemc.worldguardwrapper.flag;
/**
* A flag that stores a string.
*/
public class WrappedStringFlag extends AbstractWrappedFlag<String> {
public WrappedStringFlag(String name) {
this(name, "");
}
public WrappedStringFlag(String name, String defaultValue) {
super(name, String.class, defaultValue);
}
}

View File

@ -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<T> {
@NonNull
private String name;
@NonNull
private Class<T> 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<T> 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);
}

View File

@ -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<Boolean> {
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;
}
}
}

View File

@ -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<Double> {
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);
}
}

View File

@ -1,47 +0,0 @@
package org.codemc.worldguardwrapper.flags;
import org.bukkit.entity.Player;
/**
* A flag that stores an enum value.
*/
public class EnumFlag<T extends Enum<T>> extends AbstractWrappedFlag<T> {
public EnumFlag(String name, Class<T> enumClass) {
this(name, enumClass, null);
}
public EnumFlag(String name, Class<T> 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<T> 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);
}
}

View File

@ -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<Integer> {
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);
}
}

View File

@ -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<Location> {
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<String, Object> 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;
}
}
}

View File

@ -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<T> extends AbstractWrappedFlag<Set<T>> {
private AbstractWrappedFlag<T> subFlag;
public SetFlag(String name, AbstractWrappedFlag<T> subFlag) {
this(name, new HashSet<>(), subFlag);
}
@SuppressWarnings("unchecked")
public SetFlag(String name, Set<T> defaultValue, AbstractWrappedFlag<T> subFlag) {
super(name, (Class<Set<T>>) defaultValue.getClass(), defaultValue);
this.subFlag = subFlag;
}
/**
* Get the type of values stored in this flag.
*
* @return The stored flag type.
*/
public AbstractWrappedFlag<T> getSubType() {
return subFlag;
}
@Override
public Set<T> deserialize(Object o) {
if (o instanceof Collection<?>) {
Collection<?> collection = (Collection<?>) o;
Set<T> items = new HashSet<T>();
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<T> o) {
List<Object> list = new ArrayList<Object>();
for (T item : o) {
list.add(subFlag.serialize(item));
}
return list;
}
@Override
public Set<T> parse(Player player, String userInput) {
if (userInput.isEmpty()) {
return new HashSet<>();
} else {
Set<T> items = new HashSet<>();
for (String str : userInput.split(",")) {
items.add(subFlag.parse(player, str));
}
return items;
}
}
}

View File

@ -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<String> {
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;
}
}

View File

@ -3,7 +3,7 @@ package org.codemc.worldguardwrapper.region;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
public interface PlayerDomain { public interface WrappedDomain {
Set<UUID> getPlayers(); Set<UUID> getPlayers();

View File

@ -1,6 +1,7 @@
package org.codemc.worldguardwrapper.region; package org.codemc.worldguardwrapper.region;
import org.bukkit.Location; import org.bukkit.Location;
import org.codemc.worldguardwrapper.flag.AbstractWrappedFlag;
import org.codemc.worldguardwrapper.selection.Selection; import org.codemc.worldguardwrapper.selection.Selection;
import java.util.Map; import java.util.Map;
@ -16,11 +17,13 @@ public interface WrappedRegion {
Map<String, Object> getFlags(); Map<String, Object> getFlags();
Map<AbstractWrappedFlag<?>, Object> getWrappedFlags();
int getPriority(); int getPriority();
PlayerDomain getOwners(); WrappedDomain getOwners();
PlayerDomain getMembers(); WrappedDomain getMembers();
boolean contains(Location location); boolean contains(Location location);

View File

@ -5,7 +5,7 @@ import org.bukkit.Location;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin; 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 org.codemc.worldguardwrapper.region.WrappedRegion;
import java.util.*; import java.util.*;
@ -26,25 +26,6 @@ public interface IWorldGuardImplementation {
*/ */
int getApiVersion(); 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<Boolean> 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. * 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 * @param flag The flag to register
* @return Whether the flag has been registered * @return Whether the flag has been registered
*/ */
<T> boolean registerFlag(@NonNull AbstractWrappedFlag<T> flag); boolean registerFlag(@NonNull AbstractWrappedFlag<?> flag);
/** /**
* Get a region by its ID. * Get a region by its ID.

View File

@ -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<W, U> {
@Getter
private Class<W> wrappedFlag;
@Getter
private Class<U> unwrappedFlag;
private Function<U, W> wrapper;
private Function<W, U> unwrapper;
public W wrap(U flag) {
return wrapper.apply(flag);
}
public U unwrap(W flag) {
return unwrapper.apply(flag);
}
}

View File

@ -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<W, U> {
private Map<Class<?>, WrapperAdapter<W, U>> wrappedToAdapter = new HashMap<>();
private Map<Class<?>, WrapperAdapter<W, U>> unwrappedToAdapter = new HashMap<>();
public void register(WrapperAdapter<W, U> adapter) {
wrappedToAdapter.put(adapter.getWrappedFlag(), adapter);
unwrappedToAdapter.put(adapter.getUnwrappedFlag(), adapter);
}
@SuppressWarnings("unchecked")
public Optional<WrapperAdapter<W, U>> fromWrapped(W wrapped) {
return Optional.ofNullable(wrappedToAdapter.get(wrapped.getClass()));
}
@SuppressWarnings("unchecked")
public Optional<WrapperAdapter<W, U>> fromUnwrapped(U unwrapped) {
return Optional.ofNullable(unwrappedToAdapter.get(unwrapped.getClass()));
}
public Optional<U> unwrap(W wrapped) {
return fromWrapped(wrapped).map(adapter -> adapter.unwrap(wrapped));
}
public Optional<W> wrap(U unwrapped) {
return fromUnwrapped(unwrapped).map(adapter -> adapter.wrap(unwrapped));
}
}

View File

@ -11,7 +11,7 @@ import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.Event.Result; import org.bukkit.event.Event.Result;
import org.codemc.worldguardwrapper.event.AbstractWrappedEvent; import org.codemc.worldguardwrapper.event.*;
@NoArgsConstructor @NoArgsConstructor
public class EventListener implements Listener { public class EventListener implements Listener {
@ -24,7 +24,7 @@ public class EventListener implements Listener {
return; return;
} }
AbstractWrappedEvent event = new org.codemc.worldguardwrapper.event.UseBlockEvent( AbstractWrappedEvent event = new WrappedUseBlockEvent(
worldGuardEvent.getOriginalEvent(), worldGuardEvent.getOriginalEvent(),
player, worldGuardEvent.getWorld(), player, worldGuardEvent.getWorld(),
worldGuardEvent.getBlocks(), worldGuardEvent.getBlocks(),
@ -46,7 +46,7 @@ public class EventListener implements Listener {
return; return;
} }
AbstractWrappedEvent event = new org.codemc.worldguardwrapper.event.UseEntityEvent( AbstractWrappedEvent event = new WrappedUseEntityEvent(
worldGuardEvent.getOriginalEvent(), worldGuardEvent.getOriginalEvent(),
player, player,
worldGuardEvent.getTarget(), worldGuardEvent.getTarget(),
@ -68,7 +68,7 @@ public class EventListener implements Listener {
return; return;
} }
AbstractWrappedEvent event = new org.codemc.worldguardwrapper.event.DamageEntityEvent( AbstractWrappedEvent event = new WrappedDamageEntityEvent(
worldGuardEvent.getOriginalEvent(), worldGuardEvent.getOriginalEvent(),
player, player,
worldGuardEvent.getTarget(), worldGuardEvent.getTarget(),
@ -84,7 +84,7 @@ public class EventListener implements Listener {
@EventHandler(priority = EventPriority.LOW) @EventHandler(priority = EventPriority.LOW)
public void onDisallowedPVP(DisallowedPVPEvent worldGuardEvent) { public void onDisallowedPVP(DisallowedPVPEvent worldGuardEvent) {
AbstractWrappedEvent event = new org.codemc.worldguardwrapper.event.DisallowedPVPEvent( AbstractWrappedEvent event = new WrappedDisallowedPVPEvent(
worldGuardEvent.getAttacker(), worldGuardEvent.getAttacker(),
worldGuardEvent.getDefender(), worldGuardEvent.getDefender(),
worldGuardEvent.getCause()); worldGuardEvent.getCause());

View File

@ -5,9 +5,8 @@ import com.sk89q.worldedit.BlockVector2D;
import com.sk89q.worldguard.LocalPlayer; import com.sk89q.worldguard.LocalPlayer;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin; import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
import com.sk89q.worldguard.protection.ApplicableRegionSet; 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.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.StateFlag;
import com.sk89q.worldguard.protection.flags.registry.FlagConflictException; import com.sk89q.worldguard.protection.flags.registry.FlagConflictException;
import com.sk89q.worldguard.protection.flags.registry.FlagRegistry; 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.ProtectedCuboidRegion;
import com.sk89q.worldguard.protection.regions.ProtectedPolygonalRegion; import com.sk89q.worldguard.protection.regions.ProtectedPolygonalRegion;
import com.sk89q.worldguard.protection.regions.ProtectedRegion; import com.sk89q.worldguard.protection.regions.ProtectedRegion;
import lombok.NoArgsConstructor;
import lombok.NonNull; import lombok.NonNull;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin; 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.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.region.WrappedRegion;
import org.codemc.worldguardwrapper.selection.CuboidSelection; import org.codemc.worldguardwrapper.selection.CuboidSelection;
import org.codemc.worldguardwrapper.selection.PolygonalSelection; import org.codemc.worldguardwrapper.selection.PolygonalSelection;
@ -32,12 +33,20 @@ import org.codemc.worldguardwrapper.selection.Selection;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@NoArgsConstructor
public class WorldGuardImplementation implements IWorldGuardImplementation { public class WorldGuardImplementation implements IWorldGuardImplementation {
private final WorldGuardPlugin plugin = WorldGuardPlugin.inst(); private final WorldGuardPlugin plugin = WorldGuardPlugin.inst();
private final FlagRegistry flagRegistry = plugin.getFlagRegistry(); private final FlagRegistry flagRegistry = plugin.getFlagRegistry();
private final WrapperAdapterRegister<AbstractWrappedFlag<?>, 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<LocalPlayer> wrapPlayer(Player player) { private Optional<LocalPlayer> wrapPlayer(Player player) {
return Optional.ofNullable(player).map(bukkitPlayer -> plugin.wrapPlayer(player)); return Optional.ofNullable(player).map(bukkitPlayer -> plugin.wrapPlayer(player));
} }
@ -130,6 +139,14 @@ public class WorldGuardImplementation implements IWorldGuardImplementation {
return map; return map;
} }
@Override
public Map<AbstractWrappedFlag<?>, Object> getWrappedFlags() {
Map<AbstractWrappedFlag<?>, Object> map = new HashMap<>();
region.getFlags().forEach((flag, value) ->
flagAdapter.wrap(flag).ifPresent(wrapped -> map.put(wrapped, value)));
return map;
}
@Override @Override
public Optional<Object> getFlag(String name) { public Optional<Object> getFlag(String name) {
return Optional.ofNullable(flagRegistry.get(name)) return Optional.ofNullable(flagRegistry.get(name))
@ -142,8 +159,8 @@ public class WorldGuardImplementation implements IWorldGuardImplementation {
} }
@Override @Override
public PlayerDomain getOwners() { public WrappedDomain getOwners() {
return new PlayerDomain() { return new WrappedDomain() {
@Override @Override
public Set<UUID> getPlayers() { public Set<UUID> getPlayers() {
return region.getOwners().getUniqueIds(); return region.getOwners().getUniqueIds();
@ -177,8 +194,8 @@ public class WorldGuardImplementation implements IWorldGuardImplementation {
} }
@Override @Override
public PlayerDomain getMembers() { public WrappedDomain getMembers() {
return new PlayerDomain() { return new WrappedDomain() {
@Override @Override
public Set<UUID> getPlayers() { public Set<UUID> getPlayers() {
return region.getMembers().getUniqueIds(); return region.getMembers().getUniqueIds();
@ -228,25 +245,6 @@ public class WorldGuardImplementation implements IWorldGuardImplementation {
return 6; return 6;
} }
@Override
public Optional<Boolean> 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 @Override
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T> Optional<T> queryFlag(Player player, Location location, String flagName, Class<T> type) { public <T> Optional<T> queryFlag(Player player, Location location, String flagName, Class<T> type) {
@ -259,34 +257,15 @@ public class WorldGuardImplementation implements IWorldGuardImplementation {
} }
@Override @Override
public <T> boolean registerFlag(AbstractWrappedFlag<T> flag) { public boolean registerFlag(AbstractWrappedFlag<?> flag) {
Flag<T> wgFlag = new Flag<T>(flag.getName()) { Optional<Flag<?>> unwrapped = flagAdapter.unwrap(flag);
@Override if (unwrapped.isPresent()) {
public T getDefault() {
return flag.getDefaultValue();
}
@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 { try {
flagRegistry.register(wgFlag); flagRegistry.register(unwrapped.get());
return true; return true;
} catch (FlagConflictException ignored) { } catch (FlagConflictException ignored) {
} }
}
return false; return false;
} }

View File

@ -11,7 +11,7 @@ import org.bukkit.event.Event.Result;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.codemc.worldguardwrapper.event.AbstractWrappedEvent; import org.codemc.worldguardwrapper.event.*;
@NoArgsConstructor @NoArgsConstructor
public class EventListener implements Listener { public class EventListener implements Listener {
@ -24,7 +24,7 @@ public class EventListener implements Listener {
return; return;
} }
AbstractWrappedEvent event = new org.codemc.worldguardwrapper.event.UseBlockEvent( AbstractWrappedEvent event = new WrappedUseBlockEvent(
worldGuardEvent.getOriginalEvent(), worldGuardEvent.getOriginalEvent(),
player, worldGuardEvent.getWorld(), player, worldGuardEvent.getWorld(),
worldGuardEvent.getBlocks(), worldGuardEvent.getBlocks(),
@ -46,7 +46,7 @@ public class EventListener implements Listener {
return; return;
} }
AbstractWrappedEvent event = new org.codemc.worldguardwrapper.event.UseEntityEvent( AbstractWrappedEvent event = new WrappedUseEntityEvent(
worldGuardEvent.getOriginalEvent(), worldGuardEvent.getOriginalEvent(),
player, player,
worldGuardEvent.getTarget(), worldGuardEvent.getTarget(),
@ -68,7 +68,7 @@ public class EventListener implements Listener {
return; return;
} }
AbstractWrappedEvent event = new org.codemc.worldguardwrapper.event.DamageEntityEvent( AbstractWrappedEvent event = new WrappedDamageEntityEvent(
worldGuardEvent.getOriginalEvent(), worldGuardEvent.getOriginalEvent(),
player, player,
worldGuardEvent.getTarget(), worldGuardEvent.getTarget(),
@ -84,7 +84,7 @@ public class EventListener implements Listener {
@EventHandler(priority = EventPriority.LOW) @EventHandler(priority = EventPriority.LOW)
public void onDisallowedPVP(DisallowedPVPEvent worldGuardEvent) { public void onDisallowedPVP(DisallowedPVPEvent worldGuardEvent) {
AbstractWrappedEvent event = new org.codemc.worldguardwrapper.event.DisallowedPVPEvent( AbstractWrappedEvent event = new WrappedDisallowedPVPEvent(
worldGuardEvent.getAttacker(), worldGuardEvent.getAttacker(),
worldGuardEvent.getDefender(), worldGuardEvent.getDefender(),
worldGuardEvent.getCause()); worldGuardEvent.getCause());

View File

@ -23,9 +23,9 @@ import org.bukkit.Location;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin; 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.implementation.IWorldGuardImplementation;
import org.codemc.worldguardwrapper.region.PlayerDomain; import org.codemc.worldguardwrapper.region.WrappedDomain;
import org.codemc.worldguardwrapper.region.WrappedRegion; import org.codemc.worldguardwrapper.region.WrappedRegion;
import org.codemc.worldguardwrapper.selection.CuboidSelection; import org.codemc.worldguardwrapper.selection.CuboidSelection;
import org.codemc.worldguardwrapper.selection.PolygonalSelection; import org.codemc.worldguardwrapper.selection.PolygonalSelection;
@ -142,8 +142,8 @@ public class WorldGuardImplementation implements IWorldGuardImplementation {
} }
@Override @Override
public PlayerDomain getOwners() { public WrappedDomain getOwners() {
return new PlayerDomain() { return new WrappedDomain() {
@Override @Override
public Set<UUID> getPlayers() { public Set<UUID> getPlayers() {
return region.getOwners().getUniqueIds(); return region.getOwners().getUniqueIds();
@ -177,8 +177,8 @@ public class WorldGuardImplementation implements IWorldGuardImplementation {
} }
@Override @Override
public PlayerDomain getMembers() { public WrappedDomain getMembers() {
return new PlayerDomain() { return new WrappedDomain() {
@Override @Override
public Set<UUID> getPlayers() { public Set<UUID> getPlayers() {
return region.getMembers().getUniqueIds(); return region.getMembers().getUniqueIds();