Reduced packet load of machines

Should help out a bit with over loaded connections.
This commit is contained in:
DarkGuardsman 2013-09-27 18:01:21 -04:00
parent 4659fcf404
commit e5e212dec9
7 changed files with 202 additions and 58 deletions

View file

@ -1,5 +1,7 @@
package dark.assembly.common.armbot;
import java.io.DataInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@ -11,6 +13,7 @@ import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.network.packet.Packet;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.MathHelper;
@ -40,6 +43,7 @@ import dark.core.common.DarkMain;
import dark.core.network.PacketHandler;
import dark.core.prefab.IMultiBlock;
import dark.core.prefab.helpers.ItemWorldHelper;
import dark.core.prefab.machine.BlockMulti;
public class TileEntityArmbot extends TileEntityAssembly implements IMultiBlock, IPacketReceiver, IArmbot, IPeripheral
{
@ -69,7 +73,6 @@ public class TileEntityArmbot extends TileEntityAssembly implements IMultiBlock,
public TileEntityArmbot()
{
super(.02f);
// TODO Auto-generated constructor stub
}
@Override
@ -83,8 +86,10 @@ public class TileEntityArmbot extends TileEntityAssembly implements IMultiBlock,
}
@Override
public void onUpdate()
//TODO separate out functions of this method to make it easier to read and work with
public void updateEntity()
{
super.updateEntity();
Vector3 handPosition = this.getHandPosition();
for (Entity entity : this.grabbedEntities)
@ -169,25 +174,37 @@ public class TileEntityArmbot extends TileEntityAssembly implements IMultiBlock,
if (this.renderYaw > this.rotationYaw)
{
if (Math.abs(this.renderYaw - this.rotationYaw) >= 180)
{
speedYaw = this.ROTATION_SPEED;
}
else
{
speedYaw = -this.ROTATION_SPEED;
}
}
else
{
if (Math.abs(this.renderYaw - this.rotationYaw) >= 180)
{
speedYaw = -this.ROTATION_SPEED;
}
else
{
speedYaw = this.ROTATION_SPEED;
}
}
this.renderYaw += speedYaw;
// keep it within 0 - 360 degrees so ROTATE commands work properly
while (this.renderYaw < 0)
{
this.renderYaw += 360;
}
while (this.renderYaw > 360)
{
this.renderYaw -= 360;
}
if (this.ticks % 5 == 0 && FMLCommonHandler.instance().getEffectiveSide() == Side.CLIENT)
{
@ -220,13 +237,20 @@ public class TileEntityArmbot extends TileEntityAssembly implements IMultiBlock,
this.renderPitch += speedPitch;
//Clamp pitch between 0 - 60
while (this.renderPitch < 0)
{
this.renderPitch += 60;
}
while (this.renderPitch > 60)
{
this.renderPitch -= 60;
}
if (this.ticks % 4 == 0 && FMLCommonHandler.instance().getEffectiveSide() == Side.CLIENT)
{
this.worldObj.playSound(this.xCoord, this.yCoord, this.zCoord, "mods.assemblyline.conveyor", 2f, 2.5f, true);
}
if (Math.abs(this.renderPitch - this.rotationPitch) < this.ROTATION_SPEED + 0.1f)
{
@ -239,25 +263,37 @@ public class TileEntityArmbot extends TileEntityAssembly implements IMultiBlock,
}
}
//Clamp angles between 0 - 360
while (this.rotationYaw < 0)
this.rotationYaw += 360;
while (this.rotationYaw > 360)
this.rotationYaw -= 360;
while (this.rotationPitch < 0)
this.rotationPitch += 60;
while (this.rotationPitch > 60)
this.rotationPitch -= 60;
if (FMLCommonHandler.instance().getEffectiveSide() == Side.SERVER && this.ticks % 20 == 0)
{
PacketHandler.instance().sendPacketToClients(this.getDescriptionPacket(), this.worldObj, new Vector3(this), 50);
this.rotationYaw += 360;
}
while (this.rotationYaw > 360)
{
this.rotationYaw -= 360;
}
while (this.rotationPitch < 0)
{
this.rotationPitch += 60;
}
while (this.rotationPitch > 60)
{
this.rotationPitch -= 60;
}
//TODO reduce this to an event based system were it only updates the client when something changes
if (!this.worldObj.isRemote && this.ticks % 20 == 0)
{
this.sendRotationPacket();
}
}
public Command getCurrentCommand()
{
if (this.commandManager.hasTasks() && this.commandManager.getCurrentTask() >= 0 && this.commandManager.getCurrentTask() < this.commandManager.getCommands().size())
{
return this.commandManager.getCommands().get(this.commandManager.getCurrentTask());
}
return null;
}
@ -265,8 +301,8 @@ public class TileEntityArmbot extends TileEntityAssembly implements IMultiBlock,
public Vector3 getHandPosition()
{
Vector3 position = new Vector3(this);
position.add(0.5);
position.add(this.getDeltaHandPosition());
position.translate(0.5);
position.translate(this.getDeltaHandPosition());
return position;
}
@ -364,22 +400,6 @@ public class TileEntityArmbot extends TileEntityAssembly implements IMultiBlock,
return 1;
}
@Override
public boolean isUseableByPlayer(EntityPlayer par1EntityPlayer)
{
return this.worldObj.getBlockTileEntity(this.xCoord, this.yCoord, this.zCoord) != this ? false : par1EntityPlayer.getDistanceSq(this.xCoord + 0.5D, this.yCoord + 0.5D, this.zCoord + 0.5D) <= 64.0D;
}
@Override
public void openChest()
{
}
@Override
public void closeChest()
{
}
public String getCommandDisplayText()
{
return this.displayText;
@ -491,6 +511,46 @@ public class TileEntityArmbot extends TileEntityAssembly implements IMultiBlock,
nbt.setTag("items", items);
}
@Override
public Packet getDescriptionPacket()
{
return PacketHandler.instance().getPacket(this.getChannel(), this, "armbot", this.functioning, this.rotationYaw, this.rotationPitch);
}
public void sendRotationPacket()
{
PacketHandler.instance().sendPacketToClients(PacketHandler.instance().getPacket(this.getChannel(), "arbotRotation", this.rotationYaw, this.rotationPitch), worldObj, new Vector3(this).translate(new Vector3(.5f, 1f, .5f)), 40);
}
@Override
public boolean simplePacket(String id, DataInputStream dis, EntityPlayer player)
{
try
{
if (this.worldObj.isRemote && !super.simplePacket(id, dis, player))
{
if (id.equalsIgnoreCase("armbot"))
{
this.functioning = dis.readBoolean();
this.rotationYaw = dis.readFloat();
this.rotationPitch = dis.readFloat();
return true;
}
else if (id.equalsIgnoreCase("arbotRotation"))
{
this.rotationYaw = dis.readFloat();
this.rotationPitch = dis.readFloat();
return true;
}
}
}
catch (IOException e)
{
e.printStackTrace();
}
return false;
}
@Override
public boolean onActivated(EntityPlayer player)
{
@ -583,7 +643,7 @@ public class TileEntityArmbot extends TileEntityAssembly implements IMultiBlock,
@Override
public void onCreate(Vector3 placedPosition)
{
if (DarkMain.blockMulti != null)
if (DarkMain.blockMulti instanceof BlockMulti)
{
DarkMain.blockMulti.makeFakeBlock(this.worldObj, Vector3.translate(placedPosition, new Vector3(0, 1, 0)), placedPosition);
}

View file

@ -28,7 +28,7 @@ public abstract class TileEntityFilterable extends TileEntityAssembly implements
}
/** Looks through the things in the filter and finds out which item is being filtered.
*
*
* @return Is this filterable block filtering this specific ItemStack? */
public boolean isFiltering(ItemStack itemStack)
{
@ -38,14 +38,11 @@ public abstract class TileEntityFilterable extends TileEntityAssembly implements
if (checkStacks != null)
{
for (int i = 0; i < checkStacks.size(); i++)
for (ItemStack stack : checkStacks)
{
if (checkStacks.get(i) != null)
if (stack.isItemEqual(itemStack))
{
if (checkStacks.get(i).isItemEqual(itemStack))
{
return !inverted;
}
return !inverted;
}
}
}

View file

@ -17,7 +17,7 @@ import dark.core.prefab.tilenetwork.NetworkTileEntities;
/** A class to be inherited by all machines on the assembly line. This class acts as a single peace
* in a network of similar tiles allowing all to share power from one or more sources
*
*
* @author DarkGuardsman */
public abstract class TileEntityAssembly extends TileEntityEnergyMachine implements INetworkEnergyPart
{
@ -68,8 +68,6 @@ public abstract class TileEntityAssembly extends TileEntityEnergyMachine impleme
this.refresh();
}
}
this.onUpdate();
}
@Override
@ -79,13 +77,6 @@ public abstract class TileEntityAssembly extends TileEntityEnergyMachine impleme
return super.canFunction() || AssemblyLine.REQUIRE_NO_POWER;
}
/** Same as updateEntity */
@Deprecated
public void onUpdate()
{
}
@Override
public boolean canTileConnect(Connection type, ForgeDirection dir)
{

View file

@ -1,16 +1,21 @@
package dark.assembly.common.machine;
import java.io.DataInputStream;
import java.io.IOException;
import java.util.ArrayList;
import net.minecraft.entity.Entity;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.packet.Packet;
import net.minecraft.util.AxisAlignedBB;
import net.minecraftforge.common.ForgeDirection;
import dark.assembly.common.AssemblyLine;
import dark.assembly.common.imprinter.prefab.TileEntityFilterable;
import dark.core.network.PacketHandler;
import dark.core.prefab.machine.TileEntityMachine.SimplePacketTypes;
public class TileEntityDetector extends TileEntityFilterable
{
@ -100,6 +105,33 @@ public class TileEntityDetector extends TileEntityFilterable
tag.setBoolean("powering", this.powering);
}
@Override
public Packet getDescriptionPacket()
{
return PacketHandler.instance().getPacket(this.getChannel(), this, "detector", this.functioning, this.isInverted());
}
public boolean simplePacket(String id, DataInputStream dis, EntityPlayer player)
{
try
{
if (this.worldObj.isRemote && !super.simplePacket(id, dis, player))
{
if (id.equalsIgnoreCase("detector"))
{
this.functioning = dis.readBoolean();
this.setInverted(dis.readBoolean());
return true;
}
}
}
catch (IOException e)
{
e.printStackTrace();
}
return false;
}
public int isPoweringTo(ForgeDirection side)
{
return this.powering && this.getDirection() != side.getOpposite() ? 15 : 0;
@ -116,11 +148,4 @@ public class TileEntityDetector extends TileEntityFilterable
return direction != this.getDirection();
}
@Override
public void onUpdate()
{
// TODO Auto-generated method stub
}
}

View file

@ -1,10 +1,14 @@
package dark.assembly.common.machine;
import java.io.DataInputStream;
import java.io.IOException;
import java.util.List;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.packet.Packet;
import net.minecraft.util.AxisAlignedBB;
import net.minecraftforge.common.ForgeDirection;
import universalelectricity.core.vector.Vector3;
@ -12,6 +16,7 @@ import universalelectricity.prefab.tile.IRotatable;
import dark.assembly.api.IManipulator;
import dark.assembly.common.imprinter.ItemImprinter;
import dark.assembly.common.imprinter.prefab.TileEntityFilterable;
import dark.core.network.PacketHandler;
public class TileEntityManipulator extends TileEntityFilterable implements IRotatable, IManipulator
{
@ -31,8 +36,9 @@ public class TileEntityManipulator extends TileEntityFilterable implements IRota
}
@Override
public void onUpdate()
public void updateEntity()
{
super.updateEntity();
if (!this.worldObj.isRemote)
{
if (this.isFunctioning())
@ -172,6 +178,35 @@ public class TileEntityManipulator extends TileEntityFilterable implements IRota
nbt.setBoolean("selfpulse", this.isSelfPulse());
}
@Override
public Packet getDescriptionPacket()
{
return PacketHandler.instance().getPacket(this.getChannel(), this, "manipulator", this.functioning, this.isInverted(), this.isSelfPulse(), this.isOutput());
}
public boolean simplePacket(String id, DataInputStream dis, EntityPlayer player)
{
try
{
if (this.worldObj.isRemote && !super.simplePacket(id, dis, player))
{
if (id.equalsIgnoreCase("manipulator"))
{
this.functioning = dis.readBoolean();
this.setInverted(dis.readBoolean());
this.setSelfPulse(dis.readBoolean());
this.setOutput(dis.readBoolean());
return true;
}
}
}
catch (IOException e)
{
e.printStackTrace();
}
return false;
}
@Override
public boolean canConnect(ForgeDirection dir)
{

View file

@ -1,16 +1,21 @@
package dark.assembly.common.machine;
import java.io.DataInputStream;
import java.io.IOException;
import java.util.List;
import net.minecraft.entity.Entity;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.network.packet.Packet;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.AxisAlignedBB;
import net.minecraftforge.common.ForgeDirection;
import universalelectricity.core.vector.Vector3;
import dark.assembly.api.IBelt;
import dark.assembly.common.imprinter.prefab.TileEntityFilterable;
import dark.core.network.PacketHandler;
/** @author Darkguardsman */
public class TileEntityRejector extends TileEntityFilterable
@ -25,8 +30,9 @@ public class TileEntityRejector extends TileEntityFilterable
}
@Override
public void onUpdate()
public void updateEntity()
{
super.updateEntity();
/** Has to update a bit faster than a conveyer belt */
if (this.ticks % 5 == 0 && !this.isDisabled())
{
@ -96,4 +102,33 @@ public class TileEntityRejector extends TileEntityFilterable
{
return dir != this.getDirection();
}
@Override
public Packet getDescriptionPacket()
{
return PacketHandler.instance().getPacket(this.getChannel(), this, "rejector", this.functioning, this.isInverted(), this.firePiston);
}
public boolean simplePacket(String id, DataInputStream dis, EntityPlayer player)
{
try
{
if (this.worldObj.isRemote && !super.simplePacket(id, dis, player))
{
if (id.equalsIgnoreCase("rejector"))
{
this.functioning = dis.readBoolean();
this.setInverted(dis.readBoolean());
this.firePiston = dis.readBoolean();
return true;
}
}
}
catch (IOException e)
{
e.printStackTrace();
}
return false;
}
}

View file

@ -1,6 +1,7 @@
package dark.assembly.common.machine.belt;
import java.io.DataInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@ -22,7 +23,7 @@ import dark.core.common.DarkMain;
import dark.core.network.PacketHandler;
/** Conveyer belt TileEntity that allows entities of all kinds to be moved
*
*
* @author DarkGuardsman */
public class TileEntityConveyorBelt extends TileEntityAssembly implements IPacketReceiver, IBelt, IRotatable
{