From 379433ea3595273c2b2945be6d4499a1508f784e Mon Sep 17 00:00:00 2001 From: Gabriele C Date: Sun, 13 Jan 2019 12:33:47 +0100 Subject: [PATCH] Update, add FAWE support --- .travis.yml | 6 +- api/pom.xml | 2 +- .../region/IWrappedRegion.java | 1 - implementation/pom.xml | 13 +- implementation/v6/pom.xml | 14 +- .../v6/WorldGuardImplementation.java | 2 +- .../v6/region/WrappedRegion.java | 4 +- implementation/v7/pom.xml | 14 +- implementation/v7fawe/pom.xml | 66 ++++++ .../v7fawe/WorldGuardImplementation.java | 223 ++++++++++++++++++ .../v7fawe/event/EventListener.java | 100 ++++++++ .../v7fawe/flag/AbstractWrappedFlag.java | 30 +++ .../v7fawe/flag/WrappedPrimitiveFlag.java | 23 ++ .../v7fawe/flag/WrappedStatusFlag.java | 28 +++ .../v7fawe/region/WrappedRegion.java | 169 +++++++++++++ library/pom.xml | 13 +- .../worldguardwrapper/WorldGuardWrapper.java | 9 +- pom.xml | 2 +- 18 files changed, 690 insertions(+), 29 deletions(-) create mode 100644 implementation/v7fawe/pom.xml create mode 100644 implementation/v7fawe/src/main/java/org/codemc/worldguardwrapper/implementation/v7fawe/WorldGuardImplementation.java create mode 100644 implementation/v7fawe/src/main/java/org/codemc/worldguardwrapper/implementation/v7fawe/event/EventListener.java create mode 100644 implementation/v7fawe/src/main/java/org/codemc/worldguardwrapper/implementation/v7fawe/flag/AbstractWrappedFlag.java create mode 100644 implementation/v7fawe/src/main/java/org/codemc/worldguardwrapper/implementation/v7fawe/flag/WrappedPrimitiveFlag.java create mode 100644 implementation/v7fawe/src/main/java/org/codemc/worldguardwrapper/implementation/v7fawe/flag/WrappedStatusFlag.java create mode 100644 implementation/v7fawe/src/main/java/org/codemc/worldguardwrapper/implementation/v7fawe/region/WrappedRegion.java diff --git a/.travis.yml b/.travis.yml index 683051e..9f42049 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,7 +2,7 @@ sudo: false dist: trusty language: java jdk: - - oraclejdk8 + - oraclejdk8 cache: - directories: - - '$HOME/.m2/repository' + directories: + - '$HOME/.m2/repository' diff --git a/api/pom.xml b/api/pom.xml index ce72445..1d9e419 100644 --- a/api/pom.xml +++ b/api/pom.xml @@ -7,7 +7,7 @@ org.codemc.worldguardwrapper worldguardwrapper-parent - 1.1.2-SNAPSHOT + 1.1.3-SNAPSHOT worldguardwrapper-api diff --git a/api/src/main/java/org/codemc/worldguardwrapper/region/IWrappedRegion.java b/api/src/main/java/org/codemc/worldguardwrapper/region/IWrappedRegion.java index 8d7df85..53c8ed0 100644 --- a/api/src/main/java/org/codemc/worldguardwrapper/region/IWrappedRegion.java +++ b/api/src/main/java/org/codemc/worldguardwrapper/region/IWrappedRegion.java @@ -4,7 +4,6 @@ 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 { diff --git a/implementation/pom.xml b/implementation/pom.xml index 6675187..12ca5d3 100644 --- a/implementation/pom.xml +++ b/implementation/pom.xml @@ -7,7 +7,7 @@ org.codemc.worldguardwrapper worldguardwrapper-parent - 1.1.2-SNAPSHOT + 1.1.3-SNAPSHOT worldguardwrapper-implementation @@ -16,6 +16,7 @@ v6 v7 + v7fawe WorldGuardWrapper-Implementation @@ -23,14 +24,4 @@ true - - - - sk89q-repo - http://maven.sk89q.com/repo/ - - always - - - diff --git a/implementation/v6/pom.xml b/implementation/v6/pom.xml index 6d90abc..13a0850 100644 --- a/implementation/v6/pom.xml +++ b/implementation/v6/pom.xml @@ -7,18 +7,28 @@ org.codemc.worldguardwrapper worldguardwrapper-implementation - 1.1.2-SNAPSHOT + 1.1.3-SNAPSHOT worldguardwrapper-implementation-v6 WorldGuardWrapper-Implementation-V6 + + + sk89q-repo + http://maven.sk89q.com/repo/ + + always + + + + ${project.groupId} worldguardwrapper-api - 1.1.2-SNAPSHOT + 1.1.3-SNAPSHOT provided diff --git a/implementation/v6/src/main/java/org/codemc/worldguardwrapper/implementation/v6/WorldGuardImplementation.java b/implementation/v6/src/main/java/org/codemc/worldguardwrapper/implementation/v6/WorldGuardImplementation.java index 78ac6c4..2852a71 100644 --- a/implementation/v6/src/main/java/org/codemc/worldguardwrapper/implementation/v6/WorldGuardImplementation.java +++ b/implementation/v6/src/main/java/org/codemc/worldguardwrapper/implementation/v6/WorldGuardImplementation.java @@ -20,8 +20,8 @@ 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.v6.flag.WrappedPrimitiveFlag; import org.codemc.worldguardwrapper.implementation.v6.flag.AbstractWrappedFlag; +import org.codemc.worldguardwrapper.implementation.v6.flag.WrappedPrimitiveFlag; import org.codemc.worldguardwrapper.implementation.v6.flag.WrappedStatusFlag; import org.codemc.worldguardwrapper.implementation.v6.region.WrappedRegion; import org.codemc.worldguardwrapper.implementation.v6.utility.WorldGuardVectorUtilities; diff --git a/implementation/v6/src/main/java/org/codemc/worldguardwrapper/implementation/v6/region/WrappedRegion.java b/implementation/v6/src/main/java/org/codemc/worldguardwrapper/implementation/v6/region/WrappedRegion.java index 5f1b534..75cfcb8 100644 --- a/implementation/v6/src/main/java/org/codemc/worldguardwrapper/implementation/v6/region/WrappedRegion.java +++ b/implementation/v6/src/main/java/org/codemc/worldguardwrapper/implementation/v6/region/WrappedRegion.java @@ -16,7 +16,9 @@ import org.codemc.worldguardwrapper.selection.ICuboidSelection; import org.codemc.worldguardwrapper.selection.IPolygonalSelection; import org.codemc.worldguardwrapper.selection.ISelection; -import java.util.*; +import java.util.Optional; +import java.util.Set; +import java.util.UUID; import java.util.stream.Collectors; @AllArgsConstructor diff --git a/implementation/v7/pom.xml b/implementation/v7/pom.xml index 520d702..e33e671 100644 --- a/implementation/v7/pom.xml +++ b/implementation/v7/pom.xml @@ -7,18 +7,28 @@ org.codemc.worldguardwrapper worldguardwrapper-implementation - 1.1.2-SNAPSHOT + 1.1.3-SNAPSHOT worldguardwrapper-implementation-v7 WorldGuardWrapper-Implementation-V7 + + + sk89q-repo + http://maven.sk89q.com/repo/ + + always + + + + ${project.groupId} worldguardwrapper-api - 1.1.2-SNAPSHOT + 1.1.3-SNAPSHOT provided diff --git a/implementation/v7fawe/pom.xml b/implementation/v7fawe/pom.xml new file mode 100644 index 0000000..fea0c9a --- /dev/null +++ b/implementation/v7fawe/pom.xml @@ -0,0 +1,66 @@ + + + 4.0.0 + + + org.codemc.worldguardwrapper + worldguardwrapper-implementation + 1.1.3-SNAPSHOT + + + worldguardwrapper-implementation-v7-fawe + + WorldGuardWrapper-Implementation-V7-FAWE + + + + fawe-repo + http://ci.athion.net/job/FAWE-1.13/ws/mvn/ + + always + + + + worldguard-repo + http://ci.athion.net/job/FAWE-WorldGuard-1.13/ws/mvn/ + + always + + + + + + + ${project.groupId} + worldguardwrapper-api + 1.1.3-SNAPSHOT + provided + + + com.boydti + fawe-api + latest + provided + + + com.boydti + fawe-bukkit + latest + provided + + + com.sk89q + worldguard-core + latest + provided + + + com.sk89q + worldguard-legacy + latest + provided + + + diff --git a/implementation/v7fawe/src/main/java/org/codemc/worldguardwrapper/implementation/v7fawe/WorldGuardImplementation.java b/implementation/v7fawe/src/main/java/org/codemc/worldguardwrapper/implementation/v7fawe/WorldGuardImplementation.java new file mode 100644 index 0000000..d67d91d --- /dev/null +++ b/implementation/v7fawe/src/main/java/org/codemc/worldguardwrapper/implementation/v7fawe/WorldGuardImplementation.java @@ -0,0 +1,223 @@ +package org.codemc.worldguardwrapper.implementation.v7fawe; + +import com.sk89q.worldedit.BlockVector; +import com.sk89q.worldedit.BlockVector2D; +import com.sk89q.worldedit.bukkit.BukkitAdapter; +import com.sk89q.worldedit.bukkit.BukkitWorld; +import com.sk89q.worldguard.LocalPlayer; +import com.sk89q.worldguard.WorldGuard; +import com.sk89q.worldguard.bukkit.WorldGuardPlugin; +import com.sk89q.worldguard.protection.ApplicableRegionSet; +import com.sk89q.worldguard.protection.flags.*; +import com.sk89q.worldguard.protection.flags.registry.FlagConflictException; +import com.sk89q.worldguard.protection.flags.registry.FlagRegistry; +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.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.v7fawe.flag.AbstractWrappedFlag; +import org.codemc.worldguardwrapper.implementation.v7fawe.flag.WrappedPrimitiveFlag; +import org.codemc.worldguardwrapper.implementation.v7fawe.flag.WrappedStatusFlag; +import org.codemc.worldguardwrapper.implementation.v7fawe.region.WrappedRegion; +import org.codemc.worldguardwrapper.region.IWrappedRegion; + +import java.util.*; +import java.util.stream.Collectors; + +@NoArgsConstructor +public class WorldGuardImplementation implements IWorldGuardImplementation { + + private final WorldGuard core = WorldGuard.getInstance(); + private final FlagRegistry flagRegistry = core.getFlagRegistry(); + private final WorldGuardPlugin plugin = WorldGuardPlugin.inst(); + + public static BlockVector asBlockVector(@NonNull Location location) { + return new BlockVector(BukkitAdapter.asVector(location)); + } + + private Optional wrapPlayer(Player player) { + return Optional.ofNullable(player).map(bukkitPlayer -> plugin.wrapPlayer(player)); + } + + private Optional getWorldManager(@NonNull World world) { + return Optional.ofNullable(core.getPlatform().getRegionContainer().get(BukkitAdapter.adapt(world))); + } + + private Optional getApplicableRegions(@NonNull Location location) { + return getWorldManager(location.getWorld()).map(manager -> manager.getApplicableRegions(asBlockVector(location))); + } + + private Optional getApplicableRegions(@NonNull Location minimum, @NonNull Location maximum) { + return getWorldManager(minimum.getWorld()).map(manager -> manager.getApplicableRegions( + new ProtectedCuboidRegion("temp", asBlockVector(minimum), asBlockVector(maximum)))); + } + + private Optional queryValue(Player player, @NonNull Location location, @NonNull Flag flag) { + return getApplicableRegions(location).map(applicableRegions -> applicableRegions.queryValue(wrapPlayer(player) + .orElse(null), flag)); + } + + // TODO: find a better way to define wrapper mappings and register mappings + @SuppressWarnings("unchecked") + private IWrappedFlag wrap(Flag flag, Class type) { + final IWrappedFlag wrappedFlag; + if (type.equals(WrappedState.class)) { + wrappedFlag = (IWrappedFlag) new WrappedStatusFlag((Flag) flag); + } else if (type.equals(Boolean.class) || type.equals(boolean.class)) { + wrappedFlag = new WrappedPrimitiveFlag(flag); + } else if (type.equals(Double.class) || type.equals(double.class)) { + wrappedFlag = new WrappedPrimitiveFlag(flag); + } else if (type.equals(Enum.class)) { + wrappedFlag = new WrappedPrimitiveFlag(flag); + } else if (type.equals(Integer.class) || type.equals(int.class)) { + wrappedFlag = new WrappedPrimitiveFlag(flag); + } else if (type.equals(Location.class)) { + wrappedFlag = new WrappedPrimitiveFlag(flag); + } else if (type.equals(String.class)) { + wrappedFlag = new WrappedPrimitiveFlag(flag); + } else if (type.equals(Vector.class)) { + wrappedFlag = new WrappedPrimitiveFlag(flag); + } else { + throw new IllegalArgumentException("Unsupported flag type " + type.getName()); + } + return wrappedFlag; + } + + @Override + public JavaPlugin getWorldGuardPlugin() { + return WorldGuardPlugin.inst(); + } + + @Override + public int getApiVersion() { + return 7; + } + + @Override + public Optional> getFlag(String name, Class type) { + return Optional.ofNullable(flagRegistry.get(name)) + .map(flag -> wrap(flag, type)); + } + + @Override + public Optional queryFlag(Player player, Location location, IWrappedFlag flag) { + AbstractWrappedFlag wrappedFlag = (AbstractWrappedFlag) flag; + return wrappedFlag.fromWGValue(queryValue(player, location, wrappedFlag.getHandle())); + } + + @SuppressWarnings("unchecked") + @Override + public Optional> registerFlag(String name, Class type, T defaultValue) { + final Flag flag; + if (type.equals(WrappedState.class)) { + flag = new StateFlag(name, defaultValue == WrappedState.ALLOW); + } else if (type.equals(Boolean.class) || type.equals(boolean.class)) { + flag = new BooleanFlag(name); + } else if (type.equals(Double.class) || type.equals(double.class)) { + flag = new DoubleFlag(name); + } else if (type.equals(Enum.class)) { + flag = new EnumFlag(name, type); + } else if (type.equals(Integer.class) || type.equals(int.class)) { + flag = new IntegerFlag(name); + } else if (type.equals(Location.class)) { + flag = new LocationFlag(name); + } else if (type.equals(String.class)) { + flag = new StringFlag(name, (String) defaultValue); + } else if (type.equals(Vector.class)) { + flag = new VectorFlag(name); + } else { + throw new IllegalArgumentException("Unsupported flag type " + type.getName()); + } + try { + flagRegistry.register(flag); + return Optional.of(wrap(flag, type)); + } catch (FlagConflictException ignored) { + } + return Optional.empty(); + } + + @Override + public Optional getRegion(World world, String id) { + return getWorldManager(world) + .map(regionManager -> regionManager.getRegion(id)) + .map(region -> new WrappedRegion(world, region)); + } + + @Override + public Map getRegions(World world) { + RegionManager regionManager = core.getPlatform().getRegionContainer().get(new BukkitWorld(world)); + if (regionManager == null) { + return Collections.emptyMap(); + } + + Map regions = regionManager.getRegions(); + Map map = new HashMap<>(); + regions.forEach((name, region) -> map.put(name, new WrappedRegion(world, region))); + return map; + } + + @Override + public Set getRegions(Location location) { + ApplicableRegionSet regionSet = getApplicableRegions(location).orElse(null); + if (regionSet == null) { + return Collections.emptySet(); + } + + return regionSet.getRegions().stream() + .map(region -> new WrappedRegion(location.getWorld(), region)) + .collect(Collectors.toSet()); + } + + @Override + public Set getRegions(Location minimum, Location maximum) { + ApplicableRegionSet regionSet = getApplicableRegions(minimum, maximum).orElse(null); + if (regionSet == null) { + return Collections.emptySet(); + } + + return regionSet.getRegions().stream() + .map(region -> new WrappedRegion(minimum.getWorld(), region)) + .collect(Collectors.toSet()); + } + + @Override + public Optional addRegion(String id, List points, int minY, int maxY) { + ProtectedRegion region; + World world = points.get(0).getWorld(); + if (points.size() == 2) { + region = new ProtectedCuboidRegion(id, asBlockVector(points.get(0)), + asBlockVector(points.get(1))); + } else { + List vectorPoints = points.stream() + .map(location -> asBlockVector(location).toBlockVector2D()) + .collect(Collectors.toList()); + + region = new ProtectedPolygonalRegion(id, vectorPoints, minY, maxY); + } + + Optional manager = getWorldManager(world); + if (manager.isPresent()) { + manager.get().addRegion(region); + return Optional.of(new WrappedRegion(world, region)); + } else { + return Optional.empty(); + } + } + + @Override + public Optional> removeRegion(World world, String id) { + Optional> set = getWorldManager(world).map(manager -> manager.removeRegion(id)); + return set.map(protectedRegions -> protectedRegions.stream() + .map(region -> new WrappedRegion(world, region)).collect(Collectors.toSet())); + } +} diff --git a/implementation/v7fawe/src/main/java/org/codemc/worldguardwrapper/implementation/v7fawe/event/EventListener.java b/implementation/v7fawe/src/main/java/org/codemc/worldguardwrapper/implementation/v7fawe/event/EventListener.java new file mode 100644 index 0000000..71f52b2 --- /dev/null +++ b/implementation/v7fawe/src/main/java/org/codemc/worldguardwrapper/implementation/v7fawe/event/EventListener.java @@ -0,0 +1,100 @@ +package org.codemc.worldguardwrapper.implementation.v7fawe.event; + +import com.sk89q.worldguard.bukkit.event.block.UseBlockEvent; +import com.sk89q.worldguard.bukkit.event.entity.DamageEntityEvent; +import com.sk89q.worldguard.bukkit.event.entity.UseEntityEvent; +import com.sk89q.worldguard.bukkit.protection.events.DisallowedPVPEvent; +import lombok.NoArgsConstructor; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +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.*; + +@NoArgsConstructor +public class EventListener implements Listener { + + @EventHandler(priority = EventPriority.LOW) + public void onUseBlock(UseBlockEvent worldGuardEvent) { + Player player = worldGuardEvent.getCause().getFirstPlayer(); + if (player == null) { + // Only forward player events for now + return; + } + + AbstractWrappedEvent event = new WrappedUseBlockEvent( + worldGuardEvent.getOriginalEvent(), + player, worldGuardEvent.getWorld(), + worldGuardEvent.getBlocks(), + worldGuardEvent.getEffectiveMaterial()); + Bukkit.getServer().getPluginManager().callEvent(event); + + if (event.getResult() != Result.DEFAULT) { + // DEFAULT = Result probably has not been touched by the handler, + // so don't touch the original result either. + worldGuardEvent.setResult(event.getResult()); + } + } + + @EventHandler(priority = EventPriority.LOW) + public void onUseEntity(UseEntityEvent worldGuardEvent) { + Player player = worldGuardEvent.getCause().getFirstPlayer(); + if (player == null) { + // Only forward player events for now + return; + } + + AbstractWrappedEvent event = new WrappedUseEntityEvent( + worldGuardEvent.getOriginalEvent(), + player, + worldGuardEvent.getTarget(), + worldGuardEvent.getEntity()); + Bukkit.getServer().getPluginManager().callEvent(event); + + if (event.getResult() != Result.DEFAULT) { + // DEFAULT = Result probably has not been touched by the handler, + // so don't touch the original result either. + worldGuardEvent.setResult(event.getResult()); + } + } + + @EventHandler(priority = EventPriority.LOW) + public void onDamageEntity(DamageEntityEvent worldGuardEvent) { + Player player = worldGuardEvent.getCause().getFirstPlayer(); + if (player == null) { + // Only forward player events for now + return; + } + + AbstractWrappedEvent event = new WrappedDamageEntityEvent( + worldGuardEvent.getOriginalEvent(), + player, + worldGuardEvent.getTarget(), + worldGuardEvent.getEntity()); + Bukkit.getServer().getPluginManager().callEvent(event); + + if (event.getResult() != Result.DEFAULT) { + // DEFAULT = Result probably has not been touched by the handler, + // so don't touch the original result either. + worldGuardEvent.setResult(event.getResult()); + } + } + + @EventHandler(priority = EventPriority.LOW) + public void onDisallowedPVP(DisallowedPVPEvent worldGuardEvent) { + AbstractWrappedEvent event = new WrappedDisallowedPVPEvent( + worldGuardEvent.getAttacker(), + worldGuardEvent.getDefender(), + worldGuardEvent.getCause()); + Bukkit.getServer().getPluginManager().callEvent(event); + + if (event.getResult() != Result.DEFAULT) { + // DEFAULT = Result probably has not been touched by the handler, + // so don't touch the original result either. + worldGuardEvent.setCancelled(event.getResult() == Result.DENY); + } + } + +} diff --git a/implementation/v7fawe/src/main/java/org/codemc/worldguardwrapper/implementation/v7fawe/flag/AbstractWrappedFlag.java b/implementation/v7fawe/src/main/java/org/codemc/worldguardwrapper/implementation/v7fawe/flag/AbstractWrappedFlag.java new file mode 100644 index 0000000..7b819c4 --- /dev/null +++ b/implementation/v7fawe/src/main/java/org/codemc/worldguardwrapper/implementation/v7fawe/flag/AbstractWrappedFlag.java @@ -0,0 +1,30 @@ +package org.codemc.worldguardwrapper.implementation.v7fawe.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 abstract class AbstractWrappedFlag implements IWrappedFlag { + + private final Flag handle; + + @Override + public String getName() { + return handle.getName(); + } + + public abstract Optional fromWGValue(Object value); + + public abstract Optional fromWrapperValue(T value); + + @Override + public Optional getDefaultValue() { + return fromWGValue(handle.getDefault()); + } + +} diff --git a/implementation/v7fawe/src/main/java/org/codemc/worldguardwrapper/implementation/v7fawe/flag/WrappedPrimitiveFlag.java b/implementation/v7fawe/src/main/java/org/codemc/worldguardwrapper/implementation/v7fawe/flag/WrappedPrimitiveFlag.java new file mode 100644 index 0000000..36c8d3b --- /dev/null +++ b/implementation/v7fawe/src/main/java/org/codemc/worldguardwrapper/implementation/v7fawe/flag/WrappedPrimitiveFlag.java @@ -0,0 +1,23 @@ +package org.codemc.worldguardwrapper.implementation.v7fawe.flag; + +import com.sk89q.worldguard.protection.flags.Flag; + +import java.util.Optional; + +public class WrappedPrimitiveFlag extends AbstractWrappedFlag { + + public WrappedPrimitiveFlag(Flag handle) { + super(handle); + } + + @Override + public Optional fromWGValue(Object value) { + return Optional.ofNullable((T) value); + } + + @Override + public Optional fromWrapperValue(T value) { + return Optional.ofNullable(value); + } + +} diff --git a/implementation/v7fawe/src/main/java/org/codemc/worldguardwrapper/implementation/v7fawe/flag/WrappedStatusFlag.java b/implementation/v7fawe/src/main/java/org/codemc/worldguardwrapper/implementation/v7fawe/flag/WrappedStatusFlag.java new file mode 100644 index 0000000..def467c --- /dev/null +++ b/implementation/v7fawe/src/main/java/org/codemc/worldguardwrapper/implementation/v7fawe/flag/WrappedStatusFlag.java @@ -0,0 +1,28 @@ +package org.codemc.worldguardwrapper.implementation.v7fawe.flag; + +import com.sk89q.worldguard.protection.flags.Flag; +import com.sk89q.worldguard.protection.flags.StateFlag; +import org.codemc.worldguardwrapper.flag.IWrappedStatusFlag; +import org.codemc.worldguardwrapper.flag.WrappedState; + +import java.util.Optional; + +public class WrappedStatusFlag extends AbstractWrappedFlag implements IWrappedStatusFlag { + + public WrappedStatusFlag(Flag handle) { + super(handle); + } + + @Override + public Optional fromWGValue(Object value) { + return Optional.ofNullable(value) + .map(state -> state == StateFlag.State.ALLOW ? WrappedState.ALLOW : WrappedState.DENY); + } + + @Override + public Optional fromWrapperValue(WrappedState value) { + return Optional.ofNullable(value) + .map(state -> state == WrappedState.ALLOW ? StateFlag.State.ALLOW : StateFlag.State.DENY); + } + +} diff --git a/implementation/v7fawe/src/main/java/org/codemc/worldguardwrapper/implementation/v7fawe/region/WrappedRegion.java b/implementation/v7fawe/src/main/java/org/codemc/worldguardwrapper/implementation/v7fawe/region/WrappedRegion.java new file mode 100644 index 0000000..72c3e7e --- /dev/null +++ b/implementation/v7fawe/src/main/java/org/codemc/worldguardwrapper/implementation/v7fawe/region/WrappedRegion.java @@ -0,0 +1,169 @@ +package org.codemc.worldguardwrapper.implementation.v7fawe.region; + +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.v7fawe.WorldGuardImplementation; +import org.codemc.worldguardwrapper.implementation.v7fawe.flag.AbstractWrappedFlag; +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.Optional; +import java.util.Set; +import java.util.UUID; +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 getPoints() { + return handle.getPoints().stream() + .map(blockVector2D -> blockVector2D.toVector(0)) + .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 Optional getFlag(IWrappedFlag flag) { + AbstractWrappedFlag wrappedFlag = (AbstractWrappedFlag) flag; + return Optional.ofNullable(handle.getFlag(wrappedFlag.getHandle())) + .map(value -> (T) wrappedFlag.fromWGValue(value)); + } + + @SuppressWarnings("unchecked") + @Override + public void setFlag(IWrappedFlag flag, T value) { + AbstractWrappedFlag wrappedFlag = (AbstractWrappedFlag) flag; + handle.setFlag((Flag) wrappedFlag.getHandle(), wrappedFlag.fromWrapperValue(value).orElse(null)); + } + + @Override + public int getPriority() { + return handle.getPriority(); + } + + @Override + public IWrappedDomain getOwners() { + return new IWrappedDomain() { + @Override + public Set 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 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 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 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(WorldGuardImplementation.asBlockVector(location)); + } + +} diff --git a/library/pom.xml b/library/pom.xml index db9b878..96f2a08 100644 --- a/library/pom.xml +++ b/library/pom.xml @@ -7,7 +7,7 @@ org.codemc.worldguardwrapper worldguardwrapper-parent - 1.1.2-SNAPSHOT + 1.1.3-SNAPSHOT worldguardwrapper @@ -18,17 +18,22 @@ ${project.groupId} worldguardwrapper-api - 1.1.2-SNAPSHOT + 1.1.3-SNAPSHOT ${project.groupId} worldguardwrapper-implementation-v6 - 1.1.2-SNAPSHOT + 1.1.3-SNAPSHOT ${project.groupId} worldguardwrapper-implementation-v7 - 1.1.2-SNAPSHOT + 1.1.3-SNAPSHOT + + + ${project.groupId} + worldguardwrapper-implementation-v7-fawe + 1.1.3-SNAPSHOT diff --git a/library/src/main/java/org/codemc/worldguardwrapper/WorldGuardWrapper.java b/library/src/main/java/org/codemc/worldguardwrapper/WorldGuardWrapper.java index da8ebf0..c607e95 100644 --- a/library/src/main/java/org/codemc/worldguardwrapper/WorldGuardWrapper.java +++ b/library/src/main/java/org/codemc/worldguardwrapper/WorldGuardWrapper.java @@ -38,8 +38,13 @@ public class WorldGuardWrapper implements IWorldGuardImplementation { implementation = new org.codemc.worldguardwrapper.implementation.v6.WorldGuardImplementation(); listener = new org.codemc.worldguardwrapper.implementation.v6.event.EventListener(); } else { - implementation = new org.codemc.worldguardwrapper.implementation.v7.WorldGuardImplementation(); - listener = new org.codemc.worldguardwrapper.implementation.v7.event.EventListener(); + if (Bukkit.getPluginManager().isPluginEnabled("FastAsyncWorldEdit")) { + implementation = new org.codemc.worldguardwrapper.implementation.v7fawe.WorldGuardImplementation(); + listener = new org.codemc.worldguardwrapper.implementation.v7fawe.event.EventListener(); + } else { + implementation = new org.codemc.worldguardwrapper.implementation.v7.WorldGuardImplementation(); + listener = new org.codemc.worldguardwrapper.implementation.v7.event.EventListener(); + } } } diff --git a/pom.xml b/pom.xml index e7f7e80..9c44404 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ org.codemc.worldguardwrapper worldguardwrapper-parent - 1.1.2-SNAPSHOT + 1.1.3-SNAPSHOT pom