From 3288d22334b16f0505d2cfa5612e45c1f4151154 Mon Sep 17 00:00:00 2001 From: tastybento Date: Sat, 26 Oct 2024 09:26:22 -0700 Subject: [PATCH] Add NMS support for 1.21.3 --- .../bentobox/bentobox/api/user/User.java | 1 - .../PasteHandlerImpl.java | 52 +++++++++++++++++++ .../WorldRegeneratorImpl.java | 26 ++++++++++ .../world/bentobox/bentobox/util/Util.java | 2 + .../versions/ServerCompatibility.java | 7 ++- 5 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 src/main/java/world/bentobox/bentobox/nms/v1_21_3_R0_1_SNAPSHOT/PasteHandlerImpl.java create mode 100644 src/main/java/world/bentobox/bentobox/nms/v1_21_3_R0_1_SNAPSHOT/WorldRegeneratorImpl.java diff --git a/src/main/java/world/bentobox/bentobox/api/user/User.java b/src/main/java/world/bentobox/bentobox/api/user/User.java index 6ce478236..0951ae4b5 100644 --- a/src/main/java/world/bentobox/bentobox/api/user/User.java +++ b/src/main/java/world/bentobox/bentobox/api/user/User.java @@ -616,7 +616,6 @@ public class User implements MetaDataAble { * @param message The message to send, containing inline commands in square brackets. */ public void sendRawMessage(String message) { - BentoBox.getInstance().logDebug(message); // Create a base TextComponent for the message TextComponent baseComponent = new TextComponent(); diff --git a/src/main/java/world/bentobox/bentobox/nms/v1_21_3_R0_1_SNAPSHOT/PasteHandlerImpl.java b/src/main/java/world/bentobox/bentobox/nms/v1_21_3_R0_1_SNAPSHOT/PasteHandlerImpl.java new file mode 100644 index 000000000..dbdd672f9 --- /dev/null +++ b/src/main/java/world/bentobox/bentobox/nms/v1_21_3_R0_1_SNAPSHOT/PasteHandlerImpl.java @@ -0,0 +1,52 @@ +package world.bentobox.bentobox.nms.v1_21_3_R0_1_SNAPSHOT; + +import java.util.concurrent.CompletableFuture; + +import org.bukkit.Location; +import org.bukkit.block.Block; +import org.bukkit.block.data.BlockData; +import org.bukkit.craftbukkit.v1_21_R2.CraftWorld; +import org.bukkit.craftbukkit.v1_21_R2.block.data.CraftBlockData; + +import net.minecraft.core.BlockPosition; +import net.minecraft.world.level.block.state.IBlockData; +import net.minecraft.world.level.chunk.Chunk; +import world.bentobox.bentobox.blueprints.dataobjects.BlueprintBlock; +import world.bentobox.bentobox.database.objects.Island; +import world.bentobox.bentobox.nms.PasteHandler; +import world.bentobox.bentobox.util.DefaultPasteUtil; +import world.bentobox.bentobox.util.Util; + +public class PasteHandlerImpl implements PasteHandler { + + protected static final IBlockData AIR = ((CraftBlockData) AIR_BLOCKDATA).getState(); + + /** + * Set the block to the location + * + * @param island - island + * @param location - location + * @param bpBlock - blueprint block + */ + @Override + public CompletableFuture setBlock(Island island, Location location, BlueprintBlock bpBlock) { + return Util.getChunkAtAsync(location).thenRun(() -> { + Block block = location.getBlock(); + // Set the block data - default is AIR + BlockData bd = DefaultPasteUtil.createBlockData(bpBlock); + CraftBlockData craft = (CraftBlockData) bd; + net.minecraft.world.level.World nmsWorld = ((CraftWorld) location.getWorld()).getHandle(); + Chunk nmsChunk = nmsWorld.d(location.getBlockX() >> 4, location.getBlockZ() >> 4); + BlockPosition bp = new BlockPosition(location.getBlockX(), location.getBlockY(), location.getBlockZ()); + // Setting the block to air before setting to another state prevents some console errors + nmsChunk.a(bp, AIR, false); + nmsChunk.a(bp, craft.getState(), false); + block.setBlockData(bd, false); + DefaultPasteUtil.setBlockState(island, block, bpBlock); + // Set biome + if (bpBlock.getBiome() != null) { + block.setBiome(bpBlock.getBiome()); + } + }); + } +} diff --git a/src/main/java/world/bentobox/bentobox/nms/v1_21_3_R0_1_SNAPSHOT/WorldRegeneratorImpl.java b/src/main/java/world/bentobox/bentobox/nms/v1_21_3_R0_1_SNAPSHOT/WorldRegeneratorImpl.java new file mode 100644 index 000000000..818644e14 --- /dev/null +++ b/src/main/java/world/bentobox/bentobox/nms/v1_21_3_R0_1_SNAPSHOT/WorldRegeneratorImpl.java @@ -0,0 +1,26 @@ +package world.bentobox.bentobox.nms.v1_21_3_R0_1_SNAPSHOT; + +import org.bukkit.block.data.BlockData; +import org.bukkit.craftbukkit.v1_21_R2.CraftWorld; +import org.bukkit.craftbukkit.v1_21_R2.block.data.CraftBlockData; + +import net.minecraft.core.BlockPosition; +import net.minecraft.world.level.World; +import net.minecraft.world.level.chunk.Chunk; +import world.bentobox.bentobox.nms.CopyWorldRegenerator; + +public class WorldRegeneratorImpl extends CopyWorldRegenerator { + + @Override + public void setBlockInNativeChunk(org.bukkit.Chunk chunk, int x, int y, int z, BlockData blockData, + boolean applyPhysics) { + CraftBlockData craft = (CraftBlockData) blockData; + World nmsWorld = ((CraftWorld) chunk.getWorld()).getHandle(); + Chunk nmsChunk = nmsWorld.d(chunk.getX(), chunk.getZ()); + BlockPosition bp = new BlockPosition((chunk.getX() << 4) + x, y, (chunk.getZ() << 4) + z); + // Setting the block to air before setting to another state prevents some console errors + nmsChunk.a(bp, PasteHandlerImpl.AIR, applyPhysics); + nmsChunk.a(bp, craft.getState(), applyPhysics); + } + +} \ No newline at end of file diff --git a/src/main/java/world/bentobox/bentobox/util/Util.java b/src/main/java/world/bentobox/bentobox/util/Util.java index 411409f8b..02674d784 100644 --- a/src/main/java/world/bentobox/bentobox/util/Util.java +++ b/src/main/java/world/bentobox/bentobox/util/Util.java @@ -743,6 +743,7 @@ public class Util { throw new IllegalStateException("Class " + clazz.getName() + " does not implement WorldRegenerator"); } } catch (Exception e) { + e.printStackTrace(); plugin.logWarning("No Regenerator found for " + bukkitVersion + ", falling back to Bukkit API."); handler = new world.bentobox.bentobox.nms.fallback.WorldRegeneratorImpl(); } @@ -772,6 +773,7 @@ public class Util { throw new IllegalStateException("Class " + clazz.getName() + " does not implement PasteHandler"); } } catch (Exception e) { + e.printStackTrace(); plugin.logWarning("No PasteHandler found for " + bukkitVersion + ", falling back to Bukkit API."); handler = new world.bentobox.bentobox.nms.fallback.PasteHandlerImpl(); } diff --git a/src/main/java/world/bentobox/bentobox/versions/ServerCompatibility.java b/src/main/java/world/bentobox/bentobox/versions/ServerCompatibility.java index d0a79cbcb..2b356e31e 100644 --- a/src/main/java/world/bentobox/bentobox/versions/ServerCompatibility.java +++ b/src/main/java/world/bentobox/bentobox/versions/ServerCompatibility.java @@ -249,7 +249,12 @@ public class ServerCompatibility { /** * @since 2.5.0 */ - V1_21_1(Compatibility.COMPATIBLE); + V1_21_1(Compatibility.COMPATIBLE), + + /** + * @since 2.7.0 + */ + V1_21_2(Compatibility.INCOMPATIBLE), V1_21_3(Compatibility.COMPATIBLE); private final Compatibility compatibility;