From 15498cc8bed75fee4ae06473050b5a51f043089b Mon Sep 17 00:00:00 2001 From: Outspending Date: Fri, 29 Dec 2023 12:58:03 -0700 Subject: [PATCH] Converted All NMS Methods to new `0.0.2: ExecuteNMS` Method :100: --- .../me/outspending/biomesapi/BiomeLock.java | 14 +--- .../me/outspending/biomesapi/BiomeSetter.java | 10 +-- .../biomesapi/BiomeSetterImpl.java | 10 +-- .../outspending/biomesapi/BiomeUpdater.java | 4 - .../biomesapi/biome/BiomeHandler.java | 2 +- .../biomesapi/biome/CustomBiome.java | 4 +- .../biomesapi/biome/CustomBiomeImpl.java | 7 +- .../registry/CustomBiomeRegistry.java | 84 +++++++++---------- 8 files changed, 56 insertions(+), 79 deletions(-) diff --git a/src/main/java/me/outspending/biomesapi/BiomeLock.java b/src/main/java/me/outspending/biomesapi/BiomeLock.java index b57a415..1a49679 100644 --- a/src/main/java/me/outspending/biomesapi/BiomeLock.java +++ b/src/main/java/me/outspending/biomesapi/BiomeLock.java @@ -1,11 +1,8 @@ package me.outspending.biomesapi; -import lombok.experimental.UtilityClass; import me.outspending.biomesapi.annotations.AsOf; -import me.outspending.biomesapi.nms.NMS; import me.outspending.biomesapi.nms.NMSHandler; -import java.util.Optional; import java.util.function.Supplier; /** @@ -16,8 +13,7 @@ * @version 0.0.1 */ @AsOf("0.0.1") -@UtilityClass -public final class BiomeLock { +public interface BiomeLock { /** * Unlocks the biome registry, performs an operation, and then locks the biome registry again. @@ -37,9 +33,7 @@ public final class BiomeLock { */ @AsOf("0.0.1") static void unlock(Supplier supplier) { - Optional nms = NMSHandler.getNMS(); - - nms.ifPresent(nmsInstance -> nmsInstance.unlockRegistry(supplier)); + NMSHandler.executeNMS(nms -> nms.unlockRegistry(supplier)); } /** @@ -56,9 +50,7 @@ static void unlock(Supplier supplier) { */ @AsOf("0.0.1") static void changeRegistryLock(boolean isLocked) { - Optional nms = NMSHandler.getNMS(); - - nms.ifPresent(nmsInstance -> nmsInstance.biomeRegistryLock(isLocked)); + NMSHandler.executeNMS(nms -> nms.biomeRegistryLock(isLocked)); } } diff --git a/src/main/java/me/outspending/biomesapi/BiomeSetter.java b/src/main/java/me/outspending/biomesapi/BiomeSetter.java index 55fd48c..74cfd32 100644 --- a/src/main/java/me/outspending/biomesapi/BiomeSetter.java +++ b/src/main/java/me/outspending/biomesapi/BiomeSetter.java @@ -3,17 +3,15 @@ import lombok.experimental.UtilityClass; import me.outspending.biomesapi.annotations.AsOf; import me.outspending.biomesapi.biome.CustomBiome; -import me.outspending.biomesapi.misc.PointRange3D; -import me.outspending.biomesapi.nms.NMS; -import me.outspending.biomesapi.nms.NMSHandler; -import org.bukkit.*; +import org.bukkit.Chunk; +import org.bukkit.Location; +import org.bukkit.RegionAccessor; +import org.bukkit.World; import org.bukkit.block.Block; import org.bukkit.util.BoundingBox; import org.bukkit.util.Vector; import org.jetbrains.annotations.NotNull; -import java.util.Optional; - /** * This utility class provides methods to set the biome of blocks, chunks, and regions in the game. * It uses the @AsOf annotation to indicate the version since the class or its methods have been present or modified. diff --git a/src/main/java/me/outspending/biomesapi/BiomeSetterImpl.java b/src/main/java/me/outspending/biomesapi/BiomeSetterImpl.java index c12b22e..c145a04 100644 --- a/src/main/java/me/outspending/biomesapi/BiomeSetterImpl.java +++ b/src/main/java/me/outspending/biomesapi/BiomeSetterImpl.java @@ -2,7 +2,6 @@ import me.outspending.biomesapi.biome.CustomBiome; import me.outspending.biomesapi.misc.PointRange3D; -import me.outspending.biomesapi.nms.NMS; import me.outspending.biomesapi.nms.NMSHandler; import org.bukkit.*; import org.bukkit.block.Block; @@ -10,8 +9,6 @@ import org.bukkit.util.Vector; import org.jetbrains.annotations.NotNull; -import java.util.Optional; - public class BiomeSetterImpl implements BiomeSetter { @SuppressWarnings("deprecation") @@ -115,10 +112,11 @@ public void setRegionBiome(@NotNull World world, @NotNull Vector from, @NotNull @Override public void setRegionBiome(@NotNull World world, @NotNull Location from, @NotNull Location to, @NotNull CustomBiome customBiome, boolean updateBiome) { - PointRange3D range = PointRange3D.of(from, to); - Optional nms = NMSHandler.getNMS(); + NMSHandler.executeNMS(nms -> { + PointRange3D range = PointRange3D.of(from, to); - nms.ifPresent(n -> n.updateBiome(range.getMinLocation(world), range.getMaxLocation(world), customBiome.toNamespacedKey())); + nms.updateBiome(range.getMinLocation(world), range.getMaxLocation(world), customBiome.toNamespacedKey()); + }); if (updateBiome) { BIOME_UPDATER.updateChunks(from, to); diff --git a/src/main/java/me/outspending/biomesapi/BiomeUpdater.java b/src/main/java/me/outspending/biomesapi/BiomeUpdater.java index eb4ecde..8a98749 100644 --- a/src/main/java/me/outspending/biomesapi/BiomeUpdater.java +++ b/src/main/java/me/outspending/biomesapi/BiomeUpdater.java @@ -1,10 +1,7 @@ package me.outspending.biomesapi; -import lombok.experimental.UtilityClass; import me.outspending.biomesapi.annotations.AsOf; import me.outspending.biomesapi.misc.PointRange2D; -import me.outspending.biomesapi.nms.NMS; -import me.outspending.biomesapi.nms.NMSHandler; import org.bukkit.Chunk; import org.bukkit.Location; import org.bukkit.World; @@ -13,7 +10,6 @@ import java.util.ArrayList; import java.util.List; -import java.util.Optional; /** * Utility class for updating biomes in Minecraft. diff --git a/src/main/java/me/outspending/biomesapi/biome/BiomeHandler.java b/src/main/java/me/outspending/biomesapi/biome/BiomeHandler.java index a03564b..acb0f9f 100644 --- a/src/main/java/me/outspending/biomesapi/biome/BiomeHandler.java +++ b/src/main/java/me/outspending/biomesapi/biome/BiomeHandler.java @@ -2,9 +2,9 @@ import lombok.Getter; import lombok.experimental.UtilityClass; -import me.outspending.biomesapi.registry.BiomeResourceKey; import me.outspending.biomesapi.annotations.AsOf; import me.outspending.biomesapi.exceptions.UnknownBiomeException; +import me.outspending.biomesapi.registry.BiomeResourceKey; import org.bukkit.Location; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; diff --git a/src/main/java/me/outspending/biomesapi/biome/CustomBiome.java b/src/main/java/me/outspending/biomesapi/biome/CustomBiome.java index aca6bf0..97cc97e 100644 --- a/src/main/java/me/outspending/biomesapi/biome/CustomBiome.java +++ b/src/main/java/me/outspending/biomesapi/biome/CustomBiome.java @@ -1,10 +1,10 @@ package me.outspending.biomesapi.biome; import com.google.common.base.Preconditions; -import me.outspending.biomesapi.registry.BiomeResourceKey; import me.outspending.biomesapi.BiomeSettings; -import me.outspending.biomesapi.renderer.ParticleRenderer; import me.outspending.biomesapi.annotations.AsOf; +import me.outspending.biomesapi.registry.BiomeResourceKey; +import me.outspending.biomesapi.renderer.ParticleRenderer; import org.bukkit.Color; import org.bukkit.NamespacedKey; import org.jetbrains.annotations.NotNull; diff --git a/src/main/java/me/outspending/biomesapi/biome/CustomBiomeImpl.java b/src/main/java/me/outspending/biomesapi/biome/CustomBiomeImpl.java index 871b7fc..5e74c4c 100644 --- a/src/main/java/me/outspending/biomesapi/biome/CustomBiomeImpl.java +++ b/src/main/java/me/outspending/biomesapi/biome/CustomBiomeImpl.java @@ -1,9 +1,9 @@ package me.outspending.biomesapi.biome; -import me.outspending.biomesapi.registry.BiomeResourceKey; import me.outspending.biomesapi.BiomeSettings; -import me.outspending.biomesapi.renderer.ParticleRenderer; import me.outspending.biomesapi.annotations.AsOf; +import me.outspending.biomesapi.registry.BiomeResourceKey; +import me.outspending.biomesapi.renderer.ParticleRenderer; import net.minecraft.resources.ResourceLocation; import org.bukkit.NamespacedKey; import org.jetbrains.annotations.NotNull; @@ -115,9 +115,8 @@ public int getGrassColor() { return grassColor; } - @NotNull @Override - public ParticleRenderer getParticleRenderer() { + public @NotNull ParticleRenderer getParticleRenderer() { return particleRenderer; } diff --git a/src/main/java/me/outspending/biomesapi/registry/CustomBiomeRegistry.java b/src/main/java/me/outspending/biomesapi/registry/CustomBiomeRegistry.java index 9851b30..78d475d 100644 --- a/src/main/java/me/outspending/biomesapi/registry/CustomBiomeRegistry.java +++ b/src/main/java/me/outspending/biomesapi/registry/CustomBiomeRegistry.java @@ -2,13 +2,11 @@ import me.outspending.biomesapi.BiomeLock; import me.outspending.biomesapi.BiomeSettings; -import me.outspending.biomesapi.renderer.ParticleRenderer; import me.outspending.biomesapi.annotations.AsOf; import me.outspending.biomesapi.biome.BiomeHandler; import me.outspending.biomesapi.biome.CustomBiome; -import me.outspending.biomesapi.exceptions.UnknownNMSVersionException; -import me.outspending.biomesapi.nms.NMS; import me.outspending.biomesapi.nms.NMSHandler; +import me.outspending.biomesapi.renderer.ParticleRenderer; import net.minecraft.core.Registry; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.level.biome.*; @@ -38,61 +36,57 @@ public class CustomBiomeRegistry implements BiomeRegistry { @SuppressWarnings("unchecked") public void register(@NotNull CustomBiome biome) { BiomeLock.unlock(() -> { + // Retrieve the biome registry from NMS - NMS nms = NMSHandler.getNMS().orElseThrow(() -> new UnknownNMSVersionException("Unknown NMS version")); - Registry registry = (Registry) nms.getRegistry(); + NMSHandler.executeNMS(nms -> { + Registry registry = (Registry) nms.getRegistry(); - // Get the ResourceLocation and BiomeSettings from the CustomBiome object - ResourceLocation resourceLocation = biome.getResourceKey().resourceLocation(); - BiomeSettings settings = biome.getSettings(); + // Get the ResourceLocation and BiomeSettings from the CustomBiome object + ResourceLocation resourceLocation = biome.getResourceKey().resourceLocation(); + BiomeSettings settings = biome.getSettings(); - // Create a new Biome object with the settings and colors from the CustomBiome object - BiomeSpecialEffects.Builder effectsBuilder = new BiomeSpecialEffects.Builder() - .fogColor(biome.getFogColor()) - .foliageColorOverride(biome.getFoliageColor()) - .skyColor(biome.getSkyColor()) - .waterColor(biome.getWaterColor()) - .waterFogColor(biome.getWaterFogColor()) - .grassColorOverride(biome.getGrassColor()); + // Create a new Biome object with the settings and colors from the CustomBiome object + BiomeSpecialEffects.Builder effectsBuilder = new BiomeSpecialEffects.Builder() + .fogColor(biome.getFogColor()) + .foliageColorOverride(biome.getFoliageColor()) + .skyColor(biome.getSkyColor()) + .waterColor(biome.getWaterColor()) + .waterFogColor(biome.getWaterFogColor()) + .grassColorOverride(biome.getGrassColor()); - // Check if a custom particle renderer is provided - ParticleRenderer renderer; - if ((renderer = biome.getParticleRenderer()) != null) { - effectsBuilder.ambientParticle(new AmbientParticleSettings( - renderer.ambientParticle().getParticle(), - renderer.probability() - )); - } + // Check if a custom particle renderer is provided + ParticleRenderer renderer; + if ((renderer = biome.getParticleRenderer()) != null) { + effectsBuilder.ambientParticle(new AmbientParticleSettings( + renderer.ambientParticle().getParticle(), + renderer.probability() + )); + } - // Build the Biome object - Biome createdBiome = new Biome.BiomeBuilder() - .downfall(settings.downfall()) - .temperature(settings.temperature()) - .temperatureAdjustment(settings.modifier().getModifier()) - .mobSpawnSettings(MobSpawnSettings.EMPTY) - .generationSettings(BiomeGenerationSettings.EMPTY) - .specialEffects(effectsBuilder.build()) - .build(); + // Build the Biome object + Biome createdBiome = new Biome.BiomeBuilder() + .downfall(settings.downfall()) + .temperature(settings.temperature()) + .temperatureAdjustment(settings.modifier().getModifier()) + .mobSpawnSettings(MobSpawnSettings.EMPTY) + .generationSettings(BiomeGenerationSettings.EMPTY) + .specialEffects(effectsBuilder.build()) + .build(); - // Register the new Biome object to the biome registry - Registry.register(registry, resourceLocation, createdBiome); + // Register the new Biome object to the biome registry + Registry.register(registry, resourceLocation, createdBiome); + + // Add the custom biome to the list of registered biomes + BiomeHandler.getRegisteredBiomes().add(biome); + }); - // Add the custom biome to the list of registered biomes - BiomeHandler.getRegisteredBiomes().add(biome); return null; }); } @Override public void unregister(@NotNull CustomBiome biome) { - NMS nms = NMSHandler.getNMS().orElseThrow(() -> new UnknownNMSVersionException("Unknown NMS version")); - Registry registry = (Registry) nms.getRegistry(); - - BiomeLock.unlock(() -> { - - return null; - }); - BiomeHandler.getRegisteredBiomes().remove(biome); + // TODO: Implement this method } }