[Bleeding] Implemented Block.breakNaturally and Block.getDrops

This commit is contained in:
Zeerix 2012-01-12 03:10:10 +01:00 committed by Erik Broes
parent fb1cfea89e
commit 2cc70bb055
2 changed files with 63 additions and 0 deletions

View File

@ -616,4 +616,10 @@ public class Block {
r[0] = true; r[0] = true;
StatisticList.b(); StatisticList.b();
} }
// CraftBukkit start - getDropData(int) is protected
public static int getDropData(Block block, int data) {
return block.getDropData(data);
}
// CraftBukkit end
} }

View File

@ -1,5 +1,10 @@
package org.bukkit.craftbukkit.block; 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.BlockFace;
import org.bukkit.block.Biome; import org.bukkit.block.Biome;
import org.bukkit.block.Block; import org.bukkit.block.Block;
@ -12,6 +17,7 @@ import net.minecraft.server.EnumSkyBlock;
import org.bukkit.*; import org.bukkit.*;
import org.bukkit.block.BlockState; import org.bukkit.block.BlockState;
import org.bukkit.craftbukkit.CraftChunk; import org.bukkit.craftbukkit.CraftChunk;
import org.bukkit.inventory.ItemStack;
import org.bukkit.util.BlockVector; import org.bukkit.util.BlockVector;
public class CraftBlock implements Block { public class CraftBlock implements Block {
@ -347,6 +353,57 @@ public class CraftBlock implements Block {
public PistonMoveReaction getPistonMoveReaction() { public PistonMoveReaction getPistonMoveReaction() {
return PistonMoveReaction.getById(net.minecraft.server.Block.byId[this.getTypeId()].material.getPushReaction()); 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<ItemStack> getDrops() {
List<ItemStack> drops = new ArrayList<ItemStack>();
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<ItemStack> getDrops(ItemStack item) {
if (itemCausesDrops(item)) {
return getDrops();
} else {
return Collections.emptyList();
}
} }
} }