From 4c20b61eb331b545321d8bf50a4793d4a81912af Mon Sep 17 00:00:00 2001 From: Daniil Zao Date: Sun, 14 Jul 2024 18:30:01 +0300 Subject: [PATCH 1/4] [SKIP-CI] Add some basic readme --- README.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..c218bac --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# WGExtender Updated ([Releases](https://github.com/imDaniX/WGExtender/releases/latest) | [Dev Builds](https://github.com/imDaniX/WGExtender/actions?query=branch%3Amaster)) +A fork of [WGExtender](https://github.com/Shevchik/WorldGuardExtender) that aims to support latest versions of Paper. +It also adds configurable minimal region claim size limits. From 155fa07cc928a310c142bd4ff7341a46f2ae67d4 Mon Sep 17 00:00:00 2001 From: Daniil Zao Date: Sun, 14 Jul 2024 18:32:56 +0300 Subject: [PATCH 2/4] [SKIP-CI] Move downloads down --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c218bac..ed3d5b6 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,4 @@ -# WGExtender Updated ([Releases](https://github.com/imDaniX/WGExtender/releases/latest) | [Dev Builds](https://github.com/imDaniX/WGExtender/actions?query=branch%3Amaster)) +# WGExtender Fork A fork of [WGExtender](https://github.com/Shevchik/WorldGuardExtender) that aims to support latest versions of Paper. It also adds configurable minimal region claim size limits. +# ([Releases](https://github.com/imDaniX/WGExtender/releases/latest) | [Dev Builds](https://github.com/imDaniX/WGExtender/actions?query=branch%3Amaster)) \ No newline at end of file From a05ef47ddf69404a530d69d17a39b1266ee1b044 Mon Sep 17 00:00:00 2001 From: "Daniil Zao." Date: Fri, 9 Aug 2024 16:46:06 +0300 Subject: [PATCH 3/4] [SKIP-CI] Just change README a little --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ed3d5b6..a568475 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ # WGExtender Fork A fork of [WGExtender](https://github.com/Shevchik/WorldGuardExtender) that aims to support latest versions of Paper. It also adds configurable minimal region claim size limits. -# ([Releases](https://github.com/imDaniX/WGExtender/releases/latest) | [Dev Builds](https://github.com/imDaniX/WGExtender/actions?query=branch%3Amaster)) \ No newline at end of file +# [Releases](https://github.com/imDaniX/WGExtender/releases/latest) | [Dev Builds](https://github.com/imDaniX/WGExtender/actions?query=branch%3Amaster) \ No newline at end of file From 1506e56fca7ad3bb62a849da3ccfc3bbdfb04637 Mon Sep 17 00:00:00 2001 From: "Daniil Zao." Date: Sun, 1 Sep 2024 20:53:20 +0300 Subject: [PATCH 4/4] Some fixes regarding 1.16 and outdated PvP listener --- src/wgextender/WGExtender.java | 2 +- .../features/flags/OldPVPFlagsHandler.java | 7 ++- .../regionprotect/WGOverrideListener.java | 46 -------------- .../ownormembased/PvPHandlingListener.java | 62 ++++++++++++++----- .../ownormembased/RestrictCommands.java | 5 +- 5 files changed, 57 insertions(+), 65 deletions(-) delete mode 100644 src/wgextender/features/regionprotect/WGOverrideListener.java diff --git a/src/wgextender/WGExtender.java b/src/wgextender/WGExtender.java index d7afaae..e6f4287 100644 --- a/src/wgextender/WGExtender.java +++ b/src/wgextender/WGExtender.java @@ -74,7 +74,7 @@ public void onEnable() { WGRegionCommandWrapper.inject(config); WEWandCommandWrapper.inject(config); pvplistener = new PvPHandlingListener(config); - pvplistener.inject(); + pvplistener.inject(this); oldpvphandler = new OldPVPFlagsHandler(); oldpvphandler.start(this); } catch (Throwable t) { diff --git a/src/wgextender/features/flags/OldPVPFlagsHandler.java b/src/wgextender/features/flags/OldPVPFlagsHandler.java index aa59e93..eade4d7 100644 --- a/src/wgextender/features/flags/OldPVPFlagsHandler.java +++ b/src/wgextender/features/flags/OldPVPFlagsHandler.java @@ -11,6 +11,7 @@ import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; +import org.bukkit.event.block.Action; import org.bukkit.event.entity.EntityDamageByEntityEvent; import org.bukkit.event.entity.EntityDamageEvent; import org.bukkit.event.entity.EntityDamageEvent.DamageModifier; @@ -110,7 +111,7 @@ public void onEntityDamageByEntity(EntityDamageByEntityEvent event) { @EventHandler(priority = EventPriority.LOWEST) public void onInteract(PlayerInteractEvent event) { - if (event.getAction().isRightClick() && + if (isRightClick(event.getAction()) && event.getHand() == EquipmentSlot.OFF_HAND && event.getItem() != null && event.getItem().getType() == Material.BOW && @@ -118,4 +119,8 @@ public void onInteract(PlayerInteractEvent event) { event.setCancelled(true); } } + + private static boolean isRightClick(Action action) { + return action == Action.RIGHT_CLICK_AIR || action == Action.RIGHT_CLICK_BLOCK; + } } diff --git a/src/wgextender/features/regionprotect/WGOverrideListener.java b/src/wgextender/features/regionprotect/WGOverrideListener.java deleted file mode 100644 index 79e7525..0000000 --- a/src/wgextender/features/regionprotect/WGOverrideListener.java +++ /dev/null @@ -1,46 +0,0 @@ -package wgextender.features.regionprotect; - -import it.unimi.dsi.fastutil.Pair; -import org.bukkit.Bukkit; -import org.bukkit.event.EventHandler; -import org.bukkit.event.HandlerList; -import org.bukkit.event.Listener; -import org.bukkit.plugin.RegisteredListener; -import wgextender.WGExtender; - -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.ArrayList; -import java.util.List; - -public abstract class WGOverrideListener implements Listener { - - private final List> overriddenEvents = new ArrayList<>(); - - public void inject() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { - for (Method method : getClass().getMethods()) { - if (method.isAnnotationPresent(EventHandler.class)) { - Class eventClass = method.getParameterTypes()[0]; - HandlerList hl = (HandlerList) eventClass.getMethod("getHandlerList").invoke(null); - for (RegisteredListener listener : hl.getRegisteredListeners()) { - if (listener.getListener().getClass() == getClassToReplace()) { - overriddenEvents.add(Pair.of(hl, listener)); - hl.unregister(listener); - } - } - } - } - Bukkit.getPluginManager().registerEvents(this, WGExtender.getInstance()); - } - - public void uninject() { - HandlerList.unregisterAll(this); - for (var pair : overriddenEvents) { - pair.first().register(pair.second()); - } - overriddenEvents.clear(); - } - - protected abstract Class getClassToReplace(); - -} diff --git a/src/wgextender/features/regionprotect/ownormembased/PvPHandlingListener.java b/src/wgextender/features/regionprotect/ownormembased/PvPHandlingListener.java index abd2331..4d6f91f 100644 --- a/src/wgextender/features/regionprotect/ownormembased/PvPHandlingListener.java +++ b/src/wgextender/features/regionprotect/ownormembased/PvPHandlingListener.java @@ -12,6 +12,8 @@ import com.sk89q.worldguard.bukkit.util.Entities; import com.sk89q.worldguard.bukkit.util.Events; import com.sk89q.worldguard.bukkit.util.InteropUtils; +import com.sk89q.worldguard.commands.CommandUtils; +import com.sk89q.worldguard.config.WorldConfiguration; import com.sk89q.worldguard.domains.Association; import com.sk89q.worldguard.protection.association.Associables; import com.sk89q.worldguard.protection.association.DelayedRegionOverlapAssociation; @@ -29,30 +31,51 @@ import org.bukkit.entity.Player; import org.bukkit.event.Event.Result; import org.bukkit.event.EventHandler; +import org.bukkit.event.HandlerList; import org.bukkit.event.Listener; +import org.bukkit.plugin.Plugin; +import org.bukkit.plugin.RegisteredListener; import wgextender.Config; -import wgextender.features.regionprotect.WGOverrideListener; import wgextender.utils.WGRegionUtils; import java.util.Arrays; import java.util.List; +import java.util.Objects; -public class PvPHandlingListener extends WGOverrideListener { +public class PvPHandlingListener implements Listener { private final Config config; private static final String DENY_MESSAGE_KEY = "worldguard.region.lastMessage"; private static final int LAST_MESSAGE_DELAY = 500; + private RegisteredListener origin; + public PvPHandlingListener(Config config) { this.config = config; } - @Override - protected Class getClassToReplace() { - return RegionProtectionListener.class; + public void inject(Plugin plugin) { + HandlerList handlers = DamageEntityEvent.getHandlerList(); + for (var listener : handlers.getRegisteredListeners()) { + if (listener.getListener().getClass().equals(RegionProtectionListener.class)) { + origin = listener; + break; + } + } + Objects.requireNonNull(origin, "Couldn't find the original RegionProtectionListener"); + handlers.unregister(origin); + plugin.getServer().getPluginManager().registerEvents(this, plugin); } + public void uninject() { + if (origin == null) return; + HandlerList handlers = DamageEntityEvent.getHandlerList(); + handlers.unregister(this); + handlers.register(origin); + origin = null; + } + @EventHandler(ignoreCancelled = true) public void onDamageEntity(DamageEntityEvent event) { if (event.getResult() == Result.ALLOW) { @@ -155,7 +178,7 @@ public void onDamageEntity(DamageEntityEvent event) { } } - private RegionAssociable createRegionAssociable(Cause cause) { + private static RegionAssociable createRegionAssociable(Cause cause) { if (!cause.isKnown()) { return Associables.constant(Association.NON_MEMBER); } @@ -175,14 +198,19 @@ private RegionAssociable createRegionAssociable(Cause cause) { private boolean isWhitelisted(Cause cause, World world, boolean pvp) { Object rootCause = cause.getRootCause(); + if (rootCause instanceof Block block) { Material type = block.getType(); return (type == Material.HOPPER) || (type == Material.DROPPER); } else if (rootCause instanceof Player player) { - if (WGRegionUtils.getWorldConfig(world).fakePlayerBuildOverride && InteropUtils.isFakePlayer(player)) { + WorldConfiguration config = WGRegionUtils.getWorldConfig(world); + + if (config.fakePlayerBuildOverride && InteropUtils.isFakePlayer(player)) { return true; } - return !pvp && WGRegionUtils.canBypassProtection(player); + + LocalPlayer localPlayer = WorldGuardPlugin.inst().wrapPlayer(player); + return !pvp && WGRegionUtils.getPlatform().getSessionManager().hasBypass(localPlayer, localPlayer.getWorld()); } else { return false; } @@ -196,17 +224,23 @@ private void tellErrorMessage(DelegateEvent event, Cause cause, Location locatio if (cause.getRootCause() instanceof Player player) { long now = System.currentTimeMillis(); Long lastTime = WGMetadata.getIfPresent(player, DENY_MESSAGE_KEY, Long.class); - if ((lastTime == null) || ((now - lastTime) >= LAST_MESSAGE_DELAY)) { - @SuppressWarnings("deprecation") - String message = WGRegionUtils.REGION_QUERY.queryValue(BukkitAdapter.adapt(location), WorldGuardPlugin.inst().wrapPlayer(player), Flags.DENY_MESSAGE); - if (message != null && !message.isEmpty()) { - player.sendMessage(message.replace("%what%", what)); - } + if (lastTime == null || now - lastTime >= LAST_MESSAGE_DELAY) { + RegionQuery query = WGRegionUtils.getPlatform().getRegionContainer().createQuery(); + LocalPlayer localPlayer = WorldGuardPlugin.inst().wrapPlayer(player); + String message = query.queryValue(BukkitAdapter.adapt(location), localPlayer, Flags.DENY_MESSAGE); + formatAndSendDenyMessage(what, localPlayer, message); WGMetadata.put(player, DENY_MESSAGE_KEY, now); } } } + private static void formatAndSendDenyMessage(String what, LocalPlayer localPlayer, String message) { + if (message == null || message.isEmpty()) return; + message = WGRegionUtils.getPlatform().getMatcher().replaceMacros(localPlayer, message); + message = CommandUtils.replaceColorMacros(message); + localPlayer.printRaw(message.replace("%what%", what)); + } + private static StateFlag[] combine(DelegateEvent event, StateFlag... flag) { List extra = event.getRelevantFlags(); StateFlag[] flags = Arrays.copyOf(flag, flag.length + extra.size()); diff --git a/src/wgextender/features/regionprotect/ownormembased/RestrictCommands.java b/src/wgextender/features/regionprotect/ownormembased/RestrictCommands.java index 1226607..69acae7 100644 --- a/src/wgextender/features/regionprotect/ownormembased/RestrictCommands.java +++ b/src/wgextender/features/regionprotect/ownormembased/RestrictCommands.java @@ -33,7 +33,6 @@ import java.util.HashSet; import java.util.Locale; import java.util.Set; -import java.util.concurrent.TimeUnit; import java.util.regex.Pattern; public class RestrictCommands implements Listener { @@ -46,7 +45,7 @@ public class RestrictCommands implements Listener { public RestrictCommands(Config config) { this.config = config; restrictedCommands = config.restrictedCommandsInRegion; - Bukkit.getAsyncScheduler().runAtFixedRate(WGExtender.getInstance(), (task) -> { + Bukkit.getScheduler().runTaskTimerAsynchronously(WGExtender.getInstance(), () -> { if (!config.restrictCommandsInRegionEnabled) { return; } @@ -59,7 +58,7 @@ public RestrictCommands(Config config) { } } restrictedCommands = computedRestrictedCommands; - }, TICK, TICK * 100, TimeUnit.MILLISECONDS); + }, 1, 100); } @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)