diff --git a/Hooks/SpawnerLegacy/build.gradle b/Hooks/SpawnerLegacy/build.gradle new file mode 100644 index 0000000..53b8b30 --- /dev/null +++ b/Hooks/SpawnerLegacy/build.gradle @@ -0,0 +1,19 @@ +group 'Hooks:SpawnerLegacy' + +java { + toolchain { + languageVersion.set(JavaLanguageVersion.of(16)) + } +} + +dependencies { + compileOnly "mc.rellox:SpawnerLegacy:1.2.4" + compileOnly "org.spigotmc:v1_8_R3-Taco:latest" + compileOnly project(":API") + compileOnly rootProject +} + +if (project.hasProperty('hook.compile_spawnerlegacy') && + !Boolean.valueOf(project.findProperty("hook.compile_spawnerlegacy").toString())) { + project.tasks.all { task -> task.enabled = false } +} \ No newline at end of file diff --git a/Hooks/SpawnerLegacy/src/main/java/com/bgsoftware/wildloaders/hooks/SpawnersProvider_SpawnerLegacy.java b/Hooks/SpawnerLegacy/src/main/java/com/bgsoftware/wildloaders/hooks/SpawnersProvider_SpawnerLegacy.java new file mode 100644 index 0000000..a483ff6 --- /dev/null +++ b/Hooks/SpawnerLegacy/src/main/java/com/bgsoftware/wildloaders/hooks/SpawnersProvider_SpawnerLegacy.java @@ -0,0 +1,130 @@ +package com.bgsoftware.wildloaders.hooks; + +import com.bgsoftware.common.reflection.ReflectField; +import com.bgsoftware.wildloaders.WildLoadersPlugin; +import com.bgsoftware.wildloaders.api.hooks.SpawnersProvider; +import mc.rellox.spawnerlegacy.SpawnerLegacy; +import mc.rellox.spawnerlegacy.api.APIInstance; +import mc.rellox.spawnerlegacy.api.event.EventExecutor; +import mc.rellox.spawnerlegacy.api.event.block.SpawnerPlaceEvent; +import mc.rellox.spawnerlegacy.api.event.upgrade.SpawnerUpgradeEvent; +import mc.rellox.spawnerlegacy.api.region.IBox; +import mc.rellox.spawnerlegacy.api.spawner.IGenerator; +import mc.rellox.spawnerlegacy.spawner.generator.ActiveGenerator; +import mc.rellox.spawnerlegacy.utility.region.SphereBox; +import org.bukkit.Bukkit; +import org.bukkit.Chunk; +import org.bukkit.Location; +import org.bukkit.block.Block; +import org.bukkit.entity.Player; + +public class SpawnersProvider_SpawnerLegacy implements SpawnersProvider { + + private static final ReflectField GENERATOR_BOX = new ReflectField<>( + ActiveGenerator.class, IBox.class, "box"); + + private final WildLoadersPlugin plugin; + private final APIInstance api; + + public SpawnersProvider_SpawnerLegacy(WildLoadersPlugin plugin) { + this.plugin = plugin; + this.api = SpawnerLegacy.instance().getAPI(); + this.api.register(SpawnerUpgradeEvent.class, new SpawnerUpgradeListener()); + this.api.register(SpawnerPlaceEvent.class, new SpawnerPlaceListener()); + } + + @Override + public void setSpawnerRequiredRange(Location spawnerLocation, int requiredRange) { + updateBoxForGenerator(spawnerLocation.getBlock(), requiredRange, true); + } + + private void updateBoxForGenerator(Block block, int requiredRange, boolean retryOnNull) { + IGenerator generator = this.api.getGenerator(block); + if (generator == null) { + if (retryOnNull) + Bukkit.getScheduler().runTaskLater(plugin, () -> updateBoxForGenerator(block, requiredRange, false), 20L); + return; + } + + IBox box; + if (requiredRange == -1) { + box = new LoaderBox(block.getX(), block.getY(), block.getZ(), generator.cache().range()); + } else { + box = IBox.sphere(block, generator.cache().range()); + } + + GENERATOR_BOX.set(generator, box); + } + + private class SpawnerUpgradeListener implements EventExecutor { + + @Override + public void execute(SpawnerUpgradeEvent e) { + if (!e.cancelled()) + setLoaderBox(e.getGenerator(), true); + } + + } + + private class SpawnerPlaceListener implements EventExecutor { + + @Override + public void execute(SpawnerPlaceEvent e) { + if (e.cancelled()) + return; + + Block block = e.block(); + + Bukkit.getScheduler().runTaskLater(plugin, () -> { + IGenerator generator = api.getGenerator(block); + if (generator != null) + setLoaderBox(generator, false); + }, 5L); + } + + } + + private void setLoaderBox(IGenerator generator, boolean delay) { + Chunk chunk = generator.center().getChunk(); + + if (plugin.getLoaders().getChunkLoader(chunk).isEmpty()) + return; + + if (delay) { + // Chunk is loaded, we want to update the box in the next tick. + Bukkit.getScheduler().runTaskLater(plugin, () -> { + // Ensure chunk is still loaded. + if (plugin.getLoaders().getChunkLoader(chunk).isEmpty()) + return; + + Block block = generator.block(); + + GENERATOR_BOX.set(generator, new LoaderBox(block.getX(), block.getY(), + block.getZ(), generator.cache().range())); + }, 1L); + } else { + Block block = generator.block(); + + GENERATOR_BOX.set(generator, new LoaderBox(block.getX(), block.getY(), + block.getZ(), generator.cache().range())); + } + } + + private static class LoaderBox extends SphereBox { + + public LoaderBox(int x, int z, int y, int r) { + super(x, y, z, r); + } + + @Override + public boolean in(Player player) { + return true; + } + + @Override + public boolean any(Iterable players) { + return true; + } + } + +} diff --git a/gradle.properties b/gradle.properties index e3350a0..c1862c5 100644 --- a/gradle.properties +++ b/gradle.properties @@ -20,4 +20,5 @@ hook.compile_folia=true hook.compile_lands=true hook.compile_massivefactions=true hook.compile_slimeworldmanager=true +hook.compile_spawnerlegacy=true hook.compile_superiorskyblock=true diff --git a/settings.gradle b/settings.gradle index cacaf52..3e53242 100644 --- a/settings.gradle +++ b/settings.gradle @@ -21,6 +21,7 @@ include 'Hooks:Folia' include 'Hooks:Lands' include 'Hooks:MassiveFactions' include 'Hooks:SlimeWorldManager' +include 'Hooks:SpawnerLegacy' include 'Hooks:SuperiorSkyblock' include 'NMS' include 'NMS:v1_7_R4' diff --git a/src/main/java/com/bgsoftware/wildloaders/handlers/ProvidersHandler.java b/src/main/java/com/bgsoftware/wildloaders/handlers/ProvidersHandler.java index ddfa431..ce95810 100644 --- a/src/main/java/com/bgsoftware/wildloaders/handlers/ProvidersHandler.java +++ b/src/main/java/com/bgsoftware/wildloaders/handlers/ProvidersHandler.java @@ -38,6 +38,7 @@ public final class ProvidersHandler implements ProvidersManager { Scheduler.runTask(() -> { loadClaimsProviders(); loadTickableProviders(); + loadSpawnersProviders(); }); } @@ -88,6 +89,13 @@ public final class ProvidersHandler implements ProvidersManager { } } + private void loadSpawnersProviders() { + if (Bukkit.getPluginManager().isPluginEnabled("SpawnerLegacy")) { + Optional spawnersProvider = createInstance("SpawnersProvider_SpawnerLegacy"); + spawnersProvider.ifPresent(this::addSpawnersProvider); + } + } + private void loadWorldProviders() { Optional worldsProvider; diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 7c33f53..c8dacd1 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -14,6 +14,7 @@ class-depends: - Factions - FactionsX - Lands + - SpawnerLegacy - SuperiorSkyblock2 commands: