Get drop from block

Discussion in 'Resources' started by Coryf88, Jul 30, 2011.

Thread Status:
Not open for further replies.
  1. Offline

    Coryf88

    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
    1. import java.lang.reflect.Method;
    2. import java.util.Random;
    3.  
    4. import org.bukkit.block.Block;
    5. import org.bukkit.inventory.ItemStack;
    6.  
    7. public class BlockHelper {
    8. private static Random random = new Random();
    9.  
    10. /**
    11. * Get a ItemStack containing what the block drops.
    12. *
    13. * @param block The block to get the drop of.
    14. * @return The drop of the block or null if the block doesn't drop anything.
    15. * @throws RuntimeException If a problem occurred while retrieving the drop.
    16. */
    17. public static ItemStack getDrop(Block block) throws RuntimeException {
    18. if (block == null) return null;
    19. int blockTypeId = block.getTypeId();
    20. if (blockTypeId < 1 || blockTypeId > 255) return null;
    21. try {
    22. net.minecraft.server.Block b = net.minecraft.server.Block.byId[blockTypeId];
    23.  
    24. int typeId = b.getDropType(blockTypeId, BlockHelper.random, 0);
    25. if (typeId < 1) return null;
    26.  
    27. int dropCount = b.getDropCount(0, BlockHelper.random);
    28. if (dropCount < 1) return null;
    29.  
    30. Method m = BlockHelper.getMethod(b.getClass(), "getDropData", new Class[] {int.class});
    31. m.setAccessible(true);
    32. byte dropData = ((Integer)m.invoke(b, block.getData())).byteValue();
    33.  
    34. return new ItemStack(typeId, dropCount, dropData);
    35. } catch (Exception e) {
    36. throw new RuntimeException("A severe error occured while retreiving the data dropped.", e);
    37. }
    38. }
    39.  
    40. /**
    41. * Equivalent to java.lang.Class.getDeclaredMethod, except also searches through the <i>clazz</i>'s superclasses.
    42. *
    43. * @param clazz The class to get the method from.
    44. * @param methodName the name of the method
    45. * @param parameters the parameter array
    46. * @return the Method object for the method of this class matching the specified name and parameters
    47. * @throws NoSuchMethodException if a matching method is not found
    48. * @throws NullPointerException if methodName is null
    49. */
    50. private static Method getMethod(Class<?> clazz, String methodName, Class<?>[] parameters) throws NoSuchMethodException, NullPointerException {
    51. if (methodName == null) throw new NullPointerException();
    52. if (clazz == null) throw new NoSuchMethodException();
    53. try {
    54. return clazz.getDeclaredMethod(methodName, parameters);
    55. } catch (Exception e) {
    56. return BlockHelper.getMethod(clazz.getSuperclass(), methodName, parameters);
    57. }
    58. }
    59. }


    CB1000 (Minecraft 1.7.*) (open)

    Code:java
    1. import java.lang.reflect.Method;
    2. import java.util.Random;
    3.  
    4. import org.bukkit.block.Block;
    5. import org.bukkit.inventory.ItemStack;
    6.  
    7. public class BlockHelper {
    8. private static Random random = new Random();
    9.  
    10. /**
    11. * Get a ItemStack containing what the block drops.
    12. *
    13. * @param block The block to get the drop of.
    14. * @return The drop of the block or null if the block doesn't drop anything.
    15. * @throws RuntimeException If a problem occurred while retrieving the drop.
    16. */
    17. public static ItemStack getDrop(Block block) throws RuntimeException {
    18. if (block == null) return null;
    19. int blockTypeId = block.getTypeId();
    20. if (blockTypeId < 1 || blockTypeId > 255) return null;
    21. try {
    22. net.minecraft.server.Block b = net.minecraft.server.Block.byId[blockTypeId];
    23.  
    24. int typeId = b.a(blockTypeId, BlockHelper.random);
    25. if (typeId < 1) return null;
    26.  
    27. int dropCount = b.a(BlockHelper.random);
    28. if (dropCount < 1) return null;
    29.  
    30. Method m = BlockHelper.getMethod(b.getClass(), "a_", new Class[] {int.class});
    31. m.setAccessible(true);
    32. byte dropData = ((Integer)m.invoke(b, block.getData())).byteValue();
    33.  
    34. return new ItemStack(typeId, dropCount, dropData);
    35. } catch (Exception e) {
    36. throw new RuntimeException("A severe error occured while retreiving the data dropped.", e);
    37. }
    38. }
    39.  
    40. /**
    41. * Equivalent to java.lang.Class.getDeclaredMethod, except also searches through the <i>clazz</i>'s superclasses.
    42. *
    43. * @param clazz The class to get the method from.
    44. * @param methodName the name of the method
    45. * @param parameters the parameter array
    46. * @return the Method object for the method of this class matching the specified name and parameters
    47. * @throws NoSuchMethodException if a matching method is not found
    48. * @throws NullPointerException if methodName is null
    49. */
    50. private static Method getMethod(Class<?> clazz, String methodName, Class<?>[] parameters) throws NoSuchMethodException, NullPointerException {
    51. if (methodName == null) throw new NullPointerException();
    52. if (clazz == null) throw new NoSuchMethodException();
    53. try {
    54. return clazz.getDeclaredMethod(methodName, parameters);
    55. } catch (Exception e) {
    56. return getMethod(clazz.getSuperclass(), methodName, parameters);
    57. }
    58. }
    59. }

     
  2. Offline

    Tim Visee

    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?
     
  3. Offline

    Coryf88

    For the NMS namespace, add CraftBukkit as a library to your project. For Method, make sure you're importing java.lang.reflect.Method.
     
  4. Offline

    Tim Visee

    Of course I already added the CraftBukkit API, but it still isn't detected... :( (The method works I think)
     
Thread Status:
Not open for further replies.

Share This Page