Skip to content

Commit

Permalink
Added Null Checks to be a bit more safe 💯
Browse files Browse the repository at this point in the history
  • Loading branch information
Outspending committed Jan 8, 2024
1 parent 69cf442 commit e642c99
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
public class BiomeUpdaterImpl implements BiomeUpdater {

@Override
public void updateChunk(Chunk chunk) {
public void updateChunk(@NotNull Chunk chunk) {
updateChunks(List.of(chunk));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package me.outspending.biomesapi.biome;

import com.google.common.base.Preconditions;
import me.outspending.biomesapi.annotations.AsOf;
import me.outspending.biomesapi.exceptions.UnknownBiomeException;
import me.outspending.biomesapi.registry.BiomeResourceKey;
Expand Down Expand Up @@ -50,6 +51,8 @@ public static List<CustomBiome> getRegisteredBiomes() {
*/
@AsOf("0.0.1")
public static @Nullable CustomBiome getBiome(@NotNull BiomeResourceKey resourceKey) {
Preconditions.checkNotNull(resourceKey, "resourceKey cannot be null");

return registeredBiomes.stream()
.filter(b -> resourceKey.equals(b.getResourceKey()))
.findFirst()
Expand All @@ -68,6 +71,8 @@ public static List<CustomBiome> getRegisteredBiomes() {
*/
@AsOf("0.0.1")
public static boolean isBiome(@NotNull BiomeResourceKey resourceKey) {
Preconditions.checkNotNull(resourceKey, "resourceKey cannot be null");

return getBiome(resourceKey) != null;
}

Expand Down
1 change: 1 addition & 0 deletions src/main/java/me/outspending/biomesapi/nms/NMSHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import me.outspending.biomesapi.annotations.AsOf;
import me.outspending.biomesapi.exceptions.UnknownNMSVersionException;
import me.outspending.biomesapi.nms.*;
import org.bukkit.Bukkit;
import org.jetbrains.annotations.NotNull;

Expand Down
11 changes: 0 additions & 11 deletions src/main/java/me/outspending/biomesapi/registry/BiomeRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,4 @@ static BiomeRegistry newRegistry() {
@AsOf("0.0.1")
void register(@NotNull CustomBiome biome);

/**
* This method unregisters a custom biome from a Minecraft server.
* It takes a DedicatedServer object and a CustomBiome object as arguments.
*
* @version 0.0.2
* @param biome The CustomBiome object that should be unregistered from the server.
*/
@AsOf("0.0.2")
@Experimental
void unregister(@NotNull CustomBiome biome);

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package me.outspending.biomesapi.registry;

import com.google.common.base.Preconditions;
import me.outspending.biomesapi.annotations.AsOf;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.NotNull;

import java.lang.annotation.Documented;

/**
* This class represents a key for a biome resource in the game.
* It uses the @AsOf annotation to indicate the version since the class or its methods have been present or modified.
Expand All @@ -26,6 +29,9 @@ public record BiomeResourceKey(@NotNull ResourceLocation resourceLocation) {
*/
@AsOf("0.0.1")
public static @NotNull BiomeResourceKey of(@NotNull String key, @NotNull String path) {
Preconditions.checkArgument(!key.isEmpty(), "key cannot be empty");
Preconditions.checkArgument(!path.isEmpty(), "path cannot be empty");

return new BiomeResourceKey(key, path);
}

Expand All @@ -40,6 +46,8 @@ public record BiomeResourceKey(@NotNull ResourceLocation resourceLocation) {
*/
@AsOf("0.0.1")
public static @NotNull BiomeResourceKey of(@NotNull ResourceLocation resourceLocation) {
Preconditions.checkNotNull(resourceLocation, "resourceLocation cannot be null");

return new BiomeResourceKey(resourceLocation);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package me.outspending.biomesapi.registry;

import com.google.common.base.Preconditions;
import me.outspending.biomesapi.BiomeLock;
import me.outspending.biomesapi.BiomeSettings;
import me.outspending.biomesapi.annotations.AsOf;
Expand Down Expand Up @@ -35,6 +36,8 @@ public class CustomBiomeRegistry implements BiomeRegistry {
@AsOf("0.0.1")
@SuppressWarnings("unchecked")
public void register(@NotNull CustomBiome biome) {
Preconditions.checkNotNull(biome, "biome cannot be null");

BiomeLock.unlock(() -> {

// Retrieve the biome registry from NMS
Expand Down Expand Up @@ -84,9 +87,4 @@ public void register(@NotNull CustomBiome biome) {
});
}

@Override
public void unregister(@NotNull CustomBiome biome) {
// TODO: Implement this method
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import org.bukkit.util.Vector;
import org.jetbrains.annotations.NotNull;

import javax.annotation.concurrent.NotThreadSafe;

/**
* 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
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package me.outspending.biomesapi.setter;

import com.google.common.base.Preconditions;
import me.outspending.biomesapi.BiomeUpdater;
import me.outspending.biomesapi.biome.CustomBiome;
import me.outspending.biomesapi.misc.PointRange3D;
Expand All @@ -9,6 +10,8 @@
import org.bukkit.util.Vector;
import org.jetbrains.annotations.NotNull;

import javax.annotation.concurrent.NotThreadSafe;

public class GlobalBiomeSetter implements BiomeSetter {

@SuppressWarnings("deprecation")
Expand All @@ -22,6 +25,9 @@ public void setBlockBiome(@NotNull Block block, @NotNull CustomBiome customBiome

@Override
public void setBlockBiome(@NotNull Block block, @NotNull CustomBiome customBiome, boolean updateBiome) {
Preconditions.checkNotNull(block, "block cannot be null");
Preconditions.checkNotNull(customBiome, "customBiome cannot be null");

Location location = block.getLocation();
RegionAccessor accessor = getRegionAccessor(location);

Expand Down Expand Up @@ -49,6 +55,9 @@ public void setChunkBiome(@NotNull Chunk chunk, int minHeight, int maxHeight, @N

@Override
public void setChunkBiome(@NotNull Chunk chunk, int minHeight, int maxHeight, @NotNull CustomBiome customBiome, boolean updateBiome) {
Preconditions.checkNotNull(chunk, "chunk cannot be null");
Preconditions.checkNotNull(customBiome, "customBiome cannot be null");

RegionAccessor accessor = chunk.getWorld();
NamespacedKey key = customBiome.toNamespacedKey();

Expand Down Expand Up @@ -109,6 +118,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) {
Preconditions.checkNotNull(world, "world cannot be null");
Preconditions.checkNotNull(from, "from cannot be null");
Preconditions.checkNotNull(to, "to cannot be null");
Preconditions.checkNotNull(customBiome, "customBiome cannot be null");

if (!from.getWorld().equals(to.getWorld())) {
throw new RuntimeException("Locations must be in the same world!");
} else {
Expand Down

0 comments on commit e642c99

Please sign in to comment.