diff --git a/src/common/assemblyline/machines/TileEntitySorter.java b/src/common/assemblyline/machines/TileEntitySorter.java index b1963087..f0f158c5 100644 --- a/src/common/assemblyline/machines/TileEntitySorter.java +++ b/src/common/assemblyline/machines/TileEntitySorter.java @@ -17,6 +17,7 @@ import universalelectricity.core.Vector3; import universalelectricity.implement.IElectricityReceiver; import universalelectricity.prefab.network.IPacketReceiver; import universalelectricity.prefab.network.PacketManager; +import assemblyline.AssemblyLine; import assemblyline.TileEntityBase; import assemblyline.belts.TileEntityConveyorBelt; @@ -24,137 +25,143 @@ import com.google.common.io.ByteArrayDataInput; import cpw.mods.fml.common.network.PacketDispatcher; -public class TileEntitySorter extends TileEntityBase implements IElectricityReceiver, IPacketReceiver -{ +public class TileEntitySorter extends TileEntityBase implements + IElectricityReceiver, IPacketReceiver { + /** + * Used to id the packet types + */ + private enum tPacketID{ANIMATION,GUI,SETTINGON} /** * Joules required per tick. */ public static final int WATTS_REQUIRED = 10; - + /** + * Stored energy + */ public double wattsReceived = 0; - + /** + * should the piston fire, or be extended + */ public boolean firePiston = false; - public boolean pFirePiston = false; - public boolean rejectItems = true; - public boolean[] onOff = new boolean[] - { true, true, true, true }; + /** + * on/off value for the GUI buttons + */ + public boolean[] onOff = new boolean[5]; + /** + * the belt found in the search area + */ public TileEntityConveyorBelt beltSide = null; + + @Override - public double wattRequest() - { - return WATTS_REQUIRED; - } - - // TODO add computer craft support to change - // onOff values, or even select what can be - // rejected. - // If option two add a rejector item to tell - // the computer what item is in front of the - // ejector - @Override - public void updateEntity() - { + public void updateEntity() { super.updateEntity(); - - if (this.ticks % 10 == 0) - { + //has to update a bit faster than a conveyer belt + if (this.ticks % 5 == 0) { + int meta = worldObj.getBlockMetadata(xCoord, yCoord, zCoord); + this.firePiston = false; + + //area to search for items ForgeDirection searchPosition = Vector3.getOrientationFromSide(ForgeDirection.getOrientation(getDirection(meta)), ForgeDirection.SOUTH); - TileEntity tileEntity = worldObj.getBlockTileEntity(xCoord + searchPosition.offsetX, yCoord + searchPosition.offsetY, zCoord + searchPosition.offsetZ); - if (tileEntity instanceof TileEntityConveyorBelt) - { + TileEntity tileEntity = worldObj.getBlockTileEntity(xCoord+ searchPosition.offsetX, yCoord + searchPosition.offsetY, zCoord + searchPosition.offsetZ); + + //find the belt in that search area + if (tileEntity instanceof TileEntityConveyorBelt) { this.beltSide = (TileEntityConveyorBelt) tileEntity; - } - else - { + } else { this.beltSide = null; } - this.firePiston = false; - try - { - AxisAlignedBB bounds = AxisAlignedBB.getBoundingBox(xCoord + searchPosition.offsetX, yCoord + searchPosition.offsetY, zCoord + searchPosition.offsetZ, xCoord + searchPosition.offsetX + 1, yCoord + searchPosition.offsetY + 1, zCoord + searchPosition.offsetZ + 1); - List itemsBehind = worldObj.getEntitiesWithinAABB(EntityItem.class, bounds); - if (itemsBehind.size() > 0 && this.wattsReceived > this.WATTS_REQUIRED) - { - for (EntityItem entity : itemsBehind) - { - if (this.canItemBeThrow(entity)) + + try { + //search area bound box + AxisAlignedBB bounds = AxisAlignedBB.getBoundingBox( + xCoord + searchPosition.offsetX, + yCoord + searchPosition.offsetY, + zCoord + searchPosition.offsetZ, + xCoord + searchPosition.offsetX + 1, + yCoord + searchPosition.offsetY + 1, + zCoord + searchPosition.offsetZ + 1); + //EntityItem list + List itemsBehind = worldObj.getEntitiesWithinAABB(EntityItem.class, bounds); + + boolean flag = false; + + if (itemsBehind.size() > 0 && this.wattsReceived > this.WATTS_REQUIRED) { + //for every item found check if can be thrown then throw item off belt if it can + for (EntityItem entity : itemsBehind) { + if (this.canItemBeThrow(entity)) { this.throwItem(searchPosition, entity); + flag = true; } } } - } - catch (Exception e) - { + //send packet with animation data if an item was rejected from the area + if(!worldObj.isRemote && flag) + { + Packet packet = PacketManager.getPacket(AssemblyLine.CHANNEL, this, this.data(tPacketID.ANIMATION)); + PacketManager.sendPacketToClients(packet, worldObj, Vector3.get(this), 30); + } + + } catch (Exception e) { e.printStackTrace(); } + if(!worldObj.isRemote && this.playerUsing > 0) + { + Packet packet = PacketManager.getPacket(AssemblyLine.CHANNEL,this, this.data(tPacketID.GUI)); + PacketManager.sendPacketToClients(packet, worldObj, Vector3.get(this), 10); + } } } /** - * Used to do the actual throwing of the item - * from the piston arm + * Used to move after it has been rejected * * @param side * - used to do the offset * @param entity * - Entity being thrown */ - public void throwItem(ForgeDirection side, Entity entity) - { + public void throwItem(ForgeDirection side, Entity entity) { this.firePiston = true; - if (this.beltSide != null) - { + if (this.beltSide != null) { this.beltSide.ignoreEntity(entity); } entity.motionX = (double) side.offsetX * 0.1; entity.motionY += 0.10000000298023224D; entity.motionZ = (double) side.offsetZ * 0.1; - this.wattsReceived -= this.WATTS_REQUIRED; + this.wattsReceived -= this.WATTS_REQUIRED; } - public boolean canItemBeThrow(Entity entity) - { - // TODO add ability to eject Entities that - // are not items, though i might want to - // create a bigger ejector for this - // TODO might also want to add damaging - // effect to items like glass once i make - // a sorter arm - if (entity instanceof EntityItem) - { + public boolean canItemBeThrow(Entity entity) { + //TODO add other things than items + if (entity instanceof EntityItem) { EntityItem itemE = (EntityItem) entity; ItemStack item = itemE.item; - if (this.rejectItems) - { + if (this.onOff[4]) { - // reject the items with same IDS - // as those placed in inventory - for (int i = 0; i < this.containingItems.length; i++) - { - if (containingItems[i] != null) - { - if (containingItems[i].itemID == item.itemID && containingItems[i].getItemDamage() == item.getItemDamage()) { return true; } + //reject matching items + for (int i = 0; i < this.containingItems.length; i++) { + if (containingItems[i] != null && onOff[i]) { + if (containingItems[i].itemID == item.itemID && containingItems[i].getItemDamage() == item.getItemDamage()) { + return true; + } } } return false; - } - else if (!this.rejectItems) - { - // reject all but the items with - // same IDS as those placed in - // inventory - for (int i = 0; i < this.containingItems.length; i++) - { - if (containingItems[i] != null) - { - if (containingItems[i].itemID == item.itemID && containingItems[i].getItemDamage() == item.getItemDamage()) { return false; } + } else if (!this.onOff[4]) { + //reject all but matching items + for (int i = 0; i < this.containingItems.length; i++) { + if (containingItems[i] != null&& onOff[i]) { + if (containingItems[i].itemID == item.itemID && containingItems[i].getItemDamage() == item.getItemDamage()) { + return false; + } } } return true; @@ -163,209 +170,176 @@ public class TileEntitySorter extends TileEntityBase implements IElectricityRece return false; } - public byte getDirection(int meta) - { + public byte getDirection(int meta) { - switch (meta) - { - case 0: - return 2; - case 1: - return 5; - case 2: - return 3; - case 3: - return 4; + switch (meta) { + case 0: + return 2; + case 1: + return 5; + case 2: + return 3; + case 3: + return 4; } return 0; } @Override - public boolean canReceiveFromSide(ForgeDirection side) - { - // TODO Auto-generated method stub + public boolean canReceiveFromSide(ForgeDirection side) { return side == ForgeDirection.DOWN; } - /* - * @Override public Object[] sendDataA() { - * return new Object[] { this.firePiston, - * this.rejectItems }; } + + /** + * Used to change any one of the boolean value of on/off array After + * changing the value if it was changed client side it will send a packet + * server side with the changes * - * @Override public Object[] sendDataG() { - * Object[] data = new - * Object[this.onOff.length]; for (int i = 0; - * i < this.onOff.length; i++) { data[i] = - * onOff[i]; } return data; } - * - * @Override public void - * guiPacket(NetworkManager network, int - * packetType, Packet250CustomPayload packet, - * EntityPlayer player, ByteArrayDataInput - * dataStream) { if (worldObj.isRemote) { try - * { for (int i = 0; i < this.onOff.length; - * i++) { this.onOff[i] = - * dataStream.readBoolean(); } - * - * } catch (Exception e) { - * e.printStackTrace(); } } - * - * } - * - * @Override public void - * animationPacket(NetworkManager network, int - * packetType, Packet250CustomPayload packet, - * EntityPlayer player, ByteArrayDataInput - * dataStream) { if (worldObj.isRemote) { try - * { this.firePiston = - * dataStream.readBoolean(); this.rejectItems - * = dataStream.readBoolean(); - * - * } catch (Exception e) { - * e.printStackTrace(); } } - * - * } - * - * @Override public void - * otherPacket(NetworkManager network, int - * packetType, Packet250CustomPayload packet, - * EntityPlayer player, ByteArrayDataInput - * dataStream) { if (worldObj.isRemote) { try - * { - * - * } catch (Exception e) { - * e.printStackTrace(); } } else if - * (!worldObj.isRemote) { try { int ID = - * dataStream.readInt(); if (ID == 0) { - * this.changeOnOff(dataStream.readInt()); } - * else if (ID == 1) { this.changeRejected(); - * } - * - * } catch (Exception e) { - * e.printStackTrace(); } } - * - * } + * @param i */ - - public void changeOnOff(int i) - { - if (i >= this.onOff.length) { return; } - boolean cc = this.onOff[i]; - if (cc) - { - cc = false; + public void changeOnOff(int i) { + if (i >= this.onOff.length) { + return; } - else - { + boolean cc = this.onOff[i]; + if (cc) { + cc = false; + } else { cc = true; } this.onOff[i] = cc; - if (worldObj.isRemote) - { - Packet packet = PacketManager.getPacket("asmLine", this, new Object[] - { 2, 0, i }); + if (worldObj.isRemote) { + Packet packet = PacketManager.getPacket("asmLine", this, + new Object[] { tPacketID.SETTINGON, i }); PacketDispatcher.sendPacketToServer(packet); } } - - public void changeRejected() - { - - boolean cc = this.rejectItems; - if (cc) - { - cc = false; - } - else - { - cc = true; - } - this.rejectItems = cc; - if (worldObj.isRemote) - { - Packet packet = PacketManager.getPacket("asmLine", this, new Object[] - { 2, 1 }); - PacketDispatcher.sendPacketToServer(packet); - } - } - + + /** + * Data methods + */ @Override - public void readFromNBT(NBTTagCompound nbt) - { + public void readFromNBT(NBTTagCompound nbt) { super.readFromNBT(nbt); - for (int i = 0; i < this.onOff.length; i++) - { + for (int i = 0; i < this.onOff.length; i++) { this.onOff[i] = nbt.getBoolean("onOff" + i); } - this.rejectItems = nbt.getBoolean("reject"); - } - - /** - * Writes a tile entity to NBT. - */ + } + @Override - public void writeToNBT(NBTTagCompound nbt) - { + public void writeToNBT(NBTTagCompound nbt) { super.writeToNBT(nbt); - for (int i = 0; i < this.onOff.length; i++) - { + for (int i = 0; i < this.onOff.length; i++) { nbt.setBoolean("onOff" + i, this.onOff[i]); } - nbt.setBoolean("reject", this.rejectItems); + } + + public Object[] data(tPacketID id) + { + if(id == tPacketID.ANIMATION) + { + return new Object[]{id.ordinal(),this.firePiston}; + } + if(id == tPacketID.GUI) + { + Object[] da = new Object[this.onOff.length]; + da[0]= id.ordinal(); + for(int i =0; i < this.onOff.length; i++) + { + da[i+1] = onOff[i]; + } + return da; + } + return new Object[]{id.ordinal()}; + } + + @Override + public void handlePacketData(INetworkManager network, int packetType, + Packet250CustomPayload packet, EntityPlayer player, + ByteArrayDataInput dataStream) { + try{ + int id = dataStream.readInt(); + tPacketID pID = tPacketID.values()[id]; + if(pID == tPacketID.ANIMATION) + { + this.firePiston = dataStream.readBoolean(); + } + if(pID == tPacketID.GUI) + { + for(int i =0; i < this.onOff.length; i++) + { + this.onOff[i] = dataStream.readBoolean(); + } + } + if(pID == tPacketID.SETTINGON) + { + int num = dataStream.readInt(); + this.changeOnOff(num); + } + + }catch(Exception e) + { + e.printStackTrace(); + } + } + + /** + * inventory methods + */ + @Override + public String getInvName() { + return "Rejector"; } @Override - public String getInvName() - { - return "Ejector"; - } - - @Override - public int getInventoryStackLimit() - { + public int getInventoryStackLimit() { return 1; } - + @Override - public void onReceive(TileEntity sender, double amps, double voltage, ForgeDirection side) - { - this.wattsReceived += (amps * voltage); + public int getSizeInventory() { + return 4; + } + + /** + * disabling methods + */ + @Override + public void onDisable(int duration) { } @Override - public void onDisable(int duration) - { - - } - - @Override - public boolean isDisabled() - { + public boolean isDisabled() { return false; } @Override - public boolean canConnect(ForgeDirection side) - { + public boolean canConnect(ForgeDirection side) { return true; } - + + /** + * UE methods + */ @Override - public double getVoltage() - { + public double getVoltage() { return 120; } - + @Override - public int getSizeInventory() - { - return 10; + public double wattRequest() { + return WATTS_REQUIRED; } - + @Override - public void handlePacketData(INetworkManager network, int packetType, Packet250CustomPayload packet, EntityPlayer player, ByteArrayDataInput dataStream) - { - + public void onReceive(TileEntity sender, double amps, double voltage, + ForgeDirection side) { + this.wattsReceived += (amps * voltage); + } + + + }