From b08825e13edf4fc7f23b5d8dda8c6039356f7296 Mon Sep 17 00:00:00 2001 From: Brendan Grieve Date: Wed, 22 Aug 2018 12:42:52 +0800 Subject: [PATCH] Add new MaskManager: WorldguardFlag --- .../com/boydti/fawe/bukkit/FaweBukkit.java | 1 + .../fawe/bukkit/regions/WorldguardFlag.java | 112 ++++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/regions/WorldguardFlag.java diff --git a/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/FaweBukkit.java b/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/FaweBukkit.java index f7bc90e4b..7767dce14 100644 --- a/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/FaweBukkit.java +++ b/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/FaweBukkit.java @@ -467,6 +467,7 @@ public class FaweBukkit implements IFawe, Listener { if ((worldguardPlugin != null) && worldguardPlugin.isEnabled()) { try { managers.add(new Worldguard(worldguardPlugin, this)); + managers.add(new WorldguardFlag(worldguardPlugin, this)); Fawe.debug("Plugin 'WorldGuard' found. Using it now."); } catch (final Throwable e) { MainUtil.handleError(e); diff --git a/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/regions/WorldguardFlag.java b/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/regions/WorldguardFlag.java new file mode 100644 index 000000000..21e673f41 --- /dev/null +++ b/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/regions/WorldguardFlag.java @@ -0,0 +1,112 @@ +package com.boydti.fawe.bukkit.regions; + +import com.boydti.fawe.bukkit.FaweBukkit; +import com.boydti.fawe.bukkit.filter.WorldGuardFilter; +import com.boydti.fawe.object.FawePlayer; +import com.boydti.fawe.regions.FaweMask; +import com.boydti.fawe.regions.general.RegionFilter; +import com.sk89q.worldedit.Vector; +import com.sk89q.worldedit.regions.AbstractRegion; +import com.sk89q.worldguard.LocalPlayer; +import com.sk89q.worldguard.WorldGuard; +import com.sk89q.worldguard.bukkit.WorldGuardPlugin; +import com.sk89q.worldguard.protection.flags.Flags; +import com.sk89q.worldguard.protection.managers.RegionManager; +import com.sk89q.worldguard.protection.regions.*; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.bukkit.event.Listener; +import org.bukkit.plugin.Plugin; + +import java.util.Map; + +public class WorldguardFlag extends BukkitMaskManager implements Listener { + private WorldGuardPlugin worldguard; + FaweBukkit plugin; + + public WorldguardFlag(final Plugin p2, final FaweBukkit p3) { + super("worldguardflag"); + this.worldguard = (WorldGuardPlugin) p2; // this.getWorldGuard(); + this.plugin = p3; + } + + @Override + public FaweMask getMask(FawePlayer fp, MaskType type) { + final Player player = fp.parent; + final LocalPlayer localplayer = this.worldguard.wrapPlayer(player); + final RegionContainer container = WorldGuard.getInstance().getPlatform().getRegionContainer(); + final RegionManager manager = container.get(fp.getWorld()); + + return new FaweMask(new ManagerRegion(manager, localplayer), null) { + @Override + public boolean isValid(FawePlayer player, MaskType type) { + // We rely on the region mask instead of this + return true; + } + }; + } + + @Override + public RegionFilter getFilter(String world) { + return new WorldGuardFilter(Bukkit.getWorld(world)); + } + + /*** + * ManagerRegion wraps a RegionManager and will provide results based upon the regions enclosed + */ + private static class ManagerRegion extends AbstractRegion { + private final RegionManager manager; + private final LocalPlayer localplayer; + + ManagerRegion(RegionManager manager, LocalPlayer localplayer) { + super(null); + this.manager = manager; + this.localplayer = localplayer; + } + + @Override + public Vector getMinimumPoint() { + Vector point = null; + for (Map.Entry entry : manager.getRegions().entrySet()) { + Vector p = entry.getValue().getMinimumPoint(); + if (point == null) { + point = p; + continue; + } + point = Vector.getMinimum(point, p); + } + return point; + } + + @Override + public Vector getMaximumPoint() { + Vector point = null; + for (Map.Entry entry : manager.getRegions().entrySet()) { + Vector p = entry.getValue().getMaximumPoint(); + if (point == null) { + point = p; + continue; + } + point = Vector.getMaximum(point, p); + } + return point; + } + + @Override + public void expand(Vector... changes) { + throw new UnsupportedOperationException("Region is immutable"); + } + + @Override + public void contract(Vector... changes) { + throw new UnsupportedOperationException("Region is immutable"); + } + + @Override + public boolean contains(Vector position) { + // Make sure that all these flags are not denied. Denies override allows. WorldGuardExtraFlags can add Flags.WORLDEDIT + return manager.getApplicableRegions(position).testState(localplayer, Flags.BUILD, Flags.BLOCK_PLACE, Flags.BLOCK_BREAK); + } + + } +}