I've seen several people looking for a way to get the item that a block drops. I wrote this for my own usage and have decided to release it for others to use. Feel free to use this however you wish, credit is not necessary. If an exception is thrown from the getDrops method, you are most likely using a newer/older version of bukkit/minecraft which contains different names for the NMS methods. CB1597 (Minecraft 1.0.1) Untested, but should work just fine. Code:java import java.lang.reflect.Method;import java.util.Random; import org.bukkit.block.Block;import org.bukkit.inventory.ItemStack; public class BlockHelper { private static Random random = new Random(); /** * Get a ItemStack containing what the block drops. * * @param block The block to get the drop of. * @return The drop of the block or null if the block doesn't drop anything. * @throws RuntimeException If a problem occurred while retrieving the drop. */ public static ItemStack getDrop(Block block) throws RuntimeException { if (block == null) return null; int blockTypeId = block.getTypeId(); if (blockTypeId < 1 || blockTypeId > 255) return null; try { net.minecraft.server.Block b = net.minecraft.server.Block.byId[blockTypeId]; int typeId = b.getDropType(blockTypeId, BlockHelper.random, 0); if (typeId < 1) return null; int dropCount = b.getDropCount(0, BlockHelper.random); if (dropCount < 1) return null; Method m = BlockHelper.getMethod(b.getClass(), "getDropData", new Class[] {int.class}); m.setAccessible(true); byte dropData = ((Integer)m.invoke(b, block.getData())).byteValue(); return new ItemStack(typeId, dropCount, dropData); } catch (Exception e) { throw new RuntimeException("A severe error occured while retreiving the data dropped.", e); } } /** * Equivalent to java.lang.Class.getDeclaredMethod, except also searches through the <i>clazz</i>'s superclasses. * * @param clazz The class to get the method from. * @param methodName the name of the method * @param parameters the parameter array * @return the Method object for the method of this class matching the specified name and parameters * @throws NoSuchMethodException if a matching method is not found * @throws NullPointerException if methodName is null */ private static Method getMethod(Class<?> clazz, String methodName, Class<?>[] parameters) throws NoSuchMethodException, NullPointerException { if (methodName == null) throw new NullPointerException(); if (clazz == null) throw new NoSuchMethodException(); try { return clazz.getDeclaredMethod(methodName, parameters); } catch (Exception e) { return BlockHelper.getMethod(clazz.getSuperclass(), methodName, parameters); } }} CB1000 (Minecraft 1.7.*) (Move your mouse to reveal the content) CB1000 (Minecraft 1.7.*) (open) CB1000 (Minecraft 1.7.*) (close) Code:java import java.lang.reflect.Method;import java.util.Random; import org.bukkit.block.Block;import org.bukkit.inventory.ItemStack; public class BlockHelper { private static Random random = new Random(); /** * Get a ItemStack containing what the block drops. * * @param block The block to get the drop of. * @return The drop of the block or null if the block doesn't drop anything. * @throws RuntimeException If a problem occurred while retrieving the drop. */ public static ItemStack getDrop(Block block) throws RuntimeException { if (block == null) return null; int blockTypeId = block.getTypeId(); if (blockTypeId < 1 || blockTypeId > 255) return null; try { net.minecraft.server.Block b = net.minecraft.server.Block.byId[blockTypeId]; int typeId = b.a(blockTypeId, BlockHelper.random); if (typeId < 1) return null; int dropCount = b.a(BlockHelper.random); if (dropCount < 1) return null; Method m = BlockHelper.getMethod(b.getClass(), "a_", new Class[] {int.class}); m.setAccessible(true); byte dropData = ((Integer)m.invoke(b, block.getData())).byteValue(); return new ItemStack(typeId, dropCount, dropData); } catch (Exception e) { throw new RuntimeException("A severe error occured while retreiving the data dropped.", e); } } /** * Equivalent to java.lang.Class.getDeclaredMethod, except also searches through the <i>clazz</i>'s superclasses. * * @param clazz The class to get the method from. * @param methodName the name of the method * @param parameters the parameter array * @return the Method object for the method of this class matching the specified name and parameters * @throws NoSuchMethodException if a matching method is not found * @throws NullPointerException if methodName is null */ private static Method getMethod(Class<?> clazz, String methodName, Class<?>[] parameters) throws NoSuchMethodException, NullPointerException { if (methodName == null) throw new NullPointerException(); if (clazz == null) throw new NoSuchMethodException(); try { return clazz.getDeclaredMethod(methodName, parameters); } catch (Exception e) { return getMethod(clazz.getSuperclass(), methodName, parameters); } }}
When I try to use this code Eclipse (my java programming software) says that the net.minecraft.... and the Method type cant be resolved, do I need to import something?
For the NMS namespace, add CraftBukkit as a library to your project. For Method, make sure you're importing java.lang.reflect.Method.
Of course I already added the CraftBukkit API, but it still isn't detected... (The method works I think)