mirror of
https://github.com/CodeMC/WorldGuardWrapper.git
synced 2024-11-10 05:05:20 +01:00
New flag system WIP
This commit is contained in:
parent
c15c99d06b
commit
4fa65012c2
|
@ -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();
|
||||
|
|
@ -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();
|
||||
|
|
@ -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();
|
||||
|
|
@ -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();
|
||||
|
|
@ -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;
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -3,7 +3,7 @@ package org.codemc.worldguardwrapper.region;
|
|||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
public interface PlayerDomain {
|
||||
public interface WrappedDomain {
|
||||
|
||||
Set<UUID> getPlayers();
|
||||
|
|
@ -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<String, Object> getFlags();
|
||||
|
||||
Map<AbstractWrappedFlag<?>, Object> getWrappedFlags();
|
||||
|
||||
int getPriority();
|
||||
|
||||
PlayerDomain getOwners();
|
||||
WrappedDomain getOwners();
|
||||
|
||||
PlayerDomain getMembers();
|
||||
WrappedDomain getMembers();
|
||||
|
||||
boolean contains(Location location);
|
||||
|
||||
|
|
|
@ -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<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.
|
||||
*
|
||||
|
@ -74,7 +55,7 @@ public interface IWorldGuardImplementation {
|
|||
* @param flag The flag to register
|
||||
* @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.
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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));
|
||||
}
|
||||
}
|
|
@ -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());
|
||||
|
|
|
@ -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<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) {
|
||||
return Optional.ofNullable(player).map(bukkitPlayer -> plugin.wrapPlayer(player));
|
||||
}
|
||||
|
@ -130,6 +139,14 @@ public class WorldGuardImplementation implements IWorldGuardImplementation {
|
|||
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
|
||||
public Optional<Object> 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<UUID> 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<UUID> getPlayers() {
|
||||
return region.getMembers().getUniqueIds();
|
||||
|
@ -228,25 +245,6 @@ public class WorldGuardImplementation implements IWorldGuardImplementation {
|
|||
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
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> Optional<T> queryFlag(Player player, Location location, String flagName, Class<T> type) {
|
||||
|
@ -259,34 +257,15 @@ public class WorldGuardImplementation implements IWorldGuardImplementation {
|
|||
}
|
||||
|
||||
@Override
|
||||
public <T> boolean registerFlag(AbstractWrappedFlag<T> flag) {
|
||||
Flag<T> wgFlag = new Flag<T>(flag.getName()) {
|
||||
@Override
|
||||
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());
|
||||
}
|
||||
};
|
||||
|
||||
public boolean registerFlag(AbstractWrappedFlag<?> flag) {
|
||||
Optional<Flag<?>> unwrapped = flagAdapter.unwrap(flag);
|
||||
if (unwrapped.isPresent()) {
|
||||
try {
|
||||
flagRegistry.register(wgFlag);
|
||||
flagRegistry.register(unwrapped.get());
|
||||
return true;
|
||||
} catch (FlagConflictException ignored) {
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
|
@ -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());
|
||||
|
|
|
@ -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<UUID> 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<UUID> getPlayers() {
|
||||
return region.getMembers().getUniqueIds();
|
||||
|
|
Loading…
Reference in New Issue
Block a user