Skip to content

Commit

Permalink
Fixed a NoSuchMethodError thrown when using 1.19.4 spigot
Browse files Browse the repository at this point in the history
  • Loading branch information
OmerBenGera committed Apr 7, 2023
1 parent 3b5ad97 commit 377d126
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
Expand Up @@ -356,7 +356,7 @@ public void injectChunkSections(org.bukkit.Chunk chunk) {

@Override
public boolean isChunkEmpty(org.bukkit.Chunk bukkitChunk) {
LevelChunk levelChunk = ((CraftChunk) bukkitChunk).getHandle();
LevelChunk levelChunk = NMSUtils.getCraftChunkHandle((CraftChunk) bukkitChunk);
return Arrays.stream(levelChunk.getSections()).allMatch(chunkSection ->
chunkSection == null || chunkSection.hasOnlyAir());
}
Expand All @@ -373,7 +373,7 @@ public void startTickingChunk(Island island, org.bukkit.Chunk chunk, boolean sto
if (plugin.getSettings().getCropsInterval() <= 0)
return;

LevelChunk levelChunk = ((CraftChunk) chunk).getHandle();
LevelChunk levelChunk = NMSUtils.getCraftChunkHandle((CraftChunk) chunk);

if (stop) {
CropsBlockEntity cropsBlockEntity = CropsBlockEntity.remove(levelChunk.getPos());
Expand Down Expand Up @@ -405,7 +405,7 @@ public void shutdown() {

@Override
public List<Location> getBlockEntities(Chunk chunk) {
LevelChunk levelChunk = ((CraftChunk) chunk).getHandle();
LevelChunk levelChunk = NMSUtils.getCraftChunkHandle((CraftChunk) chunk);
List<Location> blockEntities = new LinkedList<>();

World bukkitWorld = chunk.getWorld();
Expand Down
Expand Up @@ -36,6 +36,7 @@
import net.minecraft.world.level.chunk.ProtoChunk;
import net.minecraft.world.level.chunk.UpgradeData;
import net.minecraft.world.level.levelgen.Heightmap;
import org.bukkit.craftbukkit.v1_19_R3.CraftChunk;

import java.io.IOException;
import java.lang.reflect.Modifier;
Expand All @@ -57,6 +58,9 @@ public class NMSUtils {
private static final ReflectField<Map<Long, ChunkHolder>> VISIBLE_CHUNKS = new ReflectField<>(
ChunkMap.class, Map.class, Modifier.PUBLIC | Modifier.VOLATILE, 1);

private static final ReflectMethod<LevelChunk> CRAFT_CHUNK_GET_HANDLE = new ReflectMethod<>(
CraftChunk.class, LevelChunk.class, "getHandle");

private static final List<CompletableFuture<Void>> PENDING_CHUNK_ACTIONS = new LinkedList<>();

private NMSUtils() {
Expand Down Expand Up @@ -270,6 +274,14 @@ public static boolean isDoubleBlock(Block block, BlockState blockState) {
blockState.getValue(SlabBlock.TYPE) == SlabType.DOUBLE;
}

public static LevelChunk getCraftChunkHandle(CraftChunk craftChunk) {
if (CRAFT_CHUNK_GET_HANDLE.isValid())
return CRAFT_CHUNK_GET_HANDLE.invoke(craftChunk);

ServerLevel serverLevel = craftChunk.getCraftWorld().getHandle();
return serverLevel.getChunk(craftChunk.getX(), craftChunk.getZ());
}

public record UnloadedChunkCompound(net.minecraft.nbt.CompoundTag chunkCompound, ChunkPos chunkPos) {

public ListTag getSections() {
Expand Down
Expand Up @@ -7,6 +7,7 @@
import com.bgsoftware.superiorskyblock.core.SequentialListBuilder;
import com.bgsoftware.superiorskyblock.core.Text;
import com.bgsoftware.superiorskyblock.island.IslandUtils;
import com.bgsoftware.superiorskyblock.nms.v1194.NMSUtils;
import com.bgsoftware.superiorskyblock.nms.world.WorldEditSession;
import com.bgsoftware.superiorskyblock.tag.ByteTag;
import com.bgsoftware.superiorskyblock.tag.CompoundTag;
Expand Down Expand Up @@ -161,7 +162,7 @@ public List<ChunkPosition> getAffectedChunks() {

@Override
public void applyBlocks(Chunk bukkitChunk) {
LevelChunk levelChunk = ((CraftChunk) bukkitChunk).getHandle();
LevelChunk levelChunk = NMSUtils.getCraftChunkHandle((CraftChunk) bukkitChunk);
ChunkPos chunkPos = levelChunk.getPos();

long chunkKey = chunkPos.toLong();
Expand Down

0 comments on commit 377d126

Please sign in to comment.