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)