From 2cc70bb05588a9d99c6321e998472e76f998f04a Mon Sep 17 00:00:00 2001 From: Zeerix Date: Thu, 12 Jan 2012 03:10:10 +0100 Subject: [Bleeding] Implemented Block.breakNaturally and Block.getDrops --- .../org/bukkit/craftbukkit/block/CraftBlock.java | 57 ++++++++++++++++++++++ 1 file changed, 57 insertions(+) (limited to 'src/main/java/org') diff --git a/src/main/java/org/bukkit/craftbukkit/block/CraftBlock.java b/src/main/java/org/bukkit/craftbukkit/block/CraftBlock.java index 80ffc421..e44eb377 100644 --- a/src/main/java/org/bukkit/craftbukkit/block/CraftBlock.java +++ b/src/main/java/org/bukkit/craftbukkit/block/CraftBlock.java @@ -1,5 +1,10 @@ package org.bukkit.craftbukkit.block; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + import org.bukkit.block.BlockFace; import org.bukkit.block.Biome; import org.bukkit.block.Block; @@ -12,6 +17,7 @@ import net.minecraft.server.EnumSkyBlock; import org.bukkit.*; import org.bukkit.block.BlockState; import org.bukkit.craftbukkit.CraftChunk; +import org.bukkit.inventory.ItemStack; import org.bukkit.util.BlockVector; public class CraftBlock implements Block { @@ -347,6 +353,57 @@ public class CraftBlock implements Block { public PistonMoveReaction getPistonMoveReaction() { return PistonMoveReaction.getById(net.minecraft.server.Block.byId[this.getTypeId()].material.getPushReaction()); + } + + private boolean itemCausesDrops(ItemStack item) { + net.minecraft.server.Block block = net.minecraft.server.Block.byId[this.getTypeId()]; + net.minecraft.server.Item itemType = item != null ? net.minecraft.server.Item.byId[item.getTypeId()] : null; + return block != null && (block.material.k() || (itemType != null && itemType.a(block))); + } + + public boolean breakNaturally() { + net.minecraft.server.Block block = net.minecraft.server.Block.byId[this.getTypeId()]; + byte data = getData(); + setTypeId(Material.AIR.getId()); + if (block != null) { + block.dropNaturally(chunk.getHandle().world, x, y, z, data, 1.0F, 0); + return true; + } + return false; + } + + public boolean breakNaturally(ItemStack item) { + if (itemCausesDrops(item)) { + return breakNaturally(); + } else { + return setTypeId(Material.AIR.getId()); + } + } + + public Collection getDrops() { + List drops = new ArrayList(); + + net.minecraft.server.Block block = net.minecraft.server.Block.byId[this.getTypeId()]; + if (block != null) { + byte data = getData(); + // based on nms.Block.dropNaturally + int count = block.getDropCount(0, chunk.getHandle().world.random); + for (int i = 0; i < count; ++i) { + int item = block.getDropType(data, chunk.getHandle().world.random, 0); + if (item > 0) { + drops.add(new ItemStack(item, 1, (short) net.minecraft.server.Block.getDropData(block, data))); + } + } + } + return drops; + } + + public Collection getDrops(ItemStack item) { + if (itemCausesDrops(item)) { + return getDrops(); + } else { + return Collections.emptyList(); + } } } -- cgit v1.2.3