More Item Pipe opt to net and cpu
PipedItem IDs reduced to Short.MIN - Short.MAX to shave bytes off the packet size. It could in theory be reduced to a byte and changed to a per pipe ID, but that would require a larger rewrite. The packet no longer send the pipe location and instead derives the pipe location from the item location. In total, 13 bytes were removed from the item packet size. Also a number of cpu optimizations were performed, mainly by using more appropriate collections and cleaning up some simple but often called functions.
This commit is contained in:
parent
4cec777807
commit
4773280c5b
4 changed files with 76 additions and 65 deletions
|
@ -50,7 +50,7 @@ public class EntityPassiveItem implements IPipedItem {
|
||||||
|
|
||||||
/* CONSTRUCTORS */
|
/* CONSTRUCTORS */
|
||||||
public EntityPassiveItem(World world) {
|
public EntityPassiveItem(World world) {
|
||||||
this(world, maxId != Integer.MAX_VALUE ? ++maxId : (maxId = 0));
|
this(world, maxId < Short.MAX_VALUE ? ++maxId : (maxId = Short.MIN_VALUE));
|
||||||
}
|
}
|
||||||
|
|
||||||
public EntityPassiveItem(World world, int id) {
|
public EntityPassiveItem(World world, int id) {
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
package buildcraft.transport;
|
package buildcraft.transport;
|
||||||
|
|
||||||
import net.minecraftforge.common.ForgeDirection;
|
|
||||||
import buildcraft.api.transport.IPipedItem;
|
import buildcraft.api.transport.IPipedItem;
|
||||||
import java.util.EnumSet;
|
import java.util.EnumSet;
|
||||||
|
import net.minecraftforge.common.ForgeDirection;
|
||||||
|
|
||||||
public class EntityData {
|
public class EntityData {
|
||||||
|
|
||||||
|
|
|
@ -24,12 +24,12 @@ import buildcraft.core.proxy.CoreProxy;
|
||||||
import buildcraft.core.utils.Utils;
|
import buildcraft.core.utils.Utils;
|
||||||
import buildcraft.transport.network.PacketPipeTransportContent;
|
import buildcraft.transport.network.PacketPipeTransportContent;
|
||||||
import cpw.mods.fml.common.network.PacketDispatcher;
|
import cpw.mods.fml.common.network.PacketDispatcher;
|
||||||
import java.util.ArrayList;
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.TreeMap;
|
import java.util.Set;
|
||||||
import java.util.TreeSet;
|
import java.util.TreeSet;
|
||||||
import net.minecraft.src.EntityItem;
|
import net.minecraft.src.EntityItem;
|
||||||
import net.minecraft.src.IInventory;
|
import net.minecraft.src.IInventory;
|
||||||
|
@ -43,8 +43,8 @@ import net.minecraftforge.common.ForgeDirection;
|
||||||
public class PipeTransportItems extends PipeTransport {
|
public class PipeTransportItems extends PipeTransport {
|
||||||
|
|
||||||
public boolean allowBouncing = false;
|
public boolean allowBouncing = false;
|
||||||
public Map<Integer, EntityData> travelingEntities = new TreeMap<Integer, EntityData>();
|
public Map<Integer, EntityData> travelingEntities = new HashMap<Integer, EntityData>();
|
||||||
private final List<EntityData> entitiesToLoad = new ArrayList<EntityData>();
|
private final List<EntityData> entitiesToLoad = new LinkedList<EntityData>();
|
||||||
|
|
||||||
// TODO: generalize the use of this hook in particular for obsidian pipe
|
// TODO: generalize the use of this hook in particular for obsidian pipe
|
||||||
public IItemTravelingHook travelHook;
|
public IItemTravelingHook travelHook;
|
||||||
|
@ -57,12 +57,15 @@ public class PipeTransportItems extends PipeTransport {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void defaultReajustSpeed(IPipedItem item) {
|
public void defaultReajustSpeed(IPipedItem item) {
|
||||||
|
float speed = item.getSpeed();
|
||||||
|
|
||||||
if (item.getSpeed() > Utils.pipeNormalSpeed)
|
if (speed > Utils.pipeNormalSpeed)
|
||||||
item.setSpeed(item.getSpeed() - Utils.pipeNormalSpeed);
|
speed -= Utils.pipeNormalSpeed;
|
||||||
|
|
||||||
if (item.getSpeed() < Utils.pipeNormalSpeed)
|
if (speed < Utils.pipeNormalSpeed)
|
||||||
item.setSpeed(Utils.pipeNormalSpeed);
|
speed = Utils.pipeNormalSpeed;
|
||||||
|
|
||||||
|
item.setSpeed(speed);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -80,22 +83,26 @@ public class PipeTransportItems extends PipeTransport {
|
||||||
data = new EntityData(item, orientation);
|
data = new EntityData(item, orientation);
|
||||||
travelingEntities.put(item.getEntityId(), data);
|
travelingEntities.put(item.getEntityId(), data);
|
||||||
|
|
||||||
if (item.getContainer() != null && item.getContainer() != this.container)
|
if (item.getContainer() != null && item.getContainer() != this.container) {
|
||||||
((PipeTransportItems) ((TileGenericPipe) item.getContainer()).pipe.transport).scheduleRemoval(item);
|
((PipeTransportItems) ((TileGenericPipe) item.getContainer()).pipe.transport).scheduleRemoval(item);
|
||||||
|
}
|
||||||
|
|
||||||
item.setContainer(container);
|
item.setContainer(container);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reajusting Ypos to make sure the object looks like sitting on the
|
// Reajusting Ypos to make sure the object looks like sitting on the
|
||||||
// pipe.
|
// pipe.
|
||||||
if (orientation != ForgeDirection.UP && orientation != ForgeDirection.DOWN)
|
if (orientation != ForgeDirection.UP && orientation != ForgeDirection.DOWN) {
|
||||||
item.setPosition(item.getPosition().x, yCoord + Utils.getPipeFloorOf(item.getItemStack()), item.getPosition().z);
|
item.setPosition(item.getPosition().x, yCoord + Utils.getPipeFloorOf(item.getItemStack()), item.getPosition().z);
|
||||||
|
}
|
||||||
|
|
||||||
if (!worldObj.isRemote)
|
if (!worldObj.isRemote) {
|
||||||
data.output = resolveDestination(data);
|
data.output = resolveDestination(data);
|
||||||
|
}
|
||||||
|
|
||||||
if (container.pipe instanceof IPipeTransportItemsHook)
|
if (container.pipe instanceof IPipeTransportItemsHook) {
|
||||||
((IPipeTransportItemsHook) container.pipe).entityEntered(item, orientation);
|
((IPipeTransportItemsHook) container.pipe).entityEntered(item, orientation);
|
||||||
|
}
|
||||||
|
|
||||||
if (!worldObj.isRemote) {
|
if (!worldObj.isRemote) {
|
||||||
sendItemPacket(data);
|
sendItemPacket(data);
|
||||||
|
@ -104,10 +111,11 @@ public class PipeTransportItems extends PipeTransport {
|
||||||
if (travelingEntities.size() > BuildCraftTransport.groupItemsTrigger) {
|
if (travelingEntities.size() > BuildCraftTransport.groupItemsTrigger) {
|
||||||
groupEntities();
|
groupEntities();
|
||||||
|
|
||||||
if (travelingEntities.size() > BuildCraftTransport.maxItemsInPipes)
|
if (travelingEntities.size() > BuildCraftTransport.maxItemsInPipes) {
|
||||||
worldObj.createExplosion(null, xCoord, yCoord, zCoord, 1, false);
|
worldObj.createExplosion(null, xCoord, yCoord, zCoord, 1, false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Bounces the item back into the pipe without changing the travelingEntities map.
|
* Bounces the item back into the pipe without changing the travelingEntities map.
|
||||||
|
@ -205,7 +213,7 @@ public class PipeTransportItems extends PipeTransport {
|
||||||
moveSolids();
|
moveSolids();
|
||||||
}
|
}
|
||||||
|
|
||||||
HashSet<Integer> toRemove = new HashSet<Integer>();
|
Set<Integer> toRemove = new HashSet<Integer>();
|
||||||
|
|
||||||
public void scheduleRemoval(IPipedItem item) {
|
public void scheduleRemoval(IPipedItem item) {
|
||||||
toRemove.add(item.getEntityId());
|
toRemove.add(item.getEntityId());
|
||||||
|
@ -221,12 +229,13 @@ public class PipeTransportItems extends PipeTransport {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void moveSolids() {
|
private void moveSolids() {
|
||||||
|
if(!entitiesToLoad.isEmpty()){
|
||||||
for (EntityData data : entitiesToLoad) {
|
for (EntityData data : entitiesToLoad) {
|
||||||
data.item.setWorld(worldObj);
|
data.item.setWorld(worldObj);
|
||||||
travelingEntities.put(data.item.getEntityId(), data);
|
travelingEntities.put(data.item.getEntityId(), data);
|
||||||
}
|
}
|
||||||
|
|
||||||
entitiesToLoad.clear();
|
entitiesToLoad.clear();
|
||||||
|
}
|
||||||
performRemoval();
|
performRemoval();
|
||||||
|
|
||||||
for (EntityData data : travelingEntities.values()) {
|
for (EntityData data : travelingEntities.values()) {
|
||||||
|
@ -239,9 +248,12 @@ public class PipeTransportItems extends PipeTransport {
|
||||||
Position motion = new Position(0, 0, 0, data.toCenter ? data.input : data.output);
|
Position motion = new Position(0, 0, 0, data.toCenter ? data.input : data.output);
|
||||||
motion.moveForwards(data.item.getSpeed());
|
motion.moveForwards(data.item.getSpeed());
|
||||||
|
|
||||||
data.item.setPosition(data.item.getPosition().x + motion.x, data.item.getPosition().y + motion.y, data.item.getPosition().z + motion.z);
|
|
||||||
|
|
||||||
if ((data.toCenter && middleReached(data)) || outOfBounds(data)) {
|
Position pos = data.item.getPosition();
|
||||||
|
data.item.setPosition(pos.x + motion.x, pos.y + motion.y, pos.z + motion.z);
|
||||||
|
pos = data.item.getPosition();
|
||||||
|
|
||||||
|
if ((data.toCenter && middleReached(data, pos)) || outOfBounds(pos)) {
|
||||||
data.toCenter = false;
|
data.toCenter = false;
|
||||||
|
|
||||||
// Reajusting to the middle
|
// Reajusting to the middle
|
||||||
|
@ -265,7 +277,7 @@ public class PipeTransportItems extends PipeTransport {
|
||||||
travelHook.centerReached(this, data);
|
travelHook.centerReached(this, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
} else if (!data.toCenter && endReached(data)) {
|
} else if (!data.toCenter && endReached(pos)) {
|
||||||
Position destPos = new Position(xCoord, yCoord, zCoord, data.output);
|
Position destPos = new Position(xCoord, yCoord, zCoord, data.output);
|
||||||
|
|
||||||
destPos.moveForwards(1.0);
|
destPos.moveForwards(1.0);
|
||||||
|
@ -316,21 +328,21 @@ public class PipeTransportItems extends PipeTransport {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean middleReached(EntityData entity) {
|
protected boolean middleReached(EntityData entity, Position pos) {
|
||||||
float middleLimit = entity.item.getSpeed() * 1.01F;
|
float middleLimit = entity.item.getSpeed() * 1.01F;
|
||||||
return (Math.abs(xCoord + 0.5 - entity.item.getPosition().x) < middleLimit
|
return (Math.abs(xCoord + 0.5 - pos.x) < middleLimit
|
||||||
&& Math.abs(yCoord + Utils.getPipeFloorOf(entity.item.getItemStack()) - entity.item.getPosition().y) < middleLimit && Math.abs(zCoord
|
&& Math.abs(yCoord + Utils.getPipeFloorOf(entity.item.getItemStack()) - pos.y) < middleLimit
|
||||||
+ 0.5 - entity.item.getPosition().z) < middleLimit);
|
&& Math.abs(zCoord + 0.5 - pos.z) < middleLimit);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean endReached(EntityData entity) {
|
protected boolean endReached(Position pos) {
|
||||||
return entity.item.getPosition().x > xCoord + 1.0 || entity.item.getPosition().x < xCoord || entity.item.getPosition().y > yCoord + 1.0
|
return pos.x > xCoord + 1 ||pos.x < xCoord || pos.y > yCoord + 1
|
||||||
|| entity.item.getPosition().y < yCoord || entity.item.getPosition().z > zCoord + 1.0 || entity.item.getPosition().z < zCoord;
|
|| pos.y < yCoord || pos.z > zCoord + 1 || pos.z < zCoord;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean outOfBounds(EntityData entity) {
|
protected boolean outOfBounds(Position pos) {
|
||||||
return entity.item.getPosition().x > xCoord + 2.0 || entity.item.getPosition().x < xCoord - 1.0 || entity.item.getPosition().y > yCoord + 2.0
|
return pos.x > xCoord + 2 || pos.x < xCoord - 1 || pos.y > yCoord + 2
|
||||||
|| entity.item.getPosition().y < yCoord - 1.0 || entity.item.getPosition().z > zCoord + 2.0 || entity.item.getPosition().z < zCoord - 1.0;
|
|| pos.y < yCoord - 1 || pos.z > zCoord + 2 || pos.z < zCoord - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Position getPosition() {
|
public Position getPosition() {
|
||||||
|
@ -433,7 +445,7 @@ public class PipeTransportItems extends PipeTransport {
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public Packet createItemPacket(EntityData data) {
|
public Packet createItemPacket(EntityData data) {
|
||||||
PacketPipeTransportContent packet = new PacketPipeTransportContent(container.xCoord, container.yCoord, container.zCoord, data);
|
PacketPipeTransportContent packet = new PacketPipeTransportContent(data);
|
||||||
return packet.getPacket();
|
return packet.getPacket();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package buildcraft.transport.network;
|
package buildcraft.transport.network;
|
||||||
|
|
||||||
|
import buildcraft.core.network.BuildCraftPacket;
|
||||||
import buildcraft.core.network.PacketCoordinates;
|
import buildcraft.core.network.PacketCoordinates;
|
||||||
import buildcraft.core.network.PacketIds;
|
import buildcraft.core.network.PacketIds;
|
||||||
import buildcraft.transport.EntityData;
|
import buildcraft.transport.EntityData;
|
||||||
|
@ -7,41 +8,41 @@ import buildcraft.transport.EntityData;
|
||||||
import java.io.DataInputStream;
|
import java.io.DataInputStream;
|
||||||
import java.io.DataOutputStream;
|
import java.io.DataOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import net.minecraft.src.MathHelper;
|
||||||
|
|
||||||
import net.minecraftforge.common.ForgeDirection;
|
import net.minecraftforge.common.ForgeDirection;
|
||||||
|
|
||||||
public class PacketPipeTransportContent extends PacketCoordinates {
|
public class PacketPipeTransportContent extends BuildCraftPacket {
|
||||||
|
|
||||||
private EntityData entityData;
|
private EntityData entityData;
|
||||||
|
|
||||||
private int entityId;
|
private int entityId;
|
||||||
|
|
||||||
private ForgeDirection input;
|
private ForgeDirection input;
|
||||||
private ForgeDirection output;
|
private ForgeDirection output;
|
||||||
|
|
||||||
private int itemId;
|
private int itemId;
|
||||||
private byte stackSize;
|
private byte stackSize;
|
||||||
private int itemDamage;
|
private int itemDamage;
|
||||||
|
|
||||||
private float itemX;
|
private float itemX;
|
||||||
private float itemY;
|
private float itemY;
|
||||||
private float itemZ;
|
private float itemZ;
|
||||||
|
|
||||||
private float speed;
|
private float speed;
|
||||||
|
public int posX;
|
||||||
|
public int posY;
|
||||||
|
public int posZ;
|
||||||
|
|
||||||
public PacketPipeTransportContent() {}
|
public PacketPipeTransportContent() {
|
||||||
|
}
|
||||||
public PacketPipeTransportContent(int x, int y, int z, EntityData data) {
|
|
||||||
super(PacketIds.PIPE_CONTENTS, x, y, z);
|
|
||||||
|
|
||||||
|
public PacketPipeTransportContent(EntityData data) {
|
||||||
this.entityData = data;
|
this.entityData = data;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void writeData(DataOutputStream data) throws IOException {
|
public void writeData(DataOutputStream data) throws IOException {
|
||||||
super.writeData(data);
|
data.writeFloat((float) entityData.item.getPosition().x);
|
||||||
|
data.writeFloat((float) entityData.item.getPosition().y);
|
||||||
|
data.writeFloat((float) entityData.item.getPosition().z);
|
||||||
|
|
||||||
data.writeInt(entityData.item.getEntityId());
|
data.writeShort(entityData.item.getEntityId());
|
||||||
|
|
||||||
data.writeByte((byte) entityData.input.ordinal());
|
data.writeByte((byte) entityData.input.ordinal());
|
||||||
data.writeByte((byte) entityData.output.ordinal());
|
data.writeByte((byte) entityData.output.ordinal());
|
||||||
|
@ -50,18 +51,20 @@ public class PacketPipeTransportContent extends PacketCoordinates {
|
||||||
data.writeByte((byte) entityData.item.getItemStack().stackSize);
|
data.writeByte((byte) entityData.item.getItemStack().stackSize);
|
||||||
data.writeShort(entityData.item.getItemStack().getItemDamage());
|
data.writeShort(entityData.item.getItemStack().getItemDamage());
|
||||||
|
|
||||||
data.writeFloat((float) entityData.item.getPosition().x);
|
|
||||||
data.writeFloat((float) entityData.item.getPosition().y);
|
|
||||||
data.writeFloat((float) entityData.item.getPosition().z);
|
|
||||||
|
|
||||||
data.writeFloat(entityData.item.getSpeed());
|
data.writeFloat(entityData.item.getSpeed());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void readData(DataInputStream data) throws IOException {
|
public void readData(DataInputStream data) throws IOException {
|
||||||
super.readData(data);
|
this.itemX = data.readFloat();
|
||||||
|
this.itemY = data.readFloat();
|
||||||
|
this.itemZ = data.readFloat();
|
||||||
|
|
||||||
this.entityId = data.readInt();
|
posX = MathHelper.floor_float(itemX);
|
||||||
|
posY = MathHelper.floor_float(itemY);
|
||||||
|
posZ = MathHelper.floor_float(itemZ);
|
||||||
|
|
||||||
|
this.entityId = data.readShort();
|
||||||
|
|
||||||
this.input = ForgeDirection.getOrientation(data.readByte());
|
this.input = ForgeDirection.getOrientation(data.readByte());
|
||||||
this.output = ForgeDirection.getOrientation(data.readByte());
|
this.output = ForgeDirection.getOrientation(data.readByte());
|
||||||
|
@ -70,10 +73,6 @@ public class PacketPipeTransportContent extends PacketCoordinates {
|
||||||
this.stackSize = data.readByte();
|
this.stackSize = data.readByte();
|
||||||
this.itemDamage = data.readShort();
|
this.itemDamage = data.readShort();
|
||||||
|
|
||||||
this.itemX = data.readFloat();
|
|
||||||
this.itemY = data.readFloat();
|
|
||||||
this.itemZ = data.readFloat();
|
|
||||||
|
|
||||||
this.speed = data.readFloat();
|
this.speed = data.readFloat();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue