Skip to content

Commit

Permalink
NMS block material management fix
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Jan 6, 2022
1 parent 3e0f4b6 commit 0570d2b
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 117 deletions.
Expand Up @@ -25,8 +25,6 @@ public interface BlockHelper {

void setNbtData(Block block, CompoundTag compoundTag);

boolean hasBlock(Material material);

boolean setBlockResistance(Material material, float resistance);

float getBlockResistance(Material material);
Expand Down
Expand Up @@ -462,7 +462,7 @@ public static void registerTags() {
// Returns the explosion resistance for all blocks of this material type.
// -->
tagProcessor.registerTag(ElementTag.class, "block_resistance", (attribute, object) -> {
if (!NMSHandler.getBlockHelper().hasBlock(object.getMaterial())) {
if (!object.getMaterial().isBlock()) {
Debug.echoError("Provided material does not have a placeable block.");
return null;
}
Expand Down
Expand Up @@ -124,29 +124,9 @@ public void setNbtData(Block block, CompoundTag ctag) {
te.load(((CompoundTagImpl) ctag).toNMSTag());
}

private static net.minecraft.server.v1_14_R1.Block getBlockFrom(Material material) {
if (material == Material.FLOWER_POT) {
return Blocks.FLOWER_POT;
}
ItemStack is = CraftItemStack.asNMSCopy(new org.bukkit.inventory.ItemStack(material));
if (is == null) {
return null;
}
Item item = is.getItem();
if (!(item instanceof ItemBlock)) {
return null;
}
return ((ItemBlock) item).getBlock();
}

@Override
public boolean hasBlock(Material material) {
return getBlockFrom(material) != null;
}

@Override
public boolean setBlockResistance(Material material, float resistance) {
net.minecraft.server.v1_14_R1.Block block = getBlockFrom(material);
net.minecraft.server.v1_14_R1.Block block = getMaterialBlock(material);
if (block == null) {
return false;
}
Expand All @@ -157,7 +137,7 @@ public boolean setBlockResistance(Material material, float resistance) {

@Override
public float getBlockResistance(Material material) {
net.minecraft.server.v1_14_R1.Block block = getBlockFrom(material);
net.minecraft.server.v1_14_R1.Block block = getMaterialBlock(material);
if (block == null) {
return 0;
}
Expand All @@ -181,6 +161,9 @@ public org.bukkit.block.BlockState generateBlockState(Block block, Material mat)
public static final MethodHandle BLOCK_STRENGTH_SETTER = ReflectionHelper.getFinalSetter(net.minecraft.server.v1_14_R1.Block.class, "strength");

public net.minecraft.server.v1_14_R1.Block getMaterialBlock(Material bukkitMaterial) {
if (!bukkitMaterial.isBlock()) {
return null;
}
return ((CraftBlockData) bukkitMaterial.createBlockData()).getState().getBlock();
}

Expand Down
Expand Up @@ -117,29 +117,9 @@ public void setNbtData(Block block, CompoundTag ctag) {
te.load(((CompoundTagImpl) ctag).toNMSTag());
}

private static net.minecraft.server.v1_15_R1.Block getBlockFrom(Material material) {
if (material == Material.FLOWER_POT) {
return Blocks.FLOWER_POT;
}
ItemStack is = CraftItemStack.asNMSCopy(new org.bukkit.inventory.ItemStack(material));
if (is == null) {
return null;
}
Item item = is.getItem();
if (!(item instanceof ItemBlock)) {
return null;
}
return ((ItemBlock) item).getBlock();
}

@Override
public boolean hasBlock(Material material) {
return getBlockFrom(material) != null;
}

@Override
public boolean setBlockResistance(Material material, float resistance) {
net.minecraft.server.v1_15_R1.Block block = getBlockFrom(material);
net.minecraft.server.v1_15_R1.Block block = getMaterialBlock(material);
if (block == null) {
return false;
}
Expand All @@ -150,7 +130,7 @@ public boolean setBlockResistance(Material material, float resistance) {

@Override
public float getBlockResistance(Material material) {
net.minecraft.server.v1_15_R1.Block block = getBlockFrom(material);
net.minecraft.server.v1_15_R1.Block block = getMaterialBlock(material);
if (block == null) {
return 0;
}
Expand All @@ -174,6 +154,9 @@ public org.bukkit.block.BlockState generateBlockState(Block block, Material mat)
public static final MethodHandle BLOCK_STRENGTH_SETTER = ReflectionHelper.getFinalSetter(net.minecraft.server.v1_15_R1.Block.class, "strength");

public net.minecraft.server.v1_15_R1.Block getMaterialBlock(Material bukkitMaterial) {
if (!bukkitMaterial.isBlock()) {
return null;
}
return ((CraftBlockData) bukkitMaterial.createBlockData()).getState().getBlock();
}

Expand Down
Expand Up @@ -130,29 +130,9 @@ public void setNbtData(Block block, CompoundTag ctag) {
te.load(((CraftBlockData) block.getBlockData()).getState(), ((CompoundTagImpl) ctag).toNMSTag());
}

private static net.minecraft.server.v1_16_R3.Block getBlockFrom(Material material) {
if (material == Material.FLOWER_POT) {
return Blocks.FLOWER_POT;
}
ItemStack is = CraftItemStack.asNMSCopy(new org.bukkit.inventory.ItemStack(material));
if (is == null) {
return null;
}
Item item = is.getItem();
if (!(item instanceof ItemBlock)) {
return null;
}
return ((ItemBlock) item).getBlock();
}

@Override
public boolean hasBlock(Material material) {
return getBlockFrom(material) != null;
}

@Override
public boolean setBlockResistance(Material material, float resistance) {
net.minecraft.server.v1_16_R3.Block block = getBlockFrom(material);
net.minecraft.server.v1_16_R3.Block block = getMaterialBlock(material);
if (block == null) {
return false;
}
Expand All @@ -163,7 +143,7 @@ public boolean setBlockResistance(Material material, float resistance) {

@Override
public float getBlockResistance(Material material) {
net.minecraft.server.v1_16_R3.Block block = getBlockFrom(material);
net.minecraft.server.v1_16_R3.Block block = getMaterialBlock(material);
if (block == null) {
return 0;
}
Expand All @@ -184,6 +164,9 @@ public org.bukkit.block.BlockState generateBlockState(Block block, Material mat)
public static final MethodHandle BLOCK_STRENGTH_SETTER = ReflectionHelper.getFinalSetter(net.minecraft.server.v1_16_R3.BlockBase.BlockData.class, "strength");

public net.minecraft.server.v1_16_R3.Block getMaterialBlock(Material bukkitMaterial) {
if (!bukkitMaterial.isBlock()) {
return null;
}
return ((CraftBlockData) bukkitMaterial.createBlockData()).getState().getBlock();
}

Expand Down
Expand Up @@ -142,29 +142,9 @@ public void setNbtData(Block block, CompoundTag ctag) {
te.load(((CompoundTagImpl) ctag).toNMSTag());
}

private static net.minecraft.world.level.block.Block getBlockFrom(Material material) {
if (material == Material.FLOWER_POT) {
return Blocks.FLOWER_POT;
}
ItemStack is = CraftItemStack.asNMSCopy(new org.bukkit.inventory.ItemStack(material));
if (is == null) {
return null;
}
Item item = is.getItem();
if (!(item instanceof BlockItem)) {
return null;
}
return ((BlockItem) item).getBlock();
}

@Override
public boolean hasBlock(Material material) {
return getBlockFrom(material) != null;
}

@Override
public boolean setBlockResistance(Material material, float resistance) {
net.minecraft.world.level.block.Block block = getBlockFrom(material);
net.minecraft.world.level.block.Block block = getMaterialBlock(material);
if (block == null) {
return false;
}
Expand All @@ -174,7 +154,7 @@ public boolean setBlockResistance(Material material, float resistance) {

@Override
public float getBlockResistance(Material material) {
net.minecraft.world.level.block.Block block = getBlockFrom(material);
net.minecraft.world.level.block.Block block = getMaterialBlock(material);
if (block == null) {
return 0;
}
Expand Down Expand Up @@ -203,6 +183,9 @@ public org.bukkit.block.BlockState generateBlockState(Block block, Material mat)
public static final MethodHandle BLOCK_STRENGTH_SETTER = ReflectionHelper.getFinalSetterForFirstOfType(net.minecraft.world.level.block.state.BlockBehaviour.BlockStateBase.class, float.class); // destroySpeed

public net.minecraft.world.level.block.Block getMaterialBlock(Material bukkitMaterial) {
if (!bukkitMaterial.isBlock()) {
return null;
}
return ((CraftBlockData) bukkitMaterial.createBlockData()).getState().getBlock();
}

Expand Down Expand Up @@ -281,7 +264,7 @@ public void doRandomTick(Location location) {

@Override
public Instrument getInstrumentFor(Material mat) {
net.minecraft.world.level.block.Block blockType = getBlockFrom(mat);
net.minecraft.world.level.block.Block blockType = getMaterialBlock(mat);
NoteBlockInstrument nmsInstrument = NoteBlockInstrument.byState(blockType.defaultBlockState());
return Instrument.values()[(nmsInstrument.ordinal())];
}
Expand Down Expand Up @@ -311,7 +294,11 @@ public void ringBell(Bell block) {

@Override
public int getExpDrop(Block block, org.bukkit.inventory.ItemStack item) {
return getBlockFrom(block.getType()).getExpDrop(((CraftBlock) block).getNMS(), ((CraftBlock) block).getCraftWorld().getHandle(), ((CraftBlock) block).getPosition(),
net.minecraft.world.level.block.Block blockType = getMaterialBlock(block.getType());
if (blockType == null) {
return 0;
}
return blockType.getExpDrop(((CraftBlock) block).getNMS(), ((CraftBlock) block).getCraftWorld().getHandle(), ((CraftBlock) block).getPosition(),
item == null ? null : CraftItemStack.asNMSCopy(item));
}
}
Expand Up @@ -141,29 +141,9 @@ public void setNbtData(Block block, CompoundTag ctag) {
te.load(((CompoundTagImpl) ctag).toNMSTag());
}

private static net.minecraft.world.level.block.Block getBlockFrom(Material material) {
if (material == Material.FLOWER_POT) {
return Blocks.FLOWER_POT;
}
ItemStack is = CraftItemStack.asNMSCopy(new org.bukkit.inventory.ItemStack(material));
if (is == null) {
return null;
}
Item item = is.getItem();
if (!(item instanceof BlockItem)) {
return null;
}
return ((BlockItem) item).getBlock();
}

@Override
public boolean hasBlock(Material material) {
return getBlockFrom(material) != null;
}

@Override
public boolean setBlockResistance(Material material, float resistance) {
net.minecraft.world.level.block.Block block = getBlockFrom(material);
net.minecraft.world.level.block.Block block = getMaterialBlock(material);
if (block == null) {
return false;
}
Expand All @@ -173,7 +153,7 @@ public boolean setBlockResistance(Material material, float resistance) {

@Override
public float getBlockResistance(Material material) {
net.minecraft.world.level.block.Block block = getBlockFrom(material);
net.minecraft.world.level.block.Block block = getMaterialBlock(material);
if (block == null) {
return 0;
}
Expand Down Expand Up @@ -202,6 +182,9 @@ public org.bukkit.block.BlockState generateBlockState(Block block, Material mat)
public static final MethodHandle BLOCK_STRENGTH_SETTER = ReflectionHelper.getFinalSetterForFirstOfType(net.minecraft.world.level.block.state.BlockBehaviour.BlockStateBase.class, float.class); // destroySpeed

public net.minecraft.world.level.block.Block getMaterialBlock(Material bukkitMaterial) {
if (!bukkitMaterial.isBlock()) {
return null;
}
return ((CraftBlockData) bukkitMaterial.createBlockData()).getState().getBlock();
}

Expand Down Expand Up @@ -280,7 +263,7 @@ public void doRandomTick(Location location) {

@Override
public Instrument getInstrumentFor(Material mat) {
net.minecraft.world.level.block.Block blockType = getBlockFrom(mat);
net.minecraft.world.level.block.Block blockType = getMaterialBlock(mat);
NoteBlockInstrument nmsInstrument = NoteBlockInstrument.byState(blockType.defaultBlockState());
return Instrument.values()[(nmsInstrument.ordinal())];
}
Expand Down Expand Up @@ -310,7 +293,11 @@ public void ringBell(Bell block) {

@Override
public int getExpDrop(Block block, org.bukkit.inventory.ItemStack item) {
return getBlockFrom(block.getType()).getExpDrop(((CraftBlock) block).getNMS(), ((CraftBlock) block).getCraftWorld().getHandle(), ((CraftBlock) block).getPosition(),
net.minecraft.world.level.block.Block blockType = getMaterialBlock(block.getType());
if (blockType == null) {
return 0;
}
return blockType.getExpDrop(((CraftBlock) block).getNMS(), ((CraftBlock) block).getCraftWorld().getHandle(), ((CraftBlock) block).getPosition(),
item == null ? null : CraftItemStack.asNMSCopy(item));
}
}

0 comments on commit 0570d2b

Please sign in to comment.