From 86d63605da11cb2bca574e0d93677f1d6d6fa214 Mon Sep 17 00:00:00 2001 From: CraftBukkit/Spigot Date: Sat, 9 Jul 2022 20:09:34 +1000 Subject: [PATCH] #1079: Add support checking/survivability methods for BlockData By: Parker Hawke --- .../craftbukkit/block/CraftBlockSupport.java | 28 ++++++++++++++++ .../block/data/CraftBlockData.java | 33 +++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 paper-server/src/main/java/org/bukkit/craftbukkit/block/CraftBlockSupport.java diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/block/CraftBlockSupport.java b/paper-server/src/main/java/org/bukkit/craftbukkit/block/CraftBlockSupport.java new file mode 100644 index 0000000000..dc4b113f93 --- /dev/null +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/block/CraftBlockSupport.java @@ -0,0 +1,28 @@ +package org.bukkit.craftbukkit.block; + +import net.minecraft.world.level.block.EnumBlockSupport; +import org.bukkit.block.BlockSupport; + +public final class CraftBlockSupport { + + private CraftBlockSupport() { + } + + public static BlockSupport toBukkit(EnumBlockSupport support) { + return switch (support) { + case FULL -> BlockSupport.FULL; + case CENTER -> BlockSupport.CENTER; + case RIGID -> BlockSupport.RIGID; + default -> throw new IllegalArgumentException("Unsupported EnumBlockSupport type: " + support + ". This is a bug."); + }; + } + + public static EnumBlockSupport toNMS(BlockSupport support) { + return switch (support) { + case FULL -> EnumBlockSupport.FULL; + case CENTER -> EnumBlockSupport.CENTER; + case RIGID -> EnumBlockSupport.RIGID; + default -> throw new IllegalArgumentException("Unsupported BlockSupport type: " + support + ". This is a bug."); + }; + } +} diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/block/data/CraftBlockData.java b/paper-server/src/main/java/org/bukkit/craftbukkit/block/data/CraftBlockData.java index 1373ff20a4..546a3cc025 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/block/data/CraftBlockData.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/block/data/CraftBlockData.java @@ -10,10 +10,12 @@ import java.util.Map; import java.util.Set; import java.util.stream.Collectors; import net.minecraft.commands.arguments.blocks.ArgumentBlock; +import net.minecraft.core.BlockPosition; import net.minecraft.core.EnumDirection; import net.minecraft.core.IRegistry; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.util.INamable; +import net.minecraft.world.level.BlockAccessAir; import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.state.IBlockData; import net.minecraft.world.level.block.state.IBlockDataHolder; @@ -21,12 +23,16 @@ import net.minecraft.world.level.block.state.properties.BlockStateBoolean; import net.minecraft.world.level.block.state.properties.BlockStateEnum; import net.minecraft.world.level.block.state.properties.BlockStateInteger; import net.minecraft.world.level.block.state.properties.IBlockState; +import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.SoundGroup; import org.bukkit.block.BlockFace; +import org.bukkit.block.BlockSupport; import org.bukkit.block.data.BlockData; import org.bukkit.craftbukkit.CraftSoundGroup; +import org.bukkit.craftbukkit.CraftWorld; import org.bukkit.craftbukkit.block.CraftBlock; +import org.bukkit.craftbukkit.block.CraftBlockSupport; import org.bukkit.craftbukkit.util.CraftMagicNumbers; public class CraftBlockData implements BlockData { @@ -541,4 +547,31 @@ public class CraftBlockData implements BlockData { public SoundGroup getSoundGroup() { return CraftSoundGroup.getSoundGroup(state.getSoundType()); } + + @Override + public boolean isSupported(org.bukkit.block.Block block) { + Preconditions.checkArgument(block != null, "block must not be null"); + + CraftBlock craftBlock = (CraftBlock) block; + return state.canSurvive(craftBlock.getCraftWorld().getHandle(), craftBlock.getPosition()); + } + + @Override + public boolean isSupported(Location location) { + Preconditions.checkArgument(location != null, "location must not be null"); + + CraftWorld world = (CraftWorld) location.getWorld(); + Preconditions.checkArgument(world != null, "location must not have a null world"); + + BlockPosition position = new BlockPosition(location.getBlockX(), location.getBlockY(), location.getBlockZ()); + return state.canSurvive(world.getHandle(), position); + } + + @Override + public boolean isFaceSturdy(BlockFace face, BlockSupport support) { + Preconditions.checkArgument(face != null, "face must not be null"); + Preconditions.checkArgument(support != null, "support must not be null"); + + return state.isFaceSturdy(BlockAccessAir.INSTANCE, BlockPosition.ZERO, CraftBlock.blockFaceToNotch(face), CraftBlockSupport.toNMS(support)); + } }