Skip to content

Commit

Permalink
Some fixes regarding 1.16 and outdated PvP listener
Browse files Browse the repository at this point in the history
  • Loading branch information
imDaniX committed Sep 1, 2024
1 parent a05ef47 commit 1506e56
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 65 deletions.
2 changes: 1 addition & 1 deletion src/wgextender/WGExtender.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
7 changes: 6 additions & 1 deletion src/wgextender/features/flags/OldPVPFlagsHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -110,12 +111,16 @@ 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 &&
WGRegionUtils.isFlagTrue(event.getPlayer().getLocation(), WGExtenderFlags.OLDPVP_NOBOW)) {
event.setCancelled(true);
}
}

private static boolean isRightClick(Action action) {
return action == Action.RIGHT_CLICK_AIR || action == Action.RIGHT_CLICK_BLOCK;
}
}
46 changes: 0 additions & 46 deletions src/wgextender/features/regionprotect/WGOverrideListener.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<? extends Listener> 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) {
Expand Down Expand Up @@ -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);
}
Expand All @@ -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;
}
Expand All @@ -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<StateFlag> extra = event.getRelevantFlags();
StateFlag[] flags = Arrays.copyOf(flag, flag.length + extra.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
}
Expand All @@ -59,7 +58,7 @@ public RestrictCommands(Config config) {
}
}
restrictedCommands = computedRestrictedCommands;
}, TICK, TICK * 100, TimeUnit.MILLISECONDS);
}, 1, 100);
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
Expand Down

0 comments on commit 1506e56

Please sign in to comment.