Skip to content

Commit

Permalink
Converted All NMS Methods to new 0.0.2: ExecuteNMS Method 💯
Browse files Browse the repository at this point in the history
  • Loading branch information
Outspending committed Dec 29, 2023
1 parent 80f1fd5 commit 15498cc
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 79 deletions.
14 changes: 3 additions & 11 deletions src/main/java/me/outspending/biomesapi/BiomeLock.java
Original file line number Diff line number Diff line change
@@ -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;

/**
Expand All @@ -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.
Expand All @@ -37,9 +33,7 @@ public final class BiomeLock {
*/
@AsOf("0.0.1")
static void unlock(Supplier<?> supplier) {
Optional<NMS> nms = NMSHandler.getNMS();

nms.ifPresent(nmsInstance -> nmsInstance.unlockRegistry(supplier));
NMSHandler.executeNMS(nms -> nms.unlockRegistry(supplier));
}

/**
Expand All @@ -56,9 +50,7 @@ static void unlock(Supplier<?> supplier) {
*/
@AsOf("0.0.1")
static void changeRegistryLock(boolean isLocked) {
Optional<NMS> nms = NMSHandler.getNMS();

nms.ifPresent(nmsInstance -> nmsInstance.biomeRegistryLock(isLocked));
NMSHandler.executeNMS(nms -> nms.biomeRegistryLock(isLocked));
}

}
10 changes: 4 additions & 6 deletions src/main/java/me/outspending/biomesapi/BiomeSetter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 4 additions & 6 deletions src/main/java/me/outspending/biomesapi/BiomeSetterImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@

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;
import org.bukkit.util.BoundingBox;
import org.bukkit.util.Vector;
import org.jetbrains.annotations.NotNull;

import java.util.Optional;

public class BiomeSetterImpl implements BiomeSetter {

@SuppressWarnings("deprecation")
Expand Down Expand Up @@ -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> 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);
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/me/outspending/biomesapi/BiomeUpdater.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -13,7 +10,6 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
* Utility class for updating biomes in Minecraft.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/me/outspending/biomesapi/biome/CustomBiome.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -115,9 +115,8 @@ public int getGrassColor() {
return grassColor;
}

@NotNull
@Override
public ParticleRenderer getParticleRenderer() {
public @NotNull ParticleRenderer getParticleRenderer() {
return particleRenderer;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -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<Biome> registry = (Registry<Biome>) nms.getRegistry();
NMSHandler.executeNMS(nms -> {
Registry<Biome> registry = (Registry<Biome>) 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<Biome> registry = (Registry<Biome>) nms.getRegistry();

BiomeLock.unlock(() -> {

return null;
});
BiomeHandler.getRegisteredBiomes().remove(biome);
// TODO: Implement this method
}

}

0 comments on commit 15498cc

Please sign in to comment.