More work on lumberjacks.

RPC now support entity messages.
Robots now support item synchronization between client and server.
Lumberjack now use a variable tool.
for #1869
This commit is contained in:
SpaceToad 2014-06-08 11:58:55 +02:00
parent 10fd47b0b9
commit efcabd31a1
14 changed files with 174 additions and 92 deletions

View file

@ -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() {

View file

@ -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

View file

@ -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;
}
}

View file

@ -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);
}
}

View file

@ -40,6 +40,7 @@ public class BuildCraftChannelHandler extends FMLIndexedMessageToMessageCodec<Bu
addDiscriminator(14, PacketRPCTile.class);
addDiscriminator(15, PacketRPCPipe.class);
addDiscriminator(16, PacketRPCGui.class);
addDiscriminator(17, PacketRPCEntity.class);
}
@Override

View file

@ -92,6 +92,12 @@ public class PacketHandler extends SimpleChannelInboundHandler<BuildCraftPacket>
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.

View file

@ -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

View file

@ -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);
}
}

View file

@ -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<String, RPCHandler> handlers = new TreeMap<String, RPCHandler>();
@ -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,25 +141,26 @@ 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(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()));
}
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()));
}
PacketRPCTile packet = handlers.get (tile.getClass().getName()).createRCPPacketTile(tile, method, actuals);
BuildCraftPacket packet = createPacket(object, method, actuals);
if (packet != null) {
for (PacketRPCTile p : packet
if (packet instanceof PacketRPCTile) {
TileEntity tile = (TileEntity) object;
for (PacketRPCTile p : ((PacketRPCTile) packet)
.breakIntoSmallerPackets(MAX_PACKET_SIZE)) {
for (Object o : tile.getWorldObj().playerEntities) {
for (Object o : world.playerEntities) {
EntityPlayerMP player = (EntityPlayerMP) o;
if (Math.abs(player.posX - tile.xCoord) <= maxDistance
@ -181,23 +170,10 @@ public final class RPCHandler {
}
}
}
}
}
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) {
} else {
for (Object o : world.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();
try {
writeParameters(method, data, actuals);
} catch (IOException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
return new PacketRPCGui(getBytes(method, actuals));
}
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)

View file

@ -61,10 +61,12 @@ public class RenderRobot extends Render implements IItemRenderer {
box.render(factor);
// GL11.glTranslated(0.5, 0, 0);
if (robot.itemInUse != null) {
GL11.glPushMatrix();
GL11.glRotatef(robot.worldObj.getTotalWorldTime() % 45 + 90, 1, 0, 0);
doRenderItemAtHand(robot, new ItemStack(Items.diamond_axe));
// GL11.glTranslated(-0.5, 0, 0);
doRenderItemAtHand(robot, robot.itemInUse);
GL11.glPopMatrix();
}
if (robot.laser.isVisible) {
robot.laser.head.x = robot.posX;

View file

@ -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);
}
}

View file

@ -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<EntityRobot> {
if (!initialized) {
range = data.getInteger("range");
robot.setItemInUse(new ItemStack(Items.wooden_axe));
initialized = true;
}
@ -82,14 +85,14 @@ public class BoardRobotLumberjack implements IRedstoneBoardRobot<EntityRobot> {
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;

View file

@ -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);
}
}

View file

@ -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)