From 80c43fc1deaa164a5eb85aa779eb26a7deca7287 Mon Sep 17 00:00:00 2001 From: md_5 Date: Thu, 2 Aug 2018 20:24:56 +1000 Subject: [PATCH] SPIGOT-4214: Catch attempts to create BlockData for items --- .../bukkit/craftbukkit/block/data/CraftBlockData.java | 2 ++ .../org/bukkit/craftbukkit/util/CraftMagicNumbers.java | 1 + src/test/java/org/bukkit/BlockDataTest.java | 10 ++++++++++ 3 files changed, 13 insertions(+) diff --git a/src/main/java/org/bukkit/craftbukkit/block/data/CraftBlockData.java b/src/main/java/org/bukkit/craftbukkit/block/data/CraftBlockData.java index 882e39be33..cfe5903cda 100644 --- a/src/main/java/org/bukkit/craftbukkit/block/data/CraftBlockData.java +++ b/src/main/java/org/bukkit/craftbukkit/block/data/CraftBlockData.java @@ -417,6 +417,8 @@ public class CraftBlockData implements BlockData { } public static CraftBlockData newData(Material material, String data) { + Preconditions.checkArgument(material == null || material.isBlock(), "Cannot get data for not block %s", material); + IBlockData blockData; Block block = CraftMagicNumbers.getBlock(material); diff --git a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java index 045b5b4131..3474536f75 100644 --- a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java +++ b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java @@ -84,6 +84,7 @@ public final class CraftMagicNumbers implements UnsafeValues { for (Material material : Material.values()) { MinecraftKey key = key(material); + // TODO: only register if block/item? MATERIAL_ITEM.put(material, Item.REGISTRY.get(key)); MATERIAL_BLOCK.put(material, Block.REGISTRY.get(key)); } diff --git a/src/test/java/org/bukkit/BlockDataTest.java b/src/test/java/org/bukkit/BlockDataTest.java index bd2c249b3b..e43de76569 100644 --- a/src/test/java/org/bukkit/BlockDataTest.java +++ b/src/test/java/org/bukkit/BlockDataTest.java @@ -51,6 +51,16 @@ public class BlockDataTest extends AbstractTestingBase { 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 public void testClone() { Cake cakeTest = (Cake) CraftBlockData.fromData(Blocks.CAKE.getBlockData().set(BlockCake.BITES, 3));