diff --git a/common/buildcraft/builders/TileAbstractBuilder.java b/common/buildcraft/builders/TileAbstractBuilder.java index ac865b8f..11f0c579 100755 --- a/common/buildcraft/builders/TileAbstractBuilder.java +++ b/common/buildcraft/builders/TileAbstractBuilder.java @@ -60,7 +60,7 @@ public abstract class TileAbstractBuilder extends TileBuildCraft implements ITil @RPC (RPCSide.SERVER) private void uploadBuildersInAction (RPCMessageInfo info) { for (BuildingItem i : buildersInAction) { - RPCHandler.rpcPlayer(this, "launchItem", info.sender, i); + RPCHandler.rpcPlayer(info.sender, this, "launchItem", i); } } @@ -124,7 +124,7 @@ public abstract class TileAbstractBuilder extends TileBuildCraft implements ITil public void addBuildingItem(BuildingItem item) { buildersInAction.add(item); - RPCHandler.rpcBroadcastPlayers(this, "launchItem", item); + RPCHandler.rpcBroadcastPlayers(worldObj, this, "launchItem", item); } public final double energyAvailable() { diff --git a/common/buildcraft/builders/TileArchitect.java b/common/buildcraft/builders/TileArchitect.java index 11a9a4f0..b6d072de 100644 --- a/common/buildcraft/builders/TileArchitect.java +++ b/common/buildcraft/builders/TileArchitect.java @@ -146,7 +146,7 @@ public class TileArchitect extends TileBuildCraft implements IInventory, IBoxPro @RPC (RPCSide.SERVER) public void handleClientSetName(String nameSet) { name = nameSet; - RPCHandler.rpcBroadcastPlayers(this, "setName", name); + RPCHandler.rpcBroadcastPlayers(worldObj, this, "setName", name); } @RPC diff --git a/common/buildcraft/builders/TileBlueprintLibrary.java b/common/buildcraft/builders/TileBlueprintLibrary.java index 52f1b78b..edd9166b 100644 --- a/common/buildcraft/builders/TileBlueprintLibrary.java +++ b/common/buildcraft/builders/TileBlueprintLibrary.java @@ -219,15 +219,14 @@ public class TileBlueprintLibrary extends TileBuildCraft implements IInventory { BlueprintBase bpt = ItemBlueprint.loadBlueprint(getStackInSlot(1)); if (bpt != null && uploadingPlayer != null) { - RPCHandler.rpcPlayer(this, "downloadBlueprintToClient", - uploadingPlayer, bpt.id, bpt.getData()); + RPCHandler.rpcPlayer(uploadingPlayer, this, "downloadBlueprintToClient", + bpt.id, bpt.getData()); uploadingPlayer = null; } } if (progressOut == 100 && getStackInSlot(3) == null) { - RPCHandler.rpcPlayer(this, "requestSelectedBlueprint", - downloadingPlayer); + RPCHandler.rpcPlayer(downloadingPlayer, this, "requestSelectedBlueprint"); progressOut = 0; } } diff --git a/common/buildcraft/builders/TileBuilder.java b/common/buildcraft/builders/TileBuilder.java index 5dffc06d..bf274001 100644 --- a/common/buildcraft/builders/TileBuilder.java +++ b/common/buildcraft/builders/TileBuilder.java @@ -447,7 +447,7 @@ public class TileBuilder extends TileAbstractBuilder implements IMachine, IFluid if (!worldObj.isRemote) { if (i == 0) { - RPCHandler.rpcBroadcastPlayers(this, "setItemRequirements", + RPCHandler.rpcBroadcastPlayers(worldObj, this, "setItemRequirements", null, null); iterateBpt(false); } @@ -726,10 +726,10 @@ public class TileBuilder extends TileAbstractBuilder implements IMachine, IFluid stack.stackSize = 0; } - RPCHandler.rpcBroadcastPlayers(this, "setItemRequirements", + RPCHandler.rpcBroadcastPlayers(worldObj, this, "setItemRequirements", ((BptBuilderBlueprint) bluePrintBuilder).neededItems, realSize); } else { - RPCHandler.rpcBroadcastPlayers(this, "setItemRequirements", null, null); + RPCHandler.rpcBroadcastPlayers(worldObj, this, "setItemRequirements", null, null); } } diff --git a/common/buildcraft/core/network/BuildCraftChannelHandler.java b/common/buildcraft/core/network/BuildCraftChannelHandler.java index 6b661279..ec8617ac 100755 --- a/common/buildcraft/core/network/BuildCraftChannelHandler.java +++ b/common/buildcraft/core/network/BuildCraftChannelHandler.java @@ -40,6 +40,7 @@ public class BuildCraftChannelHandler extends FMLIndexedMessageToMessageCodec break; } + case PacketIds.RPC_ENTITY: { + ((PacketRPCEntity) packet).call(player); + + break; + } + case PacketIds.RPC_PIPE: { // TODO: RPC pipes are not used right now. Ressurect this // code if needed later. diff --git a/common/buildcraft/core/network/PacketIds.java b/common/buildcraft/core/network/PacketIds.java index a0fed707..dc31eeb7 100644 --- a/common/buildcraft/core/network/PacketIds.java +++ b/common/buildcraft/core/network/PacketIds.java @@ -42,6 +42,7 @@ public final class PacketIds { public static final int RPC_TILE = 110; public static final int RPC_PIPE = 111; public static final int RPC_GUI = 112; + public static final int RPC_ENTITY = 113; /** * Deactivate constructor diff --git a/common/buildcraft/core/network/PacketRPCEntity.java b/common/buildcraft/core/network/PacketRPCEntity.java new file mode 100755 index 00000000..541da12c --- /dev/null +++ b/common/buildcraft/core/network/PacketRPCEntity.java @@ -0,0 +1,61 @@ +/** + * Copyright (c) 2011-2014, SpaceToad and the BuildCraft Team + * http://www.mod-buildcraft.com + * + * BuildCraft is distributed under the terms of the Minecraft Mod Public + * License 1.0, or MMPL. Please check the contents of the license located in + * http://www.mod-buildcraft.com/MMPL-1.0.txt + */ +package buildcraft.core.network; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; + +import net.minecraft.entity.Entity; +import net.minecraft.entity.player.EntityPlayer; + +public class PacketRPCEntity extends BuildCraftPacket { + private byte[] contents; + private Entity entity; + private int entityId; + + public PacketRPCEntity() { + } + + public PacketRPCEntity(Entity iEntity, byte[] bytes) { + entity = iEntity; + contents = bytes; + } + + @Override + public int getID() { + return PacketIds.RPC_ENTITY; + } + + public void call(EntityPlayer sender) { + RPCMessageInfo info = new RPCMessageInfo(); + info.sender = sender; + + ByteBuf completeData = Unpooled.buffer(); + completeData.writeBytes(contents); + + entity = sender.worldObj.getEntityByID(entityId); + + if (entity != null) { + RPCHandler.receiveRPC(entity, info, completeData); + } + } + + @Override + public void readData(ByteBuf data) { + entityId = data.readInt(); + contents = new byte[data.readableBytes()]; + data.readBytes(contents); + } + + @Override + public void writeData(ByteBuf data) { + data.writeInt(entity.getEntityId()); + data.writeBytes(contents); + } +} diff --git a/common/buildcraft/core/network/RPCHandler.java b/common/buildcraft/core/network/RPCHandler.java index f8d5550a..46378482 100755 --- a/common/buildcraft/core/network/RPCHandler.java +++ b/common/buildcraft/core/network/RPCHandler.java @@ -20,10 +20,12 @@ import java.util.TreeMap; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; +import net.minecraft.entity.Entity; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.inventory.Container; import net.minecraft.tileentity.TileEntity; +import net.minecraft.world.World; import buildcraft.BuildCraftCore; import buildcraft.api.core.JavaTools; @@ -39,7 +41,6 @@ import buildcraft.transport.Pipe; * RPCs must be sent and received by a tile entity. */ public final class RPCHandler { - public static int MAX_PACKET_SIZE = 30 * 1024; private static Map handlers = new TreeMap(); @@ -109,13 +110,7 @@ public final class RPCHandler { handlers.put(object.getClass().getName(), new RPCHandler(object.getClass())); } - BuildCraftPacket packet = null; - - if (object instanceof Container) { - packet = handlers.get(object.getClass().getName()).createRCPPacketContainer(method, actuals); - } else if (object instanceof TileEntity) { - packet = handlers.get(object.getClass().getName()).createRCPPacketTile((TileEntity) object, method, actuals); - } + BuildCraftPacket packet = createPacket(object, method, actuals); if (packet != null) { if (packet instanceof PacketRPCTile) { @@ -128,19 +123,12 @@ public final class RPCHandler { } } - public static void rpcPlayer(Object object, String method, EntityPlayer player, Object... actuals) { + public static void rpcPlayer(EntityPlayer player, Object object, String method, Object... actuals) { if (!handlers.containsKey(object.getClass().getName())) { handlers.put(object.getClass().getName(), new RPCHandler(object.getClass())); } - BuildCraftPacket packet = null; - - if (object instanceof Container) { - packet = handlers.get(object.getClass().getName()).createRCPPacketContainer(method, actuals); - } else if (object instanceof TileEntity) { - packet = handlers.get(object.getClass().getName()) - .createRCPPacketTile((TileEntity) object, method, actuals); - } + BuildCraftPacket packet = createPacket(object, method, actuals); if (packet != null) { if (packet instanceof PacketRPCTile) { @@ -153,51 +141,39 @@ public final class RPCHandler { } } - public static void rpcBroadcastDefaultPlayers (Pipe pipe, String method, Object ... actuals) { - RPCHandler.rpcBroadcastPlayers(pipe, method, DefaultProps.NETWORK_UPDATE_RANGE, actuals); + public static void rpcBroadcastPlayers(World world, Object object, String method, Object... actuals) { + RPCHandler.rpcBroadcastPlayersAtDistance(world, object, method, DefaultProps.NETWORK_UPDATE_RANGE, actuals); } - public static void rpcBroadcastPlayers (TileEntity tile, String method, Object ... actuals) { - RPCHandler.rpcBroadcastPlayersAtDistance(tile, method, DefaultProps.NETWORK_UPDATE_RANGE, actuals); - } - - public static void rpcBroadcastPlayersAtDistance (TileEntity tile, String method, int maxDistance, Object ... actuals) { - if (!handlers.containsKey(tile.getClass().getName())) { - handlers.put (tile.getClass().getName(), new RPCHandler (tile.getClass())); + public static void rpcBroadcastPlayersAtDistance(World world, Object object, String method, int maxDistance, + Object... actuals) { + if (!handlers.containsKey(object.getClass().getName())) { + handlers.put(object.getClass().getName(), new RPCHandler(object.getClass())); } - PacketRPCTile packet = handlers.get (tile.getClass().getName()).createRCPPacketTile(tile, method, actuals); + BuildCraftPacket packet = createPacket(object, method, actuals); if (packet != null) { - for (PacketRPCTile p : packet - .breakIntoSmallerPackets(MAX_PACKET_SIZE)) { - for (Object o : tile.getWorldObj().playerEntities) { - EntityPlayerMP player = (EntityPlayerMP) o; + if (packet instanceof PacketRPCTile) { + TileEntity tile = (TileEntity) object; - if (Math.abs(player.posX - tile.xCoord) <= maxDistance - && Math.abs(player.posY - tile.yCoord) <= maxDistance - && Math.abs(player.posZ - tile.zCoord) <= maxDistance) { - BuildCraftCore.instance.sendToPlayer(player, p); + for (PacketRPCTile p : ((PacketRPCTile) packet) + .breakIntoSmallerPackets(MAX_PACKET_SIZE)) { + + for (Object o : world.playerEntities) { + EntityPlayerMP player = (EntityPlayerMP) o; + + if (Math.abs(player.posX - tile.xCoord) <= maxDistance + && Math.abs(player.posY - tile.yCoord) <= maxDistance + && Math.abs(player.posZ - tile.zCoord) <= maxDistance) { + BuildCraftCore.instance.sendToPlayer(player, p); + } } } - } - } - } + } else { + for (Object o : world.playerEntities) { + EntityPlayerMP player = (EntityPlayerMP) o; - public static void rpcBroadcastPlayers (Pipe pipe, String method, int maxDistance, Object ... actuals) { - if (!handlers.containsKey(pipe.getClass().getName())) { - handlers.put (pipe.getClass().getName(), new RPCHandler (pipe.getClass())); - } - - PacketRPCPipe packet = handlers.get (pipe.getClass().getName()).createRCPPacketPipe(pipe, method, actuals); - - if (packet != null) { - for (Object o : pipe.container.getWorld().playerEntities) { - EntityPlayerMP player = (EntityPlayerMP) o; - - if (Math.abs(player.posX - pipe.container.xCoord) <= maxDistance - && Math.abs(player.posY - pipe.container.yCoord) <= maxDistance - && Math.abs(player.posZ - pipe.container.zCoord) <= maxDistance) { BuildCraftCore.instance.sendToPlayer(player, packet); } } @@ -244,7 +220,22 @@ public final class RPCHandler { return new PacketRPCPipe(bytes); } - private PacketRPCTile createRCPPacketTile (TileEntity tile, String method, Object ... actuals) { + private static BuildCraftPacket createPacket(Object object, String method, Object... actuals) { + BuildCraftPacket packet = null; + + if (object instanceof Container) { + packet = handlers.get(object.getClass().getName()).createRCPPacketContainer(method, actuals); + } else if (object instanceof TileEntity) { + packet = handlers.get(object.getClass().getName()) + .createRCPPacketTile((TileEntity) object, method, actuals); + } else if (object instanceof Entity) { + packet = handlers.get(object.getClass().getName()).createRCPPacketEntity((Entity) object, method, actuals); + } + + return packet; + } + + private byte[] getBytes(String method, Object... actuals) { ByteBuf data = Unpooled.buffer(); try { @@ -260,26 +251,19 @@ public final class RPCHandler { byte [] bytes = new byte [data.readableBytes()]; data.readBytes(bytes); - return new PacketRPCTile(tile, bytes); + return bytes; + } + + private PacketRPCTile createRCPPacketTile(TileEntity tile, String method, Object... actuals) { + return new PacketRPCTile(tile, getBytes(method, actuals)); } private PacketRPCGui createRCPPacketContainer(String method, Object... actuals) { - ByteBuf data = Unpooled.buffer(); + return new PacketRPCGui(getBytes(method, actuals)); + } - try { - writeParameters(method, data, actuals); - } catch (IOException e) { - e.printStackTrace(); - } catch (IllegalArgumentException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } - - byte[] bytes = new byte[data.readableBytes()]; - data.readBytes(bytes); - - return new PacketRPCGui(bytes); + private PacketRPCEntity createRCPPacketEntity(Entity entity, String method, Object... actuals) { + return new PacketRPCEntity(entity, getBytes(method, actuals)); } private void writeParameters(String method, ByteBuf data, Object... actuals) diff --git a/common/buildcraft/core/render/RenderRobot.java b/common/buildcraft/core/render/RenderRobot.java index 7233aba2..b5cf3eff 100644 --- a/common/buildcraft/core/render/RenderRobot.java +++ b/common/buildcraft/core/render/RenderRobot.java @@ -61,10 +61,12 @@ public class RenderRobot extends Render implements IItemRenderer { box.render(factor); - // GL11.glTranslated(0.5, 0, 0); - GL11.glRotatef(robot.worldObj.getTotalWorldTime() % 45 + 90, 1, 0, 0); - doRenderItemAtHand(robot, new ItemStack(Items.diamond_axe)); - // GL11.glTranslated(-0.5, 0, 0); + if (robot.itemInUse != null) { + GL11.glPushMatrix(); + GL11.glRotatef(robot.worldObj.getTotalWorldTime() % 45 + 90, 1, 0, 0); + doRenderItemAtHand(robot, robot.itemInUse); + GL11.glPopMatrix(); + } if (robot.laser.isVisible) { robot.laser.head.x = robot.posX; diff --git a/common/buildcraft/core/robots/EntityRobot.java b/common/buildcraft/core/robots/EntityRobot.java index 02ca788a..4bc0f4d2 100755 --- a/common/buildcraft/core/robots/EntityRobot.java +++ b/common/buildcraft/core/robots/EntityRobot.java @@ -29,6 +29,10 @@ import buildcraft.api.boards.RedstoneBoardRobotNBT; import buildcraft.api.core.SafeTimeTracker; import buildcraft.core.DefaultProps; import buildcraft.core.LaserData; +import buildcraft.core.network.RPC; +import buildcraft.core.network.RPCHandler; +import buildcraft.core.network.RPCMessageInfo; +import buildcraft.core.network.RPCSide; import buildcraft.transport.TileGenericPipe; public class EntityRobot extends EntityLiving implements @@ -55,6 +59,8 @@ public class EntityRobot extends EntityLiving implements public IRedstoneBoardRobot board; public RobotAIBase currentAI; + public ItemStack itemInUse; + protected RobotAIBase nextAI; private boolean needsUpdate = false; @@ -72,6 +78,10 @@ public class EntityRobot extends EntityLiving implements board = iBoard; dataWatcher.updateObject(16, board.getNBTHandler().getID()); + + if (world.isRemote) { + + } } public EntityRobot(World par1World) { @@ -445,4 +455,19 @@ public class EntityRobot extends EntityLiving implements public boolean isMoving() { return motionX != 0 || motionY != 0 || motionZ != 0; } + + public void setItemInUse(ItemStack stack) { + itemInUse = stack; + RPCHandler.rpcBroadcastPlayers(worldObj, this, "clientSetItemInUse", stack); + } + + @RPC(RPCSide.CLIENT) + private void clientSetItemInUse(ItemStack stack) { + itemInUse = stack; + } + + @RPC(RPCSide.SERVER) + public void requestInitialization(RPCMessageInfo info) { + RPCHandler.rpcPlayer(info.sender, this, "clientSetItemInUse", itemInUse); + } } diff --git a/common/buildcraft/core/robots/boards/BoardRobotLumberjack.java b/common/buildcraft/core/robots/boards/BoardRobotLumberjack.java index b0cd12fb..283143c1 100755 --- a/common/buildcraft/core/robots/boards/BoardRobotLumberjack.java +++ b/common/buildcraft/core/robots/boards/BoardRobotLumberjack.java @@ -9,6 +9,7 @@ import net.minecraft.init.Items; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.world.IBlockAccess; +import net.minecraft.world.WorldServer; import net.minecraftforge.common.ForgeHooks; @@ -19,6 +20,7 @@ import buildcraft.api.boards.RedstoneBoardRobotNBT; import buildcraft.core.BlockIndex; import buildcraft.core.robots.EntityRobot; import buildcraft.core.robots.RobotAIMoveTo; +import buildcraft.core.utils.BlockUtil; import buildcraft.core.utils.IPathFound; import buildcraft.core.utils.PathFinding; @@ -51,6 +53,7 @@ public class BoardRobotLumberjack implements IRedstoneBoardRobot { if (!initialized) { range = data.getInteger("range"); + robot.setItemInUse(new ItemStack(Items.wooden_axe)); initialized = true; } @@ -82,14 +85,14 @@ public class BoardRobotLumberjack implements IRedstoneBoardRobot { Block block = robot.worldObj.getBlock(woodToChop.x, woodToChop.y, woodToChop.z); int meta = robot.worldObj.getBlockMetadata(woodToChop.x, woodToChop.y, woodToChop.z); float hardness = block.getBlockHardness(robot.worldObj, woodToChop.x, woodToChop.y, woodToChop.z); - float speed = getBreakSpeed(robot, new ItemStack(Items.wooden_axe), block, meta); + float speed = getBreakSpeed(robot, robot.itemInUse, block, meta); blockDamage += speed / hardness / 30F; - if (blockDamage > 1.0F) { robot.worldObj.destroyBlockInWorldPartially(robot.getEntityId(), woodToChop.x, woodToChop.y, woodToChop.z, -1); blockDamage = 0; + BlockUtil.breakBlock((WorldServer) robot.worldObj, woodToChop.x, woodToChop.y, woodToChop.z, 6000); robot.worldObj.setBlockToAir(woodToChop.x, woodToChop.y, woodToChop.z); stage = Stages.LOOK_FOR_WOOD; woodToChop = null; diff --git a/common/buildcraft/energy/TileEnergyEmitter.java b/common/buildcraft/energy/TileEnergyEmitter.java index 493fa7ac..4dfc5645 100755 --- a/common/buildcraft/energy/TileEnergyEmitter.java +++ b/common/buildcraft/energy/TileEnergyEmitter.java @@ -94,7 +94,7 @@ public class TileEnergyEmitter extends TileBuildCraft { addLaser(receiver.xCoord, receiver.yCoord, receiver.zCoord); - RPCHandler.rpcBroadcastPlayers(this, "addLaser", + RPCHandler.rpcBroadcastPlayers(worldObj, this, "addLaser", receiver.xCoord, receiver.yCoord, receiver.zCoord); @@ -111,7 +111,7 @@ public class TileEnergyEmitter extends TileBuildCraft { accumulated++; if (syncMJ.markTimeIfDelay(worldObj)) { - RPCHandler.rpcBroadcastPlayers(this, "synchronizeMJ", mjAcc + RPCHandler.rpcBroadcastPlayers(worldObj, this, "synchronizeMJ", mjAcc / accumulated); mjAcc = 0; accumulated = 0; @@ -121,7 +121,7 @@ public class TileEnergyEmitter extends TileBuildCraft { for (Target t : targets.values()) { if (t.data.isVisible) { t.data.isVisible = false; - RPCHandler.rpcBroadcastPlayers(this, "disableLaser", + RPCHandler.rpcBroadcastPlayers(worldObj, this, "disableLaser", t.receiver.xCoord, t.receiver.yCoord, t.receiver.zCoord); } @@ -139,7 +139,7 @@ public class TileEnergyEmitter extends TileBuildCraft { for (Target t : targets.values()) { if (!t.data.isVisible) { t.data.isVisible = true; - RPCHandler.rpcBroadcastPlayers(this, "enableLaser", + RPCHandler.rpcBroadcastPlayers(worldObj, this, "enableLaser", t.receiver.xCoord, t.receiver.yCoord, t.receiver.zCoord); } @@ -196,7 +196,7 @@ public class TileEnergyEmitter extends TileBuildCraft { @RPC (RPCSide.SERVER) public void requestLasers (RPCMessageInfo info) { for (BlockIndex b : targets.keySet()) { - RPCHandler.rpcPlayer(this, "addLaser", info.sender, b.x, b.y, b.z); + RPCHandler.rpcPlayer(info.sender, this, "addLaser", b.x, b.y, b.z); } } diff --git a/tests/buildcraft/tests/testcase/TileTestCase.java b/tests/buildcraft/tests/testcase/TileTestCase.java index 56209472..b0037ff7 100755 --- a/tests/buildcraft/tests/testcase/TileTestCase.java +++ b/tests/buildcraft/tests/testcase/TileTestCase.java @@ -87,7 +87,7 @@ public class TileTestCase extends TileEntity { information = "test clear"; } - RPCHandler.rpcBroadcastPlayers(this, "setInformation", information); + RPCHandler.rpcBroadcastPlayers(worldObj, this, "setInformation", information); } @RPC(RPCSide.CLIENT) @@ -154,7 +154,7 @@ public class TileTestCase extends TileEntity { @RPC(RPCSide.SERVER) private void setName(String name) { testName = name; - RPCHandler.rpcBroadcastPlayers(this, "setNameClient", name); + RPCHandler.rpcBroadcastPlayers(worldObj, this, "setNameClient", name); } @RPC(RPCSide.CLIENT)