Skip to content

Commit

Permalink
Moved Major Classes to Interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
Outspending committed Dec 29, 2023
1 parent dff016d commit 80f1fd5
Show file tree
Hide file tree
Showing 13 changed files with 299 additions and 147 deletions.
133 changes: 14 additions & 119 deletions src/main/java/me/outspending/biomesapi/BiomeSetter.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,7 @@
*/
@UtilityClass
@AsOf("0.0.1")
public final class BiomeSetter {

@SuppressWarnings("deprecation")
private static final UnsafeValues UNSAFE = Bukkit.getUnsafe();

private static final int MAX_HEIGHT = 320;
private static final int MIN_HEIGHT = -64;
public interface BiomeSetter {

/**
* Returns the RegionAccessor for the given location.
Expand All @@ -39,7 +33,7 @@ public final class BiomeSetter {
* @version 0.0.1
*/
@AsOf("0.0.1")
private static @NotNull RegionAccessor getRegionAccessor(@NotNull Location location) {
default @NotNull RegionAccessor getRegionAccessor(@NotNull Location location) {
return location.getWorld();
}

Expand All @@ -51,9 +45,7 @@ public final class BiomeSetter {
* @version 0.0.1
*/
@AsOf("0.0.1")
public static void setBlockBiome(@NotNull Block block, @NotNull CustomBiome customBiome) {
setBlockBiome(block, customBiome, false);
}
void setBlockBiome(@NotNull Block block, @NotNull CustomBiome customBiome);

/**
* Sets the biome of a block to a custom biome.
Expand All @@ -67,16 +59,7 @@ public static void setBlockBiome(@NotNull Block block, @NotNull CustomBiome cust
* @version 0.0.1
*/
@AsOf("0.0.1")
public static void setBlockBiome(@NotNull Block block, @NotNull CustomBiome customBiome, boolean updateBiome) {
Location location = block.getLocation();
RegionAccessor accessor = getRegionAccessor(location);

UNSAFE.setBiomeKey(accessor, location.getBlockX(), location.getBlockY(), location.getBlockZ(), customBiome.toNamespacedKey());

if (updateBiome) {
BiomeUpdater.updateChunk(location.getChunk());
}
}
void setBlockBiome(@NotNull Block block, @NotNull CustomBiome customBiome, boolean updateBiome);

/**
* Sets the biome of a chunk to a custom biome.
Expand All @@ -86,9 +69,7 @@ public static void setBlockBiome(@NotNull Block block, @NotNull CustomBiome cust
* @version 0.0.1
*/
@AsOf("0.0.1")
public static void setChunkBiome(@NotNull Chunk chunk, @NotNull CustomBiome customBiome) {
setChunkBiome(chunk, MIN_HEIGHT, MAX_HEIGHT, customBiome);
}
void setChunkBiome(@NotNull Chunk chunk, @NotNull CustomBiome customBiome);

/**
* Sets the biome of a chunk to a custom biome within the default height range.
Expand All @@ -101,9 +82,7 @@ public static void setChunkBiome(@NotNull Chunk chunk, @NotNull CustomBiome cust
* @version 0.0.1
*/
@AsOf("0.0.1")
public static void setChunkBiome(@NotNull Chunk chunk, @NotNull CustomBiome customBiome, boolean updateBiome) {
setChunkBiome(chunk, MIN_HEIGHT, MAX_HEIGHT, customBiome, updateBiome);
}
void setChunkBiome(@NotNull Chunk chunk, @NotNull CustomBiome customBiome, boolean updateBiome);

/**
* Sets the biome of a chunk to a custom biome within a height range.
Expand All @@ -115,9 +94,7 @@ public static void setChunkBiome(@NotNull Chunk chunk, @NotNull CustomBiome cust
* @version 0.0.1
*/
@AsOf("0.0.1")
public static void setChunkBiome(@NotNull Chunk chunk, int minHeight, int maxHeight, @NotNull CustomBiome customBiome) {
setChunkBiome(chunk, minHeight, maxHeight, customBiome, false);
}
void setChunkBiome(@NotNull Chunk chunk, int minHeight, int maxHeight, @NotNull CustomBiome customBiome);

/**
* Sets the biome of a chunk to a custom biome within a specified height range.
Expand All @@ -133,35 +110,7 @@ public static void setChunkBiome(@NotNull Chunk chunk, int minHeight, int maxHei
* @version 0.0.1
*/
@AsOf("0.0.1")
public static void setChunkBiome(
@NotNull Chunk chunk,
int minHeight,
int maxHeight,
@NotNull CustomBiome customBiome,
boolean updateBiome
) {
RegionAccessor accessor = chunk.getWorld();
NamespacedKey key = customBiome.toNamespacedKey();

int minX = chunk.getX() << 4;
int maxX = minX + 16;

int minZ = chunk.getZ() << 4;
int maxZ = minZ + 16;

for (int x = minX; x < maxX; x++) {
for (int y = minHeight; y < maxHeight; y++) {
for (int z = minZ; z < maxZ; z++) {
// Set the biome of each block to the custom biome
UNSAFE.setBiomeKey(accessor, x, y, z, key);
}
}
}

if (updateBiome) {
BiomeUpdater.updateChunk(chunk);
}
}
void setChunkBiome(@NotNull Chunk chunk, int minHeight, int maxHeight, @NotNull CustomBiome customBiome, boolean updateBiome);

/**
* Sets the biome of a bounding box to a custom biome.
Expand All @@ -172,9 +121,7 @@ public static void setChunkBiome(
* @version 0.0.1
*/
@AsOf("0.0.1")
public static void setBoundingBoxBiome(@NotNull World world, @NotNull BoundingBox boundingBox, @NotNull CustomBiome customBiome) {
setRegionBiome(world, boundingBox.getMin(), boundingBox.getMax(), customBiome);
}
void setBoundingBoxBiome(@NotNull World world, @NotNull BoundingBox boundingBox, @NotNull CustomBiome customBiome);

/**
* Sets the biome of a region to a custom biome.
Expand All @@ -185,14 +132,7 @@ public static void setBoundingBoxBiome(@NotNull World world, @NotNull BoundingBo
* @version 0.0.1
*/
@AsOf("0.0.1")
public static void setRegionBiome(@NotNull Location from, @NotNull Location to, @NotNull CustomBiome customBiome) {
if (from.getWorld().equals(to.getWorld())) {
setRegionBiome(from.getWorld(), from, to, customBiome, true);
return;
}

throw new IllegalArgumentException("Locations must be in the same world!");
}
void setRegionBiome(@NotNull Location from, @NotNull Location to, @NotNull CustomBiome customBiome);

/**
* Sets the biome of a region to a custom biome.
Expand All @@ -205,14 +145,7 @@ public static void setRegionBiome(@NotNull Location from, @NotNull Location to,
* @version 0.0.1
*/
@AsOf("0.0.1")
public static void setRegionBiome(@NotNull Location from, @NotNull Location to, @NotNull CustomBiome customBiome, boolean updateBiome) {
if (from.getWorld().equals(to.getWorld())) {
setRegionBiome(from.getWorld(), from, to, customBiome, updateBiome);
return;
}

throw new IllegalArgumentException("Locations must be in the same world!");
}
void setRegionBiome(@NotNull Location from, @NotNull Location to, @NotNull CustomBiome customBiome, boolean updateBiome);

/**
* Sets the biome of a region to a custom biome.
Expand All @@ -224,9 +157,7 @@ public static void setRegionBiome(@NotNull Location from, @NotNull Location to,
* @version 0.0.1
*/
@AsOf("0.0.1")
public static void setRegionBiome(@NotNull World world, @NotNull Vector from, @NotNull Vector to, @NotNull CustomBiome customBiome) {
setRegionBiome(world, from, to, customBiome, false);
}
void setRegionBiome(@NotNull World world, @NotNull Vector from, @NotNull Vector to, @NotNull CustomBiome customBiome);

/**
* Sets the biome of a region to a custom biome.
Expand All @@ -240,15 +171,7 @@ public static void setRegionBiome(@NotNull World world, @NotNull Vector from, @N
* @version 0.0.1
*/
@AsOf("0.0.2")
public static void setRegionBiome(
@NotNull World world,
@NotNull Vector from,
@NotNull Vector to,
@NotNull CustomBiome customBiome,
boolean updateBiome
) {
setRegionBiome(world, from.toLocation(world), to.toLocation(world), customBiome, updateBiome);
}
void setRegionBiome(@NotNull World world, @NotNull Vector from, @NotNull Vector to, @NotNull CustomBiome customBiome, boolean updateBiome);

/**
* Sets the biome of a region to a custom biome.
Expand All @@ -264,34 +187,6 @@ public static void setRegionBiome(
* @version 0.0.2
*/
@AsOf("0.0.1")
public static 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();

nms.ifPresent(n -> n.updateBiome(range.getMinLocation(world), range.getMaxLocation(world), customBiome.toNamespacedKey()));

if (updateBiome) {
BiomeUpdater.updateChunks(from, to);
}
// int minHeight = Math.max(range.minY(), MIN_HEIGHT);
// int maxHeight = Math.min(range.maxY(), MAX_HEIGHT);
//
// // Iterate over the blocks in the region
// for (int x = range.minX(); x <= range.maxX(); x++) {
// for (int y = minHeight; y <= maxHeight; y++) {
// for (int z = range.minZ(); z <= range.maxZ(); z++) {
// // Set the biome of each block to the custom biome
// UNSAFE.setBiomeKey(accessor, x, y, z, key);
// }
// }
// }

}
void setRegionBiome(@NotNull World world, @NotNull Location from, @NotNull Location to, @NotNull CustomBiome customBiome, boolean updateBiome);

}
128 changes: 128 additions & 0 deletions src/main/java/me/outspending/biomesapi/BiomeSetterImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package me.outspending.biomesapi;

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")
private static final UnsafeValues UNSAFE = Bukkit.getUnsafe();
private static final BiomeUpdater BIOME_UPDATER = BiomeUpdater.of();

private static final int MAX_HEIGHT = 320;
private static final int MIN_HEIGHT = -64;

@Override
public void setBlockBiome(@NotNull Block block, @NotNull CustomBiome customBiome) {
setBlockBiome(block, customBiome, false);
}

@Override
public void setBlockBiome(@NotNull Block block, @NotNull CustomBiome customBiome, boolean updateBiome) {
Location location = block.getLocation();
RegionAccessor accessor = getRegionAccessor(location);

UNSAFE.setBiomeKey(accessor, location.getBlockX(), location.getBlockY(), location.getBlockZ(), customBiome.toNamespacedKey());

if (updateBiome) {
BIOME_UPDATER.updateChunk(location.getChunk());
}
}

@Override
public void setChunkBiome(@NotNull Chunk chunk, @NotNull CustomBiome customBiome) {
setChunkBiome(chunk, MIN_HEIGHT, MAX_HEIGHT, customBiome);
}

@Override
public void setChunkBiome(@NotNull Chunk chunk, @NotNull CustomBiome customBiome, boolean updateBiome) {
setChunkBiome(chunk, MIN_HEIGHT, MAX_HEIGHT, customBiome, updateBiome);
}

@Override
public void setChunkBiome(@NotNull Chunk chunk, int minHeight, int maxHeight, @NotNull CustomBiome customBiome) {
setChunkBiome(chunk, minHeight, maxHeight, customBiome, false);
}

@Override
public void setChunkBiome(@NotNull Chunk chunk, int minHeight, int maxHeight, @NotNull CustomBiome customBiome, boolean updateBiome) {
RegionAccessor accessor = chunk.getWorld();
NamespacedKey key = customBiome.toNamespacedKey();

int minX = chunk.getX() << 4;
int maxX = minX + 16;

int minZ = chunk.getZ() << 4;
int maxZ = minZ + 16;

for (int x = minX; x < maxX; x++) {
for (int y = minHeight; y < maxHeight; y++) {
for (int z = minZ; z < maxZ; z++) {
// Set the biome of each block to the custom biome
UNSAFE.setBiomeKey(accessor, x, y, z, key);
}
}
}

if (updateBiome) {
BIOME_UPDATER.updateChunk(chunk);
}
}

@Override
public void setBoundingBoxBiome(@NotNull World world, @NotNull BoundingBox boundingBox, @NotNull CustomBiome customBiome) {
setRegionBiome(world, boundingBox.getMin(), boundingBox.getMax(), customBiome);
}

@Override
public void setRegionBiome(@NotNull Location from, @NotNull Location to, @NotNull CustomBiome customBiome) {
World world = from.getWorld();
if (!world.equals(to.getWorld())) {
throw new IllegalArgumentException("Locations must be in the same world!");
}

setRegionBiome(world, from, to, customBiome, false);
}

@Override
public void setRegionBiome(@NotNull Location from, @NotNull Location to, @NotNull CustomBiome customBiome, boolean updateBiome) {
World world = from.getWorld();
if (!world.equals(to.getWorld())) {
throw new IllegalArgumentException("Locations must be in the same world!");
}

setRegionBiome(world, from, to, customBiome, updateBiome);
}

@Override
public void setRegionBiome(@NotNull World world, @NotNull Vector from, @NotNull Vector to, @NotNull CustomBiome customBiome) {
setRegionBiome(world, from, to, customBiome, false);
}

@Override
public void setRegionBiome(@NotNull World world, @NotNull Vector from, @NotNull Vector to, @NotNull CustomBiome customBiome, boolean updateBiome) {
setRegionBiome(world, from.toLocation(world), to.toLocation(world), customBiome, updateBiome);
}

@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();

nms.ifPresent(n -> n.updateBiome(range.getMinLocation(world), range.getMaxLocation(world), customBiome.toNamespacedKey()));

if (updateBiome) {
BIOME_UPDATER.updateChunks(from, to);
}
}

}
Loading

0 comments on commit 80f1fd5

Please sign in to comment.