SPIGOT-4214: Catch attempts to create BlockData for items

By: md_5 <git@md-5.net>
This commit is contained in:
CraftBukkit/Spigot 2018-08-02 20:24:56 +10:00
parent bb6a5d8612
commit e9fc2452ed
3 changed files with 13 additions and 0 deletions

View file

@ -417,6 +417,8 @@ public class CraftBlockData implements BlockData {
} }
public static CraftBlockData newData(Material material, String data) { public static CraftBlockData newData(Material material, String data) {
Preconditions.checkArgument(material == null || material.isBlock(), "Cannot get data for not block %s", material);
IBlockData blockData; IBlockData blockData;
Block block = CraftMagicNumbers.getBlock(material); Block block = CraftMagicNumbers.getBlock(material);

View file

@ -84,6 +84,7 @@ public final class CraftMagicNumbers implements UnsafeValues {
for (Material material : Material.values()) { for (Material material : Material.values()) {
MinecraftKey key = key(material); MinecraftKey key = key(material);
// TODO: only register if block/item?
MATERIAL_ITEM.put(material, Item.REGISTRY.get(key)); MATERIAL_ITEM.put(material, Item.REGISTRY.get(key));
MATERIAL_BLOCK.put(material, Block.REGISTRY.get(key)); MATERIAL_BLOCK.put(material, Block.REGISTRY.get(key));
} }

View file

@ -51,6 +51,16 @@ public class BlockDataTest extends AbstractTestingBase {
CraftBlockData.newData(Material.CAKE, cakeTest.toString()); CraftBlockData.newData(Material.CAKE, cakeTest.toString());
} }
@Test(expected = IllegalArgumentException.class)
public void testItem() {
CraftBlockData.newData(Material.DIAMOND_AXE, null);
}
@Test(expected = IllegalArgumentException.class)
public void testItemParse() {
CraftBlockData.newData(null, "minecraft:diamond_axe");
}
@Test @Test
public void testClone() { public void testClone() {
Cake cakeTest = (Cake) CraftBlockData.fromData(Blocks.CAKE.getBlockData().set(BlockCake.BITES, 3)); Cake cakeTest = (Cake) CraftBlockData.fromData(Blocks.CAKE.getBlockData().set(BlockCake.BITES, 3));