mirror of
https://github.com/EngineHub/WorldGuard.git
synced 2025-02-28 02:01:36 +01:00
Improve NPC handling in WorldGuard's listeners (#1988)
* fix: added more npc checks in listeners * Do not listen to events from NPCs such as Citizens. Don't handle NPCs as players. * replaced hasMetaData calls by Entities utilities class
This commit is contained in:
parent
1e81a91d4d
commit
ece376a69e
@ -60,6 +60,7 @@
|
||||
import com.sk89q.worldguard.bukkit.listener.WorldRulesListener;
|
||||
import com.sk89q.worldguard.bukkit.session.BukkitSessionManager;
|
||||
import com.sk89q.worldguard.bukkit.util.ClassSourceValidator;
|
||||
import com.sk89q.worldguard.bukkit.util.Entities;
|
||||
import com.sk89q.worldguard.bukkit.util.Events;
|
||||
import com.sk89q.worldguard.commands.GeneralCommands;
|
||||
import com.sk89q.worldguard.commands.ProtectionCommands;
|
||||
@ -419,8 +420,9 @@ public LocalPlayer wrapPlayer(Player player, boolean silenced) {
|
||||
}
|
||||
|
||||
public Actor wrapCommandSender(CommandSender sender) {
|
||||
if (sender instanceof Player) {
|
||||
return wrapPlayer((Player) sender);
|
||||
if (sender instanceof Player player) {
|
||||
if (Entities.isNPC(player)) return null;
|
||||
return wrapPlayer(player);
|
||||
}
|
||||
|
||||
try {
|
||||
|
@ -24,6 +24,7 @@
|
||||
import com.sk89q.worldguard.bukkit.BukkitWorldConfiguration;
|
||||
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
|
||||
import com.sk89q.worldguard.bukkit.internal.WGMetadata;
|
||||
import com.sk89q.worldguard.bukkit.util.Entities;
|
||||
import io.papermc.lib.PaperLib;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.block.Block;
|
||||
@ -134,8 +135,8 @@ public Object getRootCause() {
|
||||
@Nullable
|
||||
public Player getFirstPlayer() {
|
||||
for (Object object : causes) {
|
||||
if (object instanceof Player) {
|
||||
return (Player) object;
|
||||
if (object instanceof Player p && !Entities.isNPC(p)) {
|
||||
return p;
|
||||
}
|
||||
}
|
||||
|
||||
@ -145,8 +146,8 @@ public Player getFirstPlayer() {
|
||||
@Nullable
|
||||
public Entity getFirstEntity() {
|
||||
for (Object object : causes) {
|
||||
if (object instanceof Entity) {
|
||||
return (Entity) object;
|
||||
if (object instanceof Entity e) {
|
||||
return e;
|
||||
}
|
||||
}
|
||||
|
||||
@ -156,8 +157,8 @@ public Entity getFirstEntity() {
|
||||
@Nullable
|
||||
public Entity getFirstNonPlayerEntity() {
|
||||
for (Object object : causes) {
|
||||
if (object instanceof Entity && !(object instanceof Player)) {
|
||||
return (Entity) object;
|
||||
if (object instanceof Entity e && (!(object instanceof Player) || Entities.isNPC(e))) {
|
||||
return e;
|
||||
}
|
||||
}
|
||||
|
||||
@ -167,8 +168,8 @@ public Entity getFirstNonPlayerEntity() {
|
||||
@Nullable
|
||||
public Block getFirstBlock() {
|
||||
for (Object object : causes) {
|
||||
if (object instanceof Block) {
|
||||
return (Block) object;
|
||||
if (object instanceof Block b) {
|
||||
return b;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -29,6 +29,7 @@
|
||||
import com.sk89q.worldguard.bukkit.BukkitWorldConfiguration;
|
||||
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
|
||||
import com.sk89q.worldguard.bukkit.cause.Cause;
|
||||
import com.sk89q.worldguard.bukkit.util.Entities;
|
||||
import com.sk89q.worldguard.config.WorldConfiguration;
|
||||
import com.sk89q.worldguard.domains.Association;
|
||||
import com.sk89q.worldguard.protection.association.DelayedRegionOverlapAssociation;
|
||||
@ -124,13 +125,12 @@ protected RegionAssociable createRegionAssociable(Cause cause) {
|
||||
|
||||
if (!cause.isKnown()) {
|
||||
return Associables.constant(Association.NON_MEMBER);
|
||||
} else if (rootCause instanceof Player) {
|
||||
return getPlugin().wrapPlayer((Player) rootCause);
|
||||
} else if (rootCause instanceof OfflinePlayer) {
|
||||
return getPlugin().wrapOfflinePlayer((OfflinePlayer) rootCause);
|
||||
} else if (rootCause instanceof Entity) {
|
||||
} else if (rootCause instanceof Player player && !Entities.isNPC(player)) {
|
||||
return getPlugin().wrapPlayer(player);
|
||||
} else if (rootCause instanceof OfflinePlayer offlinePlayer) {
|
||||
return getPlugin().wrapOfflinePlayer(offlinePlayer);
|
||||
} else if (rootCause instanceof Entity entity) {
|
||||
RegionQuery query = WorldGuard.getInstance().getPlatform().getRegionContainer().createQuery();
|
||||
final Entity entity = (Entity) rootCause;
|
||||
BukkitWorldConfiguration config = getWorldConfig(entity.getWorld());
|
||||
Location loc;
|
||||
if (PaperLib.isPaper() && config.usePaperEntityOrigin) {
|
||||
@ -144,9 +144,9 @@ protected RegionAssociable createRegionAssociable(Cause cause) {
|
||||
}
|
||||
return new DelayedRegionOverlapAssociation(query, BukkitAdapter.adapt(loc),
|
||||
config.useMaxPriorityAssociation);
|
||||
} else if (rootCause instanceof Block) {
|
||||
} else if (rootCause instanceof Block block) {
|
||||
RegionQuery query = WorldGuard.getInstance().getPlatform().getRegionContainer().createQuery();
|
||||
Location loc = ((Block) rootCause).getLocation();
|
||||
Location loc = block.getLocation();
|
||||
return new DelayedRegionOverlapAssociation(query, BukkitAdapter.adapt(loc),
|
||||
getWorldConfig(loc.getWorld()).useMaxPriorityAssociation);
|
||||
} else {
|
||||
|
@ -22,6 +22,7 @@
|
||||
import com.sk89q.worldguard.LocalPlayer;
|
||||
import com.sk89q.worldguard.WorldGuard;
|
||||
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
|
||||
import com.sk89q.worldguard.bukkit.util.Entities;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.LivingEntity;
|
||||
import org.bukkit.entity.Player;
|
||||
@ -57,9 +58,9 @@ private boolean isInvincible(LocalPlayer player) {
|
||||
@EventHandler(ignoreCancelled = true)
|
||||
public void onEntityDamage(EntityDamageEvent event) {
|
||||
Entity victim = event.getEntity();
|
||||
if (Entities.isNPC(victim)) return;
|
||||
|
||||
if (victim instanceof Player) {
|
||||
Player player = (Player) victim;
|
||||
if (victim instanceof Player player) {
|
||||
LocalPlayer localPlayer = WorldGuardPlugin.inst().wrapPlayer(player);
|
||||
|
||||
if (isInvincible(localPlayer)) {
|
||||
@ -87,9 +88,9 @@ public void onEntityDamage(EntityDamageEvent event) {
|
||||
@EventHandler(ignoreCancelled = true)
|
||||
public void onEntityCombust(EntityCombustEvent event) {
|
||||
Entity entity = event.getEntity();
|
||||
if (Entities.isNPC(entity)) return;
|
||||
|
||||
if (entity instanceof Player) {
|
||||
Player player = (Player) entity;
|
||||
if (entity instanceof Player player) {
|
||||
LocalPlayer localPlayer = WorldGuardPlugin.inst().wrapPlayer(player);
|
||||
|
||||
if (isInvincible(localPlayer)) {
|
||||
@ -100,8 +101,9 @@ public void onEntityCombust(EntityCombustEvent event) {
|
||||
|
||||
@EventHandler(ignoreCancelled = true)
|
||||
public void onFoodLevelChange(FoodLevelChangeEvent event) {
|
||||
if (event.getEntity() instanceof Player) {
|
||||
Player player = (Player) event.getEntity();
|
||||
if (Entities.isNPC(event.getEntity())) return;
|
||||
|
||||
if (event.getEntity() instanceof Player player) {
|
||||
LocalPlayer localPlayer = WorldGuardPlugin.inst().wrapPlayer(player);
|
||||
|
||||
if (event.getFoodLevel() < player.getFoodLevel() && isInvincible(localPlayer)) {
|
||||
|
@ -25,6 +25,7 @@
|
||||
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
|
||||
import com.sk89q.worldguard.bukkit.event.block.BreakBlockEvent;
|
||||
import com.sk89q.worldguard.bukkit.event.block.PlaceBlockEvent;
|
||||
import com.sk89q.worldguard.bukkit.util.Entities;
|
||||
import com.sk89q.worldguard.bukkit.util.Materials;
|
||||
import com.sk89q.worldguard.config.WorldConfiguration;
|
||||
import com.sk89q.worldguard.protection.association.RegionAssociable;
|
||||
@ -118,24 +119,24 @@ public void onEntityDamage(EntityDamageEvent event) {
|
||||
World world = entity.getWorld();
|
||||
if (!isRegionSupportEnabled(world)) return; // Region support disabled
|
||||
|
||||
if (Entities.isNPC(entity)) return;
|
||||
if (!(entity instanceof Player player)) return;
|
||||
|
||||
RegionQuery query = WorldGuard.getInstance().getPlatform().getRegionContainer().createQuery();
|
||||
|
||||
if (entity instanceof Player && event.getCause() == DamageCause.FALL) {
|
||||
LocalPlayer localPlayer = WorldGuardPlugin.inst().wrapPlayer((Player) entity);
|
||||
if (event.getCause() == DamageCause.FALL) {
|
||||
LocalPlayer localPlayer = WorldGuardPlugin.inst().wrapPlayer(player);
|
||||
if (!query.testState(BukkitAdapter.adapt(entity.getLocation()), localPlayer, Flags.FALL_DAMAGE)) {
|
||||
event.setCancelled(true);
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
if (entity instanceof Player && event.getCause() == DamageCause.FLY_INTO_WALL) {
|
||||
LocalPlayer localPlayer = WorldGuardPlugin.inst().wrapPlayer((Player) entity);
|
||||
if (!query.testState(BukkitAdapter.adapt(entity.getLocation()), localPlayer, Flags.FALL_DAMAGE)) {
|
||||
event.setCancelled(true);
|
||||
return;
|
||||
}
|
||||
} else if (event.getCause() == DamageCause.FLY_INTO_WALL) {
|
||||
LocalPlayer localPlayer = WorldGuardPlugin.inst().wrapPlayer(player);
|
||||
if (!query.testState(BukkitAdapter.adapt(entity.getLocation()), localPlayer, Flags.FALL_DAMAGE)) {
|
||||
event.setCancelled(true);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -522,8 +522,7 @@ public void onVehicleExit(VehicleExitEvent event) {
|
||||
if (!isRegionSupportEnabled(vehicle.getWorld())) return; // Region support disabled
|
||||
Entity exited = event.getExited();
|
||||
|
||||
if (vehicle instanceof Tameable && exited instanceof Player) {
|
||||
Player player = (Player) exited;
|
||||
if (vehicle instanceof Tameable && exited instanceof Player player && !Entities.isNPC(player)) {
|
||||
LocalPlayer localPlayer = WorldGuardPlugin.inst().wrapPlayer(player);
|
||||
if (!isWhitelisted(Cause.create(player), vehicle.getWorld(), false)) {
|
||||
RegionQuery query = WorldGuard.getInstance().getPlatform().getRegionContainer().createQuery();
|
||||
|
@ -138,8 +138,7 @@ private void onEntityDamageByBlock(EntityDamageByBlockEvent event) {
|
||||
event.setCancelled(true);
|
||||
return;
|
||||
}
|
||||
} else if (defender instanceof Player) {
|
||||
Player player = (Player) defender;
|
||||
} else if (defender instanceof Player player && !Entities.isNPC(defender)) {
|
||||
LocalPlayer localPlayer = WorldGuardPlugin.inst().wrapPlayer(player);
|
||||
|
||||
if (wcfg.disableLavaDamage && type == DamageCause.LAVA) {
|
||||
@ -227,8 +226,7 @@ private void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
|
||||
}
|
||||
}
|
||||
|
||||
if (defender instanceof Player) {
|
||||
Player player = (Player) defender;
|
||||
if (defender instanceof Player player && !Entities.isNPC(defender)) {
|
||||
LocalPlayer localPlayer = getPlugin().wrapPlayer(player);
|
||||
|
||||
if (wcfg.disableLightningDamage && event.getCause() == DamageCause.LIGHTNING) {
|
||||
@ -289,8 +287,7 @@ private void onEntityDamageByProjectile(EntityDamageByEntityEvent event) {
|
||||
}
|
||||
|
||||
WorldConfiguration wcfg = getWorldConfig(defender.getWorld());
|
||||
if (defender instanceof Player) {
|
||||
Player player = (Player) defender;
|
||||
if (defender instanceof Player player && !Entities.isNPC(defender)) {
|
||||
LocalPlayer localPlayer = getPlugin().wrapPlayer(player);
|
||||
|
||||
|
||||
@ -363,8 +360,7 @@ public void onEntityDamage(EntityDamageEvent event) {
|
||||
event.setCancelled(true);
|
||||
return;
|
||||
}
|
||||
} else if (defender instanceof Player) {
|
||||
Player player = (Player) defender;
|
||||
} else if (defender instanceof Player player && !Entities.isNPC(defender)) {
|
||||
LocalPlayer localPlayer = WorldGuardPlugin.inst().wrapPlayer(player);
|
||||
|
||||
if (type == DamageCause.WITHER) {
|
||||
@ -665,8 +661,8 @@ public void onCreatePortal(PortalCreateEvent event) {
|
||||
.get(world);
|
||||
if (regionManager == null) return;
|
||||
LocalPlayer associable = null;
|
||||
if (event.getEntity() instanceof Player) {
|
||||
associable = getPlugin().wrapPlayer(((Player) event.getEntity()));
|
||||
if (event.getEntity() instanceof Player player) {
|
||||
associable = getPlugin().wrapPlayer(player);
|
||||
if (WorldGuard.getInstance().getPlatform().getSessionManager().hasBypass(associable, world)) {
|
||||
return;
|
||||
}
|
||||
@ -745,10 +741,10 @@ public void onEntityRegainHealth(EntityRegainHealthEvent event) {
|
||||
event.setCancelled(true);
|
||||
return;
|
||||
}
|
||||
if (wcfg.useRegions && ent instanceof Player
|
||||
if (wcfg.useRegions && ent instanceof Player player && !Entities.isNPC(ent)
|
||||
&& !WorldGuard.getInstance().getPlatform().getRegionContainer().createQuery().testState(
|
||||
BukkitAdapter.adapt(ent.getLocation()),
|
||||
WorldGuardPlugin.inst().wrapPlayer((Player) ent),
|
||||
WorldGuardPlugin.inst().wrapPlayer(player),
|
||||
Flags.HEALTH_REGEN)) {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
@ -758,10 +754,11 @@ public void onEntityRegainHealth(EntityRegainHealthEvent event) {
|
||||
public void onFoodChange(FoodLevelChangeEvent event) {
|
||||
if (event.getItem() != null) return;
|
||||
HumanEntity ent = event.getEntity();
|
||||
if (!(ent instanceof Player)) return;
|
||||
if (Entities.isNPC(ent)) return;
|
||||
if (!(ent instanceof Player bukkitPlayer)) return;
|
||||
if (event.getFoodLevel() > ent.getFoodLevel()) return;
|
||||
|
||||
LocalPlayer player = WorldGuardPlugin.inst().wrapPlayer((Player) ent);
|
||||
LocalPlayer player = WorldGuardPlugin.inst().wrapPlayer(bukkitPlayer);
|
||||
WorldConfiguration wcfg = getWorldConfig(ent.getWorld());
|
||||
|
||||
if (wcfg.useRegions
|
||||
|
@ -314,6 +314,7 @@ private void handlePhysicalInteract(PlayerInteractEvent event) {
|
||||
@EventHandler(priority = EventPriority.HIGHEST)
|
||||
public void onPlayerRespawn(PlayerRespawnEvent event) {
|
||||
Player player = event.getPlayer();
|
||||
if (com.sk89q.worldguard.bukkit.util.Entities.isNPC(player)) return;
|
||||
WorldConfiguration wcfg = getWorldConfig(player.getWorld());
|
||||
|
||||
if (wcfg.useRegions) {
|
||||
@ -348,6 +349,7 @@ public void onItemHeldChange(PlayerItemHeldEvent event) {
|
||||
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
|
||||
public void onPlayerTeleport(PlayerTeleportEvent event) {
|
||||
Player player = event.getPlayer();
|
||||
if (com.sk89q.worldguard.bukkit.util.Entities.isNPC(player)) return;
|
||||
LocalPlayer localPlayer = getPlugin().wrapPlayer(player);
|
||||
ConfigurationManager cfg = getConfig();
|
||||
WorldConfiguration wcfg = getWorldConfig(player.getWorld());
|
||||
|
@ -24,6 +24,7 @@
|
||||
import com.sk89q.worldguard.LocalPlayer;
|
||||
import com.sk89q.worldguard.WorldGuard;
|
||||
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
|
||||
import com.sk89q.worldguard.bukkit.util.Entities;
|
||||
import com.sk89q.worldguard.config.WorldConfiguration;
|
||||
import com.sk89q.worldguard.session.MoveType;
|
||||
import com.sk89q.worldguard.util.Locations;
|
||||
@ -59,6 +60,7 @@ public void onVehicleMove(VehicleMoveEvent event) {
|
||||
// Did we move a block?
|
||||
if (Locations.isDifferentBlock(BukkitAdapter.adapt(event.getFrom()), BukkitAdapter.adapt(event.getTo()))) {
|
||||
for (Player player : playerPassengers) {
|
||||
if (Entities.isNPC(player)) continue;
|
||||
LocalPlayer localPlayer = getPlugin().wrapPlayer(player);
|
||||
Location lastValid;
|
||||
if ((lastValid = WorldGuard.getInstance().getPlatform().getSessionManager().get(localPlayer)
|
||||
|
@ -25,6 +25,7 @@
|
||||
import com.sk89q.worldguard.bukkit.BukkitPlayer;
|
||||
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
|
||||
import com.sk89q.worldguard.bukkit.event.player.ProcessPlayerEvent;
|
||||
import com.sk89q.worldguard.bukkit.util.Entities;
|
||||
import com.sk89q.worldguard.session.AbstractSessionManager;
|
||||
import com.sk89q.worldguard.session.Session;
|
||||
import com.sk89q.worldguard.session.handler.Handler;
|
||||
@ -81,9 +82,9 @@ public void run() {
|
||||
|
||||
@Override
|
||||
public boolean hasBypass(LocalPlayer player, World world) {
|
||||
if (player instanceof BukkitPlayer) {
|
||||
if (((BukkitPlayer) player).getPlayer().hasMetadata("NPC")
|
||||
&& WorldGuard.getInstance().getPlatform().getGlobalStateManager().get(world).fakePlayerBuildOverride) {
|
||||
if (player instanceof BukkitPlayer bukkitPlayer) {
|
||||
if (Entities.isNPC(bukkitPlayer.getPlayer())
|
||||
&& WorldGuard.getInstance().getPlatform().getGlobalStateManager().get(world).fakePlayerBuildOverride) {
|
||||
return true;
|
||||
}
|
||||
if (!((BukkitPlayer) player).getPlayer().isOnline()) {
|
||||
|
@ -70,7 +70,7 @@ public static boolean isFakePlayer(Player player) {
|
||||
UUID uuid = player.getUniqueId();
|
||||
String name = player.getName();
|
||||
|
||||
if (player.hasMetadata("NPC")) {
|
||||
if (Entities.isNPC(player)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user