From 5162ce3dbd404fd141c9057795f2d8c0110b6b39 Mon Sep 17 00:00:00 2001 From: JOO200 Date: Thu, 30 Jul 2020 20:24:18 +0200 Subject: [PATCH] Implement new alternative association mode to limit piston movement --- .../bukkit/BukkitWorldConfiguration.java | 2 ++ .../bukkit/listener/AbstractListener.java | 5 ++-- .../worldguard/config/WorldConfiguration.java | 1 + .../DelayedRegionOverlapAssociation.java | 27 ++++++++++++++----- 4 files changed, 27 insertions(+), 8 deletions(-) diff --git a/worldguard-bukkit/src/main/java/com/sk89q/worldguard/bukkit/BukkitWorldConfiguration.java b/worldguard-bukkit/src/main/java/com/sk89q/worldguard/bukkit/BukkitWorldConfiguration.java index bb4336cc..f8e83b1d 100644 --- a/worldguard-bukkit/src/main/java/com/sk89q/worldguard/bukkit/BukkitWorldConfiguration.java +++ b/worldguard-bukkit/src/main/java/com/sk89q/worldguard/bukkit/BukkitWorldConfiguration.java @@ -160,6 +160,8 @@ public void loadConfiguration() { disableExpDrops = getBoolean("protection.disable-xp-orb-drops", false); disableObsidianGenerators = getBoolean("protection.disable-obsidian-generators", false); + useBetterAssociationMode = getBoolean("protection.useBetterAssociationMode", false); + blockPotions = new HashSet<>(); for (String potionName : getStringList("gameplay.block-potions", null)) { PotionEffectType effect = PotionEffectType.getByName(potionName); diff --git a/worldguard-bukkit/src/main/java/com/sk89q/worldguard/bukkit/listener/AbstractListener.java b/worldguard-bukkit/src/main/java/com/sk89q/worldguard/bukkit/listener/AbstractListener.java index 749e6351..a844543a 100644 --- a/worldguard-bukkit/src/main/java/com/sk89q/worldguard/bukkit/listener/AbstractListener.java +++ b/worldguard-bukkit/src/main/java/com/sk89q/worldguard/bukkit/listener/AbstractListener.java @@ -137,10 +137,11 @@ protected RegionAssociable createRegionAssociable(Cause cause) { } else { loc = entity.getLocation(); } - return new DelayedRegionOverlapAssociation(query, BukkitAdapter.adapt(loc)); + return new DelayedRegionOverlapAssociation(query, BukkitAdapter.adapt(loc.getWorld()), BukkitAdapter.adapt(loc)); } else if (rootCause instanceof Block) { RegionQuery query = WorldGuard.getInstance().getPlatform().getRegionContainer().createQuery(); - return new DelayedRegionOverlapAssociation(query, BukkitAdapter.adapt(((Block) rootCause).getLocation())); + Location loc = ((Block) rootCause).getLocation(); + return new DelayedRegionOverlapAssociation(query, BukkitAdapter.adapt(loc.getWorld()), BukkitAdapter.adapt(loc)); } else { return Associables.constant(Association.NON_MEMBER); } diff --git a/worldguard-core/src/main/java/com/sk89q/worldguard/config/WorldConfiguration.java b/worldguard-core/src/main/java/com/sk89q/worldguard/config/WorldConfiguration.java index 97bf513e..ebb71056 100644 --- a/worldguard-core/src/main/java/com/sk89q/worldguard/config/WorldConfiguration.java +++ b/worldguard-core/src/main/java/com/sk89q/worldguard/config/WorldConfiguration.java @@ -171,6 +171,7 @@ public abstract class WorldConfiguration { public boolean strictEntitySpawn; public boolean ignoreHopperMoveEvents; public boolean breakDeniedHoppers; + public boolean useBetterAssociationMode; protected Map maxRegionCounts; /** diff --git a/worldguard-core/src/main/java/com/sk89q/worldguard/protection/DelayedRegionOverlapAssociation.java b/worldguard-core/src/main/java/com/sk89q/worldguard/protection/DelayedRegionOverlapAssociation.java index cc75ae10..426ac5c3 100644 --- a/worldguard-core/src/main/java/com/sk89q/worldguard/protection/DelayedRegionOverlapAssociation.java +++ b/worldguard-core/src/main/java/com/sk89q/worldguard/protection/DelayedRegionOverlapAssociation.java @@ -20,11 +20,13 @@ package com.sk89q.worldguard.protection; import com.sk89q.worldedit.util.Location; -import com.sk89q.worldguard.protection.regions.RegionQuery; +import com.sk89q.worldedit.world.World; +import com.sk89q.worldguard.WorldGuard; +import com.sk89q.worldguard.config.ConfigurationManager; import com.sk89q.worldguard.domains.Association; -import com.sk89q.worldguard.protection.ApplicableRegionSet; import com.sk89q.worldguard.protection.association.RegionAssociable; import com.sk89q.worldguard.protection.regions.ProtectedRegion; +import com.sk89q.worldguard.protection.regions.RegionQuery; import javax.annotation.Nullable; import java.util.List; @@ -45,18 +47,21 @@ public class DelayedRegionOverlapAssociation implements RegionAssociable { private final Location location; @Nullable private Set source; + private boolean betterAssociationMode; /** * Create a new instance. - * - * @param query the query + * @param query the query + * @param world the world * @param location the location */ - public DelayedRegionOverlapAssociation(RegionQuery query, Location location) { + public DelayedRegionOverlapAssociation(RegionQuery query, World world, Location location) { checkNotNull(query); checkNotNull(location); this.query = query; this.location = location; + ConfigurationManager cfg = WorldGuard.getInstance().getPlatform().getGlobalStateManager(); + betterAssociationMode = cfg.get(world).useBetterAssociationMode; } @Override @@ -67,9 +72,19 @@ public Association getAssociation(List regions) { } for (ProtectedRegion region : regions) { - if ((region.getId().equals(ProtectedRegion.GLOBAL_REGION) && source.isEmpty()) || source.contains(region)) { + if ((region.getId().equals(ProtectedRegion.GLOBAL_REGION) && source.isEmpty())) { return Association.OWNER; } + + if (source.contains(region)) { + if (betterAssociationMode) { + int priority = region.getPriority(); + int maxPriority = source.stream().mapToInt(ProtectedRegion::getPriority).max().orElse(0); + if (priority == maxPriority) return Association.OWNER; + } else { + return Association.OWNER; + } + } } return Association.NON_MEMBER;