New flag system, bump version

This commit is contained in:
Gabriele C 2018-11-27 08:49:46 +01:00
parent 4fa65012c2
commit b54027de6d
36 changed files with 677 additions and 861 deletions

View File

@ -7,7 +7,7 @@
<parent> <parent>
<groupId>org.codemc.worldguardwrapper</groupId> <groupId>org.codemc.worldguardwrapper</groupId>
<artifactId>worldguardwrapper-parent</artifactId> <artifactId>worldguardwrapper-parent</artifactId>
<version>1.0.4-SNAPSHOT</version> <version>1.1.0-SNAPSHOT</version>
</parent> </parent>
<artifactId>worldguardwrapper-api</artifactId> <artifactId>worldguardwrapper-api</artifactId>

View File

@ -1,17 +0,0 @@
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;
import java.util.Optional;
public interface IWrappedFlag<T> {
String getName();
Optional<T> getDefaultValue();
}

View File

@ -1,11 +0,0 @@
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

@ -1,11 +0,0 @@
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

@ -1,15 +0,0 @@
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

@ -1,15 +0,0 @@
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

@ -1,17 +0,0 @@
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

@ -1,31 +0,0 @@
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,9 @@
package org.codemc.worldguardwrapper.flag;
public enum WrappedState {
ALLOW,
DENY;
WrappedState() {
}
}

View File

@ -1,15 +0,0 @@
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

@ -5,8 +5,8 @@ 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.flag.AbstractWrappedFlag; import org.codemc.worldguardwrapper.flag.IWrappedFlag;
import org.codemc.worldguardwrapper.region.WrappedRegion; import org.codemc.worldguardwrapper.region.IWrappedRegion;
import java.util.*; import java.util.*;
@ -26,17 +26,6 @@ public interface IWorldGuardImplementation {
*/ */
int getApiVersion(); int getApiVersion();
/**
* Query a flag's value for a given player at a given location.
*
* @param player The player
* @param location The location
* @param flagName The name of the flag
* @param type The type of the flag's value
* @return The flag's value
*/
<T> Optional<T> queryFlag(Player player, @NonNull Location location, @NonNull String flagName, Class<T> type);
/** /**
* 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.
* *
@ -45,17 +34,36 @@ public interface IWorldGuardImplementation {
* @param flag The flag * @param flag The flag
* @return The flag's value * @return The flag's value
*/ */
default <T> Optional<T> queryFlag(Player player, @NonNull Location location, @NonNull AbstractWrappedFlag<T> flag) { <T> Optional<T> queryFlag(Player player, @NonNull Location location, @NonNull IWrappedFlag<T> flag);
return queryFlag(player, location, flag.getName(), flag.getType());
}
/** /**
* Register a flag to WorldGuard's flag registry. * Returns the flag with the given name.
* *
* @param flag The flag to register * @param name The flag name
* @return Whether the flag has been registered * @return The flag, empty if it doesn't exists
*/ */
boolean registerFlag(@NonNull AbstractWrappedFlag<?> flag); Optional<IWrappedFlag<?>> getFlag(@NonNull String name);
/**
* Registers a flag to WorldGuard's flag registry.
*
* @param name The flag name
* @param type The flag type
* @param defaultValue the flag default value (if supported by the type), can be null
* @return The created flag, empty if a name conflict occurred
*/
<T> Optional<IWrappedFlag<T>> registerFlag(@NonNull String name, @NonNull Class<T> type, T defaultValue);
/**
* Registers a flag to WorldGuard's flag registry.
*
* @param name The flag name
* @param type The flag type
* @return The created flag, empty if a name conflict occurred
*/
default <T> Optional<IWrappedFlag<T>> registerFlag(@NonNull String name, @NonNull Class<T> type) {
return registerFlag(name, type, null);
}
/** /**
* Get a region by its ID. * Get a region by its ID.
@ -64,7 +72,7 @@ public interface IWorldGuardImplementation {
* @param id ID of the region * @param id ID of the region
* @return The region * @return The region
*/ */
Optional<WrappedRegion> getRegion(@NonNull World world, @NonNull String id); Optional<IWrappedRegion> getRegion(@NonNull World world, @NonNull String id);
/** /**
* Get an unmodifiable map of regions containing the state of the * Get an unmodifiable map of regions containing the state of the
@ -76,7 +84,7 @@ public interface IWorldGuardImplementation {
* @param world The world * @param world The world
* @return A map of regions * @return A map of regions
*/ */
Map<String, WrappedRegion> getRegions(@NonNull World world); Map<String, IWrappedRegion> getRegions(@NonNull World world);
/** /**
* Get a set of regions at the given location. * Get a set of regions at the given location.
@ -84,7 +92,7 @@ public interface IWorldGuardImplementation {
* @param location The location * @param location The location
* @return A set of regions * @return A set of regions
*/ */
Set<WrappedRegion> getRegions(@NonNull Location location); Set<IWrappedRegion> getRegions(@NonNull Location location);
/** /**
@ -94,7 +102,7 @@ public interface IWorldGuardImplementation {
* @param maximum The maximum location of the area * @param maximum The maximum location of the area
* @return A set of regions * @return A set of regions
*/ */
Set<WrappedRegion> getRegions(@NonNull Location minimum, @NonNull Location maximum); Set<IWrappedRegion> getRegions(@NonNull Location minimum, @NonNull Location maximum);
/** /**
* Add a region. If only two points are given, a cuboid region will be created. * Add a region. If only two points are given, a cuboid region will be created.
@ -105,7 +113,7 @@ public interface IWorldGuardImplementation {
* @param maxY The maximum y coordinate * @param maxY The maximum y coordinate
* @return The added region * @return The added region
*/ */
Optional<WrappedRegion> addRegion(@NonNull String id, @NonNull List<Location> points, int minY, int maxY); Optional<IWrappedRegion> addRegion(@NonNull String id, @NonNull List<Location> points, int minY, int maxY);
/** /**
* Add a cuboid region. * Add a cuboid region.
@ -115,7 +123,7 @@ public interface IWorldGuardImplementation {
* @param point2 The second point of the region * @param point2 The second point of the region
* @return The added region * @return The added region
*/ */
default Optional<WrappedRegion> addCuboidRegion(@NonNull String id, @NonNull Location point1, @NonNull Location point2) { default Optional<IWrappedRegion> addCuboidRegion(@NonNull String id, @NonNull Location point1, @NonNull Location point2) {
return addRegion(id, Arrays.asList(point1, point2), 0, 0); return addRegion(id, Arrays.asList(point1, point2), 0, 0);
} }
@ -126,6 +134,6 @@ public interface IWorldGuardImplementation {
* @param id The region ID * @param id The region ID
* @return A list of removed regions where the first entry is the region specified by {@code id} * @return A list of removed regions where the first entry is the region specified by {@code id}
*/ */
Optional<Set<WrappedRegion>> removeRegion(@NonNull World world, @NonNull String id); Optional<Set<IWrappedRegion>> removeRegion(@NonNull World world, @NonNull String id);
} }

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 WrappedDomain { public interface IWrappedDomain {
Set<UUID> getPlayers(); Set<UUID> getPlayers();

View File

@ -0,0 +1,30 @@
package org.codemc.worldguardwrapper.region;
import org.bukkit.Location;
import org.codemc.worldguardwrapper.flag.IWrappedFlag;
import org.codemc.worldguardwrapper.selection.ISelection;
import java.util.Map;
import java.util.Optional;
public interface IWrappedRegion {
ISelection getSelection();
String getId();
<T> Optional<T> getFlag(IWrappedFlag<T> flag);
<T> void setFlag(IWrappedFlag<T> flag, T value);
Map<IWrappedFlag<?>, Object> getFlags();
int getPriority();
IWrappedDomain getOwners();
IWrappedDomain getMembers();
boolean contains(Location location);
}

View File

@ -1,30 +0,0 @@
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;
import java.util.Optional;
public interface WrappedRegion {
Selection getSelection();
String getId();
Optional<Object> getFlag(String name);
Map<String, Object> getFlags();
Map<AbstractWrappedFlag<?>, Object> getWrappedFlags();
int getPriority();
WrappedDomain getOwners();
WrappedDomain getMembers();
boolean contains(Location location);
}

View File

@ -2,7 +2,7 @@ package org.codemc.worldguardwrapper.selection;
import org.bukkit.Location; import org.bukkit.Location;
public interface CuboidSelection extends Selection { public interface ICuboidSelection extends ISelection {
Location getMinimumPoint(); Location getMinimumPoint();

View File

@ -4,7 +4,7 @@ import org.bukkit.Location;
import java.util.Set; import java.util.Set;
public interface PolygonalSelection extends Selection { public interface IPolygonalSelection extends ISelection {
Set<Location> getPoints(); Set<Location> getPoints();

View File

@ -1,4 +1,4 @@
package org.codemc.worldguardwrapper.selection; package org.codemc.worldguardwrapper.selection;
public interface Selection { public interface ISelection {
} }

View File

@ -1,24 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.codemc.worldguardwrapper</groupId>
<artifactId>worldguardwrapper-implementation</artifactId>
<version>1.0.4-SNAPSHOT</version>
</parent>
<artifactId>worldguardwrapper-implementation-interface</artifactId>
<name>WorldGuardWrapper-Implementation-Interface</name>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>worldguardwrapper-api</artifactId>
<version>1.0.4-SNAPSHOT</version>
</dependency>
</dependencies>
</project>

View File

@ -1,25 +0,0 @@
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

@ -1,34 +0,0 @@
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

@ -7,14 +7,13 @@
<parent> <parent>
<groupId>org.codemc.worldguardwrapper</groupId> <groupId>org.codemc.worldguardwrapper</groupId>
<artifactId>worldguardwrapper-parent</artifactId> <artifactId>worldguardwrapper-parent</artifactId>
<version>1.0.4-SNAPSHOT</version> <version>1.1.0-SNAPSHOT</version>
</parent> </parent>
<artifactId>worldguardwrapper-implementation</artifactId> <artifactId>worldguardwrapper-implementation</artifactId>
<packaging>pom</packaging> <packaging>pom</packaging>
<modules> <modules>
<module>interface</module>
<module>v6</module> <module>v6</module>
<module>v7</module> <module>v7</module>
</modules> </modules>

View File

@ -7,7 +7,7 @@
<parent> <parent>
<groupId>org.codemc.worldguardwrapper</groupId> <groupId>org.codemc.worldguardwrapper</groupId>
<artifactId>worldguardwrapper-implementation</artifactId> <artifactId>worldguardwrapper-implementation</artifactId>
<version>1.0.4-SNAPSHOT</version> <version>1.1.0-SNAPSHOT</version>
</parent> </parent>
<artifactId>worldguardwrapper-implementation-v6</artifactId> <artifactId>worldguardwrapper-implementation-v6</artifactId>
@ -17,8 +17,9 @@
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>${project.groupId}</groupId> <groupId>${project.groupId}</groupId>
<artifactId>worldguardwrapper-implementation-interface</artifactId> <artifactId>worldguardwrapper-api</artifactId>
<version>1.0.4-SNAPSHOT</version> <version>1.1.0-SNAPSHOT</version>
<scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.sk89q.worldguard</groupId> <groupId>com.sk89q.worldguard</groupId>

View File

@ -1,52 +1,39 @@
package org.codemc.worldguardwrapper.implementation.v6; package org.codemc.worldguardwrapper.implementation.v6;
import com.sk89q.worldedit.BlockVector;
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.*;
import com.sk89q.worldguard.protection.flags.Flag;
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;
import com.sk89q.worldguard.protection.managers.RegionManager; 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.flag.AbstractWrappedFlag; import org.bukkit.util.Vector;
import org.codemc.worldguardwrapper.flag.WrappedBooleanFlag; import org.codemc.worldguardwrapper.flag.IWrappedFlag;
import org.codemc.worldguardwrapper.flag.WrappedState;
import org.codemc.worldguardwrapper.implementation.IWorldGuardImplementation; import org.codemc.worldguardwrapper.implementation.IWorldGuardImplementation;
import org.codemc.worldguardwrapper.implementation.wrapper.WrapperAdapter; import org.codemc.worldguardwrapper.implementation.v6.flag.WrappedFlag;
import org.codemc.worldguardwrapper.implementation.wrapper.WrapperAdapterRegister; import org.codemc.worldguardwrapper.implementation.v6.region.WrappedRegion;
import org.codemc.worldguardwrapper.region.WrappedDomain; import org.codemc.worldguardwrapper.implementation.v6.utility.WorldGuardVectorUtilities;
import org.codemc.worldguardwrapper.region.WrappedRegion; import org.codemc.worldguardwrapper.region.IWrappedRegion;
import org.codemc.worldguardwrapper.selection.CuboidSelection;
import org.codemc.worldguardwrapper.selection.PolygonalSelection;
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));
} }
@ -61,7 +48,7 @@ public class WorldGuardImplementation implements IWorldGuardImplementation {
private Optional<ApplicableRegionSet> getApplicableRegions(@NonNull Location minimum, @NonNull Location maximum) { private Optional<ApplicableRegionSet> getApplicableRegions(@NonNull Location minimum, @NonNull Location maximum) {
return getWorldManager(minimum.getWorld()).map(manager -> manager.getApplicableRegions( return getWorldManager(minimum.getWorld()).map(manager -> manager.getApplicableRegions(
new ProtectedCuboidRegion("temp", toBlockVector(minimum), toBlockVector(maximum)))); new ProtectedCuboidRegion("temp", WorldGuardVectorUtilities.toBlockVector(minimum), WorldGuardVectorUtilities.toBlockVector(maximum))));
} }
private <V> Optional<V> queryValue(Player player, @NonNull Location location, @NonNull Flag<V> flag) { private <V> Optional<V> queryValue(Player player, @NonNull Location location, @NonNull Flag<V> flag) {
@ -69,172 +56,6 @@ public class WorldGuardImplementation implements IWorldGuardImplementation {
.orElse(null), flag)); .orElse(null), flag));
} }
private Optional<StateFlag.State> queryState(Player player, @NonNull Location location, @NonNull StateFlag... stateFlags) {
return getApplicableRegions(location).map(applicableRegions -> applicableRegions.queryState(wrapPlayer(player)
.orElse(null), stateFlags));
}
private BlockVector toBlockVector(Location location) {
return new BlockVector(location.getX(), location.getY(), location.getZ());
}
private Location fromBlockVector(World world, BlockVector vector) {
return new Location(world, vector.getX(), vector.getY(), vector.getZ());
}
private List<BlockVector2D> toBlockVector2DList(List<Location> locations) {
return locations.stream().map(location -> new BlockVector2D(location.getX(), location.getZ())).collect(Collectors.toList());
}
private WrappedRegion toRegion(World world, ProtectedRegion region) {
return new WrappedRegion() {
@Override
public Selection getSelection() {
if (region instanceof PolygonalSelection) {
return new PolygonalSelection() {
@Override
public Set<Location> getPoints() {
return region.getPoints().stream()
.map(vector -> new BlockVector(vector.toVector()))
.map(vector -> fromBlockVector(world, vector))
.collect(Collectors.toSet());
}
@Override
public int getMinimumY() {
return ((PolygonalSelection) region).getMinimumY();
}
@Override
public int getMaximumY() {
return ((PolygonalSelection) region).getMaximumY();
}
};
}
return new CuboidSelection() {
@Override
public Location getMinimumPoint() {
return fromBlockVector(world, region.getMinimumPoint());
}
@Override
public Location getMaximumPoint() {
return fromBlockVector(world, region.getMaximumPoint());
}
};
}
@Override
public String getId() {
return region.getId();
}
@Override
public Map<String, Object> getFlags() {
Map<String, Object> map = new HashMap<>();
region.getFlags().forEach((flag, value) -> map.put(flag.getName(), value));
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))
.map(region::getFlag);
}
@Override
public int getPriority() {
return region.getPriority();
}
@Override
public WrappedDomain getOwners() {
return new WrappedDomain() {
@Override
public Set<UUID> getPlayers() {
return region.getOwners().getUniqueIds();
}
@Override
public void addPlayer(UUID uuid) {
region.getOwners().addPlayer(uuid);
}
@Override
public void removePlayer(UUID uuid) {
region.getOwners().removePlayer(uuid);
}
@Override
public Set<String> getGroups() {
return region.getOwners().getGroups();
}
@Override
public void addGroup(String name) {
region.getOwners().addGroup(name);
}
@Override
public void removeGroup(String name) {
region.getOwners().removeGroup(name);
}
};
}
@Override
public WrappedDomain getMembers() {
return new WrappedDomain() {
@Override
public Set<UUID> getPlayers() {
return region.getMembers().getUniqueIds();
}
@Override
public void addPlayer(UUID uuid) {
region.getMembers().addPlayer(uuid);
}
@Override
public void removePlayer(UUID uuid) {
region.getMembers().removePlayer(uuid);
}
@Override
public Set<String> getGroups() {
return region.getMembers().getGroups();
}
@Override
public void addGroup(String name) {
region.getMembers().addGroup(name);
}
@Override
public void removeGroup(String name) {
region.getMembers().removeGroup(name);
}
};
}
@Override
public boolean contains(Location location) {
return region.contains(toBlockVector(location));
}
};
}
@Override @Override
public JavaPlugin getWorldGuardPlugin() { public JavaPlugin getWorldGuardPlugin() {
return WorldGuardPlugin.inst(); return WorldGuardPlugin.inst();
@ -246,94 +67,113 @@ public class WorldGuardImplementation implements IWorldGuardImplementation {
} }
@Override @Override
public <T> Optional<T> queryFlag(Player player, Location location, IWrappedFlag<T> flag) {
Flag<T> wrappedFlag = ((WrappedFlag<T>) flag).getHandle();
return queryValue(player, location, wrappedFlag);
}
@Override
public Optional<IWrappedFlag<?>> getFlag(String name) {
return Optional.ofNullable(flagRegistry.get(name))
.map(WrappedFlag::new);
}
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T> Optional<T> queryFlag(Player player, Location location, String flagName, Class<T> type) { @Override
Flag<?> flag = flagRegistry.get(flagName); public <T> Optional<IWrappedFlag<T>> registerFlag(String name, Class<T> type, T defaultValue) {
Object value = queryValue(player, location, flag).orElse(null); final Flag<T> wrappedFlag;
if (type.isInstance(value)) { if (type.equals(Boolean.class) || type.equals(boolean.class)) {
return Optional.of((T) value); wrappedFlag = (Flag<T>) new BooleanFlag(name);
} else if (type.equals(Double.class) || type.equals(double.class)) {
wrappedFlag = (Flag<T>) new DoubleFlag(name);
} else if (type.equals(Enum.class)) {
wrappedFlag = new EnumFlag(name, type);
} else if (type.equals(Integer.class) || type.equals(int.class)) {
wrappedFlag = (Flag<T>) new IntegerFlag(name);
} else if (type.equals(Location.class)) {
wrappedFlag = (Flag<T>) new LocationFlag(name);
} else if (type.equals(WrappedState.class)) {
wrappedFlag = (Flag<T>) new StateFlag(name, defaultValue == WrappedState.ALLOW);
} else if (type.equals(String.class)) {
wrappedFlag = (Flag<T>) new StringFlag(name, (String) defaultValue);
} else if (type.equals(Vector.class)) {
wrappedFlag = (Flag<T>) new VectorFlag(name);
} else {
throw new IllegalArgumentException("Unsupported flag type " + type.getName());
}
try {
flagRegistry.register(wrappedFlag);
return Optional.of(new WrappedFlag<>(wrappedFlag));
} catch (FlagConflictException ignored) {
} }
return Optional.empty(); return Optional.empty();
} }
@Override @Override
public boolean registerFlag(AbstractWrappedFlag<?> flag) { public Optional<IWrappedRegion> getRegion(World world, String id) {
Optional<Flag<?>> unwrapped = flagAdapter.unwrap(flag); return getWorldManager(world).map(regionManager -> new WrappedRegion(world, regionManager.getRegion(id)));
if (unwrapped.isPresent()) {
try {
flagRegistry.register(unwrapped.get());
return true;
} catch (FlagConflictException ignored) {
}
}
return false;
} }
@Override @Override
public Optional<WrappedRegion> getRegion(World world, String id) { public Map<String, IWrappedRegion> getRegions(World world) {
return getWorldManager(world).map(regionManager -> toRegion(world, regionManager.getRegion(id)));
}
@Override
public Map<String, WrappedRegion> getRegions(World world) {
RegionManager regionManager = plugin.getRegionManager(world); RegionManager regionManager = plugin.getRegionManager(world);
Map<String, ProtectedRegion> regions = regionManager.getRegions(); Map<String, ProtectedRegion> regions = regionManager.getRegions();
Map<String, WrappedRegion> map = new HashMap<>(); Map<String, IWrappedRegion> map = new HashMap<>();
regions.forEach((name, region) -> map.put(name, toRegion(world, region))); regions.forEach((name, region) -> map.put(name, new WrappedRegion(world, region)));
return map; return map;
} }
@Override @Override
public Set<WrappedRegion> getRegions(Location location) { public Set<IWrappedRegion> getRegions(Location location) {
ApplicableRegionSet regionSet = getApplicableRegions(location).orElse(null); ApplicableRegionSet regionSet = getApplicableRegions(location).orElse(null);
Set<WrappedRegion> set = new HashSet<>(); Set<IWrappedRegion> set = new HashSet<>();
if (regionSet == null) { if (regionSet == null) {
return set; return set;
} }
regionSet.forEach(region -> set.add(toRegion(location.getWorld(), region))); regionSet.forEach(region -> set.add(new WrappedRegion(location.getWorld(), region)));
return set; return set;
} }
@Override @Override
public Set<WrappedRegion> getRegions(Location minimum, Location maximum) { public Set<IWrappedRegion> getRegions(Location minimum, Location maximum) {
ApplicableRegionSet regionSet = getApplicableRegions(minimum, maximum).orElse(null); ApplicableRegionSet regionSet = getApplicableRegions(minimum, maximum).orElse(null);
Set<WrappedRegion> set = new HashSet<>(); Set<IWrappedRegion> set = new HashSet<>();
if (regionSet == null) { if (regionSet == null) {
return set; return set;
} }
regionSet.forEach(region -> set.add(toRegion(minimum.getWorld(), region))); regionSet.forEach(region -> set.add(new WrappedRegion(minimum.getWorld(), region)));
return set; return set;
} }
@Override @Override
public Optional<WrappedRegion> addRegion(String id, List<Location> points, int minY, int maxY) { public Optional<IWrappedRegion> addRegion(String id, List<Location> points, int minY, int maxY) {
ProtectedRegion region; ProtectedRegion region;
World world = points.get(0).getWorld(); World world = points.get(0).getWorld();
if (points.size() == 2) { if (points.size() == 2) {
region = new ProtectedCuboidRegion(id, toBlockVector(points.get(0)), toBlockVector(points.get(1))); region = new ProtectedCuboidRegion(id, WorldGuardVectorUtilities.toBlockVector(points.get(0)), WorldGuardVectorUtilities.toBlockVector(points.get(1)));
} else { } else {
region = new ProtectedPolygonalRegion(id, toBlockVector2DList(points), minY, maxY); region = new ProtectedPolygonalRegion(id, WorldGuardVectorUtilities.toBlockVector2DList(points), minY, maxY);
} }
Optional<RegionManager> manager = getWorldManager(world); Optional<RegionManager> manager = getWorldManager(world);
if (manager.isPresent()) { if (manager.isPresent()) {
manager.get().addRegion(region); manager.get().addRegion(region);
return Optional.of(toRegion(world, region)); return Optional.of(new WrappedRegion(world, region));
} else { } else {
return Optional.empty(); return Optional.empty();
} }
} }
@Override @Override
public Optional<Set<WrappedRegion>> removeRegion(World world, String id) { public Optional<Set<IWrappedRegion>> removeRegion(World world, String id) {
Optional<Set<ProtectedRegion>> set = getWorldManager(world).map(manager -> manager.removeRegion(id)); Optional<Set<ProtectedRegion>> set = getWorldManager(world).map(manager -> manager.removeRegion(id));
return set.map(protectedRegions -> protectedRegions.stream().map(region -> toRegion(world, region)) return set.map(protectedRegions -> protectedRegions.stream().map(region -> new WrappedRegion(world, region))
.collect(Collectors.toSet())); .collect(Collectors.toSet()));
} }

View File

@ -1,4 +1,4 @@
package org.codemc.worldguardwrapper.implementation.v6; package org.codemc.worldguardwrapper.implementation.v6.event;
import com.sk89q.worldguard.bukkit.event.block.UseBlockEvent; import com.sk89q.worldguard.bukkit.event.block.UseBlockEvent;
import com.sk89q.worldguard.bukkit.event.entity.DamageEntityEvent; import com.sk89q.worldguard.bukkit.event.entity.DamageEntityEvent;

View File

@ -0,0 +1,27 @@
package org.codemc.worldguardwrapper.implementation.v6.flag;
import com.sk89q.worldguard.protection.flags.Flag;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.codemc.worldguardwrapper.flag.IWrappedFlag;
import java.util.Optional;
@AllArgsConstructor
@Getter
public class WrappedFlag<T> implements IWrappedFlag<T> {
private final Flag<T> handle;
@Override
public String getName() {
return handle.getName();
}
@Override
public Optional<T> getDefaultValue() {
return Optional.ofNullable(handle.getDefault());
}
}

View File

@ -0,0 +1,175 @@
package org.codemc.worldguardwrapper.implementation.v6.region;
import com.sk89q.worldedit.BlockVector;
import com.sk89q.worldguard.protection.flags.Flag;
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.bukkit.Location;
import org.bukkit.World;
import org.codemc.worldguardwrapper.flag.IWrappedFlag;
import org.codemc.worldguardwrapper.implementation.v6.flag.WrappedFlag;
import org.codemc.worldguardwrapper.implementation.v6.utility.WorldGuardVectorUtilities;
import org.codemc.worldguardwrapper.region.IWrappedDomain;
import org.codemc.worldguardwrapper.region.IWrappedRegion;
import org.codemc.worldguardwrapper.selection.ICuboidSelection;
import org.codemc.worldguardwrapper.selection.IPolygonalSelection;
import org.codemc.worldguardwrapper.selection.ISelection;
import java.util.*;
import java.util.stream.Collectors;
@AllArgsConstructor
@Getter
public class WrappedRegion implements IWrappedRegion {
private final World world;
private final ProtectedRegion handle;
@Override
public ISelection getSelection() {
if (handle instanceof IPolygonalSelection) {
return new IPolygonalSelection() {
@Override
public Set<Location> getPoints() {
return handle.getPoints().stream()
.map(vector -> new BlockVector(vector.toVector()))
.map(vector -> WorldGuardVectorUtilities.fromBlockVector(world, vector))
.collect(Collectors.toSet());
}
@Override
public int getMinimumY() {
return ((IPolygonalSelection) handle).getMinimumY();
}
@Override
public int getMaximumY() {
return ((IPolygonalSelection) handle).getMaximumY();
}
};
}
return new ICuboidSelection() {
@Override
public Location getMinimumPoint() {
return WorldGuardVectorUtilities.fromBlockVector(world, handle.getMinimumPoint());
}
@Override
public Location getMaximumPoint() {
return WorldGuardVectorUtilities.fromBlockVector(world, handle.getMaximumPoint());
}
};
}
@Override
public String getId() {
return handle.getId();
}
@SuppressWarnings("unchecked")
@Override
public <T> Optional<T> getFlag(IWrappedFlag<T> flag) {
Flag wrappedFlag = ((WrappedFlag) flag).getHandle();
return Optional.ofNullable(handle.getFlag(wrappedFlag))
.map(value -> (T) value);
}
@SuppressWarnings("unchecked")
@Override
public <T> void setFlag(IWrappedFlag<T> flag, T value) {
Flag wrappedFlag = ((WrappedFlag) flag).getHandle();
handle.setFlag(wrappedFlag, value);
}
@Override
public Map<IWrappedFlag<?>, Object> getFlags() {
Map<IWrappedFlag<?>, Object> map = new HashMap<>();
handle.getFlags().forEach((flag, value) -> map.put(new WrappedFlag<>(flag), value));
return map;
}
@Override
public int getPriority() {
return handle.getPriority();
}
@Override
public IWrappedDomain getOwners() {
return new IWrappedDomain() {
@Override
public Set<UUID> getPlayers() {
return handle.getOwners().getUniqueIds();
}
@Override
public void addPlayer(UUID uuid) {
handle.getOwners().addPlayer(uuid);
}
@Override
public void removePlayer(UUID uuid) {
handle.getOwners().removePlayer(uuid);
}
@Override
public Set<String> getGroups() {
return handle.getOwners().getGroups();
}
@Override
public void addGroup(String name) {
handle.getOwners().addGroup(name);
}
@Override
public void removeGroup(String name) {
handle.getOwners().removeGroup(name);
}
};
}
@Override
public IWrappedDomain getMembers() {
return new IWrappedDomain() {
@Override
public Set<UUID> getPlayers() {
return handle.getMembers().getUniqueIds();
}
@Override
public void addPlayer(UUID uuid) {
handle.getMembers().addPlayer(uuid);
}
@Override
public void removePlayer(UUID uuid) {
handle.getMembers().removePlayer(uuid);
}
@Override
public Set<String> getGroups() {
return handle.getMembers().getGroups();
}
@Override
public void addGroup(String name) {
handle.getMembers().addGroup(name);
}
@Override
public void removeGroup(String name) {
handle.getMembers().removeGroup(name);
}
};
}
@Override
public boolean contains(Location location) {
return handle.contains(WorldGuardVectorUtilities.toBlockVector(location));
}
}

View File

@ -0,0 +1,26 @@
package org.codemc.worldguardwrapper.implementation.v6.utility;
import com.sk89q.worldedit.BlockVector;
import com.sk89q.worldedit.BlockVector2D;
import lombok.experimental.UtilityClass;
import org.bukkit.Location;
import org.bukkit.World;
import java.util.List;
import java.util.stream.Collectors;
@UtilityClass
public class WorldGuardVectorUtilities {
public BlockVector toBlockVector(Location location) {
return new BlockVector(location.getX(), location.getY(), location.getZ());
}
public Location fromBlockVector(World world, BlockVector vector) {
return new Location(world, vector.getX(), vector.getY(), vector.getZ());
}
public List<BlockVector2D> toBlockVector2DList(List<Location> locations) {
return locations.stream().map(location -> new BlockVector2D(location.getX(), location.getZ())).collect(Collectors.toList());
}
}

View File

@ -7,7 +7,7 @@
<parent> <parent>
<groupId>org.codemc.worldguardwrapper</groupId> <groupId>org.codemc.worldguardwrapper</groupId>
<artifactId>worldguardwrapper-implementation</artifactId> <artifactId>worldguardwrapper-implementation</artifactId>
<version>1.0.4-SNAPSHOT</version> <version>1.1.0-SNAPSHOT</version>
</parent> </parent>
<artifactId>worldguardwrapper-implementation-v7</artifactId> <artifactId>worldguardwrapper-implementation-v7</artifactId>
@ -17,13 +17,14 @@
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>${project.groupId}</groupId> <groupId>${project.groupId}</groupId>
<artifactId>worldguardwrapper-implementation-interface</artifactId> <artifactId>worldguardwrapper-api</artifactId>
<version>1.0.4-SNAPSHOT</version> <version>1.1.0-SNAPSHOT</version>
<scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.sk89q.worldguard</groupId> <groupId>com.sk89q.worldguard</groupId>
<artifactId>worldguard-legacy</artifactId> <artifactId>worldguard-legacy</artifactId>
<version>7.0.0-20181117.063337-9</version> <!-- Manually set version --> <version>7.0.0-SNAPSHOT</version>
<scope>provided</scope> <scope>provided</scope>
<exclusions> <exclusions>
<exclusion> <exclusion>
@ -36,121 +37,5 @@
</exclusion> </exclusion>
</exclusions> </exclusions>
</dependency> </dependency>
<dependency> <!-- Override worldguard-core to manually set version -->
<groupId>com.sk89q.worldguard</groupId>
<artifactId>worldguard-core</artifactId>
<version>7.0.0-20181117.063337-9</version>
<scope>provided</scope>
<exclusions>
<exclusion>
<groupId>com.sk89q.intake</groupId>
<artifactId>intake</artifactId>
</exclusion>
<exclusion>
<groupId>com.sk89q</groupId>
<artifactId>squirrelid</artifactId>
</exclusion>
<exclusion>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.khelekore</groupId>
<artifactId>prtree</artifactId>
</exclusion>
<exclusion>
<groupId>net.sf.opencsv</groupId>
<artifactId>opencsv</artifactId>
</exclusion>
<exclusion>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
</exclusion>
<exclusion>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency> <!-- Override worldedit-bukkit to manually set version -->
<groupId>com.sk89q.worldedit</groupId>
<artifactId>worldedit-bukkit</artifactId>
<version>7.0.0-20181118.055910-28</version>
<scope>provided</scope>
<exclusions>
<exclusion>
<groupId>com.sk89q</groupId>
<artifactId>dummypermscompat</artifactId>
</exclusion>
<exclusion>
<groupId>org.bukkit</groupId>
<artifactId>bukkit</artifactId>
</exclusion>
<exclusion>
<groupId>org.bstats</groupId>
<artifactId>bstats-bukkit</artifactId>
</exclusion>
<exclusion>
<groupId>io.papermc</groupId>
<artifactId>paperlib</artifactId>
</exclusion>
<exclusion>
<groupId>net.milkbowl.vault</groupId>
<artifactId>VaultAPI</artifactId>
</exclusion>
<exclusion>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency> <!-- Override worldedit-core to manually set version -->
<groupId>com.sk89q.worldedit</groupId>
<artifactId>worldedit-core</artifactId>
<version>7.0.0-20181118.055910-28</version>
<scope>provided</scope>
<exclusions>
<exclusion>
<groupId>de.schlichtherle</groupId>
<artifactId>truezip</artifactId>
</exclusion>
<exclusion>
<groupId>rhino</groupId>
<artifactId>js</artifactId>
</exclusion>
<exclusion>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
</exclusion>
<exclusion>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</exclusion>
<exclusion>
<groupId>com.sk89q</groupId>
<artifactId>jchronic</artifactId>
</exclusion>
<exclusion>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
</exclusion>
<exclusion>
<groupId>com.thoughtworks.paranamer</groupId>
<artifactId>paranamer</artifactId>
</exclusion>
<exclusion>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</exclusion>
<exclusion>
<groupId>com.sk89q.lib</groupId>
<artifactId>jlibnoise</artifactId>
</exclusion>
<exclusion>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -1,16 +1,14 @@
package org.codemc.worldguardwrapper.implementation.v7; package org.codemc.worldguardwrapper.implementation.v7;
import com.sk89q.worldedit.BlockVector;
import com.sk89q.worldedit.BlockVector2D;
import com.sk89q.worldedit.bukkit.BukkitAdapter; import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.bukkit.BukkitWorld; import com.sk89q.worldedit.bukkit.BukkitWorld;
import com.sk89q.worldedit.math.BlockVector2;
import com.sk89q.worldguard.LocalPlayer; import com.sk89q.worldguard.LocalPlayer;
import com.sk89q.worldguard.WorldGuard; import com.sk89q.worldguard.WorldGuard;
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.Flag; import com.sk89q.worldguard.protection.flags.*;
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.FlagConflictException;
import com.sk89q.worldguard.protection.flags.registry.FlagRegistry; import com.sk89q.worldguard.protection.flags.registry.FlagRegistry;
import com.sk89q.worldguard.protection.managers.RegionManager; import com.sk89q.worldguard.protection.managers.RegionManager;
@ -23,13 +21,13 @@ 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.flag.AbstractWrappedFlag; import org.bukkit.util.Vector;
import org.codemc.worldguardwrapper.flag.IWrappedFlag;
import org.codemc.worldguardwrapper.flag.WrappedState;
import org.codemc.worldguardwrapper.implementation.IWorldGuardImplementation; import org.codemc.worldguardwrapper.implementation.IWorldGuardImplementation;
import org.codemc.worldguardwrapper.region.WrappedDomain; import org.codemc.worldguardwrapper.implementation.v7.flag.WrappedFlag;
import org.codemc.worldguardwrapper.region.WrappedRegion; import org.codemc.worldguardwrapper.implementation.v7.region.WrappedRegion;
import org.codemc.worldguardwrapper.selection.CuboidSelection; import org.codemc.worldguardwrapper.region.IWrappedRegion;
import org.codemc.worldguardwrapper.selection.PolygonalSelection;
import org.codemc.worldguardwrapper.selection.Selection;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -59,12 +57,12 @@ public class WorldGuardImplementation implements IWorldGuardImplementation {
} }
private Optional<ApplicableRegionSet> getApplicableRegions(@NonNull Location location) { private Optional<ApplicableRegionSet> getApplicableRegions(@NonNull Location location) {
return getWorldManager(location.getWorld()).map(manager -> manager.getApplicableRegions(BukkitAdapter.asBlockVector(location))); return getWorldManager(location.getWorld()).map(manager -> manager.getApplicableRegions(BukkitAdapter.asVector(location)));
} }
private Optional<ApplicableRegionSet> getApplicableRegions(@NonNull Location minimum, @NonNull Location maximum) { private Optional<ApplicableRegionSet> getApplicableRegions(@NonNull Location minimum, @NonNull Location maximum) {
return getWorldManager(minimum.getWorld()).map(manager -> manager.getApplicableRegions( return getWorldManager(minimum.getWorld()).map(manager -> manager.getApplicableRegions(
new ProtectedCuboidRegion("temp", BukkitAdapter.asBlockVector(minimum), BukkitAdapter.asBlockVector(maximum)))); new ProtectedCuboidRegion("temp", new BlockVector(BukkitAdapter.asVector(minimum)), new BlockVector(BukkitAdapter.asVector(maximum)))));
} }
private <V> Optional<V> queryValue(Player player, @NonNull Location location, @NonNull Flag<V> flag) { private <V> Optional<V> queryValue(Player player, @NonNull Location location, @NonNull Flag<V> flag) {
@ -77,147 +75,6 @@ public class WorldGuardImplementation implements IWorldGuardImplementation {
.orElse(null), stateFlags)); .orElse(null), stateFlags));
} }
private WrappedRegion toRegion(World world, ProtectedRegion region) {
return new WrappedRegion() {
@Override
public Selection getSelection() {
if (region instanceof PolygonalSelection) {
return new PolygonalSelection() {
@Override
public Set<Location> getPoints() {
return region.getPoints().stream()
.map(BlockVector2::toBlockVector3)
.map(vector -> BukkitAdapter.adapt(world, vector))
.collect(Collectors.toSet());
}
@Override
public int getMinimumY() {
return ((PolygonalSelection) region).getMinimumY();
}
@Override
public int getMaximumY() {
return ((PolygonalSelection) region).getMaximumY();
}
};
}
return new CuboidSelection() {
@Override
public Location getMinimumPoint() {
return BukkitAdapter.adapt(world, region.getMinimumPoint());
}
@Override
public Location getMaximumPoint() {
return BukkitAdapter.adapt(world, region.getMaximumPoint());
}
};
}
@Override
public String getId() {
return region.getId();
}
@Override
public Map<String, Object> getFlags() {
Map<String, Object> map = new HashMap<>();
region.getFlags().forEach((flag, value) -> map.put(flag.getName(), value));
return map;
}
@Override
public Optional<Object> getFlag(String name) {
return Optional.ofNullable(flagRegistry.get(name))
.map(region::getFlag);
}
@Override
public int getPriority() {
return region.getPriority();
}
@Override
public WrappedDomain getOwners() {
return new WrappedDomain() {
@Override
public Set<UUID> getPlayers() {
return region.getOwners().getUniqueIds();
}
@Override
public void addPlayer(UUID uuid) {
region.getOwners().addPlayer(uuid);
}
@Override
public void removePlayer(UUID uuid) {
region.getOwners().removePlayer(uuid);
}
@Override
public Set<String> getGroups() {
return region.getOwners().getGroups();
}
@Override
public void addGroup(String name) {
region.getOwners().addGroup(name);
}
@Override
public void removeGroup(String name) {
region.getOwners().removeGroup(name);
}
};
}
@Override
public WrappedDomain getMembers() {
return new WrappedDomain() {
@Override
public Set<UUID> getPlayers() {
return region.getMembers().getUniqueIds();
}
@Override
public void addPlayer(UUID uuid) {
region.getMembers().addPlayer(uuid);
}
@Override
public void removePlayer(UUID uuid) {
region.getMembers().removePlayer(uuid);
}
@Override
public Set<String> getGroups() {
return region.getMembers().getGroups();
}
@Override
public void addGroup(String name) {
region.getMembers().addGroup(name);
}
@Override
public void removeGroup(String name) {
region.getMembers().removeGroup(name);
}
};
}
@Override
public boolean contains(Location location) {
return region.contains(BukkitAdapter.asBlockVector(location));
}
};
}
@Override @Override
public JavaPlugin getWorldGuardPlugin() { public JavaPlugin getWorldGuardPlugin() {
return WorldGuardPlugin.inst(); return WorldGuardPlugin.inst();
@ -229,121 +86,102 @@ public class WorldGuardImplementation implements IWorldGuardImplementation {
} }
@Override @Override
public Optional<Boolean> queryStateFlag(Player player, @NonNull Location location, @NonNull String flagId) { public <T> Optional<T> queryFlag(Player player, Location location, IWrappedFlag<T> flag) {
Flag<?> flag = flagRegistry.get(flagId); Flag<T> wrappedFlag = ((WrappedFlag<T>) flag).getHandle();
if (!(flag instanceof StateFlag)) { return queryValue(player, location, wrappedFlag);
return Optional.empty();
}
return queryState(player, location, (StateFlag) flag).map(state -> state == StateFlag.State.ALLOW);
} }
@Override @Override
public boolean registerStateFlag(@NonNull String flagId, @NonNull Boolean defaultValue) { public Optional<IWrappedFlag<?>> getFlag(String name) {
try { return Optional.ofNullable(flagRegistry.get(name))
flagRegistry.register(new StateFlag(flagId, defaultValue)); .map(WrappedFlag::new);
return true;
} catch (FlagConflictException ignored) {
}
return false;
} }
@Override
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T> Optional<T> queryFlag(Player player, Location location, String flagName, Class<T> type) { @Override
Flag<?> flag = flagRegistry.get(flagName); public <T> Optional<IWrappedFlag<T>> registerFlag(String name, Class<T> type, T defaultValue) {
Object value = queryValue(player, location, flag).orElse(null); final Flag<T> wrappedFlag;
if (type.isInstance(value)) { if (type.equals(Boolean.class) || type.equals(boolean.class)) {
return Optional.of((T) value); wrappedFlag = (Flag<T>) new BooleanFlag(name);
} else if (type.equals(Double.class) || type.equals(double.class)) {
wrappedFlag = (Flag<T>) new DoubleFlag(name);
} else if (type.equals(Enum.class)) {
wrappedFlag = new EnumFlag(name, type);
} else if (type.equals(Integer.class) || type.equals(int.class)) {
wrappedFlag = (Flag<T>) new IntegerFlag(name);
} else if (type.equals(Location.class)) {
wrappedFlag = (Flag<T>) new LocationFlag(name);
} else if (type.equals(WrappedState.class)) {
wrappedFlag = (Flag<T>) new StateFlag(name, defaultValue == WrappedState.ALLOW);
} else if (type.equals(String.class)) {
wrappedFlag = (Flag<T>) new StringFlag(name, (String) defaultValue);
} else if (type.equals(Vector.class)) {
wrappedFlag = (Flag<T>) new VectorFlag(name);
} else {
throw new IllegalArgumentException("Unsupported flag type " + type.getName());
}
try {
flagRegistry.register(wrappedFlag);
return Optional.of(new WrappedFlag<>(wrappedFlag));
} catch (FlagConflictException ignored) {
} }
return Optional.empty(); return Optional.empty();
} }
@Override @Override
public <T> boolean registerFlag(AbstractWrappedFlag<T> flag) { public Optional<IWrappedRegion> getRegion(World world, String id) {
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(getPlayer(context.getPlayerSender()).orElse(null), context.getUserInput());
}
};
try {
flagRegistry.register(wgFlag);
return true;
} catch (FlagConflictException ignored) {
}
return false;
}
@Override
public Optional<WrappedRegion> getRegion(World world, String id) {
return getWorldManager(world) return getWorldManager(world)
.map(regionManager -> regionManager.getRegion(id)) .map(regionManager -> regionManager.getRegion(id))
.map(region -> toRegion(world, region)); .map(region -> new WrappedRegion(world, region));
} }
@Override @Override
public Map<String, WrappedRegion> getRegions(World world) { public Map<String, IWrappedRegion> getRegions(World world) {
RegionManager regionManager = core.getPlatform().getRegionContainer().get(new BukkitWorld(world)); RegionManager regionManager = core.getPlatform().getRegionContainer().get(new BukkitWorld(world));
if (regionManager == null) { if (regionManager == null) {
return Collections.emptyMap(); return Collections.emptyMap();
} }
Map<String, ProtectedRegion> regions = regionManager.getRegions(); Map<String, ProtectedRegion> regions = regionManager.getRegions();
Map<String, WrappedRegion> map = new HashMap<>(); Map<String, IWrappedRegion> map = new HashMap<>();
regions.forEach((name, region) -> map.put(name, toRegion(world, region))); regions.forEach((name, region) -> map.put(name, new WrappedRegion(world, region)));
return map; return map;
} }
@Override @Override
public Set<WrappedRegion> getRegions(Location location) { public Set<IWrappedRegion> getRegions(Location location) {
ApplicableRegionSet regionSet = getApplicableRegions(location).orElse(null); ApplicableRegionSet regionSet = getApplicableRegions(location).orElse(null);
if (regionSet == null) { if (regionSet == null) {
return Collections.emptySet(); return Collections.emptySet();
} }
return regionSet.getRegions().stream() return regionSet.getRegions().stream()
.map(region -> toRegion(location.getWorld(), region)) .map(region -> new WrappedRegion(location.getWorld(), region))
.collect(Collectors.toSet()); .collect(Collectors.toSet());
} }
@Override @Override
public Set<WrappedRegion> getRegions(Location minimum, Location maximum) { public Set<IWrappedRegion> getRegions(Location minimum, Location maximum) {
ApplicableRegionSet regionSet = getApplicableRegions(minimum, maximum).orElse(null); ApplicableRegionSet regionSet = getApplicableRegions(minimum, maximum).orElse(null);
if (regionSet == null) { if (regionSet == null) {
return Collections.emptySet(); return Collections.emptySet();
} }
return regionSet.getRegions().stream() return regionSet.getRegions().stream()
.map(region -> toRegion(minimum.getWorld(), region)) .map(region -> new WrappedRegion(minimum.getWorld(), region))
.collect(Collectors.toSet()); .collect(Collectors.toSet());
} }
@Override @Override
public Optional<WrappedRegion> addRegion(String id, List<Location> points, int minY, int maxY) { public Optional<IWrappedRegion> addRegion(String id, List<Location> points, int minY, int maxY) {
ProtectedRegion region; ProtectedRegion region;
World world = points.get(0).getWorld(); World world = points.get(0).getWorld();
if (points.size() == 2) { if (points.size() == 2) {
region = new ProtectedCuboidRegion(id, BukkitAdapter.asBlockVector(points.get(0)), region = new ProtectedCuboidRegion(id, new BlockVector(BukkitAdapter.asVector(points.get(0))),
BukkitAdapter.asBlockVector(points.get(1))); new BlockVector(BukkitAdapter.asVector(points.get(1))));
} else { } else {
List<BlockVector2> vectorPoints = points.stream() List<BlockVector2D> vectorPoints = points.stream()
.map(location -> BukkitAdapter.asBlockVector(location).toBlockVector2()) .map(location -> new BlockVector2D(BukkitAdapter.asVector(location).toVector2D()))
.collect(Collectors.toList()); .collect(Collectors.toList());
region = new ProtectedPolygonalRegion(id, vectorPoints, minY, maxY); region = new ProtectedPolygonalRegion(id, vectorPoints, minY, maxY);
@ -352,16 +190,16 @@ public class WorldGuardImplementation implements IWorldGuardImplementation {
Optional<RegionManager> manager = getWorldManager(world); Optional<RegionManager> manager = getWorldManager(world);
if (manager.isPresent()) { if (manager.isPresent()) {
manager.get().addRegion(region); manager.get().addRegion(region);
return Optional.of(toRegion(world, region)); return Optional.of(new WrappedRegion(world, region));
} else { } else {
return Optional.empty(); return Optional.empty();
} }
} }
@Override @Override
public Optional<Set<WrappedRegion>> removeRegion(World world, String id) { public Optional<Set<IWrappedRegion>> removeRegion(World world, String id) {
Optional<Set<ProtectedRegion>> set = getWorldManager(world).map(manager -> manager.removeRegion(id)); Optional<Set<ProtectedRegion>> set = getWorldManager(world).map(manager -> manager.removeRegion(id));
return set.map(protectedRegions -> protectedRegions.stream() return set.map(protectedRegions -> protectedRegions.stream()
.map(region -> toRegion(world, region)).collect(Collectors.toSet())); .map(region -> new WrappedRegion(world, region)).collect(Collectors.toSet()));
} }
} }

View File

@ -1,4 +1,4 @@
package org.codemc.worldguardwrapper.implementation.v7; package org.codemc.worldguardwrapper.implementation.v7.event;
import com.sk89q.worldguard.bukkit.event.block.UseBlockEvent; import com.sk89q.worldguard.bukkit.event.block.UseBlockEvent;
import com.sk89q.worldguard.bukkit.event.entity.DamageEntityEvent; import com.sk89q.worldguard.bukkit.event.entity.DamageEntityEvent;

View File

@ -0,0 +1,26 @@
package org.codemc.worldguardwrapper.implementation.v7.flag;
import com.sk89q.worldguard.protection.flags.Flag;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.codemc.worldguardwrapper.flag.IWrappedFlag;
import java.util.Optional;
@AllArgsConstructor
@Getter
public class WrappedFlag<T> implements IWrappedFlag<T> {
private final Flag<T> handle;
@Override
public String getName() {
return handle.getName();
}
@Override
public Optional<T> getDefaultValue() {
return Optional.ofNullable(handle.getDefault());
}
}

View File

@ -0,0 +1,174 @@
package org.codemc.worldguardwrapper.implementation.v7.region;
import com.sk89q.worldedit.BlockVector2D;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldguard.protection.flags.Flag;
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.bukkit.Location;
import org.bukkit.World;
import org.codemc.worldguardwrapper.flag.IWrappedFlag;
import org.codemc.worldguardwrapper.implementation.v7.flag.WrappedFlag;
import org.codemc.worldguardwrapper.region.IWrappedDomain;
import org.codemc.worldguardwrapper.region.IWrappedRegion;
import org.codemc.worldguardwrapper.selection.ICuboidSelection;
import org.codemc.worldguardwrapper.selection.IPolygonalSelection;
import org.codemc.worldguardwrapper.selection.ISelection;
import java.util.*;
import java.util.stream.Collectors;
@AllArgsConstructor
@Getter
public class WrappedRegion implements IWrappedRegion {
private final World world;
private final ProtectedRegion handle;
@Override
public ISelection getSelection() {
if (handle instanceof IPolygonalSelection) {
return new IPolygonalSelection() {
@Override
public Set<Location> getPoints() {
return handle.getPoints().stream()
.map(BlockVector2D::toVector)
.map(vector -> BukkitAdapter.adapt(world, vector))
.collect(Collectors.toSet());
}
@Override
public int getMinimumY() {
return ((IPolygonalSelection) handle).getMinimumY();
}
@Override
public int getMaximumY() {
return ((IPolygonalSelection) handle).getMaximumY();
}
};
}
return new ICuboidSelection() {
@Override
public Location getMinimumPoint() {
return BukkitAdapter.adapt(world, handle.getMinimumPoint());
}
@Override
public Location getMaximumPoint() {
return BukkitAdapter.adapt(world, handle.getMaximumPoint());
}
};
}
@Override
public String getId() {
return handle.getId();
}
@SuppressWarnings("unchecked")
@Override
public <T> Optional<T> getFlag(IWrappedFlag<T> flag) {
Flag wrappedFlag = ((WrappedFlag) flag).getHandle();
return Optional.ofNullable(handle.getFlag(wrappedFlag))
.map(value -> (T) value);
}
@SuppressWarnings("unchecked")
@Override
public <T> void setFlag(IWrappedFlag<T> flag, T value) {
Flag wrappedFlag = ((WrappedFlag) flag).getHandle();
handle.setFlag(wrappedFlag, value);
}
@Override
public Map<IWrappedFlag<?>, Object> getFlags() {
Map<IWrappedFlag<?>, Object> map = new HashMap<>();
handle.getFlags().forEach((flag, value) -> map.put(new WrappedFlag<>(flag), value));
return map;
}
@Override
public int getPriority() {
return handle.getPriority();
}
@Override
public IWrappedDomain getOwners() {
return new IWrappedDomain() {
@Override
public Set<UUID> getPlayers() {
return handle.getOwners().getUniqueIds();
}
@Override
public void addPlayer(UUID uuid) {
handle.getOwners().addPlayer(uuid);
}
@Override
public void removePlayer(UUID uuid) {
handle.getOwners().removePlayer(uuid);
}
@Override
public Set<String> getGroups() {
return handle.getOwners().getGroups();
}
@Override
public void addGroup(String name) {
handle.getOwners().addGroup(name);
}
@Override
public void removeGroup(String name) {
handle.getOwners().removeGroup(name);
}
};
}
@Override
public IWrappedDomain getMembers() {
return new IWrappedDomain() {
@Override
public Set<UUID> getPlayers() {
return handle.getMembers().getUniqueIds();
}
@Override
public void addPlayer(UUID uuid) {
handle.getMembers().addPlayer(uuid);
}
@Override
public void removePlayer(UUID uuid) {
handle.getMembers().removePlayer(uuid);
}
@Override
public Set<String> getGroups() {
return handle.getMembers().getGroups();
}
@Override
public void addGroup(String name) {
handle.getMembers().addGroup(name);
}
@Override
public void removeGroup(String name) {
handle.getMembers().removeGroup(name);
}
};
}
@Override
public boolean contains(Location location) {
return handle.contains(BukkitAdapter.asVector(location));
}
}

View File

@ -7,7 +7,7 @@
<parent> <parent>
<groupId>org.codemc.worldguardwrapper</groupId> <groupId>org.codemc.worldguardwrapper</groupId>
<artifactId>worldguardwrapper-parent</artifactId> <artifactId>worldguardwrapper-parent</artifactId>
<version>1.0.4-SNAPSHOT</version> <version>1.1.0-SNAPSHOT</version>
</parent> </parent>
<artifactId>worldguardwrapper</artifactId> <artifactId>worldguardwrapper</artifactId>
@ -15,17 +15,22 @@
<name>WorldGuardWrapper-Library</name> <name>WorldGuardWrapper-Library</name>
<dependencies> <dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>worldguardwrapper-api</artifactId>
<version>1.1.0-SNAPSHOT</version>
</dependency>
<dependency> <dependency>
<groupId>${project.groupId}</groupId> <groupId>${project.groupId}</groupId>
<artifactId>worldguardwrapper-implementation-v6</artifactId> <artifactId>worldguardwrapper-implementation-v6</artifactId>
<version>1.0.4-SNAPSHOT</version> <version>1.1.0-SNAPSHOT</version>
<optional>true</optional> <scope>runtime</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>${project.groupId}</groupId> <groupId>${project.groupId}</groupId>
<artifactId>worldguardwrapper-implementation-v7</artifactId> <artifactId>worldguardwrapper-implementation-v7</artifactId>
<version>1.0.4-SNAPSHOT</version> <version>1.1.0-SNAPSHOT</version>
<optional>true</optional> <scope>runtime</scope>
</dependency> </dependency>
</dependencies> </dependencies>

View File

@ -27,13 +27,20 @@ public class WorldGuardWrapper implements IWorldGuardImplementation {
private Listener eventListener; private Listener eventListener;
private WorldGuardWrapper() { private WorldGuardWrapper() {
String version;
try { try {
Class.forName("com.sk89q.worldguard.WorldGuard"); Class.forName("com.sk89q.worldguard.WorldGuard");
delegate = new org.codemc.worldguardwrapper.implementation.v7.WorldGuardImplementation(); version = "v7";
eventListener = new org.codemc.worldguardwrapper.implementation.v7.EventListener();
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
delegate = new org.codemc.worldguardwrapper.implementation.v6.WorldGuardImplementation(); version = "v6";
eventListener = new org.codemc.worldguardwrapper.implementation.v6.EventListener(); }
try {
delegate = (IWorldGuardImplementation) Class.forName("org.codemc.worldguardwrapper.implementation."
+ version + ".WorldGuardImplementation").newInstance();
eventListener = (Listener) Class.forName("new org.codemc.worldguardwrapper.implementation."
+ version + ".event.EventListener").newInstance();
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
throw new RuntimeException("Unable to initialize WorldGuard implementation " + version, e);
} }
} }

View File

@ -6,7 +6,7 @@
<groupId>org.codemc.worldguardwrapper</groupId> <groupId>org.codemc.worldguardwrapper</groupId>
<artifactId>worldguardwrapper-parent</artifactId> <artifactId>worldguardwrapper-parent</artifactId>
<version>1.0.4-SNAPSHOT</version> <version>1.1.0-SNAPSHOT</version>
<packaging>pom</packaging> <packaging>pom</packaging>
<modules> <modules>