From 96435cfc76e129f278febe8c478a8cf6cc48e875 Mon Sep 17 00:00:00 2001 From: Tahg Date: Mon, 21 Feb 2011 19:43:12 -0500 Subject: [PATCH] refactor Items --- .../net/minecraft/server/EntityHuman.java | 4 +- .../java/net/minecraft/server/ItemBlock.java | 2 +- .../minecraft/server/NetServerHandler.java | 8 ++-- .../org/bukkit/craftbukkit/CraftWorld.java | 8 ++-- .../bukkit/craftbukkit/block/CraftBlock.java | 37 +++++++++++++++++-- .../craftbukkit/block/CraftBlockState.java | 23 +++++++++--- .../bukkit/craftbukkit/entity/CraftItem.java | 14 ++++++- .../craftbukkit/entity/CraftItemDrop.java | 34 ----------------- 8 files changed, 72 insertions(+), 58 deletions(-) delete mode 100644 src/main/java/org/bukkit/craftbukkit/entity/CraftItemDrop.java diff --git a/src/main/java/net/minecraft/server/EntityHuman.java b/src/main/java/net/minecraft/server/EntityHuman.java index 66bae6e112..c48157359b 100644 --- a/src/main/java/net/minecraft/server/EntityHuman.java +++ b/src/main/java/net/minecraft/server/EntityHuman.java @@ -4,8 +4,8 @@ import java.util.List; // CraftBukkit start import org.bukkit.craftbukkit.CraftServer; +import org.bukkit.craftbukkit.entity.CraftItem; import org.bukkit.craftbukkit.TrigMath; -import org.bukkit.craftbukkit.entity.CraftItemDrop; import org.bukkit.entity.Player; import org.bukkit.event.entity.EntityDamageByEntityEvent; import org.bukkit.event.entity.EntityDamageEvent; @@ -223,7 +223,7 @@ public abstract class EntityHuman extends EntityLiving { // CraftBukkit start Player player = (Player)this.getBukkitEntity(); CraftServer server = ((WorldServer)world).getServer(); - CraftItemDrop drop = new CraftItemDrop(server, entityitem); + CraftItem drop = new CraftItem(server, entityitem); PlayerDropItemEvent event = new PlayerDropItemEvent(player, drop); server.getPluginManager().callEvent(event); diff --git a/src/main/java/net/minecraft/server/ItemBlock.java b/src/main/java/net/minecraft/server/ItemBlock.java index a400cae51c..7c182e907d 100644 --- a/src/main/java/net/minecraft/server/ItemBlock.java +++ b/src/main/java/net/minecraft/server/ItemBlock.java @@ -116,7 +116,7 @@ public class ItemBlock extends Item { world.setTypeId(i, j, k, 20); } - world.setTypeIdAndData(i, j, k, replacedBlockState.getTypeId(), replacedBlockState.getData().getData()); + world.setTypeIdAndData(i, j, k, replacedBlockState.getTypeId(), replacedBlockState.getRawData()); } } else { diff --git a/src/main/java/net/minecraft/server/NetServerHandler.java b/src/main/java/net/minecraft/server/NetServerHandler.java index a38b768d6f..ec8cabb48b 100644 --- a/src/main/java/net/minecraft/server/NetServerHandler.java +++ b/src/main/java/net/minecraft/server/NetServerHandler.java @@ -330,8 +330,8 @@ public class NetServerHandler extends NetHandler implements ICommandListener { // if we are destroying either a redstone wire with a current greater than 0 or // a redstone torch that is on, then we should notify plugins that this block has // returned to a current value of 0 (since it will once the redstone is destroyed) - if ((blockId == Block.REDSTONE_WIRE.id && block.getData() > 0) || blockId == Block.REDSTONE_TORCH_ON.id) { - server.getPluginManager().callEvent( new BlockRedstoneEvent(block, (blockId == Block.REDSTONE_WIRE.id ? block.getData() : 15), 0)); + if ((blockId == Block.REDSTONE_WIRE.id && block.getRawData() > 0) || blockId == Block.REDSTONE_TORCH_ON.id) { + server.getPluginManager().callEvent( new BlockRedstoneEvent(block, (blockId == Block.REDSTONE_WIRE.id ? block.getRawData() : 15), 0)); } event = new BlockDamageEvent(Type.BLOCK_DAMAGED, block, BlockDamageLevel.BROKEN, player); } else { @@ -364,8 +364,8 @@ public class NetServerHandler extends NetHandler implements ICommandListener { // if we are destroying either a redstone wire with a current greater than 0 or // a redstone torch that is on, then we should notify plugins that this block has // returned to a current value of 0 (since it will once the redstone is destroyed) - if ((blockId == Block.REDSTONE_WIRE.id && block.getData() > 0) || blockId == Block.REDSTONE_TORCH_ON.id) { - server.getPluginManager().callEvent( new BlockRedstoneEvent(block, (blockId == Block.REDSTONE_WIRE.id ? block.getData() : 15), 0)); + if ((blockId == Block.REDSTONE_WIRE.id && block.getRawData() > 0) || blockId == Block.REDSTONE_TORCH_ON.id) { + server.getPluginManager().callEvent( new BlockRedstoneEvent(block, (blockId == Block.REDSTONE_WIRE.id ? block.getRawData() : 15), 0)); } event = new BlockDamageEvent(Type.BLOCK_DAMAGED, block, BlockDamageLevel.BROKEN, player); } else { diff --git a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java index aca525176d..53bfed9c34 100644 --- a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java +++ b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java @@ -7,7 +7,6 @@ import org.bukkit.entity.Entity; import java.util.ArrayList; import java.util.HashMap; import java.util.List; -import java.util.Map; import java.util.Random; import net.minecraft.server.*; @@ -15,7 +14,6 @@ import org.bukkit.entity.Arrow; import org.bukkit.block.Block; import org.bukkit.entity.Boat; import org.bukkit.Chunk; -import org.bukkit.entity.ItemDrop; import org.bukkit.inventory.ItemStack; import org.bukkit.util.Vector; import org.bukkit.BlockChangeDelegate; @@ -212,7 +210,7 @@ public class CraftWorld implements World { return world; } - public ItemDrop dropItem(Location loc, ItemStack item) { + public org.bukkit.entity.Item dropItem(Location loc, ItemStack item) { net.minecraft.server.ItemStack stack = new net.minecraft.server.ItemStack( item.getTypeId(), item.getAmount(), @@ -223,10 +221,10 @@ public class CraftWorld implements World { world.a(entity); //TODO this is inconsistent with how Entity.getBukkitEntity() works. // However, this entity is not at the moment backed by a server entity class so it may be left. - return new CraftItemDrop(world.getServer(), entity); + return new CraftItem(world.getServer(), entity); } - public ItemDrop dropItemNaturally(Location loc, ItemStack item) { + public org.bukkit.entity.Item dropItemNaturally(Location loc, ItemStack item) { double xs = world.l.nextFloat() * 0.7F + (1.0F - 0.7F) * 0.5D; double ys = world.l.nextFloat() * 0.7F + (1.0F - 0.7F) * 0.5D; double zs = world.l.nextFloat() * 0.7F + (1.0F - 0.7F) * 0.5D; diff --git a/src/main/java/org/bukkit/craftbukkit/block/CraftBlock.java b/src/main/java/org/bukkit/craftbukkit/block/CraftBlock.java index f1ff00f302..0b9fc7aa50 100644 --- a/src/main/java/org/bukkit/craftbukkit/block/CraftBlock.java +++ b/src/main/java/org/bukkit/craftbukkit/block/CraftBlock.java @@ -4,14 +4,11 @@ package org.bukkit.craftbukkit.block; import org.bukkit.block.BlockFace; import org.bukkit.block.Biome; import org.bukkit.block.Block; -import org.bukkit.Location; import net.minecraft.server.BiomeBase; import org.bukkit.*; import org.bukkit.block.BlockState; import org.bukkit.craftbukkit.CraftChunk; -import org.bukkit.craftbukkit.block.CraftBlockState; -import org.bukkit.craftbukkit.block.CraftSign; import org.bukkit.util.BlockVector; public class CraftBlock implements Block { @@ -99,7 +96,7 @@ public class CraftBlock implements Block { * * @return block specific metadata */ - public byte getData() { + public byte getRawData() { return (byte) chunk.getHandle().b(this.x & 0xF, this.y & 0x7F, this.z & 0xF); } @@ -261,6 +258,25 @@ public class CraftBlock implements Block { return BlockFace.SELF; } } + + public static int blockFaceToNotch(BlockFace face) { + switch(face) { + case DOWN: + return 0; + case UP: + return 1; + case EAST: + return 2; + case WEST: + return 3; + case NORTH: + return 4; + case SOUTH: + return 5; + default: + return 7; //Good as anything here, but technically invalid + } + } public BlockState getState() { Material material = getType(); @@ -331,4 +347,17 @@ public class CraftBlock implements Block { public boolean equals( Object o ) { return this == o; } + + public boolean isBlockFacePowered(BlockFace face) { + return chunk.getHandle().d.j(x, y, z, blockFaceToNotch(face)); + } + + public boolean isBlockFaceIndirectlyPowered(BlockFace face) { + return chunk.getHandle().d.j(x, y, z, blockFaceToNotch(face)); + } + + @Deprecated + public byte getData() { + return getRawData(); + } } diff --git a/src/main/java/org/bukkit/craftbukkit/block/CraftBlockState.java b/src/main/java/org/bukkit/craftbukkit/block/CraftBlockState.java index 79724643e7..a30288d9fa 100644 --- a/src/main/java/org/bukkit/craftbukkit/block/CraftBlockState.java +++ b/src/main/java/org/bukkit/craftbukkit/block/CraftBlockState.java @@ -1,6 +1,7 @@ package org.bukkit.craftbukkit.block; +import org.bukkit.Location; import org.bukkit.block.Block; import org.bukkit.Chunk; import org.bukkit.Material; @@ -29,7 +30,7 @@ public class CraftBlockState implements BlockState { this.light = block.getLightLevel(); this.chunk = (CraftChunk)block.getChunk(); - createData(block.getData()); + createData(block.getRawData()); } /** @@ -83,8 +84,6 @@ public class CraftBlockState implements BlockState { * @param data New block specific metadata */ public void setData(final MaterialData data) { - world.getHandle().c(x, y, z, data.getData()); - Material mat = getType(); if ((mat == null) || (mat.getData() == null)) { @@ -122,11 +121,11 @@ public class CraftBlockState implements BlockState { * * @param type Type-Id to change this block to */ - public void setTypeId(final int type) { + public boolean setTypeId(final int type) { this.type = type; - world.getHandle().e(x, y, z, type); createData((byte)0); + return true; } /** @@ -176,7 +175,7 @@ public class CraftBlockState implements BlockState { } } - block.setData(data.getData()); + block.setData(getRawData()); } return true; @@ -190,4 +189,16 @@ public class CraftBlockState implements BlockState { this.data = mat.getNewData(data); } } + + public byte getRawData() { + return data.getData(); + } + + public Location getLocation() { + return new Location(world, x, y, z); + } + + public void setData(byte data) { + createData(data); + } } diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftItem.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftItem.java index b2bb9fe290..bafb1fe8a4 100644 --- a/src/main/java/org/bukkit/craftbukkit/entity/CraftItem.java +++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftItem.java @@ -1,16 +1,26 @@ package org.bukkit.craftbukkit.entity; import net.minecraft.server.EntityItem; - -import org.bukkit.craftbukkit.CraftServer; import org.bukkit.entity.Item; +import org.bukkit.inventory.ItemStack; +import org.bukkit.craftbukkit.inventory.CraftItemStack; +import org.bukkit.craftbukkit.CraftServer; public class CraftItem extends CraftEntity implements Item { + private EntityItem item; public CraftItem(CraftServer server, EntityItem entity) { super(server, entity); } + public ItemStack getItemStack() { + return new CraftItemStack(item.a); + } + + public void setItemStack(ItemStack stack) { + item.a = new net.minecraft.server.ItemStack(stack.getTypeId(), stack.getAmount(), stack.getDurability()); + } + @Override public String toString() { return "CraftItem"; diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftItemDrop.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftItemDrop.java deleted file mode 100644 index 686b854e78..0000000000 --- a/src/main/java/org/bukkit/craftbukkit/entity/CraftItemDrop.java +++ /dev/null @@ -1,34 +0,0 @@ -package org.bukkit.craftbukkit.entity; - -import net.minecraft.server.EntityItem; -import org.bukkit.entity.ItemDrop; -import org.bukkit.inventory.ItemStack; -import org.bukkit.craftbukkit.inventory.CraftItemStack; -import org.bukkit.craftbukkit.CraftServer; - -/** - * Represents an item drop. - * - * @author sk89q - */ -public class CraftItemDrop extends CraftEntity implements ItemDrop { - private EntityItem item; - - public CraftItemDrop(CraftServer server, EntityItem entity) { - super(server, entity); - this.item = entity; - } - - public ItemStack getItemStack() { - return new CraftItemStack(item.a); - } - - public void setItemStack(ItemStack stack) { - item.a = new net.minecraft.server.ItemStack(stack.getTypeId(), stack.getAmount(), stack.getDurability()); - } - - @Override - public String toString() { - return "CraftItemDrop{" + "item=" + item + '}'; - } -}