Compare commits

...
4 Commits
Author SHA1 Message Date
Maddy Miller f395a1675b Release 7.0.19 2026-09-18 22:09:17 +10:00
Brokkonaut 0aeb9d8d54 Fix lock flag restoration after session reinitialization (#2304) 2026-09-18 21:18:27 +10:00
Rasmus Kjær Damgaard 7c280a1348 Cancel blocked natural spawns before the entity is created (#2300)
* Cancel blocked natural spawns before the entity is created

The mob-spawning and deny-spawn checks in WorldGuardEntityListener fire
on CreatureSpawnEvent, at the very end of the spawn pipeline. By that
point the server has picked a spawn position, run the placement checks,
constructed the mob and run finalizeSpawn, and the cancelled mob is
thrown away. Since a cancelled spawn never counts toward the mob cap,
the natural spawner keeps retrying the same area at full rate, so
regions that deny mob spawning become permanent spawn attempt hotspots
that pay entity construction over and over for nothing.

On Paper servers the same checks can run in PreCreatureSpawnEvent,
before the entity exists. Cancelling there also ends the remaining
attempts for the chunk in that spawn cycle, so the wasted work is gone
almost entirely. Measured on a flat test world with a region denying
mob-spawning over the whole spawn range and the mob cap kept empty, the
attempt rate around a single player collapsed from roughly 75000
attempts per second to roughly 60 per second, with no entities
constructed at all.

The new listener mirrors the natural spawn conditions from
onCreatureSpawn exactly: activity halt, block-creature-spawn,
mob-spawning and deny-spawn region flags, and block-ground-slimes. All
other spawn reasons keep going through the existing CreatureSpawnEvent
checks unchanged, and the listener is only registered when the Paper
event class is present.

* Move the pre spawn handler into the existing Paper inner listener

Review feedback: platform specific code lives in the Paper inner
classes of the main listeners, and WorldGuardEntityListener already has
one for EntityZapEvent, so the handler moves in there and the separate
listener class is gone.

* Match the HIGH priority of the other protection handlers

* Extract the shared spawn checks into a method used by both handlers

The pre spawn handler duplicated the tail of onCreatureSpawn. Both now
call handleCreatureSpawn, which takes the event as a Cancellable and
the location, entity type and spawn reason as parameters, following
the same shape as handlePigZap. The entity specific checks (plugin
spawning, armor stands, tamed animals) stay in onCreatureSpawn since
no entity exists yet at pre spawn time, and the activity halt check
stays in each handler so its ordering relative to those checks is
unchanged.

* Pass cfg and wcfg into the shared spawn checks

Both handlers already have them, so the shared method no longer does a
second lookup.

* Correct the rationale on the pre spawn handler

The comment claimed that cancelling ends the chunk's remaining spawn
attempts. It does not. Only setShouldAbortSpawn(true) makes the spawner
return early; a plain cancel falls through to the next candidate position
exactly like a failed placement check.

What the handler does buy is stated accurately instead: the placement
checks, the entity construction and finalizeSpawn are skipped for a spawn
that was going to be refused.

Two consequences a server operator should know are now named. The region
query runs for every candidate position rather than every constructed mob,
because Paper fires the event before the vanilla suitability checks. And on
Paper with per-player-mob-spawns enabled, every cancelled pre spawn is
charged to a per player mob backoff counter, so a large denied region can
suppress spawning in neighbouring chunks that allow mobs.

* Add a config option for the pre spawn handler

regions.use-pre-creature-spawn-event, default true, matching how
regions.use-creature-spawn-event already gates the CreatureSpawnEvent
path. Turning it off leaves the existing handler doing all the work, so
a server that sees a regression can revert without downgrading.
2026-08-11 21:21:44 +10:00
Vladimir Turov c90c483862 Add Cause tracking for SulfurCube (#2295) 2026-08-02 18:20:44 +10:00
12 changed files with 108 additions and 22 deletions

No files matched your search

+7
View File
@@ -1,5 +1,12 @@
# Changelog
## 7.0.19
* Require 26.2+.
* Improved handling for TNT inside of sulfur cubes.
* Improve performance of mob spawn prevention on Paper servers in some situations.
* Improved handling of certain flags when `/wg flushstates` is run, or in certain login situations.
## 7.0.18
* Add lava-harden flag for cobblestone/stone/obsidian/basalt forming.
+1 -1
View File
@@ -1,4 +1,4 @@
group=com.sk89q.worldguard
version=7.0.19-SNAPSHOT
version=7.0.19
org.gradle.parallel=true
@@ -40,6 +40,7 @@ import org.bukkit.entity.Firework;
import org.bukkit.entity.LightningStrike;
import org.bukkit.entity.Player;
import org.bukkit.entity.Projectile;
import org.bukkit.entity.SulfurCube;
import org.bukkit.entity.TNTPrimed;
import org.bukkit.entity.Tameable;
import org.bukkit.entity.Vehicle;
@@ -117,7 +118,7 @@ public final class Cause {
return false;
}
if (object instanceof TNTPrimed || object instanceof Vehicle) {
if (object instanceof TNTPrimed || object instanceof Vehicle || object instanceof SulfurCube) {
if (!PaperLib.isPaper()) {
return false;
}
@@ -83,6 +83,7 @@ import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Mob;
import org.bukkit.entity.Painting;
import org.bukkit.entity.Player;
import org.bukkit.entity.SulfurCube;
import org.bukkit.entity.Tameable;
import org.bukkit.entity.ThrownPotion;
import org.bukkit.entity.WindCharge;
@@ -895,7 +896,18 @@ public class EventAbstractionListener extends AbstractListener {
if (matchingItem != null && hasInteractBypass(world, matchingItem)) {
useEntityEvent.setAllowed(true);
}
Events.fireToCancel(event, useEntityEvent);
if (!Events.fireToCancel(event, useEntityEvent)) {
/* Sulfur cube doesn't have API for interaction/ignition source tracking */
if(entity instanceof SulfurCube) {
/* Remove player in case of using shears */
if(item.getType() == Material.SHEARS) {
Cause.untrackParentCause(entity);
/* Save player overwise */
} else {
Cause.trackParentCause(entity, player);
}
}
}
}
@EventHandler(ignoreCancelled = true)
@@ -917,6 +929,8 @@ public class EventAbstractionListener extends AbstractListener {
eventToFire.getRelevantFlags().add(Flags.FIREWORK_DAMAGE);
} else if (damager instanceof Creeper) {
eventToFire.getRelevantFlags().add(Flags.CREEPER_EXPLOSION);
} else if (damager instanceof SulfurCube) {
eventToFire.getRelevantFlags().add(Flags.TNT);
}
if (Events.fireToCancel(event, eventToFire)) {
if (damager instanceof Tameable && damager instanceof Mob) {
@@ -57,6 +57,7 @@ import org.bukkit.entity.ExperienceOrb;
import org.bukkit.entity.Item;
import org.bukkit.entity.ItemFrame;
import org.bukkit.entity.Player;
import org.bukkit.entity.SulfurCube;
import org.bukkit.entity.Tameable;
import org.bukkit.event.Event;
import org.bukkit.event.Event.Result;
@@ -222,7 +223,7 @@ public class RegionProtectionListener extends AbstractListener {
String what;
/* TNT */
if (event.getCause().find(EntityType.TNT, EntityType.TNT_MINECART) != null) {
if (event.getCause().find(EntityType.TNT, EntityType.TNT_MINECART, EntityType.SULFUR_CUBE) != null) {
canBreak = query.testBuild(BukkitAdapter.adapt(target), associable, combine(event, Flags.BLOCK_BREAK, Flags.TNT));
what = "use dynamite";
@@ -20,6 +20,7 @@
package com.sk89q.worldguard.bukkit.listener;
import com.destroystokyo.paper.event.entity.EntityZapEvent;
import com.destroystokyo.paper.event.entity.PreCreatureSpawnEvent;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldguard.LocalPlayer;
@@ -61,6 +62,7 @@ import org.bukkit.entity.ItemFrame;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Projectile;
import org.bukkit.entity.SulfurCube;
import org.bukkit.entity.TNTPrimed;
import org.bukkit.entity.Tameable;
import org.bukkit.entity.WindCharge;
@@ -487,7 +489,7 @@ public class WorldGuardEntityListener extends AbstractListener {
event.blockList().clear();
return;
}
} else if (ent instanceof TNTPrimed || ent instanceof ExplosiveMinecart) {
} else if (ent instanceof TNTPrimed || ent instanceof ExplosiveMinecart || ent instanceof SulfurCube) {
if (wcfg.blockTNTExplosions) {
event.setCancelled(true);
return;
@@ -615,7 +617,8 @@ public class WorldGuardEntityListener extends AbstractListener {
return;
}
} else if (event.getEntityType() == EntityType.TNT
|| event.getEntityType() == EntityType.TNT_MINECART) {
|| event.getEntityType() == EntityType.TNT_MINECART
|| event.getEntityType() == EntityType.SULFUR_CUBE) {
if (wcfg.blockTNTExplosions) {
event.setCancelled(true);
return;
@@ -655,8 +658,11 @@ public class WorldGuardEntityListener extends AbstractListener {
return;
}
EntityType entityType = event.getEntityType();
handleCreatureSpawn(event, event.getLocation(), event.getEntityType(), event.getSpawnReason(), cfg, wcfg);
}
private static void handleCreatureSpawn(Cancellable event, Location location, EntityType entityType, SpawnReason spawnReason,
ConfigurationManager cfg, WorldConfiguration wcfg) {
com.sk89q.worldedit.world.entity.EntityType weEntityType = BukkitAdapter.adapt(entityType);
if (weEntityType != null && wcfg.blockCreatureSpawn.contains(weEntityType)) {
@@ -664,11 +670,9 @@ public class WorldGuardEntityListener extends AbstractListener {
return;
}
Location eventLoc = event.getLocation();
if (wcfg.useRegions && cfg.useRegionsCreatureSpawnEvent) {
ApplicableRegionSet set =
WorldGuard.getInstance().getPlatform().getRegionContainer().createQuery().getApplicableRegions(BukkitAdapter.adapt(eventLoc));
WorldGuard.getInstance().getPlatform().getRegionContainer().createQuery().getApplicableRegions(BukkitAdapter.adapt(location));
if (!set.testState(null, Flags.MOB_SPAWNING)) {
event.setCancelled(true);
@@ -683,8 +687,8 @@ public class WorldGuardEntityListener extends AbstractListener {
}
if (wcfg.blockGroundSlimes && entityType == EntityType.SLIME
&& eventLoc.getY() >= 60
&& event.getSpawnReason() == SpawnReason.NATURAL) {
&& location.getY() >= 60
&& spawnReason == SpawnReason.NATURAL) {
event.setCancelled(true);
return;
}
@@ -905,6 +909,53 @@ public class WorldGuardEntityListener extends AbstractListener {
handlePigZap(event.getEntity(), event);
}
}
/**
* Applies the natural spawn checks from {@link WorldGuardEntityListener#onCreatureSpawn} before the
* server constructs the entity. The CreatureSpawnEvent checks fire at the very
* end of the spawn pipeline, after the position was picked, the placement
* checks ran and the mob was constructed and finalized, so a region that denies
* mob spawning pays for a mob to be built and thrown away on every attempt.
* Cancelling here skips the placement checks, the construction and
* finalizeSpawn for a spawn that was going to be refused anyway.
*
* Cancelling does not end the chunk's remaining attempts. Only
* setShouldAbortSpawn(true) makes the spawner return early; a plain cancel
* falls through to the next candidate position exactly like a failed
* placement check.
*
* Note that Paper fires this event for every candidate position, before the
* light, block and collision checks, so the region query below runs
* considerably more often than the CreatureSpawnEvent one did. On Paper with
* per-player-mob-spawns enabled, which is the default, every cancelled pre
* spawn is also charged to a per player mob backoff counter that is added to
* the mob cap of every player within tick view distance and bleeds off one per
* spawn cycle, so a large denied region can suppress spawning in neighbouring
* chunks that allow mobs.
*
* Only NATURAL spawns are handled here; every other spawn reason keeps going
* through the CreatureSpawnEvent checks unchanged.
*/
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onPreCreatureSpawn(PreCreatureSpawnEvent event) {
if (event.getReason() != SpawnReason.NATURAL) {
return;
}
ConfigurationManager cfg = getConfig();
if (!cfg.useRegionsPreCreatureSpawnEvent) {
return;
}
if (cfg.activityHaltToggle) {
event.setCancelled(true);
return;
}
Location spawnLoc = event.getSpawnLocation();
handleCreatureSpawn(event, spawnLoc, event.getType(), event.getReason(), cfg, getWorldConfig(spawnLoc.getWorld()));
}
}
private static class SpigotListener implements Listener {
@@ -43,8 +43,8 @@ import java.util.function.Consumer;
public class BukkitSessionManager extends AbstractSessionManager implements Runnable, Listener {
/**
* Re-initialize handlers and clear "last position," "last state," etc.
* information for all players.
* Restore handler-managed state, re-initialize handlers, and clear
* "last position," "last state," etc. information for all players.
*/
@Override
@SuppressWarnings({"rawtypes", "unchecked"})
@@ -72,9 +72,14 @@ public class BukkitSessionManager extends AbstractSessionManager implements Runn
@EventHandler
public void onPlayerProcess(ProcessPlayerEvent event) {
// Pre-load a session
// Create a new session or re-initialize a cached one.
LocalPlayer player = WorldGuardPlugin.inst().wrapPlayer(event.getPlayer());
get(player).initialize(player);
Session session = getIfPresent(player);
if (session == null) {
get(player); // this also initializes the new session
} else {
session.resetState(player); // old sessions need explicit re-initialization
}
}
@Override
@@ -43,6 +43,7 @@ import org.bukkit.entity.Player;
import org.bukkit.entity.Projectile;
import org.bukkit.entity.SpectralArrow;
import org.bukkit.entity.Steerable;
import org.bukkit.entity.SulfurCube;
import org.bukkit.entity.TNTPrimed;
import org.bukkit.entity.Tameable;
import org.bukkit.entity.Vehicle;
@@ -78,7 +79,9 @@ public final class Entities {
* @return true if TNT based
*/
public static boolean isTNTBased(Entity entity) {
return entity instanceof TNTPrimed || entity instanceof ExplosiveMinecart;
return entity instanceof TNTPrimed
|| entity instanceof ExplosiveMinecart
|| entity instanceof SulfurCube sulfurCube && sulfurCube.canExplode();
}
/**
@@ -265,6 +268,7 @@ public final class Entities {
case Wither wither -> Flags.WITHER_DAMAGE;
case Creeper creeper -> Flags.CREEPER_EXPLOSION;
case TNTPrimed tnt -> Flags.TNT;
case SulfurCube sulfurCube -> Flags.TNT;
case ExplosiveMinecart minecart -> Flags.TNT;
case EnderDragon dragon -> Flags.ENDERDRAGON_BLOCK_DAMAGE;
case null, default -> Flags.OTHER_EXPLOSION;
@@ -65,6 +65,7 @@ public abstract class ConfigurationManager {
"#\r\n";
public boolean useRegionsCreatureSpawnEvent;
public boolean useRegionsPreCreatureSpawnEvent;
public boolean activityHaltToggle = false;
public boolean useGodPermission;
public boolean useGodGroup;
@@ -56,6 +56,7 @@ public abstract class YamlConfigurationManager extends ConfigurationManager {
migrateRegionsToUuid = config.getBoolean("regions.uuid-migration.perform-on-next-start", true);
keepUnresolvedNames = config.getBoolean("regions.uuid-migration.keep-names-that-lack-uuids", true);
useRegionsCreatureSpawnEvent = config.getBoolean("regions.use-creature-spawn-event", true);
useRegionsPreCreatureSpawnEvent = config.getBoolean("regions.use-pre-creature-spawn-event", true);
disableDefaultBypass = config.getBoolean("regions.disable-bypass-by-default", false);
announceBypassStatus = config.getBoolean("regions.announce-bypass-status", false);
@@ -161,11 +161,12 @@ public class Session {
}
/**
* Re-initialize the session.
* Restore handler-managed state and then re-initialize the session.
*
* @param player The player
*/
public void resetState(LocalPlayer player) {
uninitialize(player);
initialize(player);
needRefresh.set(true);
}
@@ -41,14 +41,14 @@ public interface SessionManager {
boolean hasBypass(LocalPlayer player, World world);
/**
* Re-initialize handlers and clear "last position," "last state," etc.
* information for all players.
* Restore handler-managed state, re-initialize handlers, and clear
* "last position," "last state," etc. information for all players.
*/
void resetAllStates();
/**
* Re-initialize handlers and clear "last position," "last state," etc.
* information.
* Restore handler-managed state, re-initialize handlers, and clear
* "last position," "last state," etc. information.
*
* @param player The player
*/