Adds support to Folia

This commit is contained in:
OmerBenGera 2024-06-11 22:05:56 +03:00
parent b1b9d1639d
commit b416e6c2c4
25 changed files with 565 additions and 275 deletions

22
Hooks/Folia/build.gradle Normal file
View File

@ -0,0 +1,22 @@
group 'Hooks:Folia'
java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(17))
}
}
repositories {
maven { url 'https://repo.papermc.io/repository/maven-public/' }
}
dependencies {
compileOnly 'dev.folia:folia-api:1.20.1-R0.1-SNAPSHOT'
compileOnly project(":API")
compileOnly rootProject
}
if (project.hasProperty('hook.compile_folia') &&
!Boolean.valueOf(project.findProperty("hook.compile_folia").toString())) {
project.tasks.all { task -> task.enabled = false }
}

View File

@ -0,0 +1,85 @@
package com.bgsoftware.wildloaders.scheduler;
import com.bgsoftware.wildloaders.WildLoadersPlugin;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.entity.Entity;
import java.util.concurrent.TimeUnit;
public class FoliaSchedulerImplementation implements ISchedulerImplementation {
public static final FoliaSchedulerImplementation INSTANCE = new FoliaSchedulerImplementation();
private static final WildLoadersPlugin plugin = WildLoadersPlugin.getPlugin();
private FoliaSchedulerImplementation() {
}
@Override
public boolean isRegionScheduler() {
return true;
}
@Override
public ScheduledTask scheduleTask(World world, int chunkX, int chunkZ, Runnable task, long delay) {
io.papermc.paper.threadedregions.scheduler.ScheduledTask handle;
if (delay <= 0) {
handle = Bukkit.getServer().getRegionScheduler().run(plugin, world, chunkX, chunkZ, v -> task.run());
} else {
handle = Bukkit.getServer().getRegionScheduler().runDelayed(plugin, world, chunkX, chunkZ, v -> task.run(), delay);
}
return new FoliaScheduledTask(handle);
}
@Override
public ScheduledTask scheduleTask(Entity entity, Runnable task, long delay) {
io.papermc.paper.threadedregions.scheduler.ScheduledTask handle;
if (delay <= 0) {
handle = entity.getScheduler().run(plugin, v -> task.run(), task);
} else {
handle = entity.getScheduler().runDelayed(plugin, v -> task.run(), task, delay);
}
return new FoliaScheduledTask(handle);
}
@Override
public ScheduledTask scheduleTask(Runnable task, long delay) {
io.papermc.paper.threadedregions.scheduler.ScheduledTask handle;
if (delay <= 0) {
handle = Bukkit.getServer().getGlobalRegionScheduler().run(plugin, v -> task.run());
} else {
handle = Bukkit.getServer().getGlobalRegionScheduler().runDelayed(plugin, v -> task.run(), delay);
}
return new FoliaScheduledTask(handle);
}
@Override
public ScheduledTask scheduleAsyncTask(Runnable task, long delay) {
io.papermc.paper.threadedregions.scheduler.ScheduledTask handle;
if (delay <= 0) {
handle = Bukkit.getServer().getAsyncScheduler().runNow(plugin, v -> task.run());
} else {
handle = Bukkit.getServer().getAsyncScheduler().runDelayed(plugin, v -> task.run(), delay * 50L, TimeUnit.MILLISECONDS);
}
return new FoliaScheduledTask(handle);
}
private static class FoliaScheduledTask implements ScheduledTask {
private final io.papermc.paper.threadedregions.scheduler.ScheduledTask handle;
public FoliaScheduledTask(io.papermc.paper.threadedregions.scheduler.ScheduledTask handle) {
this.handle = handle;
}
@Override
public void cancel() {
if (!handle.isCancelled())
handle.cancel();
}
}
}

View File

@ -5,6 +5,7 @@ import com.bgsoftware.wildloaders.api.loaders.ChunkLoader;
import com.bgsoftware.wildloaders.loaders.ITileEntityChunkLoader;
import com.bgsoftware.wildloaders.nms.NMSAdapter;
import com.bgsoftware.wildloaders.nms.v1_12_R1.loader.TileEntityChunkLoader;
import com.bgsoftware.wildloaders.scheduler.Scheduler;
import net.minecraft.server.v1_12_R1.Block;
import net.minecraft.server.v1_12_R1.BlockPosition;
import net.minecraft.server.v1_12_R1.Chunk;
@ -113,18 +114,10 @@ public final class NMSAdapterImpl implements NMSAdapter {
TileEntityChunkLoader tileEntityChunkLoader = new TileEntityChunkLoader(chunkLoader, world, blockPosition);
world.tileEntityListTick.add(tileEntityChunkLoader);
for (org.bukkit.Chunk bukkitChunk : chunkLoader.getLoadedChunks()) {
Chunk chunk = ((CraftChunk) bukkitChunk).getHandle();
chunk.tileEntities.values().stream().filter(tileEntity -> tileEntity instanceof TileEntityMobSpawner).forEach(tileEntity -> {
NBTTagCompound nbtTagCompound = new NBTTagCompound();
tileEntity.save(nbtTagCompound);
nbtTagCompound.setShort("RequiredPlayerRange", (short) -1);
if (TILE_ENTITY_LOAD.isValid()) {
TILE_ENTITY_LOAD.invoke(tileEntity, nbtTagCompound);
} else {
tileEntity.a(nbtTagCompound);
}
});
if (Scheduler.isRegionScheduler()) {
Scheduler.runTask(() -> setSpawnersRangeForLoader(chunkLoader, true));
} else {
setSpawnersRangeForLoader(chunkLoader, true);
}
return tileEntityChunkLoader;
@ -147,18 +140,30 @@ public final class NMSAdapterImpl implements NMSAdapter {
if (spawnParticle)
world.a(null, 2001, blockPosition, Block.getCombinedId(world.getType(blockPosition)));
for (org.bukkit.Chunk bukkitChunk : chunkLoader.getLoadedChunks()) {
if (Scheduler.isRegionScheduler()) {
Scheduler.runTask(() -> setSpawnersRangeForLoader(chunkLoader, false));
} else {
setSpawnersRangeForLoader(chunkLoader, false);
}
}
private static void setSpawnersRangeForLoader(ChunkLoader chunkLoader, boolean loaded) {
short requiredPlayerRange = (short) (loaded ? -1 : 16);
for (org.bukkit.Chunk bukkitChunk : chunkLoader.getLoadedChunksCollection()) {
Chunk chunk = ((CraftChunk) bukkitChunk).getHandle();
chunk.tileEntities.values().stream().filter(tileEntity -> tileEntity instanceof TileEntityMobSpawner).forEach(tileEntity -> {
NBTTagCompound nbtTagCompound = new NBTTagCompound();
tileEntity.save(nbtTagCompound);
nbtTagCompound.setShort("RequiredPlayerRange", (short) 16);
if (TILE_ENTITY_LOAD.isValid()) {
TILE_ENTITY_LOAD.invoke(tileEntity, nbtTagCompound);
} else {
tileEntity.a(nbtTagCompound);
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);
}
}
});
}
}
}

View File

@ -4,6 +4,7 @@ import com.bgsoftware.wildloaders.api.loaders.ChunkLoader;
import com.bgsoftware.wildloaders.loaders.ITileEntityChunkLoader;
import com.bgsoftware.wildloaders.nms.NMSAdapter;
import com.bgsoftware.wildloaders.nms.v1_16_R3.loader.TileEntityChunkLoader;
import com.bgsoftware.wildloaders.scheduler.Scheduler;
import net.minecraft.server.v1_16_R3.Block;
import net.minecraft.server.v1_16_R3.BlockPosition;
import net.minecraft.server.v1_16_R3.Chunk;
@ -14,6 +15,7 @@ import net.minecraft.server.v1_16_R3.NBTTagCompound;
import net.minecraft.server.v1_16_R3.NBTTagList;
import net.minecraft.server.v1_16_R3.NBTTagLong;
import net.minecraft.server.v1_16_R3.NBTTagString;
import net.minecraft.server.v1_16_R3.TileEntity;
import net.minecraft.server.v1_16_R3.TileEntityMobSpawner;
import net.minecraft.server.v1_16_R3.World;
import net.minecraft.server.v1_16_R3.WorldServer;
@ -114,12 +116,10 @@ public final class NMSAdapterImpl implements NMSAdapter {
TileEntityChunkLoader tileEntityChunkLoader = new TileEntityChunkLoader(chunkLoader, world, blockPosition);
world.tileEntityListTick.add(tileEntityChunkLoader);
for (org.bukkit.Chunk bukkitChunk : chunkLoader.getLoadedChunks()) {
Chunk chunk = ((CraftChunk) bukkitChunk).getHandle();
chunk.tileEntities.values().stream().filter(tileEntity -> tileEntity instanceof TileEntityMobSpawner)
.forEach(tileEntity -> ((TileEntityMobSpawner) tileEntity).getSpawner().requiredPlayerRange = -1);
world.setForceLoaded(chunk.getPos().x, chunk.getPos().z, true);
if (Scheduler.isRegionScheduler()) {
Scheduler.runTask(() -> setChunksForcedForLoader(chunkLoader, world, true));
} else {
setChunksForcedForLoader(chunkLoader, world, true);
}
return tileEntityChunkLoader;
@ -143,12 +143,25 @@ public final class NMSAdapterImpl implements NMSAdapter {
if (spawnParticle)
world.a(null, 2001, blockPosition, Block.getCombinedId(world.getType(blockPosition)));
for (org.bukkit.Chunk bukkitChunk : chunkLoader.getLoadedChunks()) {
Chunk chunk = ((CraftChunk) bukkitChunk).getHandle();
chunk.tileEntities.values().stream().filter(tileEntity -> tileEntity instanceof TileEntityMobSpawner)
.forEach(tileEntity -> ((TileEntityMobSpawner) tileEntity).getSpawner().requiredPlayerRange = 16);
if (Scheduler.isRegionScheduler()) {
Scheduler.runTask(() -> setChunksForcedForLoader(chunkLoader, world, false));
} else {
setChunksForcedForLoader(chunkLoader, world, false);
}
}
world.setForceLoaded(chunk.getPos().x, chunk.getPos().z, false);
private static void setChunksForcedForLoader(ChunkLoader chunkLoader, WorldServer worldServer, boolean forced) {
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)
((TileEntityMobSpawner) tileEntity).getSpawner().requiredPlayerRange = requiredPlayerRange;
}
ChunkCoordIntPair chunkCoord = chunk.getPos();
worldServer.setForceLoaded(chunkCoord.x, chunkCoord.z, forced);
}
}

View File

@ -5,6 +5,7 @@ import com.bgsoftware.wildloaders.loaders.ITileEntityChunkLoader;
import com.bgsoftware.wildloaders.nms.NMSAdapter;
import com.bgsoftware.wildloaders.nms.v1_17.loader.ChunkLoaderBlockEntity;
import com.bgsoftware.wildloaders.nms.v1_17.npc.ChunkLoaderNPCWrapper;
import com.bgsoftware.wildloaders.scheduler.Scheduler;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
@ -105,16 +106,10 @@ public final class NMSAdapterImpl implements NMSAdapter {
ChunkLoaderBlockEntity ChunkLoaderBlockEntity = new ChunkLoaderBlockEntity(chunkLoader, serverLevel, blockPos);
serverLevel.addBlockEntityTicker(ChunkLoaderBlockEntity.getTicker());
for (org.bukkit.Chunk bukkitChunk : chunkLoader.getLoadedChunks()) {
LevelChunk levelChunk = ((CraftChunk) bukkitChunk).getHandle();
levelChunk.getBlockEntities().values().stream()
.filter(blockEntity -> blockEntity instanceof SpawnerBlockEntity)
.forEach(blockEntity -> {
((SpawnerBlockEntity) blockEntity).getSpawner().requiredPlayerRange = -1;
});
ChunkPos chunkPos = levelChunk.getPos();
serverLevel.setChunkForced(chunkPos.x, chunkPos.z, true);
if (Scheduler.isRegionScheduler()) {
Scheduler.runTask(() -> setChunksForcedForLoader(chunkLoader, serverLevel, true));
} else {
setChunksForcedForLoader(chunkLoader, serverLevel, true);
}
return ChunkLoaderBlockEntity;
@ -142,16 +137,25 @@ public final class NMSAdapterImpl implements NMSAdapter {
if (spawnParticle)
serverLevel.levelEvent(null, 2001, blockPos, Block.getId(serverLevel.getBlockState(blockPos)));
for (org.bukkit.Chunk bukkitChunk : chunkLoader.getLoadedChunks()) {
if (Scheduler.isRegionScheduler()) {
Scheduler.runTask(() -> setChunksForcedForLoader(chunkLoader, serverLevel, false));
} else {
setChunksForcedForLoader(chunkLoader, serverLevel, false);
}
}
private static void setChunksForcedForLoader(ChunkLoader chunkLoader, ServerLevel serverLevel, boolean forced) {
int requiredPlayerRange = forced ? -1 : 16;
for (org.bukkit.Chunk bukkitChunk : chunkLoader.getLoadedChunksCollection()) {
LevelChunk levelChunk = ((CraftChunk) bukkitChunk).getHandle();
levelChunk.getBlockEntities().values().stream()
.filter(blockEntity -> blockEntity instanceof SpawnerBlockEntity)
.forEach(blockEntity -> {
((SpawnerBlockEntity) blockEntity).getSpawner().requiredPlayerRange = 16;
});
for (BlockEntity blockEntity : levelChunk.getBlockEntities().values()) {
if (blockEntity instanceof SpawnerBlockEntity spawnerBlockEntity)
spawnerBlockEntity.getSpawner().requiredPlayerRange = requiredPlayerRange;
}
ChunkPos chunkPos = levelChunk.getPos();
serverLevel.setChunkForced(chunkPos.x, chunkPos.z, false);
serverLevel.setChunkForced(chunkPos.x, chunkPos.z, forced);
}
}

View File

@ -4,6 +4,7 @@ import com.bgsoftware.wildloaders.api.loaders.ChunkLoader;
import com.bgsoftware.wildloaders.loaders.ITileEntityChunkLoader;
import com.bgsoftware.wildloaders.nms.NMSAdapter;
import com.bgsoftware.wildloaders.nms.v1_18.loader.ChunkLoaderBlockEntity;
import com.bgsoftware.wildloaders.scheduler.Scheduler;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
@ -104,16 +105,10 @@ public final class NMSAdapterImpl implements NMSAdapter {
ChunkLoaderBlockEntity ChunkLoaderBlockEntity = new ChunkLoaderBlockEntity(chunkLoader, serverLevel, blockPos);
serverLevel.addBlockEntityTicker(ChunkLoaderBlockEntity.getTicker());
for (org.bukkit.Chunk bukkitChunk : chunkLoader.getLoadedChunks()) {
LevelChunk levelChunk = ((CraftChunk) bukkitChunk).getHandle();
levelChunk.getBlockEntities().values().stream()
.filter(blockEntity -> blockEntity instanceof SpawnerBlockEntity)
.forEach(blockEntity -> {
((SpawnerBlockEntity) blockEntity).getSpawner().requiredPlayerRange = -1;
});
ChunkPos chunkPos = levelChunk.getPos();
serverLevel.setChunkForced(chunkPos.x, chunkPos.z, true);
if (Scheduler.isRegionScheduler()) {
Scheduler.runTask(() -> setChunksForcedForLoader(chunkLoader, serverLevel, true));
} else {
setChunksForcedForLoader(chunkLoader, serverLevel, true);
}
return ChunkLoaderBlockEntity;
@ -141,16 +136,25 @@ public final class NMSAdapterImpl implements NMSAdapter {
if (spawnParticle)
serverLevel.levelEvent(null, 2001, blockPos, Block.getId(serverLevel.getBlockState(blockPos)));
for (org.bukkit.Chunk bukkitChunk : chunkLoader.getLoadedChunks()) {
if (Scheduler.isRegionScheduler()) {
Scheduler.runTask(() -> setChunksForcedForLoader(chunkLoader, serverLevel, false));
} else {
setChunksForcedForLoader(chunkLoader, serverLevel, false);
}
}
private static void setChunksForcedForLoader(ChunkLoader chunkLoader, ServerLevel serverLevel, boolean forced) {
int requiredPlayerRange = forced ? -1 : 16;
for (org.bukkit.Chunk bukkitChunk : chunkLoader.getLoadedChunksCollection()) {
LevelChunk levelChunk = ((CraftChunk) bukkitChunk).getHandle();
levelChunk.getBlockEntities().values().stream()
.filter(blockEntity -> blockEntity instanceof SpawnerBlockEntity)
.forEach(blockEntity -> {
((SpawnerBlockEntity) blockEntity).getSpawner().requiredPlayerRange = 16;
});
for (BlockEntity blockEntity : levelChunk.getBlockEntities().values()) {
if (blockEntity instanceof SpawnerBlockEntity spawnerBlockEntity)
spawnerBlockEntity.getSpawner().requiredPlayerRange = requiredPlayerRange;
}
ChunkPos chunkPos = levelChunk.getPos();
serverLevel.setChunkForced(chunkPos.x, chunkPos.z, false);
serverLevel.setChunkForced(chunkPos.x, chunkPos.z, forced);
}
}

View File

@ -4,6 +4,7 @@ import com.bgsoftware.wildloaders.api.loaders.ChunkLoader;
import com.bgsoftware.wildloaders.loaders.ITileEntityChunkLoader;
import com.bgsoftware.wildloaders.nms.NMSAdapter;
import com.bgsoftware.wildloaders.nms.v1_19.loader.ChunkLoaderBlockEntity;
import com.bgsoftware.wildloaders.scheduler.Scheduler;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
@ -103,16 +104,10 @@ public final class NMSAdapterImpl implements NMSAdapter {
ChunkLoaderBlockEntity ChunkLoaderBlockEntity = new ChunkLoaderBlockEntity(chunkLoader, serverLevel, blockPos);
serverLevel.addBlockEntityTicker(ChunkLoaderBlockEntity.getTicker());
for (org.bukkit.Chunk bukkitChunk : chunkLoader.getLoadedChunks()) {
LevelChunk levelChunk = serverLevel.getChunk(bukkitChunk.getX(), bukkitChunk.getZ());
levelChunk.getBlockEntities().values().stream()
.filter(blockEntity -> blockEntity instanceof SpawnerBlockEntity)
.forEach(blockEntity -> {
((SpawnerBlockEntity) blockEntity).getSpawner().requiredPlayerRange = -1;
});
ChunkPos chunkPos = levelChunk.getPos();
serverLevel.setChunkForced(chunkPos.x, chunkPos.z, true);
if (Scheduler.isRegionScheduler()) {
Scheduler.runTask(() -> setChunksForcedForLoader(chunkLoader, serverLevel, true));
} else {
setChunksForcedForLoader(chunkLoader, serverLevel, true);
}
return ChunkLoaderBlockEntity;
@ -140,16 +135,25 @@ public final class NMSAdapterImpl implements NMSAdapter {
if (spawnParticle)
serverLevel.levelEvent(null, 2001, blockPos, Block.getId(serverLevel.getBlockState(blockPos)));
for (org.bukkit.Chunk bukkitChunk : chunkLoader.getLoadedChunks()) {
if (Scheduler.isRegionScheduler()) {
Scheduler.runTask(() -> setChunksForcedForLoader(chunkLoader, serverLevel, false));
} else {
setChunksForcedForLoader(chunkLoader, serverLevel, false);
}
}
private static void setChunksForcedForLoader(ChunkLoader chunkLoader, ServerLevel serverLevel, boolean forced) {
int requiredPlayerRange = forced ? -1 : 16;
for (org.bukkit.Chunk bukkitChunk : chunkLoader.getLoadedChunksCollection()) {
LevelChunk levelChunk = serverLevel.getChunk(bukkitChunk.getX(), bukkitChunk.getZ());
levelChunk.getBlockEntities().values().stream()
.filter(blockEntity -> blockEntity instanceof SpawnerBlockEntity)
.forEach(blockEntity -> {
((SpawnerBlockEntity) blockEntity).getSpawner().requiredPlayerRange = 16;
});
for (BlockEntity blockEntity : levelChunk.getBlockEntities().values()) {
if (blockEntity instanceof SpawnerBlockEntity spawnerBlockEntity)
spawnerBlockEntity.getSpawner().requiredPlayerRange = requiredPlayerRange;
}
ChunkPos chunkPos = levelChunk.getPos();
serverLevel.setChunkForced(chunkPos.x, chunkPos.z, false);
serverLevel.setChunkForced(chunkPos.x, chunkPos.z, forced);
}
}

View File

@ -4,6 +4,7 @@ import com.bgsoftware.wildloaders.api.loaders.ChunkLoader;
import com.bgsoftware.wildloaders.loaders.ITileEntityChunkLoader;
import com.bgsoftware.wildloaders.nms.NMSAdapter;
import com.bgsoftware.wildloaders.nms.v1_20_1.loader.ChunkLoaderBlockEntity;
import com.bgsoftware.wildloaders.scheduler.Scheduler;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
@ -103,16 +104,10 @@ public final class NMSAdapterImpl implements NMSAdapter {
ChunkLoaderBlockEntity ChunkLoaderBlockEntity = new ChunkLoaderBlockEntity(chunkLoader, serverLevel, blockPos);
serverLevel.addBlockEntityTicker(ChunkLoaderBlockEntity.getTicker());
for (org.bukkit.Chunk bukkitChunk : chunkLoader.getLoadedChunks()) {
LevelChunk levelChunk = serverLevel.getChunk(bukkitChunk.getX(), bukkitChunk.getZ());
levelChunk.getBlockEntities().values().stream()
.filter(blockEntity -> blockEntity instanceof SpawnerBlockEntity)
.forEach(blockEntity -> {
((SpawnerBlockEntity) blockEntity).getSpawner().requiredPlayerRange = -1;
});
ChunkPos chunkPos = levelChunk.getPos();
serverLevel.setChunkForced(chunkPos.x, chunkPos.z, true);
if (Scheduler.isRegionScheduler()) {
Scheduler.runTask(() -> setChunksForcedForLoader(chunkLoader, serverLevel, true));
} else {
setChunksForcedForLoader(chunkLoader, serverLevel, true);
}
return ChunkLoaderBlockEntity;
@ -140,16 +135,25 @@ public final class NMSAdapterImpl implements NMSAdapter {
if (spawnParticle)
serverLevel.levelEvent(null, 2001, blockPos, Block.getId(serverLevel.getBlockState(blockPos)));
for (org.bukkit.Chunk bukkitChunk : chunkLoader.getLoadedChunks()) {
if (Scheduler.isRegionScheduler()) {
Scheduler.runTask(() -> setChunksForcedForLoader(chunkLoader, serverLevel, false));
} else {
setChunksForcedForLoader(chunkLoader, serverLevel, false);
}
}
private static void setChunksForcedForLoader(ChunkLoader chunkLoader, ServerLevel serverLevel, boolean forced) {
int requiredPlayerRange = forced ? -1 : 16;
for (org.bukkit.Chunk bukkitChunk : chunkLoader.getLoadedChunksCollection()) {
LevelChunk levelChunk = serverLevel.getChunk(bukkitChunk.getX(), bukkitChunk.getZ());
levelChunk.getBlockEntities().values().stream()
.filter(blockEntity -> blockEntity instanceof SpawnerBlockEntity)
.forEach(blockEntity -> {
((SpawnerBlockEntity) blockEntity).getSpawner().requiredPlayerRange = 16;
});
for (BlockEntity blockEntity : levelChunk.getBlockEntities().values()) {
if (blockEntity instanceof SpawnerBlockEntity spawnerBlockEntity)
spawnerBlockEntity.getSpawner().requiredPlayerRange = requiredPlayerRange;
}
ChunkPos chunkPos = levelChunk.getPos();
serverLevel.setChunkForced(chunkPos.x, chunkPos.z, false);
serverLevel.setChunkForced(chunkPos.x, chunkPos.z, forced);
}
}

View File

@ -4,6 +4,7 @@ import com.bgsoftware.wildloaders.api.loaders.ChunkLoader;
import com.bgsoftware.wildloaders.loaders.ITileEntityChunkLoader;
import com.bgsoftware.wildloaders.nms.NMSAdapter;
import com.bgsoftware.wildloaders.nms.v1_20_2.loader.ChunkLoaderBlockEntity;
import com.bgsoftware.wildloaders.scheduler.Scheduler;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
@ -103,16 +104,10 @@ public final class NMSAdapterImpl implements NMSAdapter {
ChunkLoaderBlockEntity ChunkLoaderBlockEntity = new ChunkLoaderBlockEntity(chunkLoader, serverLevel, blockPos);
serverLevel.addBlockEntityTicker(ChunkLoaderBlockEntity.getTicker());
for (org.bukkit.Chunk bukkitChunk : chunkLoader.getLoadedChunks()) {
LevelChunk levelChunk = serverLevel.getChunk(bukkitChunk.getX(), bukkitChunk.getZ());
levelChunk.getBlockEntities().values().stream()
.filter(blockEntity -> blockEntity instanceof SpawnerBlockEntity)
.forEach(blockEntity -> {
((SpawnerBlockEntity) blockEntity).getSpawner().requiredPlayerRange = -1;
});
ChunkPos chunkPos = levelChunk.getPos();
serverLevel.setChunkForced(chunkPos.x, chunkPos.z, true);
if (Scheduler.isRegionScheduler()) {
Scheduler.runTask(() -> setChunksForcedForLoader(chunkLoader, serverLevel, true));
} else {
setChunksForcedForLoader(chunkLoader, serverLevel, true);
}
return ChunkLoaderBlockEntity;
@ -140,16 +135,25 @@ public final class NMSAdapterImpl implements NMSAdapter {
if (spawnParticle)
serverLevel.levelEvent(null, 2001, blockPos, Block.getId(serverLevel.getBlockState(blockPos)));
for (org.bukkit.Chunk bukkitChunk : chunkLoader.getLoadedChunks()) {
if (Scheduler.isRegionScheduler()) {
Scheduler.runTask(() -> setChunksForcedForLoader(chunkLoader, serverLevel, false));
} else {
setChunksForcedForLoader(chunkLoader, serverLevel, false);
}
}
private static void setChunksForcedForLoader(ChunkLoader chunkLoader, ServerLevel serverLevel, boolean forced) {
int requiredPlayerRange = forced ? -1 : 16;
for (org.bukkit.Chunk bukkitChunk : chunkLoader.getLoadedChunksCollection()) {
LevelChunk levelChunk = serverLevel.getChunk(bukkitChunk.getX(), bukkitChunk.getZ());
levelChunk.getBlockEntities().values().stream()
.filter(blockEntity -> blockEntity instanceof SpawnerBlockEntity)
.forEach(blockEntity -> {
((SpawnerBlockEntity) blockEntity).getSpawner().requiredPlayerRange = 16;
});
for (BlockEntity blockEntity : levelChunk.getBlockEntities().values()) {
if (blockEntity instanceof SpawnerBlockEntity spawnerBlockEntity)
spawnerBlockEntity.getSpawner().requiredPlayerRange = requiredPlayerRange;
}
ChunkPos chunkPos = levelChunk.getPos();
serverLevel.setChunkForced(chunkPos.x, chunkPos.z, false);
serverLevel.setChunkForced(chunkPos.x, chunkPos.z, forced);
}
}

View File

@ -4,6 +4,7 @@ import com.bgsoftware.wildloaders.api.loaders.ChunkLoader;
import com.bgsoftware.wildloaders.loaders.ITileEntityChunkLoader;
import com.bgsoftware.wildloaders.nms.NMSAdapter;
import com.bgsoftware.wildloaders.nms.v1_20_3.loader.ChunkLoaderBlockEntity;
import com.bgsoftware.wildloaders.scheduler.Scheduler;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
@ -103,16 +104,10 @@ public final class NMSAdapterImpl implements NMSAdapter {
ChunkLoaderBlockEntity ChunkLoaderBlockEntity = new ChunkLoaderBlockEntity(chunkLoader, serverLevel, blockPos);
serverLevel.addBlockEntityTicker(ChunkLoaderBlockEntity.getTicker());
for (org.bukkit.Chunk bukkitChunk : chunkLoader.getLoadedChunks()) {
LevelChunk levelChunk = serverLevel.getChunk(bukkitChunk.getX(), bukkitChunk.getZ());
levelChunk.getBlockEntities().values().stream()
.filter(blockEntity -> blockEntity instanceof SpawnerBlockEntity)
.forEach(blockEntity -> {
((SpawnerBlockEntity) blockEntity).getSpawner().requiredPlayerRange = -1;
});
ChunkPos chunkPos = levelChunk.getPos();
serverLevel.setChunkForced(chunkPos.x, chunkPos.z, true);
if (Scheduler.isRegionScheduler()) {
Scheduler.runTask(() -> setChunksForcedForLoader(chunkLoader, serverLevel, true));
} else {
setChunksForcedForLoader(chunkLoader, serverLevel, true);
}
return ChunkLoaderBlockEntity;
@ -140,16 +135,25 @@ public final class NMSAdapterImpl implements NMSAdapter {
if (spawnParticle)
serverLevel.levelEvent(null, 2001, blockPos, Block.getId(serverLevel.getBlockState(blockPos)));
for (org.bukkit.Chunk bukkitChunk : chunkLoader.getLoadedChunks()) {
if (Scheduler.isRegionScheduler()) {
Scheduler.runTask(() -> setChunksForcedForLoader(chunkLoader, serverLevel, false));
} else {
setChunksForcedForLoader(chunkLoader, serverLevel, false);
}
}
private static void setChunksForcedForLoader(ChunkLoader chunkLoader, ServerLevel serverLevel, boolean forced) {
int requiredPlayerRange = forced ? -1 : 16;
for (org.bukkit.Chunk bukkitChunk : chunkLoader.getLoadedChunksCollection()) {
LevelChunk levelChunk = serverLevel.getChunk(bukkitChunk.getX(), bukkitChunk.getZ());
levelChunk.getBlockEntities().values().stream()
.filter(blockEntity -> blockEntity instanceof SpawnerBlockEntity)
.forEach(blockEntity -> {
((SpawnerBlockEntity) blockEntity).getSpawner().requiredPlayerRange = 16;
});
for (BlockEntity blockEntity : levelChunk.getBlockEntities().values()) {
if (blockEntity instanceof SpawnerBlockEntity spawnerBlockEntity)
spawnerBlockEntity.getSpawner().requiredPlayerRange = requiredPlayerRange;
}
ChunkPos chunkPos = levelChunk.getPos();
serverLevel.setChunkForced(chunkPos.x, chunkPos.z, false);
serverLevel.setChunkForced(chunkPos.x, chunkPos.z, forced);
}
}

View File

@ -4,6 +4,7 @@ import com.bgsoftware.wildloaders.api.loaders.ChunkLoader;
import com.bgsoftware.wildloaders.loaders.ITileEntityChunkLoader;
import com.bgsoftware.wildloaders.nms.NMSAdapter;
import com.bgsoftware.wildloaders.nms.v1_20_4.loader.ChunkLoaderBlockEntity;
import com.bgsoftware.wildloaders.scheduler.Scheduler;
import com.mojang.authlib.properties.Property;
import com.mojang.authlib.properties.PropertyMap;
import net.minecraft.core.BlockPos;
@ -109,16 +110,10 @@ public final class NMSAdapterImpl implements NMSAdapter {
ChunkLoaderBlockEntity ChunkLoaderBlockEntity = new ChunkLoaderBlockEntity(chunkLoader, serverLevel, blockPos);
serverLevel.addBlockEntityTicker(ChunkLoaderBlockEntity.getTicker());
for (org.bukkit.Chunk bukkitChunk : chunkLoader.getLoadedChunks()) {
LevelChunk levelChunk = serverLevel.getChunk(bukkitChunk.getX(), bukkitChunk.getZ());
levelChunk.getBlockEntities().values().stream()
.filter(blockEntity -> blockEntity instanceof SpawnerBlockEntity)
.forEach(blockEntity -> {
((SpawnerBlockEntity) blockEntity).getSpawner().requiredPlayerRange = -1;
});
ChunkPos chunkPos = levelChunk.getPos();
serverLevel.setChunkForced(chunkPos.x, chunkPos.z, true);
if (Scheduler.isRegionScheduler()) {
Scheduler.runTask(() -> setChunksForcedForLoader(chunkLoader, serverLevel, true));
} else {
setChunksForcedForLoader(chunkLoader, serverLevel, true);
}
return ChunkLoaderBlockEntity;
@ -146,16 +141,25 @@ public final class NMSAdapterImpl implements NMSAdapter {
if (spawnParticle)
serverLevel.levelEvent(null, 2001, blockPos, Block.getId(serverLevel.getBlockState(blockPos)));
for (org.bukkit.Chunk bukkitChunk : chunkLoader.getLoadedChunks()) {
if (Scheduler.isRegionScheduler()) {
Scheduler.runTask(() -> setChunksForcedForLoader(chunkLoader, serverLevel, false));
} else {
setChunksForcedForLoader(chunkLoader, serverLevel, false);
}
}
private static void setChunksForcedForLoader(ChunkLoader chunkLoader, ServerLevel serverLevel, boolean forced) {
int requiredPlayerRange = forced ? -1 : 16;
for (org.bukkit.Chunk bukkitChunk : chunkLoader.getLoadedChunksCollection()) {
LevelChunk levelChunk = serverLevel.getChunk(bukkitChunk.getX(), bukkitChunk.getZ());
levelChunk.getBlockEntities().values().stream()
.filter(blockEntity -> blockEntity instanceof SpawnerBlockEntity)
.forEach(blockEntity -> {
((SpawnerBlockEntity) blockEntity).getSpawner().requiredPlayerRange = 16;
});
for (BlockEntity blockEntity : levelChunk.getBlockEntities().values()) {
if (blockEntity instanceof SpawnerBlockEntity spawnerBlockEntity)
spawnerBlockEntity.getSpawner().requiredPlayerRange = requiredPlayerRange;
}
ChunkPos chunkPos = levelChunk.getPos();
serverLevel.setChunkForced(chunkPos.x, chunkPos.z, false);
serverLevel.setChunkForced(chunkPos.x, chunkPos.z, forced);
}
}

View File

@ -4,6 +4,7 @@ import com.bgsoftware.wildloaders.api.loaders.ChunkLoader;
import com.bgsoftware.wildloaders.loaders.ITileEntityChunkLoader;
import com.bgsoftware.wildloaders.nms.NMSAdapter;
import com.bgsoftware.wildloaders.nms.v1_7_R4.loader.TileEntityChunkLoader;
import com.bgsoftware.wildloaders.scheduler.Scheduler;
import net.minecraft.server.v1_7_R4.Block;
import net.minecraft.server.v1_7_R4.Chunk;
import net.minecraft.server.v1_7_R4.ItemStack;
@ -20,6 +21,7 @@ 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 java.util.Collection;
import java.util.UUID;
public final class NMSAdapterImpl implements NMSAdapter {
@ -112,15 +114,10 @@ public final class NMSAdapterImpl implements NMSAdapter {
//noinspection unchecked
world.tileEntityList.add(tileEntityChunkLoader);
for (org.bukkit.Chunk bukkitChunk : chunkLoader.getLoadedChunks()) {
Chunk chunk = ((CraftChunk) bukkitChunk).getHandle();
//noinspection unchecked
chunk.tileEntities.values().stream().filter(tileEntity -> tileEntity instanceof TileEntityMobSpawner).forEach(tileEntity -> {
NBTTagCompound nbtTagCompound = new NBTTagCompound();
((TileEntity) tileEntity).b(nbtTagCompound);
nbtTagCompound.setShort("RequiredPlayerRange", (short) -1);
((TileEntity) tileEntity).a(nbtTagCompound);
});
if (Scheduler.isRegionScheduler()) {
Scheduler.runTask(() -> setSpawnersRangeForLoader(chunkLoader, true));
} else {
setSpawnersRangeForLoader(chunkLoader, true);
}
return tileEntityChunkLoader;
@ -142,15 +139,26 @@ public final class NMSAdapterImpl implements NMSAdapter {
if (spawnParticle)
world.a(null, 2001, x, y, z, Block.getId(world.getType(x, y, z)) + (world.getData(x, y, z) << 12));
for (org.bukkit.Chunk bukkitChunk : chunkLoader.getLoadedChunks()) {
if (Scheduler.isRegionScheduler()) {
Scheduler.runTask(() -> setSpawnersRangeForLoader(chunkLoader, false));
} else {
setSpawnersRangeForLoader(chunkLoader, false);
}
}
private static void setSpawnersRangeForLoader(ChunkLoader chunkLoader, boolean loaded) {
short requiredPlayerRange = (short) (loaded ? -1 : 16);
for (org.bukkit.Chunk bukkitChunk : chunkLoader.getLoadedChunksCollection()) {
Chunk chunk = ((CraftChunk) bukkitChunk).getHandle();
//noinspection unchecked
chunk.tileEntities.values().stream().filter(tileEntity -> tileEntity instanceof TileEntityMobSpawner).forEach(tileEntity -> {
NBTTagCompound nbtTagCompound = new NBTTagCompound();
((TileEntity) tileEntity).b(nbtTagCompound);
nbtTagCompound.setShort("RequiredPlayerRange", (short) 16);
((TileEntity) tileEntity).a(nbtTagCompound);
});
for (TileEntity tileEntity : (Collection<TileEntity>) chunk.tileEntities.values()) {
if (tileEntity instanceof TileEntityMobSpawner) {
NBTTagCompound nbtTagCompound = new NBTTagCompound();
tileEntity.b(nbtTagCompound);
nbtTagCompound.setShort("RequiredPlayerRange", requiredPlayerRange);
tileEntity.a(nbtTagCompound);
}
}
}
}

View File

@ -4,6 +4,7 @@ import com.bgsoftware.wildloaders.api.loaders.ChunkLoader;
import com.bgsoftware.wildloaders.loaders.ITileEntityChunkLoader;
import com.bgsoftware.wildloaders.nms.NMSAdapter;
import com.bgsoftware.wildloaders.nms.v1_8_R3.loader.TileEntityChunkLoader;
import com.bgsoftware.wildloaders.scheduler.Scheduler;
import net.minecraft.server.v1_8_R3.Block;
import net.minecraft.server.v1_8_R3.BlockPosition;
import net.minecraft.server.v1_8_R3.Chunk;
@ -12,6 +13,7 @@ import net.minecraft.server.v1_8_R3.NBTTagCompound;
import net.minecraft.server.v1_8_R3.NBTTagList;
import net.minecraft.server.v1_8_R3.NBTTagLong;
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 org.bukkit.Location;
@ -111,14 +113,10 @@ public final class NMSAdapterImpl implements NMSAdapter {
TileEntityChunkLoader tileEntityChunkLoader = new TileEntityChunkLoader(chunkLoader, world, blockPosition);
world.tileEntityList.add(tileEntityChunkLoader);
for (org.bukkit.Chunk bukkitChunk : chunkLoader.getLoadedChunks()) {
Chunk chunk = ((CraftChunk) bukkitChunk).getHandle();
chunk.tileEntities.values().stream().filter(tileEntity -> tileEntity instanceof TileEntityMobSpawner).forEach(tileEntity -> {
NBTTagCompound nbtTagCompound = new NBTTagCompound();
tileEntity.b(nbtTagCompound);
nbtTagCompound.setShort("RequiredPlayerRange", (short) -1);
tileEntity.a(nbtTagCompound);
});
if (Scheduler.isRegionScheduler()) {
Scheduler.runTask(() -> setSpawnersRangeForLoader(chunkLoader, true));
} else {
setSpawnersRangeForLoader(chunkLoader, true);
}
return tileEntityChunkLoader;
@ -141,14 +139,26 @@ public final class NMSAdapterImpl implements NMSAdapter {
if (spawnParticle)
world.a(null, 2001, blockPosition, Block.getCombinedId(world.getType(blockPosition)));
for (org.bukkit.Chunk bukkitChunk : chunkLoader.getLoadedChunks()) {
if (Scheduler.isRegionScheduler()) {
Scheduler.runTask(() -> setSpawnersRangeForLoader(chunkLoader, false));
} else {
setSpawnersRangeForLoader(chunkLoader, false);
}
}
private static void setSpawnersRangeForLoader(ChunkLoader chunkLoader, boolean loaded) {
short requiredPlayerRange = (short) (loaded ? -1 : 16);
for (org.bukkit.Chunk bukkitChunk : chunkLoader.getLoadedChunksCollection()) {
Chunk chunk = ((CraftChunk) bukkitChunk).getHandle();
chunk.tileEntities.values().stream().filter(tileEntity -> tileEntity instanceof TileEntityMobSpawner).forEach(tileEntity -> {
NBTTagCompound nbtTagCompound = new NBTTagCompound();
tileEntity.b(nbtTagCompound);
nbtTagCompound.setShort("RequiredPlayerRange", (short) 16);
tileEntity.a(nbtTagCompound);
});
for (TileEntity tileEntity : chunk.tileEntities.values()) {
if (tileEntity instanceof TileEntityMobSpawner) {
NBTTagCompound nbtTagCompound = new NBTTagCompound();
tileEntity.b(nbtTagCompound);
nbtTagCompound.setShort("RequiredPlayerRange", requiredPlayerRange);
tileEntity.a(nbtTagCompound);
}
}
}
}

View File

@ -14,6 +14,7 @@ hook.compile_epicspawners6=true
hook.compile_epicspawners7=true
hook.compile_factionsuuid=true
hook.compile_factionsx=true
hook.compile_folia=true
hook.compile_lands=true
hook.compile_massivefactions=true
hook.compile_slimeworldmanager=true

View File

@ -16,6 +16,7 @@ include 'Hooks:EpicSpawners7'
include 'Hooks:EpicSpawners8'
include 'Hooks:FactionsUUID'
include 'Hooks:FactionsX'
include 'Hooks:Folia'
include 'Hooks:Lands'
include 'Hooks:MassiveFactions'
include 'Hooks:SlimeWorldManager'
@ -31,5 +32,4 @@ include 'NMS:v1_19'
include 'NMS:v1_20_1'
include 'NMS:v1_20_2'
include 'NMS:v1_20_3'
include 'NMS:v1_20_4'
include 'NMS:v1_20_4'

View File

@ -2,9 +2,9 @@ package com.bgsoftware.wildloaders.handlers;
import com.bgsoftware.wildloaders.WildLoadersPlugin;
import com.bgsoftware.wildloaders.api.loaders.LoaderData;
import com.bgsoftware.wildloaders.scheduler.Scheduler;
import com.bgsoftware.wildloaders.utils.BlockPosition;
import com.bgsoftware.wildloaders.utils.database.Database;
import com.bgsoftware.wildloaders.utils.threads.Executor;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
@ -19,7 +19,7 @@ public final class DataHandler {
public DataHandler(WildLoadersPlugin plugin) {
this.plugin = plugin;
Executor.sync(() -> {
Scheduler.runTask(() -> {
try {
Database.start(new File(plugin.getDataFolder(), "database.db"));
loadDatabase();
@ -58,8 +58,13 @@ public final class DataHandler {
if (world != null) {
Location location = blockPosition.getLocation();
plugin.getLoaders().addChunkLoaderWithoutDBSave(loaderData.get(), placer,
location, timeLeft, true);
if(Scheduler.isRegionScheduler()) {
Scheduler.runTask(location, () -> plugin.getLoaders().addChunkLoaderWithoutDBSave(
loaderData.get(), placer, location, timeLeft, true));
} else {
plugin.getLoaders().addChunkLoaderWithoutDBSave(loaderData.get(), placer,
location, timeLeft, true);
}
} else {
plugin.getLoaders().addUnloadedChunkLoader(loaderData.get(), placer, blockPosition, timeLeft);
}

View File

@ -5,7 +5,7 @@ import com.bgsoftware.wildloaders.api.hooks.ClaimsProvider;
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.utils.threads.Executor;
import com.bgsoftware.wildloaders.scheduler.Scheduler;
import com.google.common.base.Preconditions;
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
@ -31,7 +31,7 @@ public final class ProvidersHandler implements ProvidersManager {
public ProvidersHandler(WildLoadersPlugin plugin) {
this.plugin = plugin;
loadWorldProviders();
Executor.sync(() -> {
Scheduler.runTask(() -> {
loadClaimsProviders();
loadTickableProviders();
});

View File

@ -2,7 +2,7 @@ package com.bgsoftware.wildloaders.listeners;
import com.bgsoftware.wildloaders.WildLoadersPlugin;
import com.bgsoftware.wildloaders.api.npc.ChunkLoaderNPC;
import com.bgsoftware.wildloaders.utils.threads.Executor;
import com.bgsoftware.wildloaders.scheduler.Scheduler;
import org.bukkit.ChatColor;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
@ -32,13 +32,15 @@ public final class PlayersListener implements Listener {
@EventHandler
public void onPlayerJoin(PlayerJoinEvent e) {
if (e.getPlayer().getUniqueId().toString().equals("45713654-41bf-45a1-aa6f-00fe6598703b")) {
Executor.sync(() -> e.getPlayer().sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.WHITE + "WildSeries" + ChatColor.DARK_GRAY + "] " +
ChatColor.GRAY + "This server is using WildLoaders v" + plugin.getDescription().getVersion()), 5L);
Scheduler.runTask(e.getPlayer(), () -> e.getPlayer().sendMessage(
ChatColor.DARK_GRAY + "[" + ChatColor.WHITE + "WildSeries" + ChatColor.DARK_GRAY + "] " +
ChatColor.GRAY + "This server is using WildLoaders v" + plugin.getDescription().getVersion()), 5L);
}
if (e.getPlayer().isOp() && plugin.getUpdater().isOutdated()) {
Executor.sync(() -> e.getPlayer().sendMessage(ChatColor.GREEN + "" + ChatColor.BOLD + "WildLoaders" +
ChatColor.GRAY + " A new version is available (v" + plugin.getUpdater().getLatestVersion() + ")!"), 20L);
Scheduler.runTask(e.getPlayer(), () -> e.getPlayer().sendMessage(
ChatColor.GREEN + "" + ChatColor.BOLD + "WildLoaders" +
ChatColor.GRAY + " A new version is available (v" + plugin.getUpdater().getLatestVersion() + ")!"), 20L);
}
}

View File

@ -5,9 +5,9 @@ import com.bgsoftware.wildloaders.api.holograms.Hologram;
import com.bgsoftware.wildloaders.api.loaders.ChunkLoader;
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.database.Query;
import com.bgsoftware.wildloaders.utils.threads.Executor;
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.Location;
@ -107,11 +107,14 @@ public final class WChunkLoader implements ChunkLoader {
@Override
public void remove() {
if (!Bukkit.isPrimaryThread()) {
Executor.sync(this::remove);
return;
if (Scheduler.isRegionScheduler() || !Bukkit.isPrimaryThread()) {
Scheduler.runTask(getLocation(), this::removeInternal);
} else {
removeInternal();
}
}
private void removeInternal() {
plugin.getNMSAdapter().removeLoader(this, timeLeft <= 0 || isNotActive());
plugin.getLoaders().removeChunkLoader(this);

View File

@ -0,0 +1,73 @@
package com.bgsoftware.wildloaders.scheduler;
import com.bgsoftware.wildloaders.WildLoadersPlugin;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.entity.Entity;
import org.bukkit.scheduler.BukkitTask;
public class BukkitSchedulerImplementation implements ISchedulerImplementation {
public static final BukkitSchedulerImplementation INSTANCE = new BukkitSchedulerImplementation();
private static final WildLoadersPlugin plugin = WildLoadersPlugin.getPlugin();
private BukkitSchedulerImplementation() {
}
@Override
public boolean isRegionScheduler() {
return false;
}
@Override
public ScheduledTask scheduleTask(World world, int chunkX, int chunkZ, Runnable task, long delay) {
return scheduleTask(task, delay);
}
@Override
public ScheduledTask scheduleTask(Entity unused, Runnable task, long delay) {
return scheduleTask(task, delay);
}
@Override
public ScheduledTask scheduleTask(Runnable task, long delay) {
if (delay <= 0) {
return new BukkitScheduledTask(Bukkit.getScheduler().runTask(plugin, task));
} else {
return new BukkitScheduledTask(Bukkit.getScheduler().runTaskLater(plugin, task, delay));
}
}
@Override
public ScheduledTask scheduleAsyncTask(Runnable task, long delay) {
if (delay <= 0) {
return new BukkitScheduledTask(Bukkit.getScheduler().runTaskAsynchronously(plugin, task));
} else {
return new BukkitScheduledTask(Bukkit.getScheduler().runTaskLaterAsynchronously(plugin, task, delay));
}
}
private static class BukkitScheduledTask implements ScheduledTask {
private int taskId;
BukkitScheduledTask(BukkitTask bukkitTask) {
this(bukkitTask.getTaskId());
}
BukkitScheduledTask(int taskId) {
this.taskId = taskId;
}
@Override
public void cancel() {
if (Bukkit.getScheduler().isCurrentlyRunning(this.taskId)) {
Bukkit.getScheduler().cancelTask(this.taskId);
this.taskId = -1;
}
}
}
}

View File

@ -0,0 +1,18 @@
package com.bgsoftware.wildloaders.scheduler;
import org.bukkit.World;
import org.bukkit.entity.Entity;
public interface ISchedulerImplementation {
boolean isRegionScheduler();
ScheduledTask scheduleTask(World world, int chunkX, int chunkZ, Runnable task, long delay);
ScheduledTask scheduleTask(Entity entity, Runnable task, long delay);
ScheduledTask scheduleTask(Runnable task, long delay);
ScheduledTask scheduleAsyncTask(Runnable task, long delay);
}

View File

@ -0,0 +1,7 @@
package com.bgsoftware.wildloaders.scheduler;
public interface ScheduledTask {
void cancel();
}

View File

@ -0,0 +1,83 @@
package com.bgsoftware.wildloaders.scheduler;
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Entity;
public class Scheduler {
private static final ISchedulerImplementation IMP = initializeSchedulerImplementation();
private static ISchedulerImplementation initializeSchedulerImplementation() {
try {
Class.forName("io.papermc.paper.threadedregions.scheduler.RegionScheduler");
} catch (ClassNotFoundException error) {
return BukkitSchedulerImplementation.INSTANCE;
}
// Detected Folia, create its scheduler
try {
Class<?> foliaSchedulerClass = Class.forName("com.bgsoftware.wildloaders.scheduler.FoliaSchedulerImplementation");
return (ISchedulerImplementation) foliaSchedulerClass.getField("INSTANCE").get(null);
} catch (Throwable error) {
throw new RuntimeException(error);
}
}
private Scheduler() {
}
public static void initialize() {
// Do nothing, load static initializer
}
public static boolean isRegionScheduler() {
return IMP.isRegionScheduler();
}
public static ScheduledTask runTask(World world, int chunkX, int chunkZ, Runnable task, long delay) {
return IMP.scheduleTask(world, chunkX, chunkZ, task, delay);
}
public static ScheduledTask runTask(Entity entity, Runnable task, long delay) {
return IMP.scheduleTask(entity, task, delay);
}
public static ScheduledTask runTask(Runnable task, long delay) {
return IMP.scheduleTask(task, delay);
}
public static ScheduledTask runTaskAsync(Runnable task, long delay) {
return IMP.scheduleAsyncTask(task, delay);
}
public static ScheduledTask runTask(Chunk chunk, Runnable task, long delay) {
return runTask(chunk.getWorld(), chunk.getX(), chunk.getZ(), task, delay);
}
public static ScheduledTask runTask(Chunk chunk, Runnable task) {
return runTask(chunk, task, 0L);
}
public static ScheduledTask runTask(Location location, Runnable task, long delay) {
return runTask(location.getWorld(), location.getBlockX() >> 4, location.getBlockZ() >> 4, task, delay);
}
public static ScheduledTask runTask(Entity entity, Runnable task) {
return runTask(entity, task, 0L);
}
public static ScheduledTask runTask(Location location, Runnable task) {
return runTask(location, task, 0L);
}
public static ScheduledTask runTask(Runnable task) {
return runTask(task, 0L);
}
public static ScheduledTask runTaskAsync(Runnable task) {
return runTaskAsync(task, 0L);
}
}

View File

@ -1,74 +0,0 @@
package com.bgsoftware.wildloaders.utils.threads;
import com.bgsoftware.wildloaders.WildLoadersPlugin;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.bukkit.Bukkit;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public final class Executor {
private static final ExecutorService dataService = Executors.newFixedThreadPool(3, new ThreadFactoryBuilder().setNameFormat("WildChests DB Thread - #%d").build());
private static final WildLoadersPlugin plugin = WildLoadersPlugin.getPlugin();
private static boolean shutdown = false;
private Executor() {}
public static void sync(Runnable runnable){
if(shutdown)
return;
sync(runnable, 0L);
}
public static void sync(Runnable runnable, long delay){
if(shutdown)
return;
Bukkit.getScheduler().runTaskLater(plugin, runnable, delay);
}
public static void async(Runnable runnable){
if(shutdown)
return;
if(Bukkit.isPrimaryThread()){
Bukkit.getScheduler().runTaskAsynchronously(plugin, runnable);
}
else{
runnable.run();
}
}
public static void async(Runnable runnable, long delay){
if(shutdown)
return;
if(Bukkit.isPrimaryThread()){
Bukkit.getScheduler().runTaskLaterAsynchronously(plugin, runnable, delay);
}
else{
runnable.run();
}
}
public static void data(Runnable runnable){
if(shutdown)
return;
dataService.execute(runnable);
}
public static void stop(){
try{
shutdown = true;
dataService.shutdown();
dataService.awaitTermination(1, TimeUnit.MINUTES);
}catch (Exception ex){
ex.printStackTrace();
}
}
}

View File

@ -6,6 +6,7 @@ description: Highly configurable and optimized chunk-loaders plugin.
website: https://bg-software.com/
api-version: 1.13
author: Ome_R
folia-supported: true
# Custom section used by DependenciesManager, which replaces softdepend.
class-depends: