Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save WizardlyBump17/541a0b97d9af951d6baa85e99bb5001c to your computer and use it in GitHub Desktop.
Save WizardlyBump17/541a0b97d9af951d6baa85e99bb5001c to your computer and use it in GitHub Desktop.
Get the block drops. Works with fortune and silk touch
private static final Map<Material, BlockDrop> FIXED_BLOCKS = new HashMap<Material, BlockDrop>() {{
put(Material.CARROT, new BlockDrop(new MaterialData(Material.CARROT_ITEM)));
put(Material.POTATO, new BlockDrop(new MaterialData(Material.POTATO_ITEM)));
put(Material.NETHER_WARTS, new BlockDrop(new MaterialData(Material.NETHER_STALK)));
put(Material.COCOA, new BlockDrop(new MaterialData(Material.INK_SACK, (byte) 3)));
put(Material.IRON_ORE, new BlockDrop(new MaterialData(Material.IRON_ORE)));
put(Material.GOLD_ORE, new BlockDrop(new MaterialData(Material.GOLD_ORE)));
put(Material.COAL_ORE, new BlockDrop(new MaterialData(Material.COAL), new MaterialData(Material.COAL_ORE)));
put(Material.DIAMOND_ORE, new BlockDrop(new MaterialData(Material.DIAMOND), new MaterialData(Material.DIAMOND_ORE)));
put(Material.EMERALD_ORE, new BlockDrop(new MaterialData(Material.EMERALD), new MaterialData(Material.EMERALD_ORE)));
put(Material.LAPIS_ORE, new BlockDrop(new MaterialData(Material.INK_SACK, (byte) 4), new MaterialData(Material.LAPIS_ORE)));
put(Material.REDSTONE_ORE, new BlockDrop(new MaterialData(Material.REDSTONE), new MaterialData(Material.REDSTONE_ORE)));
put(Material.GLOWING_REDSTONE_ORE, new BlockDrop(new MaterialData(Material.REDSTONE), new MaterialData(Material.REDSTONE_ORE)));
put(Material.QUARTZ_ORE, new BlockDrop(new MaterialData(Material.QUARTZ), new MaterialData(Material.QUARTZ_ORE)));
put(Material.STONE, new BlockDrop(new MaterialData(Material.COBBLESTONE), new MaterialData(Material.STONE)));
}};
public static List<ItemStack> getDrops(Block block, ItemStack itemStack, Random random) {
boolean silkTouch = itemStack.containsEnchantment(Enchantment.SILK_TOUCH);
int amount = dropCount(block, silkTouch ? -1 : itemStack.getEnchantmentLevel(Enchantment.LOOT_BONUS_BLOCKS), random);
Material type = block.getType();
switch (type) {
case CROPS:
List<ItemStack> drops = new ArrayList<>(2);
drops.add(new ItemStack(Material.WHEAT));
drops.add(new ItemStack(Material.SEEDS, amount));
return drops;
case SUGAR_CANE_BLOCK:
return Collections.singletonList(new ItemStack(Material.SUGAR_CANE));
}
BlockDrop drop = FIXED_BLOCKS.get(type);
return Collections.singletonList(new ItemStack(
silkTouch ? drop.getSilkTouchDrop().getItemType() : drop.getDrop().getItemType(),
amount,
silkTouch ? drop.getSilkTouchDrop().getData() : drop.getDrop().getData()));
}
private static int dropCount(Block block, int fortune, Random random) {
net.minecraft.server.v1_8_R3.Block nmsBlock = CraftMagicNumbers.getBlock(block);
if (nmsBlock instanceof BlockCrops) {
int drops = 1;
int j = block.getData();
if (j >= 7) {
int k = 3 + Math.max(fortune, 0);
for(int l = 0; l < k; ++l)
if (random.nextInt(15) <= j)
drops++;
}
return drops;
}
if (nmsBlock instanceof BlockNetherWart) {
int j = 1;
if (block.getData() >= 3) {
j = 2 + random.nextInt(3);
if (fortune > 0)
j += random.nextInt(fortune + 1);
}
return j;
}
return fortune == -1 ? 1 : nmsBlock.getDropCount(fortune, random);
}
@Data
@AllArgsConstructor
static class BlockDrop {
final MaterialData drop, silkTouchDrop;
BlockDrop(MaterialData drop) {
this(drop, drop);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment