Skip to content

Commit af36719

Browse files
committed
fix(folia): fix folia chunk updates
1 parent c4a5b98 commit af36719

4 files changed

Lines changed: 48 additions & 12 deletions

File tree

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.bukkit.Location;
77
import org.bukkit.World;
88
import org.bukkit.entity.Player;
9+
import org.bukkit.plugin.Plugin;
910
import org.jetbrains.annotations.Contract;
1011
import org.jetbrains.annotations.NotNull;
1112

@@ -30,11 +31,24 @@ public interface BiomeUpdater {
3031
* This method returns an instance of BiomeUpdaterImpl.
3132
*
3233
* @return an instance of BiomeUpdater.
33-
* @version 0.0.2
34+
* @since 0.0.2
35+
* @deprecated {@link #of(Plugin)} is preferred for compatability reasons (Folia).
3436
*/
35-
@AsOf("0.0.2")
37+
@Deprecated
38+
@AsOf("1.2.0")
3639
static @NotNull BiomeUpdater of() {
37-
return new BiomeUpdaterImpl();
40+
return new BiomeUpdaterImpl(null);
41+
}
42+
43+
/**
44+
* Returns an instance of BiomeUpdater with a plugin reference.
45+
* @param plugin the plugin to use for scheduling chunk updates. Necessary for Folia compatibility.
46+
* @return an instance of BiomeUpdater with a plugin reference
47+
* @since 1.2.0
48+
*/
49+
@AsOf("1.2.0")
50+
static @NotNull BiomeUpdater of(@NotNull Plugin plugin) {
51+
return new BiomeUpdaterImpl(plugin);
3852
}
3953

4054
/**

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
import org.bukkit.Location;
77
import org.bukkit.World;
88
import org.bukkit.entity.Player;
9+
import org.bukkit.plugin.Plugin;
910
import org.jetbrains.annotations.NotNull;
11+
import org.jetbrains.annotations.Nullable;
1012

1113
import java.util.ArrayList;
1214
import java.util.List;
@@ -22,6 +24,12 @@
2224
@AsOf("0.0.1")
2325
public class BiomeUpdaterImpl implements BiomeUpdater {
2426

27+
private final @Nullable Plugin plugin;
28+
29+
public BiomeUpdaterImpl(@Nullable Plugin plugin) {
30+
this.plugin = plugin;
31+
}
32+
2533
@Override
2634
public void updateChunk(@NotNull CompletableFuture<Chunk> chunk) {
2735
updateChunks(List.of(chunk));
@@ -40,7 +48,7 @@ public void updateChunks(Location from, Location to) {
4048

4149
@Override
4250
public void updateChunks(@NotNull List<CompletableFuture<Chunk>> chunks) {
43-
UnsafeNMSHandler.executeNMS(nms -> nms.updateChunks(chunks));
51+
UnsafeNMSHandler.executeNMS(nms -> nms.updateChunks(chunks, plugin));
4452
}
4553

4654

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

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
import org.bukkit.craftbukkit.CraftServer;
2727
import org.bukkit.craftbukkit.entity.CraftPlayer;
2828
import org.bukkit.entity.Player;
29+
import org.bukkit.plugin.Plugin;
2930
import org.jetbrains.annotations.NotNull;
31+
import org.jetbrains.annotations.Nullable;
3032

3133
import java.lang.reflect.Field;
3234
import java.lang.reflect.InvocationTargetException;
@@ -63,16 +65,16 @@ public class UnsafeNMS_v1_21_11 implements UnsafeNMS {
6365
* @param chunks The chunks to update.
6466
*/
6567
@Override
66-
public void updateChunks(@NotNull List<CompletableFuture<Chunk>> chunks) {
68+
public void updateChunks(@NotNull List<CompletableFuture<Chunk>> chunks, @Nullable Plugin plugin) {
6769
CompletableFuture.runAsync(() -> {
6870
for (CompletableFuture<Chunk> chunkFuture : chunks) {
6971
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);
72+
if (plugin != null) {
73+
Bukkit.getRegionScheduler().run(plugin, chunk.getWorld(), chunk.getX(), chunk.getZ(), task -> {
74+
doUpdateChunk(chunk);
75+
});
76+
} else {
77+
doUpdateChunk(chunk);
7678
}
7779
}).exceptionally(ex -> {
7880
ex.printStackTrace();
@@ -85,6 +87,16 @@ public void updateChunks(@NotNull List<CompletableFuture<Chunk>> chunks) {
8587
});
8688
}
8789

90+
private void doUpdateChunk(Chunk chunk) {
91+
LevelChunk levelChunk = (LevelChunk) ((CraftChunk) chunk).getHandle(ChunkStatus.BIOMES);
92+
LevelLightEngine levelLightEngine = levelChunk.getLevel().getLightEngine();
93+
94+
ClientboundLevelChunkWithLightPacket packet = new ClientboundLevelChunkWithLightPacket(levelChunk, levelLightEngine, null, null, true);
95+
for (Player player : getPlayersInDistance(chunk)) {
96+
((CraftPlayer) player).getHandle().connection.send(packet);
97+
}
98+
}
99+
88100
/**
89101
* Locks or unlocks the biome registry.
90102
* It uses reflection to access the private boolean field in the registry class and sets its value.

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
import org.bukkit.NamespacedKey;
77
import org.bukkit.World;
88
import org.bukkit.entity.Player;
9+
import org.bukkit.plugin.Plugin;
910
import org.jetbrains.annotations.NotNull;
11+
import org.jetbrains.annotations.Nullable;
1012

1113
import java.util.List;
1214
import java.util.concurrent.CompletableFuture;
@@ -56,7 +58,7 @@ default List<Player> getPlayersInDistance(@NotNull Chunk chunk) {
5658
*
5759
* @param chunks The chunks to update.
5860
*/
59-
void updateChunks(@NotNull List<CompletableFuture<Chunk>> chunks);
61+
void updateChunks(@NotNull List<CompletableFuture<Chunk>> chunks, @Nullable Plugin plugin);
6062

6163
/**
6264
* Locks or unlocks the biome registry.

0 commit comments

Comments
 (0)