|
14 | 14 | import org.bukkit.event.entity.EntityBreedEvent; |
15 | 15 | import org.bukkit.event.entity.EntityPortalEvent; |
16 | 16 | import org.bukkit.event.entity.EntityRemoveEvent; |
| 17 | +import org.bukkit.event.block.Action; |
17 | 18 | import org.bukkit.event.hanging.HangingPlaceEvent; |
| 19 | +import org.bukkit.event.player.PlayerInteractEntityEvent; |
| 20 | +import org.bukkit.event.player.PlayerInteractEvent; |
18 | 21 | import org.bukkit.event.vehicle.VehicleCreateEvent; |
| 22 | +import org.bukkit.inventory.ItemStack; |
19 | 23 | import org.eclipse.jdt.annotation.Nullable; |
20 | 24 | import world.bentobox.bentobox.BentoBox; |
21 | 25 | import world.bentobox.bentobox.api.localization.TextVariables; |
@@ -153,6 +157,87 @@ public void onBlock(HangingPlaceEvent hangingPlaceEvent) { |
153 | 157 | }); |
154 | 158 | } |
155 | 159 |
|
| 160 | + /** |
| 161 | + * Deny spawn-egg use the moment a player would exceed an entity limit, before the egg is |
| 162 | + * consumed. The {@link CreatureSpawnEvent} path already cancels the over-limit spawn, but by |
| 163 | + * then the egg item has been used up; cancelling the interaction here keeps the egg (#134). |
| 164 | + */ |
| 165 | + @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) |
| 166 | + public void onSpawnEggUseOnBlock(PlayerInteractEvent e) { |
| 167 | + if (e.getAction() != Action.RIGHT_CLICK_BLOCK || e.getClickedBlock() == null) { |
| 168 | + return; |
| 169 | + } |
| 170 | + EntityType type = spawnEggType(e.getItem()); |
| 171 | + if (type != null) { |
| 172 | + checkSpawnEggLimit(e, e.getPlayer(), e.getClickedBlock().getLocation(), type); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) |
| 177 | + public void onSpawnEggUseOnEntity(PlayerInteractEntityEvent e) { |
| 178 | + if (e.getHand() == null) { |
| 179 | + return; |
| 180 | + } |
| 181 | + EntityType type = spawnEggType(e.getPlayer().getInventory().getItem(e.getHand())); |
| 182 | + if (type != null) { |
| 183 | + checkSpawnEggLimit(e, e.getPlayer(), e.getRightClicked().getLocation(), type); |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + private void checkSpawnEggLimit(Cancellable event, Player player, Location location, EntityType type) { |
| 188 | + if (!addon.inGameModeWorld(location.getWorld())) { |
| 189 | + return; |
| 190 | + } |
| 191 | + addon.getIslands().getIslandAt(location).ifPresent(island -> { |
| 192 | + boolean bypass = player.isOp() || player.hasPermission( |
| 193 | + addon.getPlugin().getIWM().getPermissionPrefix(location.getWorld()) + MOD_BYPASS); |
| 194 | + if (bypass || island.isSpawn()) { |
| 195 | + return; |
| 196 | + } |
| 197 | + AtLimitResult res = atLimit(island, type, envOf(location.getWorld())); |
| 198 | + if (res.hit()) { |
| 199 | + event.setCancelled(true); |
| 200 | + notifyEntityLimit(player, type, res); |
| 201 | + } |
| 202 | + }); |
| 203 | + } |
| 204 | + |
| 205 | + /** |
| 206 | + * Resolve the entity type a spawn-egg item would create, e.g. {@code PIG_SPAWN_EGG -> PIG}. |
| 207 | + * |
| 208 | + * @return the entity type, or {@code null} if the item is not a recognised spawn egg |
| 209 | + */ |
| 210 | + private EntityType spawnEggType(ItemStack item) { |
| 211 | + if (item == null) { |
| 212 | + return null; |
| 213 | + } |
| 214 | + String name = item.getType().name(); |
| 215 | + if (!name.endsWith("_SPAWN_EGG")) { |
| 216 | + return null; |
| 217 | + } |
| 218 | + try { |
| 219 | + return EntityType.valueOf(name.substring(0, name.length() - "_SPAWN_EGG".length())); |
| 220 | + } catch (IllegalArgumentException ex) { |
| 221 | + return null; |
| 222 | + } |
| 223 | + } |
| 224 | + |
| 225 | + private void notifyEntityLimit(Player player, EntityType type, AtLimitResult res) { |
| 226 | + if (res.getTypelimit() != null) { |
| 227 | + User.getInstance(player).notify("entity-limits.hit-limit", "[entity]", |
| 228 | + Util.prettifyText(type.toString()), TextVariables.NUMBER, |
| 229 | + String.valueOf(res.getTypelimit().getValue())); |
| 230 | + } else { |
| 231 | + User.getInstance(player).notify("entity-limits.hit-limit", "[entity]", |
| 232 | + res.getGrouplimit().getKey().getName() + " (" |
| 233 | + + res.getGrouplimit().getKey().getTypes().stream() |
| 234 | + .map(x -> Util.prettifyText(x.toString())) |
| 235 | + .collect(Collectors.joining(", ")) |
| 236 | + + ")", |
| 237 | + TextVariables.NUMBER, String.valueOf(res.getGrouplimit().getValue())); |
| 238 | + } |
| 239 | + } |
| 240 | + |
156 | 241 | /* ========================================================================= |
157 | 242 | * Increment handlers (MONITOR priority — count entities that actually spawned) |
158 | 243 | * ========================================================================= */ |
@@ -473,8 +558,10 @@ private void tellPlayers(Location location, Entity entity, SpawnReason spawnReas |
473 | 558 | * for the entity's current environment. |
474 | 559 | */ |
475 | 560 | AtLimitResult atLimit(Island island, Entity entity) { |
476 | | - Environment env = envOf(entity.getWorld()); |
477 | | - EntityType type = entity.getType(); |
| 561 | + return atLimit(island, entity.getType(), envOf(entity.getWorld())); |
| 562 | + } |
| 563 | + |
| 564 | + AtLimitResult atLimit(Island island, EntityType type, Environment env) { |
478 | 565 | @Nullable |
479 | 566 | IslandBlockCount ibc = addon.getBlockLimitListener().getIsland(island.getUniqueId()); |
480 | 567 |
|
|
0 commit comments