worked on packet handling and power

changed the packet handler to use a method allowing for super classes to
work with the packet. This is without losing the packet ID or needing to
reread the data from the packet again.

As well more improvement to how the tile understands how its powered
from the network. Added ways to detect if the tile was remove, no longer
connected, and a delay in case the power source missed a power cycle.
More improvement are to come especial with how much power is used by the
entire network.
This commit is contained in:
DarkGuardsman 2013-06-30 21:48:59 -04:00
parent 12ea85d5b9
commit 7de88e4d70
4 changed files with 190 additions and 146 deletions

View file

@ -41,7 +41,6 @@ import assemblyline.common.armbot.command.CommandRotateTo;
import assemblyline.common.armbot.command.CommandUse;
import assemblyline.common.machine.TileEntityAssembly;
import assemblyline.common.machine.encoder.ItemDisk;
import buildcraft.api.power.PowerProvider;
import com.google.common.io.ByteArrayDataInput;
@ -57,18 +56,13 @@ public class TileEntityArmbot extends TileEntityAssembly implements IMultiBlock,
private final CommandManager commandManager = new CommandManager();
private static final int PACKET_COMMANDS = 128;
/**
* The items this container contains.
*/
/** The items this container contains. */
protected ItemStack disk = null;
public final double WATT_REQUEST = 20;
public double wattsReceived = 0;
private int playerUsing = 0;
private int computersAttached = 0;
private List<IComputerAccess> connectedComputers = new ArrayList<IComputerAccess>();
/**
* The rotation of the arms. In Degrees.
*/
/** The rotation of the arms. In Degrees. */
public float rotationPitch = 0;
public float rotationYaw = 0;
public float renderPitch = 0;
@ -80,22 +74,18 @@ public class TileEntityArmbot extends TileEntityAssembly implements IMultiBlock,
public boolean isProvidingPower = false;
/**
* An entity that the Armbot is grabbed onto. Entity Items are held separately.
*/
/** An entity that the Armbot is grabbed onto. Entity Items are held separately. */
private final List<Entity> grabbedEntities = new ArrayList<Entity>();
private final List<ItemStack> grabbedItems = new ArrayList<ItemStack>();
/**
* Client Side Object Storage
*/
/** Client Side Object Storage */
public EntityItem renderEntityItem = null;
@Override
public void initiate()
{
super.initiate();
if(!this.commandManager.hasTasks())
if (!this.commandManager.hasTasks())
{
this.onInventoryChanged();
}
@ -281,9 +271,7 @@ public class TileEntityArmbot extends TileEntityAssembly implements IMultiBlock,
return null;
}
/**
* @return The current hand position of the armbot.
*/
/** @return The current hand position of the armbot. */
public Vector3 getHandPosition()
{
Vector3 position = new Vector3(this);
@ -307,45 +295,16 @@ public class TileEntityArmbot extends TileEntityAssembly implements IMultiBlock,
return delta;
}
/** Data */
@Override
public Packet getDescriptionPacket()
{
NBTTagCompound nbt = new NBTTagCompound();
writeToNBT(nbt);
return PacketManager.getPacket(AssemblyLine.CHANNEL, this, nbt);
this.writeToNBT(nbt);
return PacketManager.getPacket(AssemblyLine.CHANNEL, this, AssemblyTilePacket.NBT, nbt);
}
/**
* Data
*/
@Override
public void handlePacketData(INetworkManager network, int packetType, Packet250CustomPayload packet, EntityPlayer player, ByteArrayDataInput dataStream)
{
if (this.worldObj.isRemote)
{
try
{
ByteArrayInputStream bis = new ByteArrayInputStream(packet.data);
DataInputStream dis = new DataInputStream(bis);
int id, x, y, z;
id = dis.readInt();
x = dis.readInt();
y = dis.readInt();
z = dis.readInt();
NBTTagCompound tag = Packet.readNBTTagCompound(dis);
this.readFromNBT(tag);
}
catch (IOException e)
{
FMLLog.severe("Failed to receive packet for Armbot");
e.printStackTrace();
}
}
}
/**
* Inventory
*/
/** Inventory */
@Override
public int getSizeInventory()
{
@ -358,9 +317,7 @@ public class TileEntityArmbot extends TileEntityAssembly implements IMultiBlock,
return TranslationHelper.getLocal("tile.armbot.name");
}
/**
* Inventory functions.
*/
/** Inventory functions. */
@Override
public ItemStack getStackInSlot(int par1)
{
@ -449,9 +406,7 @@ public class TileEntityArmbot extends TileEntityAssembly implements IMultiBlock,
return this.displayText;
}
/**
* NBT Data
*/
/** NBT Data */
@Override
public void readFromNBT(NBTTagCompound nbt)
{
@ -506,9 +461,7 @@ public class TileEntityArmbot extends TileEntityAssembly implements IMultiBlock,
}
}
/**
* Writes a tile entity to NBT.
*/
/** Writes a tile entity to NBT. */
@Override
public void writeToNBT(NBTTagCompound nbt)
{
@ -941,17 +894,13 @@ public class TileEntityArmbot extends TileEntityAssembly implements IMultiBlock,
this.grabbedItems.clear();
}
/**
* called by the block when another checks it too see if it is providing power to a direction
*/
/** called by the block when another checks it too see if it is providing power to a direction */
public boolean isProvidingPowerSide(ForgeDirection dir)
{
return this.isProvidingPower && dir.getOpposite() == this.getFacingDirectionFromAngle();
}
/**
* gets the facing direction using the yaw angle
*/
/** gets the facing direction using the yaw angle */
public ForgeDirection getFacingDirectionFromAngle()
{
float angle = MathHelper.wrapAngleTo180_float(this.rotationYaw);

View file

@ -40,7 +40,7 @@ public class BlockALMachine extends BlockAdvanced
output += "Channel:" + (asm.getTileNetwork() != null ? asm.getTileNetwork().toString() : "Error") + "|";
entityPlayer.sendChatToPlayer(output);
output = "Debug>>>";
output += "Powered:"+asm.powered + " By:"+(asm.powerSource != null ? asm.powerSource.toString() : "Error");
output += "Powered:"+asm.powered + " By:"+(asm.powerSource != null ? asm.powerSource.toString() : "null");
entityPlayer.sendChatToPlayer(output);
}
return super.onBlockActivated(world, x, y, z, entityPlayer, side, hitX, hitY, hitZ);

View file

@ -1,12 +1,24 @@
package assemblyline.common.machine;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.util.Random;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.network.INetworkManager;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.Packet250CustomPayload;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.ForgeDirection;
import universalelectricity.core.electricity.ElectricityPack;
import universalelectricity.core.vector.Vector3;
import universalelectricity.prefab.network.IPacketReceiver;
import universalelectricity.prefab.network.PacketManager;
import assemblyline.common.AssemblyLine;
import com.google.common.io.ByteArrayDataInput;
import dark.core.api.INetworkPart;
import dark.core.tile.network.NetworkTileEntities;
import dark.library.machine.TileEntityRunnableMachine;
@ -15,10 +27,11 @@ import dark.library.machine.TileEntityRunnableMachine;
* able to be powered through the powering of only one machine.
*
* @author Calclavia */
public abstract class TileEntityAssembly extends TileEntityRunnableMachine implements INetworkPart
public abstract class TileEntityAssembly extends TileEntityRunnableMachine implements INetworkPart, IPacketReceiver
{
/** Is this tile being powered by a non-network connection */
public boolean powered = false;
public boolean running = false;
/** Network used to link assembly machines together */
private NetworkAssembly assemblyNetwork;
/** Tiles that are connected to this */
@ -29,18 +42,33 @@ public abstract class TileEntityAssembly extends TileEntityRunnableMachine imple
public Random random = new Random();
/** Number of ticks this can go without power */
private int powerTicks = 0;
/** percent tick rate this tile will update at */
private int updateTick = 1;
/** ticks sync this tile has gotten power */
private int lastPoweredTicks = 0;
public static enum AssemblyTilePacket
{
POWER(),
NBT();
}
@Override
public void invalidate()
{
NetworkAssembly.invalidate(this);
super.invalidate();
}
@Override
public void updateEntity()
{
super.updateEntity();
boolean prevRun = this.running;
if (ticks % updateTick == 0)
{
this.updateTick = ((int) random.nextInt(10) + 20);
this.updateNetworkConnections();
this.powerSource = null;
}
if (this.wattsReceived >= this.getRequest().getWatts())
{
@ -63,6 +91,15 @@ public abstract class TileEntityAssembly extends TileEntityRunnableMachine imple
NetworkAssembly net = ((NetworkAssembly) this.getTileNetwork());
net.markAsPowerSource(this, this.powered);
}
if (!this.worldObj.isRemote)
{
this.running = this.isRunning();
if (running != prevRun)
{
Packet packet = PacketManager.getPacket(AssemblyLine.CHANNEL, this, AssemblyTilePacket.POWER.ordinal(), this.running);
PacketManager.sendPacketToClients(packet, worldObj, new Vector3(this), 64);
}
}
this.onUpdate();
}
@ -73,24 +110,47 @@ public abstract class TileEntityAssembly extends TileEntityRunnableMachine imple
/** Checks to see if this assembly tile can run using several methods */
public boolean isRunning()
{
boolean running = AssemblyLine.REQUIRE_NO_POWER || this.powered;
if (!running && this.powerSource != null)
if (!worldObj.isRemote)
{
running = this.powerSource.powered;
boolean running = AssemblyLine.REQUIRE_NO_POWER || this.powered;
if (!running && this.powerSource != null && this.getTileNetwork() instanceof NetworkAssembly)
{
NetworkAssembly net = ((NetworkAssembly) this.getTileNetwork());
running = net.getNetworkMemebers().contains(this.powerSource) && this.powerSource.powered;
if (!running)
{
this.lastPoweredTicks++;
if (this.lastPoweredTicks >= 20 && !net.getNetworkMemebers().contains(this.powerSource))
{
this.powerSource = null;
}
}
}
if (!running && this.getTileNetwork() instanceof NetworkAssembly)
{
NetworkAssembly net = ((NetworkAssembly) this.getTileNetwork());
this.powerSource = net.canRun(this);
running = this.powerSource != null && this.powerSource.powered;
}
return running;
}
if (!running && this.getTileNetwork() instanceof NetworkAssembly)
else
{
NetworkAssembly net = ((NetworkAssembly) this.getTileNetwork());
this.powerSource = net.canRun(this);
running = this.powerSource != null && this.powerSource.powered;
return this.running;
}
return running;
}
@Override
public ElectricityPack getRequest()
{
return new ElectricityPack(1, this.getVoltage());
int voltage = 120;
double amps = .1D;
if (this.getTileNetwork() instanceof NetworkAssembly)
{
}
return new ElectricityPack(amps, voltage);
}
@Override
@ -158,4 +218,62 @@ public abstract class TileEntityAssembly extends TileEntityRunnableMachine imple
{
return "AssemblyTile>>>At>>>" + (new Vector3(this).toString());
}
@Override
public void handlePacketData(INetworkManager network, int packetType, Packet250CustomPayload packet, EntityPlayer player, ByteArrayDataInput dataStream)
{
boolean packetSize = true;
try
{
ByteArrayInputStream bis = new ByteArrayInputStream(packet.data);
DataInputStream dis = new DataInputStream(bis);
int id, x, y, z;
id = dis.readInt();
x = dis.readInt();
y = dis.readInt();
z = dis.readInt();
int pId = dis.readInt();
this.handlePacket(pId, dis, player);
if (packetSize)
{
System.out.println("TileEntityAssembly>" + new Vector3(this) + ">>>Debug>>Packet" + pId + ">>Size>>bytes>>" + packet.data.length);
}
}
catch (Exception e)
{
System.out.println("Errror Reading Packet for a TileEntityAssembly instance");
e.printStackTrace();
}
}
/** Handles reduced data from the main packet method
*
* @param id - packet ID
* @param dis - data
* @param player - player
* @return true if the packet was used */
public boolean handlePacket(int id, DataInputStream dis, EntityPlayer player)
{
try
{
if (this.worldObj.isRemote)
{
if (id == AssemblyTilePacket.POWER.ordinal())
{
this.running = dis.readBoolean();
return true;
}
if (id == AssemblyTilePacket.NBT.ordinal())
{
this.readFromNBT(Packet.readNBTTagCompound(dis));
}
}
}
catch (IOException e)
{
e.printStackTrace();
}
return false;
}
}

View file

@ -1,6 +1,8 @@
package assemblyline.common.machine.belt;
import java.io.DataInputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import net.minecraft.entity.Entity;
@ -30,56 +32,46 @@ public class TileEntityConveyorBelt extends TileEntityAssembly implements IPacke
{
public enum SlantType
{
NONE, UP, DOWN, TOP
NONE,
UP,
DOWN,
TOP
}
public static final int MAX_FRAME = 13;
public static final int MAX_SLANT_FRAME = 23;
/**
* Joules required to run this thing.
*/
/** Joules required to run this thing. */
public final float acceleration = 0.01f;
public final float maxSpeed = 0.1f;
/** Current rotation of the model wheels */
public float wheelRotation = 0;
private int animFrame = 0; // this is from 0 to 15
private SlantType slantType = SlantType.NONE;
/** Entities that are ignored allowing for other tiles to interact with them */
public List<Entity> IgnoreList = new ArrayList<Entity>();
@Override
public void onUpdate()
{
if (FMLCommonHandler.instance().getEffectiveSide() == Side.SERVER && this.ticks % 10 == 0)
{
PacketManager.sendPacketToClients(this.getDescriptionPacket());
}
/* PROCESSES IGNORE LIST AND REMOVES UNNEED ENTRIES */
List<Entity> newList = new ArrayList<Entity>();
for (Entity ent : IgnoreList)
Iterator<Entity> it = this.IgnoreList.iterator();
while (it.hasNext())
{
if (this.getAffectedEntities().contains(ent))
if (!this.getAffectedEntities().contains(it.next()))
{
newList.add(ent);
it.remove();
}
}
this.IgnoreList = newList;
if (this.isRunning() && !this.worldObj.isBlockIndirectlyGettingPowered(this.xCoord, this.yCoord, this.zCoord))
if (this.worldObj.isRemote && this.isRunning() && !this.worldObj.isBlockIndirectlyGettingPowered(this.xCoord, this.yCoord, this.zCoord))
{
if (this.ticks % 10 == 0 && this.worldObj.isRemote && this.worldObj.getBlockId(this.xCoord - 1, this.yCoord, this.zCoord) != AssemblyLine.blockConveyorBelt.blockID && this.worldObj.getBlockId(xCoord, yCoord, zCoord - 1) != AssemblyLine.blockConveyorBelt.blockID)
{
this.worldObj.playSound(this.xCoord, this.yCoord, this.zCoord, "mods.assemblyline.conveyor", 0.5f, 0.7f, true);
}
this.wheelRotation += 40;
if (this.wheelRotation > 360)
this.wheelRotation = 0;
this.wheelRotation = (40 + this.wheelRotation) % 360;
float wheelRotPct = wheelRotation / 360f;
@ -107,7 +99,7 @@ public class TileEntityConveyorBelt extends TileEntityAssembly implements IPacke
@Override
public Packet getDescriptionPacket()
{
return PacketManager.getPacket(AssemblyLine.CHANNEL, this, this.wattsReceived, this.slantType.ordinal());
return PacketManager.getPacket(AssemblyLine.CHANNEL, this, 3, this.slantType.ordinal());
}
public SlantType getSlant()
@ -121,15 +113,11 @@ public class TileEntityConveyorBelt extends TileEntityAssembly implements IPacke
{
slantType = SlantType.NONE;
}
this.slantType = slantType;
PacketManager.sendPacketToClients(this.getDescriptionPacket(), this.worldObj);
this.worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
}
/**
* Is this belt in the front of a conveyor line? Used for rendering.
*/
/** Is this belt in the front of a conveyor line? Used for rendering. */
public boolean getIsFirstBelt()
{
@ -146,9 +134,7 @@ public class TileEntityConveyorBelt extends TileEntityAssembly implements IPacke
return false;
}
/**
* Is this belt in the middile of two belts? Used for rendering.
*/
/** Is this belt in the middile of two belts? Used for rendering. */
public boolean getIsMiddleBelt()
{
@ -166,9 +152,7 @@ public class TileEntityConveyorBelt extends TileEntityAssembly implements IPacke
return false;
}
/**
* Is this belt in the back of a conveyor line? Used for rendering.
*/
/** Is this belt in the back of a conveyor line? Used for rendering. */
public boolean getIsLastBelt()
{
@ -187,20 +171,24 @@ public class TileEntityConveyorBelt extends TileEntityAssembly implements IPacke
}
@Override
public void handlePacketData(INetworkManager network, int packetType, Packet250CustomPayload packet, EntityPlayer player, ByteArrayDataInput dataStream)
public boolean handlePacket(int id, DataInputStream dis, EntityPlayer player)
{
if (this.worldObj.isRemote)
if (!super.handlePacket(id, dis, player) && this.worldObj.isRemote)
{
try
{
this.wattsReceived = dataStream.readDouble();
this.slantType = SlantType.values()[dataStream.readInt()];
if (id == 3)
{
this.slantType = SlantType.values()[dis.readInt()];
return true;
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
return false;
}
@Override
@ -234,29 +222,32 @@ public class TileEntityConveyorBelt extends TileEntityAssembly implements IPacke
public int getAnimationFrame()
{
if (!this.worldObj.isBlockIndirectlyGettingPowered(this.xCoord, this.yCoord, this.zCoord))
if (!this.worldObj.isBlockIndirectlyGettingPowered(this.xCoord, this.yCoord, this.zCoord) && this.isRunning())
{
TileEntity te = null;
te = this.worldObj.getBlockTileEntity(this.xCoord - 1, this.yCoord, this.zCoord);
TileEntity tileEntity = this.worldObj.getBlockTileEntity(this.xCoord - 1, this.yCoord, this.zCoord);
if (te != null)
if (tileEntity != null)
{
if (te instanceof TileEntityConveyorBelt)
if (tileEntity instanceof TileEntityConveyorBelt)
{
if (((TileEntityConveyorBelt) te).getSlant() == this.slantType)
return ((TileEntityConveyorBelt) te).getAnimationFrame();
if (((TileEntityConveyorBelt) tileEntity).getSlant() == this.slantType)
{
return ((TileEntityConveyorBelt) tileEntity).getAnimationFrame();
}
}
}
te = this.worldObj.getBlockTileEntity(this.xCoord, this.yCoord, this.zCoord - 1);
tileEntity = this.worldObj.getBlockTileEntity(this.xCoord, this.yCoord, this.zCoord - 1);
if (te != null)
if (tileEntity != null)
{
if (te instanceof TileEntityConveyorBelt)
if (tileEntity instanceof TileEntityConveyorBelt)
{
if (((TileEntityConveyorBelt) te).getSlant() == this.slantType)
return ((TileEntityConveyorBelt) te).getAnimationFrame();
if (((TileEntityConveyorBelt) tileEntity).getSlant() == this.slantType)
{
return ((TileEntityConveyorBelt) tileEntity).getAnimationFrame();
}
}
}
@ -265,34 +256,20 @@ public class TileEntityConveyorBelt extends TileEntityAssembly implements IPacke
return this.animFrame;
}
/**
* NBT Data
*/
/** NBT Data */
@Override
public void readFromNBT(NBTTagCompound nbt)
{
super.readFromNBT(nbt);
this.slantType = SlantType.values()[nbt.getByte("slant")];
if (worldObj != null)
{
worldObj.setBlockMetadataWithNotify(this.xCoord, this.yCoord, this.zCoord, nbt.getInteger("rotation"), 3);
}
}
/**
* Writes a tile entity to NBT.
*/
/** Writes a tile entity to NBT. */
@Override
public void writeToNBT(NBTTagCompound nbt)
{
super.writeToNBT(nbt);
nbt.setByte("slant", (byte) this.slantType.ordinal());
if (worldObj != null)
{
nbt.setInteger("rotation", worldObj.getBlockMetadata(this.xCoord, this.yCoord, this.zCoord));
}
}
@Override