Skip to content

Commit c4a5b98

Browse files
committed
feat(folia): add folia support
1 parent 868bccc commit c4a5b98

7 files changed

Lines changed: 64 additions & 48 deletions

File tree

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ plugins {
88
}
99

1010
val isSnapshot: Boolean = project.hasProperty("snapshot") || System.getProperty("snapshot")?.toBoolean() == true
11-
val stable = "1.1.0"
11+
val stable = "1.2.0"
1212

1313
allprojects {
1414
apply(plugin = "java")

main/src/main/java/me/outspending/biomesapi/BiomeUpdater.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,18 @@
1111

1212
import java.util.ArrayList;
1313
import java.util.List;
14+
import java.util.concurrent.CompletableFuture;
1415

1516
/**
1617
* Utility class for updating biomes in Minecraft.
1718
* This class provides methods for updating the biomes of chunks and regions in a Minecraft world.
1819
* It uses the UnsafeValues instance to perform unsafe operations.
1920
*
20-
* @version 0.0.15
21+
* @version 1.2.0
2122
* @since 0.0.1
2223
* @author Outspending
2324
*/
24-
@AsOf("0.0.15")
25+
@AsOf("1.2.0")
2526
public interface BiomeUpdater {
2627

2728
/**
@@ -44,21 +45,21 @@ public interface BiomeUpdater {
4445
* @param from The starting location.
4546
* @param to The ending location.
4647
* @return A list of chunks between the two locations.
47-
* @version 0.0.1
48+
* @since 0.0.1
4849
*/
49-
@AsOf("0.0.1")
50-
default @NotNull List<Chunk> getChunksBetweenLocations(@NotNull Location from, @NotNull Location to) {
50+
@AsOf("1.2.0")
51+
default @NotNull List<CompletableFuture<Chunk>> getChunksBetweenLocations(@NotNull Location from, @NotNull Location to) {
5152
if (!from.getWorld().equals(to.getWorld())) {
5253
throw new IllegalArgumentException("Locations must be in the same world.");
5354
}
5455

55-
List<Chunk> chunks = new ArrayList<>();
56+
List<CompletableFuture<Chunk>> chunks = new ArrayList<>();
5657

5758
PointRange2D range = PointRange2D.of(from, to);
5859
World world = from.getWorld();
5960
for (int x = range.minX(); x <= range.maxX(); x += 16) {
6061
for (int z = range.minZ(); z <= range.maxZ(); z += 16) {
61-
chunks.add(world.getChunkAt(x >> 4, z >> 4));
62+
chunks.add(world.getChunkAtAsync(x >> 4, z >> 4));
6263
}
6364
}
6465

@@ -70,10 +71,10 @@ public interface BiomeUpdater {
7071
* This method is a convenience method that calls the updateChunks method with a list containing the chunk.
7172
*
7273
* @param chunk The chunk to update.
73-
* @version 0.0.1
74+
* @since 0.0.1
7475
*/
75-
@AsOf("0.0.1")
76-
void updateChunk(@NotNull Chunk chunk);
76+
@AsOf("1.2.0")
77+
void updateChunk(@NotNull CompletableFuture<Chunk> chunk);
7778

7879
/**
7980
* Updates the biomes of the chunks between two locations.
@@ -93,10 +94,10 @@ public interface BiomeUpdater {
9394
* The update packet contains the new biome data for the chunk.
9495
*
9596
* @param chunks The chunks to update.
96-
* @version 0.0.1
97+
* @since 0.0.1
9798
*/
98-
@AsOf("0.0.1")
99-
void updateChunks(@NotNull List<Chunk> chunks);
99+
@AsOf("1.2.0")
100+
void updateChunks(@NotNull List<CompletableFuture<Chunk>> chunks);
100101

101102

102103
/**

main/src/main/java/me/outspending/biomesapi/BiomeUpdaterImpl.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import java.util.ArrayList;
1212
import java.util.List;
13+
import java.util.concurrent.CompletableFuture;
1314

1415
/**
1516
* Implementation of the BiomeUpdater interface.
@@ -22,7 +23,7 @@
2223
public class BiomeUpdaterImpl implements BiomeUpdater {
2324

2425
@Override
25-
public void updateChunk(@NotNull Chunk chunk) {
26+
public void updateChunk(@NotNull CompletableFuture<Chunk> chunk) {
2627
updateChunks(List.of(chunk));
2728
}
2829

@@ -31,28 +32,28 @@ public void updateChunks(Location from, Location to) {
3132
if (from == null || to == null) {
3233
throw new IllegalArgumentException("Locations cannot be null.");
3334
} else {
34-
List<Chunk> updateChunks = getChunksBetweenLocations(from, to);
35+
List<CompletableFuture<Chunk>> updateChunks = getChunksBetweenLocations(from, to);
3536

3637
updateChunks(updateChunks);
3738
}
3839
}
3940

4041
@Override
41-
public void updateChunks(@NotNull List<Chunk> chunks) {
42+
public void updateChunks(@NotNull List<CompletableFuture<Chunk>> chunks) {
4243
UnsafeNMSHandler.executeNMS(nms -> nms.updateChunks(chunks));
4344
}
4445

4546

4647
@Override
4748
public void updateChunkRadius(@NotNull Chunk chunk, int radius) {
48-
List<Chunk> chunks = new ArrayList<>();
49+
List<CompletableFuture<Chunk>> chunks = new ArrayList<>();
4950
World world = chunk.getWorld();
5051
int chunkX = chunk.getX();
5152
int chunkZ = chunk.getZ();
5253

5354
for (int x = chunkX - radius; x <= chunkX + radius; x++) {
5455
for (int z = chunkZ - radius; z <= chunkZ + radius; z++) {
55-
chunks.add(world.getChunkAt(x, z));
56+
chunks.add(world.getChunkAtAsync(x, z));
5657
}
5758
}
5859
updateChunks(chunks);
@@ -66,10 +67,10 @@ public void updateChunksForPlayer(@NotNull Player player) {
6667

6768
int playerChunkX = playerLocation.getBlockX() >> 4;
6869
int playerChunkZ = playerLocation.getBlockZ() >> 4;
69-
List<Chunk> chunksToUpdate = new ArrayList<>();
70+
List<CompletableFuture<Chunk>> chunksToUpdate = new ArrayList<>();
7071
for (int x = playerChunkX - viewDistance; x <= playerChunkX + viewDistance; x++) {
7172
for (int z = playerChunkZ - viewDistance; z <= playerChunkZ + viewDistance; z++) {
72-
chunksToUpdate.add(world.getChunkAt(x, z));
73+
chunksToUpdate.add(world.getChunkAtAsync(x, z));
7374
}
7475
}
7576

main/src/main/java/me/outspending/biomesapi/renderer/packet/data/ChunkLocation.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@
88
import org.bukkit.block.Block;
99
import org.jetbrains.annotations.NotNull;
1010

11+
import java.util.concurrent.CompletableFuture;
12+
1113
/**
1214
* A data class representing a chunk location.
1315
* @param x the chunk x coordinate
1416
* @param z the chunk z coordinate
15-
* @version 0.0.11
17+
* @version 1.2.0
1618
* @since 0.0.6
1719
* @author Jsinco
1820
*/
19-
@AsOf("0.0.11")
21+
@AsOf("1.2.0")
2022
public record ChunkLocation(int x, int z) {
2123

2224
/**
@@ -122,8 +124,8 @@ public boolean isWithinRadius(ChunkLocation other, int radius) {
122124
* @return the Bukkit Chunk at this ChunkLocation in the given world
123125
*/
124126
@AsOf("1.2.0")
125-
public @NotNull Chunk toBukkitChunk(World world) {
126-
return world.getChunkAt(x, z);
127+
public @NotNull CompletableFuture<Chunk> toBukkitChunk(World world) {
128+
return world.getChunkAtAsync(x, z);
127129
}
128130

129131
/**
@@ -132,9 +134,8 @@ public boolean isWithinRadius(ChunkLocation other, int radius) {
132134
* @return the center block of this chunk in the given world
133135
*/
134136
@AsOf("1.2.0")
135-
public @NotNull Block centerBlock(@NotNull World world) {
136-
Chunk chunk = toBukkitChunk(world);
137-
return chunk.getBlock(7, 0, 7); // starts at 0, so 7 is the center block in a 16x16 chunk
137+
public @NotNull CompletableFuture<Block> centerBlock(@NotNull World world) {
138+
return toBukkitChunk(world).thenApply(chunk -> chunk.getBlock(7, 0, 7));
138139
}
139140

140141
/**
@@ -143,9 +144,8 @@ public boolean isWithinRadius(ChunkLocation other, int radius) {
143144
* @return the biome of the center block of this chunk in the given world
144145
*/
145146
@AsOf("1.2.0")
146-
public @NotNull Biome getCenterBiome(@NotNull World world) {
147-
Block centerBlock = centerBlock(world);
148-
return centerBlock.getBiome();
147+
public @NotNull CompletableFuture<Biome> getCenterBiome(@NotNull World world) {
148+
return centerBlock(world).thenApply(Block::getBiome);
149149
}
150150

151151
@Override

main/src/main/java/me/outspending/biomesapi/renderer/setter/GlobalBiomeSetter.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import org.bukkit.util.Vector;
1616
import org.jetbrains.annotations.NotNull;
1717

18+
import java.util.concurrent.CompletableFuture;
19+
1820
/**
1921
* This class provides methods to set the biome of blocks, chunks, and regions in the game.
2022
*
@@ -43,7 +45,7 @@ public void setBlockBiome(@NotNull Block block, @NotNull CustomBiome customBiome
4345
accessor.setBiome(location.getBlockX(), location.getBlockY(), location.getBlockZ(), customBiome.toBukkitBiome());
4446

4547
if (updateBiome) {
46-
BIOME_UPDATER.updateChunk(location.getChunk());
48+
BIOME_UPDATER.updateChunk(location.getWorld().getChunkAtAsync(location));
4749
}
4850
}
4951

@@ -92,7 +94,7 @@ public void setChunkBiome(@NotNull Chunk chunk, int minHeight, int maxHeight, @N
9294
}
9395

9496
if (updateBiome) {
95-
BIOME_UPDATER.updateChunk(chunk);
97+
BIOME_UPDATER.updateChunk(CompletableFuture.completedFuture(chunk));
9698
}
9799
}
98100

minecraft/1_21_11/src/main/java/me/outspending/biomesapi/UnsafeNMS_v1_21_11.java

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,25 @@ public class UnsafeNMS_v1_21_11 implements UnsafeNMS {
6363
* @param chunks The chunks to update.
6464
*/
6565
@Override
66-
public void updateChunks(@NotNull List<Chunk> chunks) {
66+
public void updateChunks(@NotNull List<CompletableFuture<Chunk>> chunks) {
6767
CompletableFuture.runAsync(() -> {
68-
for (Chunk chunk : chunks) {
69-
LevelChunk levelChunk = (LevelChunk) ((CraftChunk) chunk).getHandle(ChunkStatus.BIOMES);
70-
LevelLightEngine levelLightEngine = levelChunk.getLevel().getLightEngine();
71-
72-
ClientboundLevelChunkWithLightPacket packet = new ClientboundLevelChunkWithLightPacket(levelChunk, levelLightEngine, null, null, true);
73-
for (Player player : getPlayersInDistance(chunk)) {
74-
((CraftPlayer) player).getHandle().connection.send(packet);
75-
}
68+
for (CompletableFuture<Chunk> chunkFuture : chunks) {
69+
chunkFuture.thenAccept(chunk -> {
70+
LevelChunk levelChunk = (LevelChunk) ((CraftChunk) chunk).getHandle(ChunkStatus.BIOMES);
71+
LevelLightEngine levelLightEngine = levelChunk.getLevel().getLightEngine();
72+
73+
ClientboundLevelChunkWithLightPacket packet = new ClientboundLevelChunkWithLightPacket(levelChunk, levelLightEngine, null, null, true);
74+
for (Player player : getPlayersInDistance(chunk)) {
75+
((CraftPlayer) player).getHandle().connection.send(packet);
76+
}
77+
}).exceptionally(ex -> {
78+
ex.printStackTrace();
79+
return null;
80+
});
7681
}
77-
82+
}).exceptionally(ex -> {
83+
ex.printStackTrace();
84+
return null;
7885
});
7986
}
8087

@@ -161,10 +168,14 @@ public void updateBiome(@NotNull Location minLoc, @NotNull Location maxLoc, @Not
161168
for (int x = minLoc.getBlockX(); x <= maxLoc.getBlockX(); x++) {
162169
for (int y = minLoc.getBlockY(); y <= maxLoc.getBlockY(); y++) {
163170
for (int z = minLoc.getBlockZ(); z <= maxLoc.getBlockZ(); z++) {
164-
Chunk chunk = minLoc.getWorld().getChunkAt(x, z);
165-
LevelChunk levelChunk = (LevelChunk) ((CraftChunk) chunk).getHandle(ChunkStatus.BIOMES);
166-
167-
levelChunk.setBiome(x, y, z, biome);
171+
final int finalX = x;
172+
final int finalY = y;
173+
final int finalZ = z;
174+
175+
minLoc.getWorld().getChunkAtAsync(x, z).thenAccept(chunk -> {
176+
LevelChunk levelChunk = (LevelChunk) ((CraftChunk) chunk).getHandle(ChunkStatus.BIOMES);
177+
levelChunk.setBiome(finalX, finalY, finalZ, biome);
178+
});
168179
}
169180
}
170181
}

minecraft/Wrapper/src/main/java/me/outspending/biomesapi/unsafe/UnsafeNMS.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.jetbrains.annotations.NotNull;
1010

1111
import java.util.List;
12+
import java.util.concurrent.CompletableFuture;
1213
import java.util.function.Supplier;
1314

1415
public interface UnsafeNMS {
@@ -55,7 +56,7 @@ default List<Player> getPlayersInDistance(@NotNull Chunk chunk) {
5556
*
5657
* @param chunks The chunks to update.
5758
*/
58-
void updateChunks(@NotNull List<Chunk> chunks);
59+
void updateChunks(@NotNull List<CompletableFuture<Chunk>> chunks);
5960

6061
/**
6162
* Locks or unlocks the biome registry.

0 commit comments

Comments
 (0)