From ebe21c03294852a0ab058c82d1367d8a64f30e69 Mon Sep 17 00:00:00 2001 From: OmerBenGera Date: Sat, 16 Nov 2024 12:30:26 +0200 Subject: [PATCH] Adds the ability for plugins to have custom implementation for changing range of spawners --- .../api/hooks/SpawnersProvider.java | 16 ++++ .../api/managers/ProvidersManager.java | 7 ++ .../nms/v1_12_R1/NMSAdapterImpl.java | 76 +++++++++++++------ .../nms/v1_16_R3/NMSAdapterImpl.java | 55 +++++++++----- .../wildloaders/nms/v1_17/NMSAdapterImpl.java | 43 ++++++++--- .../wildloaders/nms/v1_18/NMSAdapterImpl.java | 43 ++++++++--- .../wildloaders/nms/v1_19/NMSAdapterImpl.java | 43 ++++++++--- .../nms/v1_20_3/NMSAdapterImpl.java | 43 ++++++++--- .../nms/v1_20_4/NMSAdapterImpl.java | 43 ++++++++--- .../wildloaders/nms/v1_21/NMSAdapterImpl.java | 43 ++++++++--- .../nms/v1_21_3/NMSAdapterImpl.java | 43 ++++++++--- .../nms/v1_7_R4/NMSAdapterImpl.java | 71 +++++++++++------ .../nms/v1_8_R3/NMSAdapterImpl.java | 70 ++++++++++++----- .../wildloaders/handlers/LoadersHandler.java | 6 +- .../handlers/ProvidersHandler.java | 17 +++++ .../wildloaders/listeners/BlocksListener.java | 3 +- .../wildloaders/loaders/WChunkLoader.java | 6 +- .../wildloaders/nms/NMSAdapter.java | 15 +++- .../utils/SpawnerChangeListener.java | 13 ++++ 19 files changed, 486 insertions(+), 170 deletions(-) create mode 100644 API/src/main/java/com/bgsoftware/wildloaders/api/hooks/SpawnersProvider.java create mode 100644 src/main/java/com/bgsoftware/wildloaders/utils/SpawnerChangeListener.java diff --git a/API/src/main/java/com/bgsoftware/wildloaders/api/hooks/SpawnersProvider.java b/API/src/main/java/com/bgsoftware/wildloaders/api/hooks/SpawnersProvider.java new file mode 100644 index 0000000..c8b3246 --- /dev/null +++ b/API/src/main/java/com/bgsoftware/wildloaders/api/hooks/SpawnersProvider.java @@ -0,0 +1,16 @@ +package com.bgsoftware.wildloaders.api.hooks; + +import org.bukkit.Location; + +public interface SpawnersProvider { + + /** + * Sets the required-player range for the spawner in {@param spawnerLocation}. + * + * @param spawnerLocation The location of the spawner to set the range to. + * @param requiredRange The range to set. + * Negative value means unlimited range. + */ + void setSpawnerRequiredRange(Location spawnerLocation, int requiredRange); + +} diff --git a/API/src/main/java/com/bgsoftware/wildloaders/api/managers/ProvidersManager.java b/API/src/main/java/com/bgsoftware/wildloaders/api/managers/ProvidersManager.java index ec8400e..afeb2aa 100644 --- a/API/src/main/java/com/bgsoftware/wildloaders/api/managers/ProvidersManager.java +++ b/API/src/main/java/com/bgsoftware/wildloaders/api/managers/ProvidersManager.java @@ -1,6 +1,7 @@ package com.bgsoftware.wildloaders.api.managers; import com.bgsoftware.wildloaders.api.hooks.ClaimsProvider; +import com.bgsoftware.wildloaders.api.hooks.SpawnersProvider; import com.bgsoftware.wildloaders.api.hooks.TickableProvider; import com.bgsoftware.wildloaders.api.hooks.WorldsProvider; @@ -12,6 +13,12 @@ public interface ProvidersManager { */ void addClaimsProvider(ClaimsProvider claimsProvider); + /** + * Add a spawners provider to the plugin. + * @param spawnersProvider The spawners provider to add. + */ + void addSpawnersProvider(SpawnersProvider spawnersProvider); + /** * Add a tickable provider to the plugin. * @param tickableProvider The tickable provider to add. diff --git a/NMS/v1_12_R1/src/main/java/com/bgsoftware/wildloaders/nms/v1_12_R1/NMSAdapterImpl.java b/NMS/v1_12_R1/src/main/java/com/bgsoftware/wildloaders/nms/v1_12_R1/NMSAdapterImpl.java index 04726cb..e6c6e23 100644 --- a/NMS/v1_12_R1/src/main/java/com/bgsoftware/wildloaders/nms/v1_12_R1/NMSAdapterImpl.java +++ b/NMS/v1_12_R1/src/main/java/com/bgsoftware/wildloaders/nms/v1_12_R1/NMSAdapterImpl.java @@ -17,12 +17,14 @@ import net.minecraft.server.v1_12_R1.NBTTagString; import net.minecraft.server.v1_12_R1.TileEntity; import net.minecraft.server.v1_12_R1.TileEntityMobSpawner; import net.minecraft.server.v1_12_R1.World; +import net.minecraft.server.v1_12_R1.WorldServer; import org.bukkit.Location; import org.bukkit.craftbukkit.v1_12_R1.CraftChunk; import org.bukkit.craftbukkit.v1_12_R1.CraftWorld; import org.bukkit.craftbukkit.v1_12_R1.inventory.CraftItemStack; import org.bukkit.craftbukkit.v1_12_R1.util.LongHash; +import javax.annotation.Nullable; import java.util.UUID; public final class NMSAdapterImpl implements NMSAdapter { @@ -106,27 +108,30 @@ public final class NMSAdapterImpl implements NMSAdapter { } @Override - public ITileEntityChunkLoader createLoader(ChunkLoader chunkLoader) { + public ITileEntityChunkLoader createLoader(ChunkLoader chunkLoader, + @Nullable OnSpawnerChangeCallback onSpawnerChangeCallback) { Location loaderLoc = chunkLoader.getLocation(); - World world = ((CraftWorld) loaderLoc.getWorld()).getHandle(); + WorldServer worldServer = ((CraftWorld) loaderLoc.getWorld()).getHandle(); BlockPosition blockPosition = new BlockPosition(loaderLoc.getX(), loaderLoc.getY(), loaderLoc.getZ()); - TileEntityChunkLoader tileEntityChunkLoader = new TileEntityChunkLoader(chunkLoader, world, blockPosition); - world.tileEntityListTick.add(tileEntityChunkLoader); + TileEntityChunkLoader tileEntityChunkLoader = new TileEntityChunkLoader(chunkLoader, worldServer, blockPosition); + worldServer.tileEntityListTick.add(tileEntityChunkLoader); if (Scheduler.isRegionScheduler()) { - Scheduler.runTask(() -> setSpawnersRangeForLoader(chunkLoader, true)); + Scheduler.runTask(() -> + setSpawnersRangeForLoader(chunkLoader, worldServer, true, onSpawnerChangeCallback)); } else { - setSpawnersRangeForLoader(chunkLoader, true); + setSpawnersRangeForLoader(chunkLoader, worldServer, true, onSpawnerChangeCallback); } return tileEntityChunkLoader; } @Override - public void removeLoader(ChunkLoader chunkLoader, boolean spawnParticle) { + public void removeLoader(ChunkLoader chunkLoader, boolean spawnParticle, + @Nullable OnSpawnerChangeCallback onSpawnerChangeCallback) { Location loaderLoc = chunkLoader.getLocation(); - World world = ((CraftWorld) loaderLoc.getWorld()).getHandle(); + WorldServer worldServer = ((CraftWorld) loaderLoc.getWorld()).getHandle(); BlockPosition blockPosition = new BlockPosition(loaderLoc.getX(), loaderLoc.getY(), loaderLoc.getZ()); long tileEntityLong = LongHash.toLong(blockPosition.getX() >> 4, blockPosition.getZ() >> 4); @@ -134,33 +139,37 @@ public final class NMSAdapterImpl implements NMSAdapter { if (tileEntityChunkLoader != null) { tileEntityChunkLoader.holograms.forEach(EntityHolograms::removeHologram); tileEntityChunkLoader.removed = true; - world.tileEntityListTick.remove(tileEntityChunkLoader); + worldServer.tileEntityListTick.remove(tileEntityChunkLoader); } if (spawnParticle) - world.a(null, 2001, blockPosition, Block.getCombinedId(world.getType(blockPosition))); + worldServer.a(null, 2001, blockPosition, + Block.getCombinedId(worldServer.getType(blockPosition))); if (Scheduler.isRegionScheduler()) { - Scheduler.runTask(() -> setSpawnersRangeForLoader(chunkLoader, false)); + Scheduler.runTask(() -> + setSpawnersRangeForLoader(chunkLoader, worldServer, false, onSpawnerChangeCallback)); } else { - setSpawnersRangeForLoader(chunkLoader, false); + setSpawnersRangeForLoader(chunkLoader, worldServer, false, onSpawnerChangeCallback); } } - private static void setSpawnersRangeForLoader(ChunkLoader chunkLoader, boolean loaded) { + private static void setSpawnersRangeForLoader(ChunkLoader chunkLoader, WorldServer worldServer, boolean loaded, + @Nullable OnSpawnerChangeCallback onSpawnerChangeCallback) { + org.bukkit.World bukkitWorld = worldServer.getWorld(); + short requiredPlayerRange = (short) (loaded ? -1 : 16); + for (org.bukkit.Chunk bukkitChunk : chunkLoader.getLoadedChunksCollection()) { Chunk chunk = ((CraftChunk) bukkitChunk).getHandle(); for (TileEntity tileEntity : chunk.tileEntities.values()) { if (tileEntity instanceof TileEntityMobSpawner) { - NBTTagCompound nbtTagCompound = new NBTTagCompound(); - tileEntity.save(nbtTagCompound); - nbtTagCompound.setShort("RequiredPlayerRange", requiredPlayerRange); - if (TILE_ENTITY_LOAD.isValid()) { - TILE_ENTITY_LOAD.invoke(tileEntity, nbtTagCompound); - } else { - tileEntity.a(nbtTagCompound); + setSpawnerRange((TileEntityMobSpawner) tileEntity, requiredPlayerRange); + if (onSpawnerChangeCallback != null) { + BlockPosition blockPosition = tileEntity.getPosition(); + Location location = new Location(bukkitWorld, blockPosition.getX(), blockPosition.getY(), blockPosition.getZ()); + onSpawnerChangeCallback.apply(location, requiredPlayerRange); } } } @@ -168,14 +177,31 @@ public final class NMSAdapterImpl implements NMSAdapter { } @Override - public void updateSpawner(Location location, boolean reset) { - World world = ((CraftWorld) location.getWorld()).getHandle(); - BlockPosition blockPosition = new BlockPosition(location.getX(), location.getY(), location.getZ()); - TileEntityMobSpawner mobSpawner = (TileEntityMobSpawner) world.getTileEntity(blockPosition); + public void updateSpawner(Location location, boolean reset, + @Nullable OnSpawnerChangeCallback onSpawnerChangeCallback) { + org.bukkit.World bukkitWorld = location.getWorld(); + if (bukkitWorld == null) + throw new IllegalArgumentException("Cannot remove loader in null world."); + + World world = ((CraftWorld) bukkitWorld).getHandle(); + BlockPosition blockPosition = new BlockPosition(location.getX(), location.getY(), location.getZ()); + TileEntity tileEntity = world.getTileEntity(blockPosition); + if (!(tileEntity instanceof TileEntityMobSpawner)) + return; + + TileEntityMobSpawner mobSpawner = (TileEntityMobSpawner) tileEntity; + + int requiredPlayerRange = reset ? 16 : -1; + setSpawnerRange(mobSpawner, requiredPlayerRange); + if (onSpawnerChangeCallback != null) + onSpawnerChangeCallback.apply(location, requiredPlayerRange); + } + + private static void setSpawnerRange(TileEntityMobSpawner mobSpawner, int range) { NBTTagCompound nbtTagCompound = new NBTTagCompound(); mobSpawner.save(nbtTagCompound); - nbtTagCompound.setShort("RequiredPlayerRange", (short) (reset ? 16 : -1)); + nbtTagCompound.setShort("RequiredPlayerRange", (short) range); if (TILE_ENTITY_LOAD.isValid()) { TILE_ENTITY_LOAD.invoke(mobSpawner, nbtTagCompound); } else { diff --git a/NMS/v1_16_R3/src/main/java/com/bgsoftware/wildloaders/nms/v1_16_R3/NMSAdapterImpl.java b/NMS/v1_16_R3/src/main/java/com/bgsoftware/wildloaders/nms/v1_16_R3/NMSAdapterImpl.java index ea6b926..e593671 100644 --- a/NMS/v1_16_R3/src/main/java/com/bgsoftware/wildloaders/nms/v1_16_R3/NMSAdapterImpl.java +++ b/NMS/v1_16_R3/src/main/java/com/bgsoftware/wildloaders/nms/v1_16_R3/NMSAdapterImpl.java @@ -9,7 +9,6 @@ import net.minecraft.server.v1_16_R3.Block; import net.minecraft.server.v1_16_R3.BlockPosition; import net.minecraft.server.v1_16_R3.Chunk; import net.minecraft.server.v1_16_R3.ChunkCoordIntPair; -import net.minecraft.server.v1_16_R3.IBlockData; import net.minecraft.server.v1_16_R3.ItemStack; import net.minecraft.server.v1_16_R3.NBTTagCompound; import net.minecraft.server.v1_16_R3.NBTTagList; @@ -24,6 +23,7 @@ import org.bukkit.craftbukkit.v1_16_R3.CraftChunk; import org.bukkit.craftbukkit.v1_16_R3.CraftWorld; import org.bukkit.craftbukkit.v1_16_R3.inventory.CraftItemStack; +import javax.annotation.Nullable; import java.util.UUID; public final class NMSAdapterImpl implements NMSAdapter { @@ -107,7 +107,8 @@ public final class NMSAdapterImpl implements NMSAdapter { } @Override - public ITileEntityChunkLoader createLoader(ChunkLoader chunkLoader) { + public ITileEntityChunkLoader createLoader(ChunkLoader chunkLoader, + @Nullable OnSpawnerChangeCallback onSpawnerChangeCallback) { Location loaderLoc = chunkLoader.getLocation(); assert loaderLoc.getWorld() != null; WorldServer world = ((CraftWorld) loaderLoc.getWorld()).getHandle(); @@ -117,16 +118,18 @@ public final class NMSAdapterImpl implements NMSAdapter { world.tileEntityListTick.add(tileEntityChunkLoader); if (Scheduler.isRegionScheduler()) { - Scheduler.runTask(() -> setChunksForcedForLoader(chunkLoader, world, true)); + Scheduler.runTask(() -> + setChunksForcedForLoader(chunkLoader, world, true, onSpawnerChangeCallback)); } else { - setChunksForcedForLoader(chunkLoader, world, true); + setChunksForcedForLoader(chunkLoader, world, true, onSpawnerChangeCallback); } return tileEntityChunkLoader; } @Override - public void removeLoader(ChunkLoader chunkLoader, boolean spawnParticle) { + public void removeLoader(ChunkLoader chunkLoader, boolean spawnParticle, + @Nullable OnSpawnerChangeCallback onSpawnerChangeCallback) { Location loaderLoc = chunkLoader.getLocation(); assert loaderLoc.getWorld() != null; WorldServer world = ((CraftWorld) loaderLoc.getWorld()).getHandle(); @@ -144,20 +147,31 @@ public final class NMSAdapterImpl implements NMSAdapter { world.a(null, 2001, blockPosition, Block.getCombinedId(world.getType(blockPosition))); if (Scheduler.isRegionScheduler()) { - Scheduler.runTask(() -> setChunksForcedForLoader(chunkLoader, world, false)); + Scheduler.runTask(() -> + setChunksForcedForLoader(chunkLoader, world, false, onSpawnerChangeCallback)); } else { - setChunksForcedForLoader(chunkLoader, world, false); + setChunksForcedForLoader(chunkLoader, world, false, onSpawnerChangeCallback); } } - private static void setChunksForcedForLoader(ChunkLoader chunkLoader, WorldServer worldServer, boolean forced) { + private static void setChunksForcedForLoader(ChunkLoader chunkLoader, WorldServer worldServer, boolean forced, + @Nullable OnSpawnerChangeCallback onSpawnerChangeCallback) { + org.bukkit.World bukkitWorld = worldServer.getWorld(); + int requiredPlayerRange = forced ? -1 : 16; + for (org.bukkit.Chunk bukkitChunk : chunkLoader.getLoadedChunksCollection()) { Chunk chunk = ((CraftChunk) bukkitChunk).getHandle(); for (TileEntity tileEntity : chunk.tileEntities.values()) { - if (tileEntity instanceof TileEntityMobSpawner) + if (tileEntity instanceof TileEntityMobSpawner) { ((TileEntityMobSpawner) tileEntity).getSpawner().requiredPlayerRange = requiredPlayerRange; + if (onSpawnerChangeCallback != null) { + BlockPosition blockPosition = tileEntity.getPosition(); + Location location = new Location(bukkitWorld, blockPosition.getX(), blockPosition.getY(), blockPosition.getZ()); + onSpawnerChangeCallback.apply(location, requiredPlayerRange); + } + } } ChunkCoordIntPair chunkCoord = chunk.getPos(); @@ -166,18 +180,25 @@ public final class NMSAdapterImpl implements NMSAdapter { } @Override - public void updateSpawner(Location location, boolean reset) { - assert location.getWorld() != null; - World world = ((CraftWorld) location.getWorld()).getHandle(); + public void updateSpawner(Location location, boolean reset, + @Nullable OnSpawnerChangeCallback onSpawnerChangeCallback) { + org.bukkit.World bukkitWorld = location.getWorld(); + if (bukkitWorld == null) + throw new IllegalArgumentException("Cannot remove loader in null world."); + + World world = ((CraftWorld) bukkitWorld).getHandle(); BlockPosition blockPosition = new BlockPosition(location.getX(), location.getY(), location.getZ()); - IBlockData blockData = world.getType(blockPosition); - TileEntityMobSpawner mobSpawner = (TileEntityMobSpawner) world.getTileEntity(blockPosition); - - if (mobSpawner == null) + TileEntity tileEntity = world.getTileEntity(blockPosition); + if (!(tileEntity instanceof TileEntityMobSpawner)) return; - mobSpawner.getSpawner().requiredPlayerRange = reset ? 16 : -1; + TileEntityMobSpawner mobSpawner = (TileEntityMobSpawner) tileEntity; + + int requiredPlayerRange = reset ? 16 : -1; + mobSpawner.getSpawner().requiredPlayerRange = requiredPlayerRange; + if (onSpawnerChangeCallback != null) + onSpawnerChangeCallback.apply(location, requiredPlayerRange); } } diff --git a/NMS/v1_17/src/main/java/com/bgsoftware/wildloaders/nms/v1_17/NMSAdapterImpl.java b/NMS/v1_17/src/main/java/com/bgsoftware/wildloaders/nms/v1_17/NMSAdapterImpl.java index eacf437..94468b2 100644 --- a/NMS/v1_17/src/main/java/com/bgsoftware/wildloaders/nms/v1_17/NMSAdapterImpl.java +++ b/NMS/v1_17/src/main/java/com/bgsoftware/wildloaders/nms/v1_17/NMSAdapterImpl.java @@ -24,6 +24,7 @@ import org.bukkit.craftbukkit.v1_17_R1.CraftServer; import org.bukkit.craftbukkit.v1_17_R1.CraftWorld; import org.bukkit.craftbukkit.v1_17_R1.inventory.CraftItemStack; +import javax.annotation.Nullable; import java.util.UUID; public final class NMSAdapterImpl implements NMSAdapter { @@ -93,7 +94,8 @@ public final class NMSAdapterImpl implements NMSAdapter { } @Override - public ITileEntityChunkLoader createLoader(ChunkLoader chunkLoader) { + public ITileEntityChunkLoader createLoader(ChunkLoader chunkLoader, + @Nullable OnSpawnerChangeCallback onSpawnerChangeCallback) { Location loaderLoc = chunkLoader.getLocation(); World bukkitWorld = loaderLoc.getWorld(); @@ -107,16 +109,18 @@ public final class NMSAdapterImpl implements NMSAdapter { serverLevel.addBlockEntityTicker(ChunkLoaderBlockEntity.getTicker()); if (Scheduler.isRegionScheduler()) { - Scheduler.runTask(() -> setChunksForcedForLoader(chunkLoader, serverLevel, true)); + Scheduler.runTask(() -> + setChunksForcedForLoader(chunkLoader, serverLevel, true, onSpawnerChangeCallback)); } else { - setChunksForcedForLoader(chunkLoader, serverLevel, true); + setChunksForcedForLoader(chunkLoader, serverLevel, true, onSpawnerChangeCallback); } return ChunkLoaderBlockEntity; } @Override - public void removeLoader(ChunkLoader chunkLoader, boolean spawnParticle) { + public void removeLoader(ChunkLoader chunkLoader, boolean spawnParticle, + @Nullable OnSpawnerChangeCallback onSpawnerChangeCallback) { Location loaderLoc = chunkLoader.getLocation(); World bukkitWorld = loaderLoc.getWorld(); @@ -138,20 +142,31 @@ public final class NMSAdapterImpl implements NMSAdapter { serverLevel.levelEvent(null, 2001, blockPos, Block.getId(serverLevel.getBlockState(blockPos))); if (Scheduler.isRegionScheduler()) { - Scheduler.runTask(() -> setChunksForcedForLoader(chunkLoader, serverLevel, false)); + Scheduler.runTask(() -> + setChunksForcedForLoader(chunkLoader, serverLevel, false, onSpawnerChangeCallback)); } else { - setChunksForcedForLoader(chunkLoader, serverLevel, false); + setChunksForcedForLoader(chunkLoader, serverLevel, false, onSpawnerChangeCallback); } } - private static void setChunksForcedForLoader(ChunkLoader chunkLoader, ServerLevel serverLevel, boolean forced) { + private static void setChunksForcedForLoader(ChunkLoader chunkLoader, ServerLevel serverLevel, boolean forced, + @Nullable OnSpawnerChangeCallback onSpawnerChangeCallback) { + World bukkitWorld = serverLevel.getWorld(); + int requiredPlayerRange = forced ? -1 : 16; + for (org.bukkit.Chunk bukkitChunk : chunkLoader.getLoadedChunksCollection()) { LevelChunk levelChunk = ((CraftChunk) bukkitChunk).getHandle(); for (BlockEntity blockEntity : levelChunk.getBlockEntities().values()) { - if (blockEntity instanceof SpawnerBlockEntity spawnerBlockEntity) + if (blockEntity instanceof SpawnerBlockEntity spawnerBlockEntity) { spawnerBlockEntity.getSpawner().requiredPlayerRange = requiredPlayerRange; + if (onSpawnerChangeCallback != null) { + BlockPos blockPos = blockEntity.getBlockPos(); + Location location = new Location(bukkitWorld, blockPos.getX(), blockPos.getY(), blockPos.getZ()); + onSpawnerChangeCallback.apply(location, requiredPlayerRange); + } + } } ChunkPos chunkPos = levelChunk.getPos(); @@ -160,7 +175,8 @@ public final class NMSAdapterImpl implements NMSAdapter { } @Override - public void updateSpawner(Location location, boolean reset) { + public void updateSpawner(Location location, boolean reset, + @Nullable OnSpawnerChangeCallback onSpawnerChangeCallback) { World bukkitWorld = location.getWorld(); if (bukkitWorld == null) @@ -169,8 +185,13 @@ public final class NMSAdapterImpl implements NMSAdapter { ServerLevel serverLevel = ((CraftWorld) bukkitWorld).getHandle(); BlockPos blockPos = new BlockPos(location.getX(), location.getY(), location.getZ()); BlockEntity blockEntity = serverLevel.getBlockEntity(blockPos); - if (blockEntity instanceof SpawnerBlockEntity spawnerBlockEntity) - spawnerBlockEntity.getSpawner().requiredPlayerRange = reset ? 16 : -1; + if (!(blockEntity instanceof SpawnerBlockEntity spawnerBlockEntity)) + return; + + int requiredPlayerRange = reset ? 16 : -1; + spawnerBlockEntity.getSpawner().requiredPlayerRange = requiredPlayerRange; + if (onSpawnerChangeCallback != null) + onSpawnerChangeCallback.apply(location, requiredPlayerRange); } } diff --git a/NMS/v1_18/src/main/java/com/bgsoftware/wildloaders/nms/v1_18/NMSAdapterImpl.java b/NMS/v1_18/src/main/java/com/bgsoftware/wildloaders/nms/v1_18/NMSAdapterImpl.java index 39ad855..a6d5e0d 100644 --- a/NMS/v1_18/src/main/java/com/bgsoftware/wildloaders/nms/v1_18/NMSAdapterImpl.java +++ b/NMS/v1_18/src/main/java/com/bgsoftware/wildloaders/nms/v1_18/NMSAdapterImpl.java @@ -23,6 +23,7 @@ import org.bukkit.craftbukkit.v1_18_R2.CraftServer; import org.bukkit.craftbukkit.v1_18_R2.CraftWorld; import org.bukkit.craftbukkit.v1_18_R2.inventory.CraftItemStack; +import javax.annotation.Nullable; import java.util.UUID; public final class NMSAdapterImpl implements NMSAdapter { @@ -92,7 +93,8 @@ public final class NMSAdapterImpl implements NMSAdapter { } @Override - public ITileEntityChunkLoader createLoader(ChunkLoader chunkLoader) { + public ITileEntityChunkLoader createLoader(ChunkLoader chunkLoader, + @Nullable OnSpawnerChangeCallback onSpawnerChangeCallback) { Location loaderLoc = chunkLoader.getLocation(); World bukkitWorld = loaderLoc.getWorld(); @@ -106,16 +108,18 @@ public final class NMSAdapterImpl implements NMSAdapter { serverLevel.addBlockEntityTicker(ChunkLoaderBlockEntity.getTicker()); if (Scheduler.isRegionScheduler()) { - Scheduler.runTask(() -> setChunksForcedForLoader(chunkLoader, serverLevel, true)); + Scheduler.runTask(() -> + setChunksForcedForLoader(chunkLoader, serverLevel, true, onSpawnerChangeCallback)); } else { - setChunksForcedForLoader(chunkLoader, serverLevel, true); + setChunksForcedForLoader(chunkLoader, serverLevel, true, onSpawnerChangeCallback); } return ChunkLoaderBlockEntity; } @Override - public void removeLoader(ChunkLoader chunkLoader, boolean spawnParticle) { + public void removeLoader(ChunkLoader chunkLoader, boolean spawnParticle, + @Nullable OnSpawnerChangeCallback onSpawnerChangeCallback) { Location loaderLoc = chunkLoader.getLocation(); World bukkitWorld = loaderLoc.getWorld(); @@ -137,20 +141,31 @@ public final class NMSAdapterImpl implements NMSAdapter { serverLevel.levelEvent(null, 2001, blockPos, Block.getId(serverLevel.getBlockState(blockPos))); if (Scheduler.isRegionScheduler()) { - Scheduler.runTask(() -> setChunksForcedForLoader(chunkLoader, serverLevel, false)); + Scheduler.runTask(() -> + setChunksForcedForLoader(chunkLoader, serverLevel, false, onSpawnerChangeCallback)); } else { - setChunksForcedForLoader(chunkLoader, serverLevel, false); + setChunksForcedForLoader(chunkLoader, serverLevel, false, onSpawnerChangeCallback); } } - private static void setChunksForcedForLoader(ChunkLoader chunkLoader, ServerLevel serverLevel, boolean forced) { + private static void setChunksForcedForLoader(ChunkLoader chunkLoader, ServerLevel serverLevel, boolean forced, + @Nullable OnSpawnerChangeCallback onSpawnerChangeCallback) { + World bukkitWorld = serverLevel.getWorld(); + int requiredPlayerRange = forced ? -1 : 16; + for (org.bukkit.Chunk bukkitChunk : chunkLoader.getLoadedChunksCollection()) { LevelChunk levelChunk = ((CraftChunk) bukkitChunk).getHandle(); for (BlockEntity blockEntity : levelChunk.getBlockEntities().values()) { - if (blockEntity instanceof SpawnerBlockEntity spawnerBlockEntity) + if (blockEntity instanceof SpawnerBlockEntity spawnerBlockEntity) { spawnerBlockEntity.getSpawner().requiredPlayerRange = requiredPlayerRange; + if (onSpawnerChangeCallback != null) { + BlockPos blockPos = blockEntity.getBlockPos(); + Location location = new Location(bukkitWorld, blockPos.getX(), blockPos.getY(), blockPos.getZ()); + onSpawnerChangeCallback.apply(location, requiredPlayerRange); + } + } } ChunkPos chunkPos = levelChunk.getPos(); @@ -159,7 +174,8 @@ public final class NMSAdapterImpl implements NMSAdapter { } @Override - public void updateSpawner(Location location, boolean reset) { + public void updateSpawner(Location location, boolean reset, + @Nullable OnSpawnerChangeCallback onSpawnerChangeCallback) { World bukkitWorld = location.getWorld(); if (bukkitWorld == null) @@ -168,8 +184,13 @@ public final class NMSAdapterImpl implements NMSAdapter { ServerLevel serverLevel = ((CraftWorld) bukkitWorld).getHandle(); BlockPos blockPos = new BlockPos(location.getX(), location.getY(), location.getZ()); BlockEntity blockEntity = serverLevel.getBlockEntity(blockPos); - if (blockEntity instanceof SpawnerBlockEntity spawnerBlockEntity) - spawnerBlockEntity.getSpawner().requiredPlayerRange = reset ? 16 : -1; + if (!(blockEntity instanceof SpawnerBlockEntity spawnerBlockEntity)) + return; + + int requiredPlayerRange = reset ? 16 : -1; + spawnerBlockEntity.getSpawner().requiredPlayerRange = requiredPlayerRange; + if (onSpawnerChangeCallback != null) + onSpawnerChangeCallback.apply(location, requiredPlayerRange); } } diff --git a/NMS/v1_19/src/main/java/com/bgsoftware/wildloaders/nms/v1_19/NMSAdapterImpl.java b/NMS/v1_19/src/main/java/com/bgsoftware/wildloaders/nms/v1_19/NMSAdapterImpl.java index d4d344e..a318f62 100644 --- a/NMS/v1_19/src/main/java/com/bgsoftware/wildloaders/nms/v1_19/NMSAdapterImpl.java +++ b/NMS/v1_19/src/main/java/com/bgsoftware/wildloaders/nms/v1_19/NMSAdapterImpl.java @@ -22,6 +22,7 @@ import org.bukkit.craftbukkit.v1_19_R3.CraftServer; import org.bukkit.craftbukkit.v1_19_R3.CraftWorld; import org.bukkit.craftbukkit.v1_19_R3.inventory.CraftItemStack; +import javax.annotation.Nullable; import java.util.UUID; public final class NMSAdapterImpl implements NMSAdapter { @@ -91,7 +92,8 @@ public final class NMSAdapterImpl implements NMSAdapter { } @Override - public ITileEntityChunkLoader createLoader(ChunkLoader chunkLoader) { + public ITileEntityChunkLoader createLoader(ChunkLoader chunkLoader, + @Nullable OnSpawnerChangeCallback onSpawnerChangeCallback) { Location loaderLoc = chunkLoader.getLocation(); World bukkitWorld = loaderLoc.getWorld(); @@ -105,16 +107,18 @@ public final class NMSAdapterImpl implements NMSAdapter { serverLevel.addBlockEntityTicker(ChunkLoaderBlockEntity.getTicker()); if (Scheduler.isRegionScheduler()) { - Scheduler.runTask(() -> setChunksForcedForLoader(chunkLoader, serverLevel, true)); + Scheduler.runTask(() -> + setChunksForcedForLoader(chunkLoader, serverLevel, true, onSpawnerChangeCallback)); } else { - setChunksForcedForLoader(chunkLoader, serverLevel, true); + setChunksForcedForLoader(chunkLoader, serverLevel, true, onSpawnerChangeCallback); } return ChunkLoaderBlockEntity; } @Override - public void removeLoader(ChunkLoader chunkLoader, boolean spawnParticle) { + public void removeLoader(ChunkLoader chunkLoader, boolean spawnParticle, + @Nullable OnSpawnerChangeCallback onSpawnerChangeCallback) { Location loaderLoc = chunkLoader.getLocation(); World bukkitWorld = loaderLoc.getWorld(); @@ -136,20 +140,31 @@ public final class NMSAdapterImpl implements NMSAdapter { serverLevel.levelEvent(null, 2001, blockPos, Block.getId(serverLevel.getBlockState(blockPos))); if (Scheduler.isRegionScheduler()) { - Scheduler.runTask(() -> setChunksForcedForLoader(chunkLoader, serverLevel, false)); + Scheduler.runTask(() -> + setChunksForcedForLoader(chunkLoader, serverLevel, false, onSpawnerChangeCallback)); } else { - setChunksForcedForLoader(chunkLoader, serverLevel, false); + setChunksForcedForLoader(chunkLoader, serverLevel, false, onSpawnerChangeCallback); } } - private static void setChunksForcedForLoader(ChunkLoader chunkLoader, ServerLevel serverLevel, boolean forced) { + private static void setChunksForcedForLoader(ChunkLoader chunkLoader, ServerLevel serverLevel, boolean forced, + @Nullable OnSpawnerChangeCallback onSpawnerChangeCallback) { + World bukkitWorld = serverLevel.getWorld(); + int requiredPlayerRange = forced ? -1 : 16; + for (org.bukkit.Chunk bukkitChunk : chunkLoader.getLoadedChunksCollection()) { LevelChunk levelChunk = serverLevel.getChunk(bukkitChunk.getX(), bukkitChunk.getZ()); for (BlockEntity blockEntity : levelChunk.getBlockEntities().values()) { - if (blockEntity instanceof SpawnerBlockEntity spawnerBlockEntity) + if (blockEntity instanceof SpawnerBlockEntity spawnerBlockEntity) { spawnerBlockEntity.getSpawner().requiredPlayerRange = requiredPlayerRange; + if (onSpawnerChangeCallback != null) { + BlockPos blockPos = blockEntity.getBlockPos(); + Location location = new Location(bukkitWorld, blockPos.getX(), blockPos.getY(), blockPos.getZ()); + onSpawnerChangeCallback.apply(location, requiredPlayerRange); + } + } } ChunkPos chunkPos = levelChunk.getPos(); @@ -158,7 +173,8 @@ public final class NMSAdapterImpl implements NMSAdapter { } @Override - public void updateSpawner(Location location, boolean reset) { + public void updateSpawner(Location location, boolean reset, + @Nullable OnSpawnerChangeCallback onSpawnerChangeCallback) { World bukkitWorld = location.getWorld(); if (bukkitWorld == null) @@ -167,8 +183,13 @@ public final class NMSAdapterImpl implements NMSAdapter { ServerLevel serverLevel = ((CraftWorld) bukkitWorld).getHandle(); BlockPos blockPos = new BlockPos(location.getBlockX(), location.getBlockY(), location.getBlockZ()); BlockEntity blockEntity = serverLevel.getBlockEntity(blockPos); - if (blockEntity instanceof SpawnerBlockEntity spawnerBlockEntity) - spawnerBlockEntity.getSpawner().requiredPlayerRange = reset ? 16 : -1; + if (!(blockEntity instanceof SpawnerBlockEntity spawnerBlockEntity)) + return; + + int requiredPlayerRange = reset ? 16 : -1; + spawnerBlockEntity.getSpawner().requiredPlayerRange = requiredPlayerRange; + if (onSpawnerChangeCallback != null) + onSpawnerChangeCallback.apply(location, requiredPlayerRange); } } diff --git a/NMS/v1_20_3/src/main/java/com/bgsoftware/wildloaders/nms/v1_20_3/NMSAdapterImpl.java b/NMS/v1_20_3/src/main/java/com/bgsoftware/wildloaders/nms/v1_20_3/NMSAdapterImpl.java index 5780bcb..d420145 100644 --- a/NMS/v1_20_3/src/main/java/com/bgsoftware/wildloaders/nms/v1_20_3/NMSAdapterImpl.java +++ b/NMS/v1_20_3/src/main/java/com/bgsoftware/wildloaders/nms/v1_20_3/NMSAdapterImpl.java @@ -22,6 +22,7 @@ import org.bukkit.craftbukkit.v1_20_R3.CraftServer; import org.bukkit.craftbukkit.v1_20_R3.CraftWorld; import org.bukkit.craftbukkit.v1_20_R3.inventory.CraftItemStack; +import javax.annotation.Nullable; import java.util.UUID; public final class NMSAdapterImpl implements NMSAdapter { @@ -91,7 +92,8 @@ public final class NMSAdapterImpl implements NMSAdapter { } @Override - public ITileEntityChunkLoader createLoader(ChunkLoader chunkLoader) { + public ITileEntityChunkLoader createLoader(ChunkLoader chunkLoader, + @Nullable OnSpawnerChangeCallback onSpawnerChangeCallback) { Location loaderLoc = chunkLoader.getLocation(); World bukkitWorld = loaderLoc.getWorld(); @@ -105,16 +107,18 @@ public final class NMSAdapterImpl implements NMSAdapter { serverLevel.addBlockEntityTicker(ChunkLoaderBlockEntity.getTicker()); if (Scheduler.isRegionScheduler()) { - Scheduler.runTask(() -> setChunksForcedForLoader(chunkLoader, serverLevel, true)); + Scheduler.runTask(() -> + setChunksForcedForLoader(chunkLoader, serverLevel, true, onSpawnerChangeCallback)); } else { - setChunksForcedForLoader(chunkLoader, serverLevel, true); + setChunksForcedForLoader(chunkLoader, serverLevel, true, onSpawnerChangeCallback); } return ChunkLoaderBlockEntity; } @Override - public void removeLoader(ChunkLoader chunkLoader, boolean spawnParticle) { + public void removeLoader(ChunkLoader chunkLoader, boolean spawnParticle, + @Nullable OnSpawnerChangeCallback onSpawnerChangeCallback) { Location loaderLoc = chunkLoader.getLocation(); World bukkitWorld = loaderLoc.getWorld(); @@ -136,20 +140,31 @@ public final class NMSAdapterImpl implements NMSAdapter { serverLevel.levelEvent(null, 2001, blockPos, Block.getId(serverLevel.getBlockState(blockPos))); if (Scheduler.isRegionScheduler()) { - Scheduler.runTask(() -> setChunksForcedForLoader(chunkLoader, serverLevel, false)); + Scheduler.runTask(() -> + setChunksForcedForLoader(chunkLoader, serverLevel, false, onSpawnerChangeCallback)); } else { - setChunksForcedForLoader(chunkLoader, serverLevel, false); + setChunksForcedForLoader(chunkLoader, serverLevel, false, onSpawnerChangeCallback); } } - private static void setChunksForcedForLoader(ChunkLoader chunkLoader, ServerLevel serverLevel, boolean forced) { + private static void setChunksForcedForLoader(ChunkLoader chunkLoader, ServerLevel serverLevel, boolean forced, + @Nullable OnSpawnerChangeCallback onSpawnerChangeCallback) { + World bukkitWorld = serverLevel.getWorld(); + int requiredPlayerRange = forced ? -1 : 16; + for (org.bukkit.Chunk bukkitChunk : chunkLoader.getLoadedChunksCollection()) { LevelChunk levelChunk = serverLevel.getChunk(bukkitChunk.getX(), bukkitChunk.getZ()); for (BlockEntity blockEntity : levelChunk.getBlockEntities().values()) { - if (blockEntity instanceof SpawnerBlockEntity spawnerBlockEntity) + if (blockEntity instanceof SpawnerBlockEntity spawnerBlockEntity) { spawnerBlockEntity.getSpawner().requiredPlayerRange = requiredPlayerRange; + if (onSpawnerChangeCallback != null) { + BlockPos blockPos = blockEntity.getBlockPos(); + Location location = new Location(bukkitWorld, blockPos.getX(), blockPos.getY(), blockPos.getZ()); + onSpawnerChangeCallback.apply(location, requiredPlayerRange); + } + } } ChunkPos chunkPos = levelChunk.getPos(); @@ -158,7 +173,8 @@ public final class NMSAdapterImpl implements NMSAdapter { } @Override - public void updateSpawner(Location location, boolean reset) { + public void updateSpawner(Location location, boolean reset, + @Nullable OnSpawnerChangeCallback onSpawnerChangeCallback) { World bukkitWorld = location.getWorld(); if (bukkitWorld == null) @@ -167,8 +183,13 @@ public final class NMSAdapterImpl implements NMSAdapter { ServerLevel serverLevel = ((CraftWorld) bukkitWorld).getHandle(); BlockPos blockPos = new BlockPos(location.getBlockX(), location.getBlockY(), location.getBlockZ()); BlockEntity blockEntity = serverLevel.getBlockEntity(blockPos); - if (blockEntity instanceof SpawnerBlockEntity spawnerBlockEntity) - spawnerBlockEntity.getSpawner().requiredPlayerRange = reset ? 16 : -1; + if (!(blockEntity instanceof SpawnerBlockEntity spawnerBlockEntity)) + return; + + int requiredPlayerRange = reset ? 16 : -1; + spawnerBlockEntity.getSpawner().requiredPlayerRange = requiredPlayerRange; + if (onSpawnerChangeCallback != null) + onSpawnerChangeCallback.apply(location, requiredPlayerRange); } } diff --git a/NMS/v1_20_4/src/main/java/com/bgsoftware/wildloaders/nms/v1_20_4/NMSAdapterImpl.java b/NMS/v1_20_4/src/main/java/com/bgsoftware/wildloaders/nms/v1_20_4/NMSAdapterImpl.java index f9fec1e..011b43c 100644 --- a/NMS/v1_20_4/src/main/java/com/bgsoftware/wildloaders/nms/v1_20_4/NMSAdapterImpl.java +++ b/NMS/v1_20_4/src/main/java/com/bgsoftware/wildloaders/nms/v1_20_4/NMSAdapterImpl.java @@ -26,6 +26,7 @@ import org.bukkit.craftbukkit.CraftServer; import org.bukkit.craftbukkit.CraftWorld; import org.bukkit.craftbukkit.inventory.CraftItemStack; +import javax.annotation.Nullable; import java.util.Optional; import java.util.UUID; @@ -97,7 +98,8 @@ public final class NMSAdapterImpl implements NMSAdapter { } @Override - public ITileEntityChunkLoader createLoader(ChunkLoader chunkLoader) { + public ITileEntityChunkLoader createLoader(ChunkLoader chunkLoader, + @Nullable OnSpawnerChangeCallback onSpawnerChangeCallback) { Location loaderLoc = chunkLoader.getLocation(); World bukkitWorld = loaderLoc.getWorld(); @@ -111,16 +113,18 @@ public final class NMSAdapterImpl implements NMSAdapter { serverLevel.addBlockEntityTicker(ChunkLoaderBlockEntity.getTicker()); if (Scheduler.isRegionScheduler()) { - Scheduler.runTask(() -> setChunksForcedForLoader(chunkLoader, serverLevel, true)); + Scheduler.runTask(() -> + setChunksForcedForLoader(chunkLoader, serverLevel, true, onSpawnerChangeCallback)); } else { - setChunksForcedForLoader(chunkLoader, serverLevel, true); + setChunksForcedForLoader(chunkLoader, serverLevel, true, onSpawnerChangeCallback); } return ChunkLoaderBlockEntity; } @Override - public void removeLoader(ChunkLoader chunkLoader, boolean spawnParticle) { + public void removeLoader(ChunkLoader chunkLoader, boolean spawnParticle, + @Nullable OnSpawnerChangeCallback onSpawnerChangeCallback) { Location loaderLoc = chunkLoader.getLocation(); World bukkitWorld = loaderLoc.getWorld(); @@ -142,20 +146,31 @@ public final class NMSAdapterImpl implements NMSAdapter { serverLevel.levelEvent(null, 2001, blockPos, Block.getId(serverLevel.getBlockState(blockPos))); if (Scheduler.isRegionScheduler()) { - Scheduler.runTask(() -> setChunksForcedForLoader(chunkLoader, serverLevel, false)); + Scheduler.runTask(() -> + setChunksForcedForLoader(chunkLoader, serverLevel, false, onSpawnerChangeCallback)); } else { - setChunksForcedForLoader(chunkLoader, serverLevel, false); + setChunksForcedForLoader(chunkLoader, serverLevel, false, onSpawnerChangeCallback); } } - private static void setChunksForcedForLoader(ChunkLoader chunkLoader, ServerLevel serverLevel, boolean forced) { + private static void setChunksForcedForLoader(ChunkLoader chunkLoader, ServerLevel serverLevel, boolean forced, + @Nullable OnSpawnerChangeCallback onSpawnerChangeCallback) { + World bukkitWorld = serverLevel.getWorld(); + int requiredPlayerRange = forced ? -1 : 16; + for (org.bukkit.Chunk bukkitChunk : chunkLoader.getLoadedChunksCollection()) { LevelChunk levelChunk = serverLevel.getChunk(bukkitChunk.getX(), bukkitChunk.getZ()); for (BlockEntity blockEntity : levelChunk.getBlockEntities().values()) { - if (blockEntity instanceof SpawnerBlockEntity spawnerBlockEntity) + if (blockEntity instanceof SpawnerBlockEntity spawnerBlockEntity) { spawnerBlockEntity.getSpawner().requiredPlayerRange = requiredPlayerRange; + if (onSpawnerChangeCallback != null) { + BlockPos blockPos = blockEntity.getBlockPos(); + Location location = new Location(bukkitWorld, blockPos.getX(), blockPos.getY(), blockPos.getZ()); + onSpawnerChangeCallback.apply(location, requiredPlayerRange); + } + } } ChunkPos chunkPos = levelChunk.getPos(); @@ -164,7 +179,8 @@ public final class NMSAdapterImpl implements NMSAdapter { } @Override - public void updateSpawner(Location location, boolean reset) { + public void updateSpawner(Location location, boolean reset, + @Nullable OnSpawnerChangeCallback onSpawnerChangeCallback) { World bukkitWorld = location.getWorld(); if (bukkitWorld == null) @@ -173,8 +189,13 @@ public final class NMSAdapterImpl implements NMSAdapter { ServerLevel serverLevel = ((CraftWorld) bukkitWorld).getHandle(); BlockPos blockPos = new BlockPos(location.getBlockX(), location.getBlockY(), location.getBlockZ()); BlockEntity blockEntity = serverLevel.getBlockEntity(blockPos); - if (blockEntity instanceof SpawnerBlockEntity spawnerBlockEntity) - spawnerBlockEntity.getSpawner().requiredPlayerRange = reset ? 16 : -1; + if (!(blockEntity instanceof SpawnerBlockEntity spawnerBlockEntity)) + return; + + int requiredPlayerRange = reset ? 16 : -1; + spawnerBlockEntity.getSpawner().requiredPlayerRange = requiredPlayerRange; + if (onSpawnerChangeCallback != null) + onSpawnerChangeCallback.apply(location, requiredPlayerRange); } } diff --git a/NMS/v1_21/src/main/java/com/bgsoftware/wildloaders/nms/v1_21/NMSAdapterImpl.java b/NMS/v1_21/src/main/java/com/bgsoftware/wildloaders/nms/v1_21/NMSAdapterImpl.java index 80a7430..0c4f825 100644 --- a/NMS/v1_21/src/main/java/com/bgsoftware/wildloaders/nms/v1_21/NMSAdapterImpl.java +++ b/NMS/v1_21/src/main/java/com/bgsoftware/wildloaders/nms/v1_21/NMSAdapterImpl.java @@ -26,6 +26,7 @@ import org.bukkit.craftbukkit.CraftServer; import org.bukkit.craftbukkit.CraftWorld; import org.bukkit.craftbukkit.inventory.CraftItemStack; +import javax.annotation.Nullable; import java.util.Optional; import java.util.UUID; @@ -97,7 +98,8 @@ public final class NMSAdapterImpl implements NMSAdapter { } @Override - public ITileEntityChunkLoader createLoader(ChunkLoader chunkLoader) { + public ITileEntityChunkLoader createLoader(ChunkLoader chunkLoader, + @Nullable OnSpawnerChangeCallback onSpawnerChangeCallback) { Location loaderLoc = chunkLoader.getLocation(); World bukkitWorld = loaderLoc.getWorld(); @@ -111,16 +113,18 @@ public final class NMSAdapterImpl implements NMSAdapter { serverLevel.addBlockEntityTicker(ChunkLoaderBlockEntity.getTicker()); if (Scheduler.isRegionScheduler()) { - Scheduler.runTask(() -> setChunksForcedForLoader(chunkLoader, serverLevel, true)); + Scheduler.runTask(() -> + setChunksForcedForLoader(chunkLoader, serverLevel, true, onSpawnerChangeCallback)); } else { - setChunksForcedForLoader(chunkLoader, serverLevel, true); + setChunksForcedForLoader(chunkLoader, serverLevel, true, onSpawnerChangeCallback); } return ChunkLoaderBlockEntity; } @Override - public void removeLoader(ChunkLoader chunkLoader, boolean spawnParticle) { + public void removeLoader(ChunkLoader chunkLoader, boolean spawnParticle, + @Nullable OnSpawnerChangeCallback onSpawnerChangeCallback) { Location loaderLoc = chunkLoader.getLocation(); World bukkitWorld = loaderLoc.getWorld(); @@ -142,20 +146,31 @@ public final class NMSAdapterImpl implements NMSAdapter { serverLevel.levelEvent(null, 2001, blockPos, Block.getId(serverLevel.getBlockState(blockPos))); if (Scheduler.isRegionScheduler()) { - Scheduler.runTask(() -> setChunksForcedForLoader(chunkLoader, serverLevel, false)); + Scheduler.runTask(() -> + setChunksForcedForLoader(chunkLoader, serverLevel, false, onSpawnerChangeCallback)); } else { - setChunksForcedForLoader(chunkLoader, serverLevel, false); + setChunksForcedForLoader(chunkLoader, serverLevel, false, onSpawnerChangeCallback); } } - private static void setChunksForcedForLoader(ChunkLoader chunkLoader, ServerLevel serverLevel, boolean forced) { + private static void setChunksForcedForLoader(ChunkLoader chunkLoader, ServerLevel serverLevel, boolean forced, + @Nullable OnSpawnerChangeCallback onSpawnerChangeCallback) { + World bukkitWorld = serverLevel.getWorld(); + int requiredPlayerRange = forced ? -1 : 16; + for (org.bukkit.Chunk bukkitChunk : chunkLoader.getLoadedChunksCollection()) { LevelChunk levelChunk = serverLevel.getChunk(bukkitChunk.getX(), bukkitChunk.getZ()); for (BlockEntity blockEntity : levelChunk.getBlockEntities().values()) { - if (blockEntity instanceof SpawnerBlockEntity spawnerBlockEntity) + if (blockEntity instanceof SpawnerBlockEntity spawnerBlockEntity) { spawnerBlockEntity.getSpawner().requiredPlayerRange = requiredPlayerRange; + if (onSpawnerChangeCallback != null) { + BlockPos blockPos = blockEntity.getBlockPos(); + Location location = new Location(bukkitWorld, blockPos.getX(), blockPos.getY(), blockPos.getZ()); + onSpawnerChangeCallback.apply(location, requiredPlayerRange); + } + } } ChunkPos chunkPos = levelChunk.getPos(); @@ -164,7 +179,8 @@ public final class NMSAdapterImpl implements NMSAdapter { } @Override - public void updateSpawner(Location location, boolean reset) { + public void updateSpawner(Location location, boolean reset, + @Nullable OnSpawnerChangeCallback onSpawnerChangeCallback) { World bukkitWorld = location.getWorld(); if (bukkitWorld == null) @@ -173,8 +189,13 @@ public final class NMSAdapterImpl implements NMSAdapter { ServerLevel serverLevel = ((CraftWorld) bukkitWorld).getHandle(); BlockPos blockPos = new BlockPos(location.getBlockX(), location.getBlockY(), location.getBlockZ()); BlockEntity blockEntity = serverLevel.getBlockEntity(blockPos); - if (blockEntity instanceof SpawnerBlockEntity spawnerBlockEntity) - spawnerBlockEntity.getSpawner().requiredPlayerRange = reset ? 16 : -1; + if (!(blockEntity instanceof SpawnerBlockEntity spawnerBlockEntity)) + return; + + int requiredPlayerRange = reset ? 16 : -1; + spawnerBlockEntity.getSpawner().requiredPlayerRange = requiredPlayerRange; + if (onSpawnerChangeCallback != null) + onSpawnerChangeCallback.apply(location, requiredPlayerRange); } } diff --git a/NMS/v1_21_3/src/main/java/com/bgsoftware/wildloaders/nms/v1_21_3/NMSAdapterImpl.java b/NMS/v1_21_3/src/main/java/com/bgsoftware/wildloaders/nms/v1_21_3/NMSAdapterImpl.java index 4511e92..6da78fa 100644 --- a/NMS/v1_21_3/src/main/java/com/bgsoftware/wildloaders/nms/v1_21_3/NMSAdapterImpl.java +++ b/NMS/v1_21_3/src/main/java/com/bgsoftware/wildloaders/nms/v1_21_3/NMSAdapterImpl.java @@ -26,6 +26,7 @@ import org.bukkit.craftbukkit.CraftServer; import org.bukkit.craftbukkit.CraftWorld; import org.bukkit.craftbukkit.inventory.CraftItemStack; +import javax.annotation.Nullable; import java.util.Optional; import java.util.UUID; @@ -97,7 +98,8 @@ public final class NMSAdapterImpl implements NMSAdapter { } @Override - public ITileEntityChunkLoader createLoader(ChunkLoader chunkLoader) { + public ITileEntityChunkLoader createLoader(ChunkLoader chunkLoader, + @Nullable OnSpawnerChangeCallback onSpawnerChangeCallback) { Location loaderLoc = chunkLoader.getLocation(); World bukkitWorld = loaderLoc.getWorld(); @@ -111,16 +113,18 @@ public final class NMSAdapterImpl implements NMSAdapter { serverLevel.addBlockEntityTicker(ChunkLoaderBlockEntity.getTicker()); if (Scheduler.isRegionScheduler()) { - Scheduler.runTask(() -> setChunksForcedForLoader(chunkLoader, serverLevel, true)); + Scheduler.runTask(() -> + setChunksForcedForLoader(chunkLoader, serverLevel, true, onSpawnerChangeCallback)); } else { - setChunksForcedForLoader(chunkLoader, serverLevel, true); + setChunksForcedForLoader(chunkLoader, serverLevel, true, onSpawnerChangeCallback); } return ChunkLoaderBlockEntity; } @Override - public void removeLoader(ChunkLoader chunkLoader, boolean spawnParticle) { + public void removeLoader(ChunkLoader chunkLoader, boolean spawnParticle, + @Nullable OnSpawnerChangeCallback onSpawnerChangeCallback) { Location loaderLoc = chunkLoader.getLocation(); World bukkitWorld = loaderLoc.getWorld(); @@ -142,20 +146,31 @@ public final class NMSAdapterImpl implements NMSAdapter { serverLevel.levelEvent(null, 2001, blockPos, Block.getId(serverLevel.getBlockState(blockPos))); if (Scheduler.isRegionScheduler()) { - Scheduler.runTask(() -> setChunksForcedForLoader(chunkLoader, serverLevel, false)); + Scheduler.runTask(() -> + setChunksForcedForLoader(chunkLoader, serverLevel, false, onSpawnerChangeCallback)); } else { - setChunksForcedForLoader(chunkLoader, serverLevel, false); + setChunksForcedForLoader(chunkLoader, serverLevel, false, onSpawnerChangeCallback); } } - private static void setChunksForcedForLoader(ChunkLoader chunkLoader, ServerLevel serverLevel, boolean forced) { + private static void setChunksForcedForLoader(ChunkLoader chunkLoader, ServerLevel serverLevel, boolean forced, + @Nullable OnSpawnerChangeCallback onSpawnerChangeCallback) { + World bukkitWorld = serverLevel.getWorld(); + int requiredPlayerRange = forced ? -1 : 16; + for (org.bukkit.Chunk bukkitChunk : chunkLoader.getLoadedChunksCollection()) { LevelChunk levelChunk = serverLevel.getChunk(bukkitChunk.getX(), bukkitChunk.getZ()); for (BlockEntity blockEntity : levelChunk.getBlockEntities().values()) { - if (blockEntity instanceof SpawnerBlockEntity spawnerBlockEntity) + if (blockEntity instanceof SpawnerBlockEntity spawnerBlockEntity) { spawnerBlockEntity.getSpawner().requiredPlayerRange = requiredPlayerRange; + if (onSpawnerChangeCallback != null) { + BlockPos blockPos = blockEntity.getBlockPos(); + Location location = new Location(bukkitWorld, blockPos.getX(), blockPos.getY(), blockPos.getZ()); + onSpawnerChangeCallback.apply(location, requiredPlayerRange); + } + } } ChunkPos chunkPos = levelChunk.getPos(); @@ -164,7 +179,8 @@ public final class NMSAdapterImpl implements NMSAdapter { } @Override - public void updateSpawner(Location location, boolean reset) { + public void updateSpawner(Location location, boolean reset, + @Nullable OnSpawnerChangeCallback onSpawnerChangeCallback) { World bukkitWorld = location.getWorld(); if (bukkitWorld == null) @@ -173,8 +189,13 @@ public final class NMSAdapterImpl implements NMSAdapter { ServerLevel serverLevel = ((CraftWorld) bukkitWorld).getHandle(); BlockPos blockPos = new BlockPos(location.getBlockX(), location.getBlockY(), location.getBlockZ()); BlockEntity blockEntity = serverLevel.getBlockEntity(blockPos); - if (blockEntity instanceof SpawnerBlockEntity spawnerBlockEntity) - spawnerBlockEntity.getSpawner().requiredPlayerRange = reset ? 16 : -1; + if (!(blockEntity instanceof SpawnerBlockEntity spawnerBlockEntity)) + return; + + int requiredPlayerRange = reset ? 16 : -1; + spawnerBlockEntity.getSpawner().requiredPlayerRange = requiredPlayerRange; + if (onSpawnerChangeCallback != null) + onSpawnerChangeCallback.apply(location, requiredPlayerRange); } } diff --git a/NMS/v1_7_R4/src/main/java/com/bgsoftware/wildloaders/nms/v1_7_R4/NMSAdapterImpl.java b/NMS/v1_7_R4/src/main/java/com/bgsoftware/wildloaders/nms/v1_7_R4/NMSAdapterImpl.java index 45be7d3..1d7810c 100644 --- a/NMS/v1_7_R4/src/main/java/com/bgsoftware/wildloaders/nms/v1_7_R4/NMSAdapterImpl.java +++ b/NMS/v1_7_R4/src/main/java/com/bgsoftware/wildloaders/nms/v1_7_R4/NMSAdapterImpl.java @@ -15,12 +15,14 @@ import net.minecraft.server.v1_7_R4.NBTTagString; import net.minecraft.server.v1_7_R4.TileEntity; import net.minecraft.server.v1_7_R4.TileEntityMobSpawner; import net.minecraft.server.v1_7_R4.World; +import net.minecraft.server.v1_7_R4.WorldServer; import org.bukkit.Location; import org.bukkit.craftbukkit.v1_7_R4.CraftChunk; import org.bukkit.craftbukkit.v1_7_R4.CraftWorld; import org.bukkit.craftbukkit.v1_7_R4.inventory.CraftItemStack; import org.bukkit.craftbukkit.v1_7_R4.util.LongHash; +import javax.annotation.Nullable; import java.util.Collection; import java.util.UUID; @@ -105,73 +107,98 @@ public final class NMSAdapterImpl implements NMSAdapter { } @Override - public ITileEntityChunkLoader createLoader(ChunkLoader chunkLoader) { + public ITileEntityChunkLoader createLoader(ChunkLoader chunkLoader, + @Nullable OnSpawnerChangeCallback onSpawnerChangeCallback) { Location loaderLoc = chunkLoader.getLocation(); - World world = ((CraftWorld) loaderLoc.getWorld()).getHandle(); + WorldServer worldServer = ((CraftWorld) loaderLoc.getWorld()).getHandle(); int x = loaderLoc.getBlockX(), y = loaderLoc.getBlockY(), z = loaderLoc.getBlockZ(); - TileEntityChunkLoader tileEntityChunkLoader = new TileEntityChunkLoader(chunkLoader, world, x, y, z); + TileEntityChunkLoader tileEntityChunkLoader = new TileEntityChunkLoader(chunkLoader, worldServer, x, y, z); //noinspection unchecked - world.tileEntityList.add(tileEntityChunkLoader); + worldServer.tileEntityList.add(tileEntityChunkLoader); if (Scheduler.isRegionScheduler()) { - Scheduler.runTask(() -> setSpawnersRangeForLoader(chunkLoader, true)); + Scheduler.runTask(() -> + setSpawnersRangeForLoader(chunkLoader, worldServer, true, onSpawnerChangeCallback)); } else { - setSpawnersRangeForLoader(chunkLoader, true); + setSpawnersRangeForLoader(chunkLoader, worldServer, true, onSpawnerChangeCallback); } return tileEntityChunkLoader; } @Override - public void removeLoader(ChunkLoader chunkLoader, boolean spawnParticle) { + public void removeLoader(ChunkLoader chunkLoader, boolean spawnParticle, + @Nullable OnSpawnerChangeCallback onSpawnerChangeCallback) { Location loaderLoc = chunkLoader.getLocation(); - World world = ((CraftWorld) loaderLoc.getWorld()).getHandle(); + WorldServer worldServer = ((CraftWorld) loaderLoc.getWorld()).getHandle(); int x = loaderLoc.getBlockX(), y = loaderLoc.getBlockY(), z = loaderLoc.getBlockZ(); long tileEntityLong = LongHash.toLong(x >> 4, z >> 4); TileEntityChunkLoader tileEntityChunkLoader = TileEntityChunkLoader.tileEntityChunkLoaderMap.remove(tileEntityLong); if (tileEntityChunkLoader != null) { tileEntityChunkLoader.removed = true; - world.tileEntityList.remove(tileEntityChunkLoader); + worldServer.tileEntityList.remove(tileEntityChunkLoader); } if (spawnParticle) - world.a(null, 2001, x, y, z, Block.getId(world.getType(x, y, z)) + (world.getData(x, y, z) << 12)); + worldServer.a(null, 2001, x, y, z, + Block.getId(worldServer.getType(x, y, z)) + (worldServer.getData(x, y, z) << 12)); if (Scheduler.isRegionScheduler()) { - Scheduler.runTask(() -> setSpawnersRangeForLoader(chunkLoader, false)); + Scheduler.runTask(() -> + setSpawnersRangeForLoader(chunkLoader, worldServer, false, onSpawnerChangeCallback)); } else { - setSpawnersRangeForLoader(chunkLoader, false); + setSpawnersRangeForLoader(chunkLoader, worldServer, false, onSpawnerChangeCallback); } } - private static void setSpawnersRangeForLoader(ChunkLoader chunkLoader, boolean loaded) { + private static void setSpawnersRangeForLoader(ChunkLoader chunkLoader, WorldServer worldServer, boolean loaded, + @Nullable OnSpawnerChangeCallback onSpawnerChangeCallback) { + org.bukkit.World bukkitWorld = worldServer.getWorld(); + short requiredPlayerRange = (short) (loaded ? -1 : 16); + for (org.bukkit.Chunk bukkitChunk : chunkLoader.getLoadedChunksCollection()) { Chunk chunk = ((CraftChunk) bukkitChunk).getHandle(); for (TileEntity tileEntity : (Collection) chunk.tileEntities.values()) { if (tileEntity instanceof TileEntityMobSpawner) { - NBTTagCompound nbtTagCompound = new NBTTagCompound(); - tileEntity.b(nbtTagCompound); - nbtTagCompound.setShort("RequiredPlayerRange", requiredPlayerRange); - tileEntity.a(nbtTagCompound); + setSpawnerRange((TileEntityMobSpawner) tileEntity, requiredPlayerRange); + if (onSpawnerChangeCallback != null) { + Location location = new Location(bukkitWorld, tileEntity.x, tileEntity.y, tileEntity.z); + onSpawnerChangeCallback.apply(location, requiredPlayerRange); + } } } } } @Override - public void updateSpawner(Location location, boolean reset) { - World world = ((CraftWorld) location.getWorld()).getHandle(); + public void updateSpawner(Location location, boolean reset, + @Nullable OnSpawnerChangeCallback onSpawnerChangeCallback) { + org.bukkit.World bukkitWorld = location.getWorld(); - TileEntityMobSpawner mobSpawner = (TileEntityMobSpawner) - world.getTileEntity(location.getBlockX(), location.getBlockY(), location.getBlockZ()); + if (bukkitWorld == null) + throw new IllegalArgumentException("Cannot remove loader in null world."); + World world = ((CraftWorld) bukkitWorld).getHandle(); + TileEntity tileEntity = world.getTileEntity(location.getBlockX(), location.getBlockY(), location.getBlockZ()); + if (!(tileEntity instanceof TileEntityMobSpawner)) + return; + + TileEntityMobSpawner mobSpawner = (TileEntityMobSpawner) tileEntity; + + int requiredPlayerRange = reset ? 16 : -1; + setSpawnerRange(mobSpawner, requiredPlayerRange); + if (onSpawnerChangeCallback != null) + onSpawnerChangeCallback.apply(location, requiredPlayerRange); + } + + private static void setSpawnerRange(TileEntityMobSpawner mobSpawner, int range) { NBTTagCompound nbtTagCompound = new NBTTagCompound(); mobSpawner.b(nbtTagCompound); - nbtTagCompound.setShort("RequiredPlayerRange", (short) (reset ? 16 : -1)); + nbtTagCompound.setShort("RequiredPlayerRange", (short) range); mobSpawner.a(nbtTagCompound); } diff --git a/NMS/v1_8_R3/src/main/java/com/bgsoftware/wildloaders/nms/v1_8_R3/NMSAdapterImpl.java b/NMS/v1_8_R3/src/main/java/com/bgsoftware/wildloaders/nms/v1_8_R3/NMSAdapterImpl.java index b708f35..738c521 100644 --- a/NMS/v1_8_R3/src/main/java/com/bgsoftware/wildloaders/nms/v1_8_R3/NMSAdapterImpl.java +++ b/NMS/v1_8_R3/src/main/java/com/bgsoftware/wildloaders/nms/v1_8_R3/NMSAdapterImpl.java @@ -16,12 +16,14 @@ import net.minecraft.server.v1_8_R3.NBTTagString; import net.minecraft.server.v1_8_R3.TileEntity; import net.minecraft.server.v1_8_R3.TileEntityMobSpawner; import net.minecraft.server.v1_8_R3.World; +import net.minecraft.server.v1_8_R3.WorldServer; import org.bukkit.Location; import org.bukkit.craftbukkit.v1_8_R3.CraftChunk; import org.bukkit.craftbukkit.v1_8_R3.CraftWorld; import org.bukkit.craftbukkit.v1_8_R3.inventory.CraftItemStack; import org.bukkit.craftbukkit.v1_8_R3.util.LongHash; +import javax.annotation.Nullable; import java.util.UUID; public final class NMSAdapterImpl implements NMSAdapter { @@ -105,27 +107,30 @@ public final class NMSAdapterImpl implements NMSAdapter { } @Override - public ITileEntityChunkLoader createLoader(ChunkLoader chunkLoader) { + public ITileEntityChunkLoader createLoader(ChunkLoader chunkLoader, + @Nullable OnSpawnerChangeCallback onSpawnerChangeCallback) { Location loaderLoc = chunkLoader.getLocation(); - World world = ((CraftWorld) loaderLoc.getWorld()).getHandle(); + WorldServer worldServer = ((CraftWorld) loaderLoc.getWorld()).getHandle(); BlockPosition blockPosition = new BlockPosition(loaderLoc.getX(), loaderLoc.getY(), loaderLoc.getZ()); - TileEntityChunkLoader tileEntityChunkLoader = new TileEntityChunkLoader(chunkLoader, world, blockPosition); - world.tileEntityList.add(tileEntityChunkLoader); + TileEntityChunkLoader tileEntityChunkLoader = new TileEntityChunkLoader(chunkLoader, worldServer, blockPosition); + worldServer.tileEntityList.add(tileEntityChunkLoader); if (Scheduler.isRegionScheduler()) { - Scheduler.runTask(() -> setSpawnersRangeForLoader(chunkLoader, true)); + Scheduler.runTask(() -> + setSpawnersRangeForLoader(chunkLoader, worldServer, true, onSpawnerChangeCallback)); } else { - setSpawnersRangeForLoader(chunkLoader, true); + setSpawnersRangeForLoader(chunkLoader, worldServer, true, onSpawnerChangeCallback); } return tileEntityChunkLoader; } @Override - public void removeLoader(ChunkLoader chunkLoader, boolean spawnParticle) { + public void removeLoader(ChunkLoader chunkLoader, boolean spawnParticle, + @Nullable OnSpawnerChangeCallback onSpawnerChangeCallback) { Location loaderLoc = chunkLoader.getLocation(); - World world = ((CraftWorld) loaderLoc.getWorld()).getHandle(); + WorldServer worldServer = ((CraftWorld) loaderLoc.getWorld()).getHandle(); BlockPosition blockPosition = new BlockPosition(loaderLoc.getX(), loaderLoc.getY(), loaderLoc.getZ()); long tileEntityLong = LongHash.toLong(blockPosition.getX() >> 4, blockPosition.getZ() >> 4); @@ -133,45 +138,68 @@ public final class NMSAdapterImpl implements NMSAdapter { if (tileEntityChunkLoader != null) { tileEntityChunkLoader.holograms.forEach(EntityHolograms::removeHologram); tileEntityChunkLoader.removed = true; - world.tileEntityList.remove(tileEntityChunkLoader); + worldServer.tileEntityList.remove(tileEntityChunkLoader); } if (spawnParticle) - world.a(null, 2001, blockPosition, Block.getCombinedId(world.getType(blockPosition))); + worldServer.a(null, 2001, blockPosition, Block.getCombinedId(worldServer.getType(blockPosition))); if (Scheduler.isRegionScheduler()) { - Scheduler.runTask(() -> setSpawnersRangeForLoader(chunkLoader, false)); + Scheduler.runTask(() -> + setSpawnersRangeForLoader(chunkLoader, worldServer, false, onSpawnerChangeCallback)); } else { - setSpawnersRangeForLoader(chunkLoader, false); + setSpawnersRangeForLoader(chunkLoader, worldServer, false, onSpawnerChangeCallback); } } - private static void setSpawnersRangeForLoader(ChunkLoader chunkLoader, boolean loaded) { + private static void setSpawnersRangeForLoader(ChunkLoader chunkLoader, WorldServer worldServer, boolean loaded, + @Nullable OnSpawnerChangeCallback onSpawnerChangeCallback) { + org.bukkit.World bukkitWorld = worldServer.getWorld(); + short requiredPlayerRange = (short) (loaded ? -1 : 16); + for (org.bukkit.Chunk bukkitChunk : chunkLoader.getLoadedChunksCollection()) { Chunk chunk = ((CraftChunk) bukkitChunk).getHandle(); for (TileEntity tileEntity : chunk.tileEntities.values()) { if (tileEntity instanceof TileEntityMobSpawner) { - NBTTagCompound nbtTagCompound = new NBTTagCompound(); - tileEntity.b(nbtTagCompound); - nbtTagCompound.setShort("RequiredPlayerRange", requiredPlayerRange); - tileEntity.a(nbtTagCompound); + setSpawnerRange((TileEntityMobSpawner) tileEntity, requiredPlayerRange); + if (onSpawnerChangeCallback != null) { + BlockPosition blockPosition = tileEntity.getPosition(); + Location location = new Location(bukkitWorld, blockPosition.getX(), blockPosition.getY(), blockPosition.getZ()); + onSpawnerChangeCallback.apply(location, requiredPlayerRange); + } } } } } @Override - public void updateSpawner(Location location, boolean reset) { - World world = ((CraftWorld) location.getWorld()).getHandle(); + public void updateSpawner(Location location, boolean reset, + @Nullable OnSpawnerChangeCallback onSpawnerChangeCallback) { + org.bukkit.World bukkitWorld = location.getWorld(); + if (bukkitWorld == null) + throw new IllegalArgumentException("Cannot remove loader in null world."); + + World world = ((CraftWorld) bukkitWorld).getHandle(); BlockPosition blockPosition = new BlockPosition(location.getX(), location.getY(), location.getZ()); - TileEntityMobSpawner mobSpawner = (TileEntityMobSpawner) world.getTileEntity(blockPosition); + TileEntity tileEntity = world.getTileEntity(blockPosition); + if (!(tileEntity instanceof TileEntityMobSpawner)) + return; + TileEntityMobSpawner mobSpawner = (TileEntityMobSpawner) tileEntity; + + int requiredPlayerRange = reset ? 16 : -1; + setSpawnerRange(mobSpawner, requiredPlayerRange); + if (onSpawnerChangeCallback != null) + onSpawnerChangeCallback.apply(location, requiredPlayerRange); + } + + private static void setSpawnerRange(TileEntityMobSpawner mobSpawner, int range) { NBTTagCompound nbtTagCompound = new NBTTagCompound(); mobSpawner.b(nbtTagCompound); - nbtTagCompound.setShort("RequiredPlayerRange", (short) (reset ? 16 : -1)); + nbtTagCompound.setShort("RequiredPlayerRange", (short) range); mobSpawner.a(nbtTagCompound); } diff --git a/src/main/java/com/bgsoftware/wildloaders/handlers/LoadersHandler.java b/src/main/java/com/bgsoftware/wildloaders/handlers/LoadersHandler.java index 490db57..dec4be2 100644 --- a/src/main/java/com/bgsoftware/wildloaders/handlers/LoadersHandler.java +++ b/src/main/java/com/bgsoftware/wildloaders/handlers/LoadersHandler.java @@ -10,6 +10,7 @@ import com.bgsoftware.wildloaders.loaders.WLoaderData; import com.bgsoftware.wildloaders.utils.BlockPosition; import com.bgsoftware.wildloaders.utils.ChunkLoaderChunks; import com.bgsoftware.wildloaders.utils.ServerVersion; +import com.bgsoftware.wildloaders.utils.SpawnerChangeListener; import com.bgsoftware.wildloaders.utils.chunks.ChunkPosition; import com.bgsoftware.wildloaders.utils.database.Query; import com.google.common.collect.Maps; @@ -143,7 +144,7 @@ public final class LoadersHandler implements LoadersManager { List unloadedChunkLoaders = new LinkedList<>(); worldChunkLoaders.forEach(chunkLoader -> { - plugin.getNMSAdapter().removeLoader(chunkLoader, false); + plugin.getNMSAdapter().removeLoader(chunkLoader, false, SpawnerChangeListener.CALLBACK); BlockPosition blockPosition = removeChunkLoaderWithoutDBSave(chunkLoader); UnloadedChunkLoader unloadedChunkLoader = new UnloadedChunkLoader(chunkLoader.getLoaderData(), chunkLoader.getWhoPlaced().getUniqueId(), blockPosition, chunkLoader.getTimeLeft()); @@ -195,7 +196,8 @@ public final class LoadersHandler implements LoadersManager { @Override public void removeChunkLoaders() { - chunkLoaders.values().forEach(chunkLoader -> plugin.getNMSAdapter().removeLoader(chunkLoader, false)); + chunkLoaders.values().forEach(chunkLoader -> + plugin.getNMSAdapter().removeLoader(chunkLoader, false, SpawnerChangeListener.CALLBACK)); chunkLoaders.clear(); chunkLoadersByChunks.clear(); chunkLoadersByWorlds.clear(); diff --git a/src/main/java/com/bgsoftware/wildloaders/handlers/ProvidersHandler.java b/src/main/java/com/bgsoftware/wildloaders/handlers/ProvidersHandler.java index cf24933..ddfa431 100644 --- a/src/main/java/com/bgsoftware/wildloaders/handlers/ProvidersHandler.java +++ b/src/main/java/com/bgsoftware/wildloaders/handlers/ProvidersHandler.java @@ -2,13 +2,16 @@ package com.bgsoftware.wildloaders.handlers; import com.bgsoftware.wildloaders.WildLoadersPlugin; import com.bgsoftware.wildloaders.api.hooks.ClaimsProvider; +import com.bgsoftware.wildloaders.api.hooks.SpawnersProvider; import com.bgsoftware.wildloaders.api.hooks.TickableProvider; import com.bgsoftware.wildloaders.api.hooks.WorldsProvider; import com.bgsoftware.wildloaders.api.managers.ProvidersManager; import com.bgsoftware.wildloaders.scheduler.Scheduler; +import com.bgsoftware.wildloaders.utils.SpawnerChangeListener; import com.google.common.base.Preconditions; import org.bukkit.Bukkit; import org.bukkit.Chunk; +import org.bukkit.Location; import org.bukkit.World; import org.bukkit.plugin.Plugin; @@ -27,6 +30,7 @@ public final class ProvidersHandler implements ProvidersManager { private final List claimsProviders = new LinkedList<>(); private final List tickableProviders = new LinkedList<>(); private final List worldsProviders = new LinkedList<>(); + private final List spawnersProviders = new LinkedList<>(); public ProvidersHandler(WildLoadersPlugin plugin) { this.plugin = plugin; @@ -108,6 +112,13 @@ public final class ProvidersHandler implements ProvidersManager { claimsProviders.add(claimsProvider); } + @Override + public void addSpawnersProvider(SpawnersProvider spawnersProvider) { + Preconditions.checkNotNull(spawnersProvider, "spawnersProvider cannot be null"); + spawnersProviders.add(spawnersProvider); + SpawnerChangeListener.CALLBACK = this::setSpawnerRequiredRange; + } + @Override public void addTickableProvider(TickableProvider tickableProvider) { Preconditions.checkNotNull(tickableProvider, "tickableProvider cannot be null"); @@ -133,6 +144,12 @@ public final class ProvidersHandler implements ProvidersManager { tickableProviders.forEach(tickableProvider -> tickableProvider.tick(chunks)); } + public void setSpawnerRequiredRange(Location spawnerLocation, int requiredRange) { + for (SpawnersProvider spawnersProvider : this.spawnersProviders) { + spawnersProvider.setSpawnerRequiredRange(spawnerLocation, requiredRange); + } + } + @Nullable public World loadWorld(String worldName) { for (WorldsProvider worldsProvider : this.worldsProviders) { diff --git a/src/main/java/com/bgsoftware/wildloaders/listeners/BlocksListener.java b/src/main/java/com/bgsoftware/wildloaders/listeners/BlocksListener.java index 3fe149a..0688b47 100644 --- a/src/main/java/com/bgsoftware/wildloaders/listeners/BlocksListener.java +++ b/src/main/java/com/bgsoftware/wildloaders/listeners/BlocksListener.java @@ -5,6 +5,7 @@ import com.bgsoftware.wildloaders.WildLoadersPlugin; import com.bgsoftware.wildloaders.api.loaders.ChunkLoader; import com.bgsoftware.wildloaders.api.loaders.LoaderData; import com.bgsoftware.wildloaders.utils.ChunkLoaderChunks; +import com.bgsoftware.wildloaders.utils.SpawnerChangeListener; import com.bgsoftware.wildloaders.utils.chunks.ChunkPosition; import com.bgsoftware.wildloaders.utils.legacy.Materials; import org.bukkit.Chunk; @@ -85,7 +86,7 @@ public final class BlocksListener implements Listener { if (!plugin.getLoaders().getChunkLoader(e.getBlock().getChunk()).isPresent()) return; - plugin.getNMSAdapter().updateSpawner(e.getBlock().getLocation(), false); + plugin.getNMSAdapter().updateSpawner(e.getBlock().getLocation(), false, SpawnerChangeListener.CALLBACK); } @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true) diff --git a/src/main/java/com/bgsoftware/wildloaders/loaders/WChunkLoader.java b/src/main/java/com/bgsoftware/wildloaders/loaders/WChunkLoader.java index b0e3a93..17fbf4e 100644 --- a/src/main/java/com/bgsoftware/wildloaders/loaders/WChunkLoader.java +++ b/src/main/java/com/bgsoftware/wildloaders/loaders/WChunkLoader.java @@ -7,6 +7,7 @@ import com.bgsoftware.wildloaders.api.loaders.LoaderData; import com.bgsoftware.wildloaders.api.npc.ChunkLoaderNPC; import com.bgsoftware.wildloaders.scheduler.Scheduler; import com.bgsoftware.wildloaders.utils.BlockPosition; +import com.bgsoftware.wildloaders.utils.SpawnerChangeListener; import com.bgsoftware.wildloaders.utils.database.Query; import org.bukkit.Bukkit; import org.bukkit.Chunk; @@ -40,7 +41,7 @@ public final class WChunkLoader implements ChunkLoader { this.blockPosition = blockPosition; this.loadedChunks = loadedChunks; this.timeLeft = timeLeft; - this.tileEntityChunkLoader = plugin.getNMSAdapter().createLoader(this); + this.tileEntityChunkLoader = plugin.getNMSAdapter().createLoader(this, SpawnerChangeListener.CALLBACK); } @Override @@ -115,7 +116,8 @@ public final class WChunkLoader implements ChunkLoader { } private void removeInternal() { - plugin.getNMSAdapter().removeLoader(this, timeLeft <= 0 || isNotActive()); + plugin.getNMSAdapter().removeLoader(this, timeLeft <= 0 || isNotActive(), + SpawnerChangeListener.CALLBACK); plugin.getLoaders().removeChunkLoader(this); getLocation().getBlock().setType(Material.AIR); diff --git a/src/main/java/com/bgsoftware/wildloaders/nms/NMSAdapter.java b/src/main/java/com/bgsoftware/wildloaders/nms/NMSAdapter.java index db77701..08f71c8 100644 --- a/src/main/java/com/bgsoftware/wildloaders/nms/NMSAdapter.java +++ b/src/main/java/com/bgsoftware/wildloaders/nms/NMSAdapter.java @@ -6,6 +6,7 @@ import com.bgsoftware.wildloaders.loaders.ITileEntityChunkLoader; import org.bukkit.Location; import org.bukkit.inventory.ItemStack; +import javax.annotation.Nullable; import java.util.UUID; public interface NMSAdapter { @@ -22,10 +23,18 @@ public interface NMSAdapter { ChunkLoaderNPC createNPC(Location location, UUID uuid); - ITileEntityChunkLoader createLoader(ChunkLoader chunkLoader); + ITileEntityChunkLoader createLoader(ChunkLoader chunkLoader, + @Nullable OnSpawnerChangeCallback onSpawnerChangeCallback); - void removeLoader(ChunkLoader chunkLoader, boolean spawnParticle); + void removeLoader(ChunkLoader chunkLoader, boolean spawnParticle, + @Nullable OnSpawnerChangeCallback onSpawnerChangeCallback); - void updateSpawner(Location location, boolean reset); + void updateSpawner(Location location, boolean reset, @Nullable OnSpawnerChangeCallback onSpawnerChangeCallback); + + interface OnSpawnerChangeCallback { + + void apply(Location blockLocation, int newRange); + + } } diff --git a/src/main/java/com/bgsoftware/wildloaders/utils/SpawnerChangeListener.java b/src/main/java/com/bgsoftware/wildloaders/utils/SpawnerChangeListener.java new file mode 100644 index 0000000..9d97398 --- /dev/null +++ b/src/main/java/com/bgsoftware/wildloaders/utils/SpawnerChangeListener.java @@ -0,0 +1,13 @@ +package com.bgsoftware.wildloaders.utils; + +import com.bgsoftware.wildloaders.nms.NMSAdapter; + +public class SpawnerChangeListener { + + public static NMSAdapter.OnSpawnerChangeCallback CALLBACK = null; + + private SpawnerChangeListener() { + + } + +}