Cleaned out the old AL prefabs for machines

This commit is contained in:
DarkGuardsman 2014-01-19 20:50:57 -05:00
parent 1fb19b287a
commit f0538c0ace
6 changed files with 721 additions and 1503 deletions

View file

@ -1,193 +0,0 @@
package resonantinduction.core.prefab.tile;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.AxisAlignedBB;
import net.minecraftforge.common.ForgeDirection;
import resonantinduction.core.tilenetwork.INetworkEnergyPart;
import resonantinduction.core.tilenetwork.ITileNetwork;
import resonantinduction.core.tilenetwork.prefab.NetworkAssembly;
import resonantinduction.core.tilenetwork.prefab.NetworkSharedPower;
import universalelectricity.api.vector.Vector3;
/**
* 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 TileAssembly extends TileEnergyMachine implements INetworkEnergyPart
{
/** lowest value the network can update at */
public static int refresh_min_rate = 20;
/** range by which the network can update at */
public static int refresh_diff = 9;
/** Network used to link assembly machines together */
private NetworkAssembly assemblyNetwork;
/** Tiles that are connected to this */
public List<TileEntity> connectedTiles = new ArrayList<TileEntity>();
/** Random instance */
public Random random = new Random();
/** Random rate by which this tile updates its network connections */
private int updateTick = 1;
public TileAssembly(long wattsPerTick)
{
super(wattsPerTick);
}
public TileAssembly(long wattsPerTick, long maxEnergy)
{
super(wattsPerTick, maxEnergy);
}
@Override
public void invalidate()
{
this.getTileNetwork().splitNetwork(this);
super.invalidate();
}
@Override
public void updateEntity()
{
super.updateEntity();
if (!this.worldObj.isRemote)
{
if (ticks % updateTick == 0)
{
this.updateTick = (random.nextInt(1 + refresh_diff) + refresh_min_rate);
this.refresh();
}
}
}
@Override
public boolean canTileConnect(Connection type, ForgeDirection dir)
{
return true;
}
@Override
public void refresh()
{
if (this.worldObj != null && !this.worldObj.isRemote)
{
this.connectedTiles.clear();
for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS)
{
TileEntity tileEntity = new Vector3(this).modifyPositionFromSide(dir).getTileEntity(this.worldObj);
if (tileEntity instanceof TileAssembly && ((TileAssembly) tileEntity).canTileConnect(Connection.NETWORK, dir.getOpposite()))
{
this.getTileNetwork().mergeNetwork(((TileAssembly) tileEntity).getTileNetwork(), this);
connectedTiles.add(tileEntity);
}
}
}
}
@Override
public List<TileEntity> getNetworkConnections()
{
return this.connectedTiles;
}
@Override
public NetworkAssembly getTileNetwork()
{
if (!(this.assemblyNetwork instanceof NetworkAssembly))
{
this.assemblyNetwork = new NetworkAssembly(this);
}
return this.assemblyNetwork;
}
@Override
public void setTileNetwork(ITileNetwork network)
{
if (network instanceof NetworkAssembly)
{
this.assemblyNetwork = (NetworkAssembly) network;
}
}
@Override
public boolean consumePower(long request, boolean doExtract)
{
return ((NetworkSharedPower) this.getTileNetwork()).removePower(this, request, doExtract) >= request;
}
@Override
public long onReceiveEnergy(ForgeDirection from, long receive, boolean doReceive)
{
if (this.canConnect(from))
{
return this.getTileNetwork().addPower(this, receive, doReceive);
}
return 0;
}
/** Amount of energy this tile runs on per tick */
public int getWattLoad()
{
return 1;// 1J/t or 20J/t
}
/** Conditional load that may not be consumed per tick */
public int getExtraLoad()
{
return 1;// 1J/t or 20J/t
}
@Override
public long getEnergyStored()
{
return ((NetworkSharedPower) this.getTileNetwork()).getEnergy();
}
@Override
public long getMaxEnergyStored()
{
return ((NetworkSharedPower) this.getTileNetwork()).getEnergyCapacity();
}
@Override
public long getEnergy(ForgeDirection from)
{
return this.getEnergyStored();
}
@Override
public long getEnergyCapacity(ForgeDirection from)
{
return this.getMaxEnergyStored();
}
@Override
public long getPartEnergy()
{
return this.energyStored;
}
@Override
public long getPartMaxEnergy()
{
return this.MAX_JOULES_STORED;
}
@Override
public void setPartEnergy(long energy)
{
this.energyStored = energy;
}
@Override
public AxisAlignedBB getRenderBoundingBox()
{
return INFINITE_EXTENT_AABB;
}
}

View file

@ -1,408 +0,0 @@
package resonantinduction.core.prefab.tile;
import java.util.EnumSet;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagFloat;
import net.minecraft.nbt.NBTTagLong;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.ForgeDirection;
import net.minecraftforge.oredict.OreDictionary;
import universalelectricity.api.CompatibilityModule;
import universalelectricity.api.electricity.IVoltageInput;
import universalelectricity.api.electricity.IVoltageOutput;
import universalelectricity.api.energy.IEnergyContainer;
import universalelectricity.api.energy.IEnergyInterface;
import universalelectricity.api.vector.Vector3;
import universalelectricity.api.vector.VectorHelper;
/**
* Basic energy tile that can consume power
*
* Based off both UE universal electrical tile, and electrical tile prefabs
*
* @author DarkGuardsman
*/
public abstract class TileEnergyMachine extends TileMachine implements IEnergyInterface, IEnergyContainer, IVoltageInput, IVoltageOutput
{
/** Forge Ore Directory name of the item to toggle infinite power mode */
public static String powerToggleItemID = "battery";
/** Demand per tick in watts */
protected long JOULES_PER_TICK;
/** Max limit of the internal battery/buffer of the machine */
protected long MAX_JOULES_STORED;
/** Current energy stored in the machine's battery/buffer */
protected long energyStored = 0;
/** Should we run without power */
private boolean runWithoutPower = true;
/** Point by which this machines suffers low voltage damage */
protected long brownOutVoltage = -1;
/** Point by which this machines suffers over voltage damage */
protected long shortOutVoltage = -1;
/** Voltage by which the machine was designed and rated for */
protected long ratedVoltage = 240;
public TileEnergyMachine()
{
this.brownOutVoltage = this.getVoltage() / 2;
this.shortOutVoltage = (long) ((Math.sqrt(2) * this.getVoltage()) + 0.05 * this.getVoltage());
}
public TileEnergyMachine(long wattsPerTick)
{
this();
this.JOULES_PER_TICK = wattsPerTick;
this.MAX_JOULES_STORED = wattsPerTick * 20;
}
public TileEnergyMachine(long wattsPerTick, long maxEnergy)
{
this(wattsPerTick);
this.MAX_JOULES_STORED = maxEnergy;
}
@Override
public void updateEntity()
{
super.updateEntity();
if (!this.worldObj.isRemote && this.isFunctioning())
{
this.consumePower(this.JOULES_PER_TICK, true);
}
}
/** Does this tile have power to run and do work */
@Override
public boolean canFunction()
{
return super.canFunction() && (this.consumePower(this.JOULES_PER_TICK, false));
}
/** Called when a player activates the tile's block */
public boolean onPlayerActivated(EntityPlayer player)
{
if (player != null && player.capabilities.isCreativeMode)
{
ItemStack itemStack = player.getHeldItem();
if (itemStack != null)
{
for (ItemStack stack : OreDictionary.getOres(powerToggleItemID))
{
if (stack.isItemEqual(itemStack))
{
this.togglePowerMode();
return true;
}
}
}
}
return false;
}
@Override
public void updateContainingBlockInfo()
{
super.updateContainingBlockInfo();
// TODO use this to reset any values that are based on the block as this gets called when
// the block changes
}
/* ********************************************
* Electricity logic
* *********************************************
*/
@Override
public long onReceiveEnergy(ForgeDirection from, long receive, boolean doReceive)
{
if (this.getInputDirections().contains(from) && receive > 0)
{
long prevEnergyStored = Math.max(this.getEnergy(from), 0);
long newStoredEnergy = Math.min(this.getEnergy(from) + receive, this.getEnergyCapacity(from));
if (doReceive)
{
this.setEnergy(from, newStoredEnergy);
}
return Math.max(newStoredEnergy - prevEnergyStored, 0);
}
return 0;
}
/** Called to consume power from the internal storage */
protected boolean consumePower(long watts, boolean doDrain)
{
if (watts <= 0)
{
return true;
}
if (this.getEnergy(ForgeDirection.UNKNOWN) >= watts)
{
if (doDrain)
{
this.setEnergy(ForgeDirection.UNKNOWN, this.getEnergyStored() - watts);
}
return true;
}
return false;
}
@Override
public long onExtractEnergy(ForgeDirection from, long request, boolean doExtract)
{
if (this.getOutputDirections().contains(from) && request > 0)
{
long requestedEnergy = Math.min(request, this.energyStored);
if (doExtract)
{
this.setEnergy(from, this.energyStored - requestedEnergy);
}
return requestedEnergy;
}
return 0;
}
/** Called to produce power using the output enumset for directions to output in */
protected void produce()
{
for (ForgeDirection direction : this.getOutputDirections())
{
if (direction != ForgeDirection.UNKNOWN)
{
TileEntity entity = VectorHelper.getTileEntityFromSide(this.worldObj, new Vector3(this), direction);
if (CompatibilityModule.canConnect(entity, direction.getOpposite()))
{
long output = this.onExtractEnergy(direction, this.JOULES_PER_TICK, false);
long input = CompatibilityModule.receiveEnergy(entity, direction.getOpposite(), output, true);
if (input > 0 && this.onExtractEnergy(direction, input, true) > 0)
{
break;
}
}
}
}
}
@Override
public long getVoltageInput(ForgeDirection direction)
{
if (this.getInputDirections().contains(direction))
{
return this.ratedVoltage;
}
return 0;
}
@Override
public void onWrongVoltage(ForgeDirection direction, long voltage)
{
if (voltage > this.ratedVoltage)
{
if (voltage > this.shortOutVoltage)
{
// TODO damage machine
}
}
else
{
if (voltage < this.brownOutVoltage)
{
// TODO cause machine to run slow
}
}
}
@Override
public long getVoltageOutput(ForgeDirection direction)
{
if (this.getOutputDirections().contains(direction))
{
return this.ratedVoltage;
}
return 0;
}
/* ********************************************
* Electricity connection logic
* *********************************************
*/
@Override
public boolean canConnect(ForgeDirection direction)
{
if (direction == null || direction.equals(ForgeDirection.UNKNOWN))
{
return false;
}
return this.getInputDirections().contains(direction) || this.getOutputDirections().contains(direction);
}
/**
* The electrical input direction.
*
* @return The direction that electricity is entered into the tile. Return null for no input. By
* default you can accept power from all sides.
*/
public EnumSet<ForgeDirection> getInputDirections()
{
return EnumSet.allOf(ForgeDirection.class);
}
/**
* The electrical output direction.
*
* @return The direction that electricity is output from the tile. Return null for no output. By
* default it will return an empty EnumSet.
*/
public EnumSet<ForgeDirection> getOutputDirections()
{
return EnumSet.noneOf(ForgeDirection.class);
}
/* ********************************************
* Machine energy parms
* *********************************************
*/
public long getVoltage()
{
return this.ratedVoltage;
}
public TileEnergyMachine setVoltage(long volts)
{
this.ratedVoltage = volts;
return this;
}
public long getMaxEnergyStored()
{
return this.MAX_JOULES_STORED;
}
@Override
public long getEnergyCapacity(ForgeDirection from)
{
if (this.canConnect(from) || from == ForgeDirection.UNKNOWN)
{
return this.getMaxEnergyStored();
}
return 0;
}
public void setMaxEnergyStored(long energy)
{
this.MAX_JOULES_STORED = energy;
}
public long getEnergyStored()
{
return this.energyStored;
}
@Override
public void setEnergy(ForgeDirection from, long energy)
{
if (this.canConnect(from) || from == ForgeDirection.UNKNOWN)
{
this.energyStored = Math.max(Math.min(energy, this.getMaxEnergyStored()), 0);
}
}
@Override
public long getEnergy(ForgeDirection from)
{
if (this.canConnect(from) || from == ForgeDirection.UNKNOWN)
{
return this.energyStored;
}
return 0;
}
public void togglePowerMode()
{
}
public long getJoulesPerTick()
{
return this.JOULES_PER_TICK;
}
public long getJoulesPerSec()
{
return getJoulesPerTick() * 20;
}
public long getJoulesPerMin()
{
return getJoulesPerSec() * 60;
}
public long getJoulesPerHour()
{
return getJoulesPerMin() * 60;
}
public TileEnergyMachine setJoulesPerTick(long energy)
{
this.JOULES_PER_TICK = energy;
return this;
}
public TileEnergyMachine setJoulesPerSecound(long energy)
{
this.JOULES_PER_TICK = energy / 20;
return this;
}
public TileEnergyMachine setJoulesPerMin(long energy)
{
this.JOULES_PER_TICK = energy / 1200;
return this;
}
public TileEnergyMachine setJoulesPerHour(long energy)
{
this.JOULES_PER_TICK = energy / 72000;
return this;
}
/* ********************************************
* DATA/SAVE/LOAD
* *********************************************
*/
@Override
public void readFromNBT(NBTTagCompound nbt)
{
super.readFromNBT(nbt);
NBTBase tag = nbt.getTag("energyStored");
if (tag instanceof NBTTagFloat)
{
this.energyStored = (long) nbt.getFloat("energyStored") * 1000;
}
else if (tag instanceof NBTTagLong)
{
this.energyStored = nbt.getLong("energyStored");
}
runWithoutPower = !nbt.getBoolean("shouldPower");
this.functioning = nbt.getBoolean("isRunning");
}
@Override
public void writeToNBT(NBTTagCompound nbt)
{
super.writeToNBT(nbt);
nbt.setBoolean("shouldPower", !runWithoutPower);
nbt.setLong("energyStored", this.getEnergyStored());
nbt.setBoolean("isRunning", this.functioning);
}
}

View file

@ -3,242 +3,143 @@ package resonantinduction.core.prefab.tile;
import java.io.IOException;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.packet.Packet;
import net.minecraft.util.AxisAlignedBB;
import net.minecraftforge.common.Configuration;
import net.minecraftforge.common.ForgeDirection;
import resonantinduction.core.ResonantInduction;
import universalelectricity.api.vector.Vector3;
import calclavia.lib.content.IExtraInfo.IExtraTileEntityInfo;
import calclavia.lib.network.IPacketReceiverWithID;
import calclavia.lib.network.PacketHandler;
import calclavia.lib.prefab.tile.TileExternalInventory;
import calclavia.lib.utility.inventory.IExternalInventory;
import calclavia.lib.utility.inventory.IExternalInventoryBox;
import calclavia.lib.prefab.tile.TileElectrical;
import com.google.common.io.ByteArrayDataInput;
import cpw.mods.fml.common.network.PacketDispatcher;
import cpw.mods.fml.common.network.Player;
public abstract class TileMachine extends TileExternalInventory implements ISidedInventory, IExternalInventory, IExtraTileEntityInfo
/** Prefab for general machines
*
* @author Darkguardsman */
public class TileMachine extends TileElectrical implements IPacketReceiverWithID
{
/** Number of players with the machine's gui container open */
protected int playersUsingMachine = 0;
/** Is the machine functioning normally */
protected boolean functioning = false;
/** Prev state of function of last update */
protected boolean prevFunctioning = false;
/** Does the machine have a gui */
protected boolean hasGUI = false;
/** Does teh machine rotate in meta groups of four */
protected boolean rotateByMetaGroup = false;
/** Can the machine be temp disabled */
protected boolean canBeDisabled = false;
/** Is the machine enabled by the player */
protected boolean enabled = true;
/** Is the machine locked by the player */
protected boolean locked = false;
/** Is the machine functioning normally */
protected boolean functioning = false;
/** Prev state of function of last update */
protected boolean prevFunctioning = false;
/** Inventory manager used by this machine */
protected IExternalInventoryBox inventory;
protected long joulesPerTick = 0;
/** Default generic packet types used by all machines */
public static enum SimplePacketTypes
{
/** Normal packet data of any kind */
GENERIC("generic"),
/** Power updates */
RUNNING("isRunning"),
/** GUI display data update */
GUI("guiGeneral"),
/** Full tile read/write data from tile NBT */
NBT("nbtAll"), GUI_EVENT("clientGui"), GUI_COMMAND("clientCommand"),
TERMINAL_OUTPUT("serverTerminal");
public static final int IS_RUN_PACKET_ID = 0;
public static final int NBT_PACKET_ID = 1;
public static final int ENERGY_PACKET_ID = 2;
public String name;
@Override
public void updateEntity()
{
super.updateEntity();
if (!this.worldObj.isRemote)
{
this.prevFunctioning = this.functioning;
this.functioning = this.isFunctioning();
private SimplePacketTypes(String name)
{
this.name = name;
}
}
if (prevFunctioning != this.functioning)
{
this.sendRunningPacket();
}
if(this.isFunctioning())
{
this.consumePower(true);
}
}
}
@Override
public void updateEntity()
{
super.updateEntity();
if (!this.worldObj.isRemote)
{
this.prevFunctioning = this.functioning;
this.functioning = this.isFunctioning();
public boolean consumePower(boolean doConsume)
{
return this.consumePower(this.joulesPerTick, doConsume);
}
if (prevFunctioning != this.functioning)
{
this.sendPowerUpdate();
}
this.sendGUIPacket();
}
}
public boolean consumePower(long joules, boolean doConsume)
{
return this.energy.extractEnergy(joules, doConsume) >= joules;
}
/** Can this tile function, or run threw normal processes */
public boolean canFunction()
{
return this.enabled;
}
/** Can this tile function, or run threw normal processes */
public boolean canFunction()
{
return this.consumePower(false);
}
public boolean isFunctioning()
{
if (this.worldObj.isRemote)
{
return this.functioning;
}
else
{
return this.canFunction();
}
}
/** Called too see if the machine is functioning, server side it redirects to canFunction */
public boolean isFunctioning()
{
if (this.worldObj.isRemote)
{
return this.functioning;
}
else
{
return this.canFunction();
}
}
public void doRunningDebug()
{
System.out.println("\n CanRun: " + this.canFunction());
System.out.println(" RedPower: " + this.worldObj.isBlockIndirectlyGettingPowered(xCoord, yCoord, zCoord));
System.out.println(" IsRunning: " + this.functioning);
}
@Override
public boolean onReceivePacket(int id, ByteArrayDataInput data, EntityPlayer player, Object... extra)
{
try
{
if (this.worldObj.isRemote)
{
if (id == IS_RUN_PACKET_ID)
{
this.functioning = data.readBoolean();
return true;
}
if (id == NBT_PACKET_ID)
{
this.readFromNBT(PacketHandler.readNBTTagCompound(data));
return true;
}
if (id == ENERGY_PACKET_ID)
{
this.energy.readFromNBT(PacketHandler.readNBTTagCompound(data));
return true;
}
}
}
catch (IOException e)
{
e.printStackTrace();
}
return false;
}
/** Called every tick while this tile entity is disabled. */
protected void whileDisable()
{
/** Sends the tileEntity save data to the client */
public void sendNBTPacket()
{
if (!this.worldObj.isRemote)
{
NBTTagCompound tag = new NBTTagCompound();
this.writeToNBT(tag);
PacketHandler.sendPacketToClients(ResonantInduction.PACKET_TILE.getPacket(this, NBT_PACKET_ID, this, tag), worldObj, new Vector3(this), 64);
}
}
}
/** Sends a simple true/false am running power update */
public void sendRunningPacket()
{
if (!this.worldObj.isRemote)
{
PacketHandler.sendPacketToClients(ResonantInduction.PACKET_TILE.getPacket(this, IS_RUN_PACKET_ID, this, this.functioning), worldObj, new Vector3(this), 64);
}
}
public ForgeDirection getDirection()
{
if (this.rotateByMetaGroup)
{
switch (this.worldObj.getBlockMetadata(xCoord, yCoord, zCoord) % 4)
{
case 0:
return ForgeDirection.NORTH;
case 1:
return ForgeDirection.SOUTH;
case 2:
return ForgeDirection.SOUTH;
default:
return ForgeDirection.WEST;
}
}
return ForgeDirection.UNKNOWN;
}
public void setDirection(ForgeDirection direction)
{
if (this.rotateByMetaGroup)
{
switch (direction)
{
case NORTH:
this.worldObj.setBlockMetadataWithNotify(xCoord, yCoord, zCoord, this.worldObj.getBlockMetadata(xCoord, yCoord, zCoord) / 4, 3);
case WEST:
this.worldObj.setBlockMetadataWithNotify(xCoord, yCoord, zCoord, (this.worldObj.getBlockMetadata(xCoord, yCoord, zCoord) / 4) + 1, 3);
case SOUTH:
this.worldObj.setBlockMetadataWithNotify(xCoord, yCoord, zCoord, (this.worldObj.getBlockMetadata(xCoord, yCoord, zCoord) / 4) + 2, 3);
default:
this.worldObj.setBlockMetadataWithNotify(xCoord, yCoord, zCoord, (this.worldObj.getBlockMetadata(xCoord, yCoord, zCoord) / 4) + 3, 3);
}
}
}
public boolean simplePacket(String id, ByteArrayDataInput dis, Player player)
{
try
{
if (this.worldObj.isRemote)
{
if (id.equalsIgnoreCase(SimplePacketTypes.RUNNING.name))
{
this.functioning = dis.readBoolean();
return true;
}
if (id.equalsIgnoreCase(SimplePacketTypes.NBT.name))
{
this.readFromNBT(PacketHandler.readNBTTagCompound(dis));
return true;
}
}
}
catch (IOException e)
{
e.printStackTrace();
}
return false;
}
/** Sends the tileEntity save data to the client */
public void sendNBTPacket()
{
if (!this.worldObj.isRemote)
{
NBTTagCompound tag = new NBTTagCompound();
this.writeToNBT(tag);
PacketHandler.sendPacketToClients(ResonantInduction.PACKET_TILE.getPacket(this, SimplePacketTypes.NBT.name, this, tag), worldObj, new Vector3(this), 64);
}
}
/** Sends a simple true/false am running power update */
public void sendPowerUpdate()
{
if (!this.worldObj.isRemote)
{
PacketHandler.sendPacketToClients(ResonantInduction.PACKET_TILE.getPacket(this, SimplePacketTypes.RUNNING.name, this, this.functioning), worldObj, new Vector3(this), 64);
}
}
/** Sends a gui packet only to the given player */
public Packet getGUIPacket()
{
return null;
}
public void sendGUIPacket()
{
Packet packet = this.getGUIPacket();
if (this.hasGUI && this.getContainer() != null && packet != null)
{
this.playersUsingMachine = 0;
for (Object entity : this.worldObj.getEntitiesWithinAABB(EntityPlayer.class, AxisAlignedBB.getBoundingBox(xCoord, yCoord, zCoord, xCoord + 1, yCoord + 1, zCoord + 1).expand(10, 10, 10)))
{
if (entity instanceof EntityPlayer && ((EntityPlayer) entity).openContainer != null)
{
if (((EntityPlayer) entity).openContainer.getClass().isAssignableFrom(this.getContainer()))
{
this.playersUsingMachine += 1;
PacketDispatcher.sendPacketToPlayer(packet, (Player) entity);
}
}
}
}
}
@Override
public Packet getDescriptionPacket()
{
return ResonantInduction.PACKET_TILE.getPacket(this, this.functioning);
}
@Override
public boolean hasExtraConfigs()
{
// TODO Auto-generated method stub
return false;
}
@Override
public void loadExtraConfigs(Configuration config)
{
// TODO Auto-generated method stub
}
public void sendPowerPacket()
{
if (!this.worldObj.isRemote)
{
PacketHandler.sendPacketToClients(ResonantInduction.PACKET_TILE.getPacket(this, ENERGY_PACKET_ID, this, this.energy.writeToNBT(new NBTTagCompound())), worldObj, new Vector3(this), 64);
}
}
@Override
public Packet getDescriptionPacket()
{
return ResonantInduction.PACKET_TILE.getPacket(this, this.functioning);
}
}

View file

@ -1,50 +0,0 @@
package resonantinduction.core.tilenetwork.prefab;
import resonantinduction.core.prefab.tile.TileAssembly;
import resonantinduction.core.tilenetwork.INetworkPart;
public class NetworkAssembly extends NetworkSharedPower
{
private long networkPartEnergyRequest = 0;
private long lastUpdateTime = 0;
static
{
NetworkUpdateHandler.registerNetworkClass("AssemblyNet", NetworkAssembly.class);
}
public NetworkAssembly()
{
super();
}
public NetworkAssembly(INetworkPart... parts)
{
super(parts);
}
/** Gets the demand of all parts of the network including network parts */
public float getNetworkDemand()
{
if (System.currentTimeMillis() - this.lastUpdateTime > 100)
{
this.networkPartEnergyRequest = 0;
this.lastUpdateTime = System.currentTimeMillis();
for (INetworkPart part : this.getMembers())
{
if (part instanceof TileAssembly)
{
networkPartEnergyRequest += ((TileAssembly) part).getWattLoad();
networkPartEnergyRequest += ((TileAssembly) part).getExtraLoad();
}
}
}
return networkPartEnergyRequest;
}
@Override
public boolean isValidMember(INetworkPart part)
{
return super.isValidMember(part) && part instanceof TileAssembly;
}
}

View file

@ -11,7 +11,7 @@ import net.minecraft.world.World;
import net.minecraftforge.common.ForgeDirection;
import resonantinduction.api.IArmbot;
import resonantinduction.core.ResonantInduction;
import resonantinduction.core.prefab.tile.TileAssembly;
import resonantinduction.core.prefab.tile.TileMachine;
import resonantinduction.electrical.Electrical;
import resonantinduction.electrical.armbot.task.TaskDrop;
import resonantinduction.electrical.armbot.task.TaskGOTO;
@ -37,505 +37,463 @@ import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.network.Player;
import cpw.mods.fml.relauncher.Side;
public class TileArmbot extends TileAssembly implements IMultiBlock, IArmbot, IBlockActivate
public class TileArmbot extends TileMachine implements IMultiBlock, IArmbot
{
protected int ROTATION_SPEED = 6;
protected int ROTATION_SPEED = 6;
/** The rotation of the arms. In Degrees. */
protected int targetPitch = 0, targetYaw = 0;
protected int actualPitch = 0, actualYaw = 0;
/** The rotation of the arms. In Degrees. */
protected int targetPitch = 0, targetYaw = 0;
protected int actualPitch = 0, actualYaw = 0;
protected boolean spawnEntity = false;
protected boolean spawnEntity = false;
protected String displayText = "";
protected String displayText = "";
/** An entity that the Armbot is grabbed onto. Entity Items are held separately. */
protected Object grabbedObject = null;
/** Helper class that does all the logic for the armbot's program */
protected ProgramHelper programHelper;
/** Cached location of the armbot to feed to program tasks */
protected Pair<World, Vector3> location;
/** Var used by the armbot renderer */
public EntityItem renderEntityItem;
/** An entity that the Armbot is grabbed onto. Entity Items are held separately. */
protected Object grabbedObject = null;
/** Helper class that does all the logic for the armbot's program */
protected ProgramHelper programHelper;
/** Cached location of the armbot to feed to program tasks */
protected Pair<World, Vector3> location;
/** Var used by the armbot renderer */
public EntityItem renderEntityItem;
public static final int ARMBOT_PACKET_ID = 3;
public static final int ROTATION_PACKET_ID = 4;
public TileArmbot()
{
super(20);
programHelper = new ProgramHelper(this).setMemoryLimit(20);
Program program = new Program();
program.setTaskAt(0, 0, new TaskDrop());
program.setTaskAt(0, 1, new TaskRotateTo(180, 0));
program.setTaskAt(0, 2, new TaskGrabItem());
program.setTaskAt(0, 3, new TaskReturn());
program.setTaskAt(0, 4, new TaskGOTO(0, 0));
programHelper.setProgram(program);
}
public TileArmbot()
{
this.joulesPerTick = 20;
programHelper = new ProgramHelper(this).setMemoryLimit(20);
Program program = new Program();
program.setTaskAt(0, 0, new TaskDrop());
program.setTaskAt(0, 1, new TaskRotateTo(180, 0));
program.setTaskAt(0, 2, new TaskGrabItem());
program.setTaskAt(0, 3, new TaskReturn());
program.setTaskAt(0, 4, new TaskGOTO(0, 0));
programHelper.setProgram(program);
}
/************************************ Armbot logic update methods *************************************/
/************************************ Armbot logic update methods *************************************/
@Override
public void updateEntity()
{
super.updateEntity();
Vector3 handPosition = this.getHandPos();
if (this.location == null || !this.location.left().equals(this.worldObj) || this.xCoord != this.location.right().intX() || this.yCoord != this.location.right().intY() || this.zCoord != this.location.right().intZ())
{
this.location = new Pair<World, Vector3>(this.worldObj, new Vector3(this));
}
if (this.grabbedObject instanceof Entity)
{
if (this.spawnEntity)
{
this.worldObj.spawnEntityInWorld((Entity) this.grabbedObject);
this.spawnEntity = false;
}
((Entity) this.grabbedObject).setPosition(handPosition.x, handPosition.y, handPosition.z);
((Entity) this.grabbedObject).motionX = 0;
((Entity) this.grabbedObject).motionY = 0;
((Entity) this.grabbedObject).motionZ = 0;
@Override
public void updateEntity()
{
super.updateEntity();
Vector3 handPosition = this.getHandPos();
if (this.location == null || !this.location.left().equals(this.worldObj) || this.xCoord != this.location.right().intX() || this.yCoord != this.location.right().intY() || this.zCoord != this.location.right().intZ())
{
this.location = new Pair<World, Vector3>(this.worldObj, new Vector3(this));
}
if (this.grabbedObject instanceof Entity)
{
if (this.spawnEntity)
{
this.worldObj.spawnEntityInWorld((Entity) this.grabbedObject);
this.spawnEntity = false;
}
((Entity) this.grabbedObject).setPosition(handPosition.x, handPosition.y, handPosition.z);
((Entity) this.grabbedObject).motionX = 0;
((Entity) this.grabbedObject).motionY = 0;
((Entity) this.grabbedObject).motionZ = 0;
if (this.grabbedObject instanceof EntityItem)
{
((EntityItem) this.grabbedObject).delayBeforeCanPickup = 20;
((EntityItem) this.grabbedObject).age = 0;
}
}
if (this.grabbedObject instanceof EntityItem)
{
((EntityItem) this.grabbedObject).delayBeforeCanPickup = 20;
((EntityItem) this.grabbedObject).age = 0;
}
}
if (this.isFunctioning())
{
float preYaw = this.targetYaw, prePitch = this.targetPitch;
if (!this.worldObj.isRemote && this.ticks % 5 == 0)
{
this.programHelper.onUpdate(this.worldObj, new Vector3(this));
if (this.targetYaw != preYaw || this.targetPitch != prePitch)
{
PacketHandler.sendPacketToClients(this.getDescriptionPacket(), worldObj, new Vector3(this).translate(new Vector3(.5f, 1f, .5f)), 64);
}
}
this.updateRotation();
}
}
if (this.isFunctioning())
{
float preYaw = this.targetYaw, prePitch = this.targetPitch;
if (!this.worldObj.isRemote && this.ticks % 5 == 0)
{
this.programHelper.onUpdate(this.worldObj, new Vector3(this));
if (this.targetYaw != preYaw || this.targetPitch != prePitch)
{
PacketHandler.sendPacketToClients(this.getDescriptionPacket(), worldObj, new Vector3(this).translate(new Vector3(.5f, 1f, .5f)), 64);
}
}
this.updateRotation();
}
}
public void updateRotation()
{
// Clamp target angles
this.targetYaw = (int) MathUtility.clampAngleTo360(this.targetYaw);
if (this.targetPitch < 0)
this.targetPitch = 0;
if (this.targetPitch > 60)
this.targetPitch = 60;
// Handle change in yaw rotation
if (Math.abs(this.actualYaw - this.targetYaw) > 1)
{
float speedYaw;
if (this.actualYaw > this.targetYaw)
{
if (Math.abs(this.actualYaw - this.targetYaw) >= 180)
{
speedYaw = this.ROTATION_SPEED;
}
else
{
speedYaw = -this.ROTATION_SPEED;
}
}
else
{
if (Math.abs(this.actualYaw - this.targetYaw) >= 180)
{
speedYaw = -this.ROTATION_SPEED;
}
else
{
speedYaw = this.ROTATION_SPEED;
}
}
public void updateRotation()
{
// Clamp target angles
this.targetYaw = (int) MathUtility.clampAngleTo360(this.targetYaw);
if (this.targetPitch < 0)
this.targetPitch = 0;
if (this.targetPitch > 60)
this.targetPitch = 60;
// Handle change in yaw rotation
if (Math.abs(this.actualYaw - this.targetYaw) > 1)
{
float speedYaw;
if (this.actualYaw > this.targetYaw)
{
if (Math.abs(this.actualYaw - this.targetYaw) >= 180)
{
speedYaw = this.ROTATION_SPEED;
}
else
{
speedYaw = -this.ROTATION_SPEED;
}
}
else
{
if (Math.abs(this.actualYaw - this.targetYaw) >= 180)
{
speedYaw = -this.ROTATION_SPEED;
}
else
{
speedYaw = this.ROTATION_SPEED;
}
}
this.actualYaw += speedYaw;
this.actualYaw += speedYaw;
if (Math.abs(this.actualYaw - this.targetYaw) < this.ROTATION_SPEED)
{
this.actualYaw = this.targetYaw;
}
this.playRotationSound();
}
// Handle change in pitch rotation
if (Math.abs(this.actualPitch - this.targetPitch) > 1)
{
if (this.actualPitch > this.targetPitch)
{
this.actualPitch -= this.ROTATION_SPEED;
}
else
{
this.actualPitch += this.ROTATION_SPEED;
}
if (Math.abs(this.actualYaw - this.targetYaw) < this.ROTATION_SPEED)
{
this.actualYaw = this.targetYaw;
}
this.playRotationSound();
}
// Handle change in pitch rotation
if (Math.abs(this.actualPitch - this.targetPitch) > 1)
{
if (this.actualPitch > this.targetPitch)
{
this.actualPitch -= this.ROTATION_SPEED;
}
else
{
this.actualPitch += this.ROTATION_SPEED;
}
if (Math.abs(this.actualPitch - this.targetPitch) < this.ROTATION_SPEED)
{
this.actualPitch = this.targetPitch;
}
this.playRotationSound();
}
// Clamp actual angles angles
this.actualYaw = (int) MathUtility.clampAngleTo360(this.actualYaw);
if (this.actualPitch < 0)
this.actualPitch = 0;
if (this.actualPitch > 60)
this.actualPitch = 60;
}
if (Math.abs(this.actualPitch - this.targetPitch) < this.ROTATION_SPEED)
{
this.actualPitch = this.targetPitch;
}
this.playRotationSound();
}
// Clamp actual angles angles
this.actualYaw = (int) MathUtility.clampAngleTo360(this.actualYaw);
if (this.actualPitch < 0)
this.actualPitch = 0;
if (this.actualPitch > 60)
this.actualPitch = 60;
}
public void playRotationSound()
{
if (this.ticks % 5 == 0 && this.worldObj.isRemote)
{
this.worldObj.playSound(this.xCoord, this.yCoord, this.zCoord, "mods.assemblyline.conveyor", 2f, 2.5f, true);
}
}
public void playRotationSound()
{
if (this.ticks % 5 == 0 && this.worldObj.isRemote)
{
this.worldObj.playSound(this.xCoord, this.yCoord, this.zCoord, "mods.assemblyline.conveyor", 2f, 2.5f, true);
}
}
@Override
public String getInvName()
{
return LanguageUtility.getLocal("tile.armbot.name");
}
public String getCommandDisplayText()
{
return this.displayText;
}
public String getCommandDisplayText()
{
return this.displayText;
}
/************************************ Save and load code *************************************/
@Override
public boolean onActivated(EntityPlayer player)
{
ItemStack containingStack = this.getStackInSlot(0);
/** NBT Data */
@Override
public void readFromNBT(NBTTagCompound nbt)
{
super.readFromNBT(nbt);
if (containingStack != null)
{
if (FMLCommonHandler.instance().getEffectiveSide() == Side.SERVER)
{
EntityItem dropStack = new EntityItem(this.worldObj, player.posX, player.posY, player.posZ, containingStack);
dropStack.delayBeforeCanPickup = 0;
this.worldObj.spawnEntityInWorld(dropStack);
}
this.targetYaw = nbt.getInteger("armYaw");
this.targetPitch = nbt.getInteger("armPitch");
this.actualYaw = nbt.getInteger("armYawActual");
this.actualPitch = nbt.getInteger("armPitchActual");
this.setInventorySlotContents(0, null);
return true;
}
else
{
if (player.getCurrentEquippedItem() != null)
{
if (player.getCurrentEquippedItem().getItem() instanceof ItemDisk)
{
this.setInventorySlotContents(0, player.getCurrentEquippedItem());
player.inventory.setInventorySlotContents(player.inventory.currentItem, null);
return true;
}
}
}
if (nbt.hasKey("grabbedEntity"))
{
NBTTagCompound tag = nbt.getCompoundTag("grabbedEntity");
Entity entity = EntityList.createEntityFromNBT(tag, worldObj);
if (entity != null)
{
this.grabbedObject = entity;
this.spawnEntity = true;
}
}
else if (nbt.hasKey("grabbedItem"))
{
ItemStack stack = ItemStack.loadItemStackFromNBT(nbt.getCompoundTag("grabbedItem"));
if (stack != null)
{
this.grabbedObject = stack;
}
}
}
return false;
}
/** Writes a tile entity to NBT. */
@Override
public void writeToNBT(NBTTagCompound nbt)
{
super.writeToNBT(nbt);
/************************************ Save and load code *************************************/
nbt.setInteger("armYaw", this.targetYaw);
nbt.setInteger("armPitch", this.targetPitch);
nbt.setInteger("armYawActual", this.actualYaw);
nbt.setInteger("armPitchActual", this.actualPitch);
/** NBT Data */
@Override
public void readFromNBT(NBTTagCompound nbt)
{
super.readFromNBT(nbt);
if (this.grabbedObject instanceof Entity)
{
NBTTagCompound entityNBT = new NBTTagCompound();
((Entity) this.grabbedObject).writeToNBT(entityNBT);
((Entity) this.grabbedObject).writeToNBTOptional(entityNBT);
nbt.setCompoundTag("grabbedEntity", entityNBT);
}
else if (this.grabbedObject instanceof ItemStack)
{
nbt.setCompoundTag("grabbedItem", ((ItemStack) this.grabbedObject).writeToNBT(new NBTTagCompound()));
}
this.targetYaw = nbt.getInteger("armYaw");
this.targetPitch = nbt.getInteger("armPitch");
this.actualYaw = nbt.getInteger("armYawActual");
this.actualPitch = nbt.getInteger("armPitchActual");
}
if (nbt.hasKey("grabbedEntity"))
{
NBTTagCompound tag = nbt.getCompoundTag("grabbedEntity");
Entity entity = EntityList.createEntityFromNBT(tag, worldObj);
if (entity != null)
{
this.grabbedObject = entity;
this.spawnEntity = true;
}
}
else if (nbt.hasKey("grabbedItem"))
{
ItemStack stack = ItemStack.loadItemStackFromNBT(nbt.getCompoundTag("grabbedItem"));
if (stack != null)
{
this.grabbedObject = stack;
}
}
}
/************************************ Network Packet code *************************************/
/** Writes a tile entity to NBT. */
@Override
public void writeToNBT(NBTTagCompound nbt)
{
super.writeToNBT(nbt);
@Override
public Packet getDescriptionPacket()
{
return ResonantInduction.PACKET_TILE.getPacket(this, "armbot", this.functioning, this.targetYaw, this.targetPitch, this.actualYaw, this.actualPitch);
}
nbt.setInteger("armYaw", this.targetYaw);
nbt.setInteger("armPitch", this.targetPitch);
nbt.setInteger("armYawActual", this.actualYaw);
nbt.setInteger("armPitchActual", this.actualPitch);
public void sendGrabItemToClient()
{
if (this.grabbedObject instanceof ItemStack)
{
PacketHandler.sendPacketToClients(ResonantInduction.PACKET_TILE.getPacket(this, "armbotItem", true, ((ItemStack) this.grabbedObject).writeToNBT(new NBTTagCompound())), worldObj, new Vector3(this), 64);
}
else
{
PacketHandler.sendPacketToClients(ResonantInduction.PACKET_TILE.getPacket(this, "armbotItem", false), worldObj, new Vector3(this), 64);
}
}
if (this.grabbedObject instanceof Entity)
{
NBTTagCompound entityNBT = new NBTTagCompound();
((Entity) this.grabbedObject).writeToNBT(entityNBT);
((Entity) this.grabbedObject).writeToNBTOptional(entityNBT);
nbt.setCompoundTag("grabbedEntity", entityNBT);
}
else if (this.grabbedObject instanceof ItemStack)
{
nbt.setCompoundTag("grabbedItem", ((ItemStack) this.grabbedObject).writeToNBT(new NBTTagCompound()));
}
@Override
public boolean onReceivePacket(int id, ByteArrayDataInput data, EntityPlayer player, Object... extra)
{
try
{
if (this.worldObj.isRemote && !super.onReceivePacket(id, data, player, extra))
{
if (id == ARMBOT_PACKET_ID)
{
this.functioning = data.readBoolean();
this.targetYaw = data.readInt();
this.targetPitch = data.readInt();
this.actualYaw = data.readInt();
this.actualPitch = data.readInt();
return true;
}
else if (id == ROTATION_PACKET_ID)
{
if (data.readBoolean())
{
this.grabbedObject = ItemStack.loadItemStackFromNBT(PacketHandler.readNBTTagCompound(data));
}
else
{
this.grabbedObject = null;
}
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
return false;
}
}
/************************************ Multi Block code *************************************/
/************************************ Network Packet code *************************************/
@Override
public Vector3[] getMultiBlockVectors()
{
return new Vector3[] { new Vector3(this).translate(0, 1, 0) };
}
@Override
public Packet getDescriptionPacket()
{
return ResonantInduction.PACKET_TILE.getPacket(this, "armbot", this.functioning, this.targetYaw, this.targetPitch, this.actualYaw, this.actualPitch);
}
/************************************ Armbot API methods *************************************/
@Override
public Object getHeldObject()
{
return this.grabbedObject;
}
public void sendGrabItemToClient()
{
if (this.grabbedObject instanceof ItemStack)
{
PacketHandler.sendPacketToClients(ResonantInduction.PACKET_TILE.getPacket(this, "armbotItem", true, ((ItemStack) this.grabbedObject).writeToNBT(new NBTTagCompound())), worldObj, new Vector3(this), 64);
}
else
{
PacketHandler.sendPacketToClients(ResonantInduction.PACKET_TILE.getPacket(this, "armbotItem", false), worldObj, new Vector3(this), 64);
}
}
@Override
public boolean grabObject(Object entity)
{
if (this.getHeldObject() == null)
{
if (entity instanceof ItemStack)
{
this.grabbedObject = entity;
this.sendGrabItemToClient();
return true;
}
else if (entity instanceof EntityItem)
{
this.grabbedObject = ((EntityItem) entity).getEntityItem();
((EntityItem) entity).setDead();
this.sendGrabItemToClient();
return true;
}
else if (entity instanceof Entity)
{
this.grabbedObject = entity;
return true;
}
}
return false;
}
@Override
public boolean simplePacket(String id, ByteArrayDataInput dis, Player player)
{
try
{
if (this.worldObj.isRemote && !super.simplePacket(id, dis, player))
{
if (id.equalsIgnoreCase("armbot"))
{
this.functioning = dis.readBoolean();
this.targetYaw = dis.readInt();
this.targetPitch = dis.readInt();
this.actualYaw = dis.readInt();
this.actualPitch = dis.readInt();
return true;
}
else if (id.equalsIgnoreCase("armbotItem"))
{
if (dis.readBoolean())
{
this.grabbedObject = ItemStack.loadItemStackFromNBT(PacketHandler.readNBTTagCompound(dis));
}
else
{
this.grabbedObject = null;
}
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
return false;
}
@Override
public boolean dropHeldObject()
{
if (this.getHeldObject() != null)
{
if (this.getHeldObject() instanceof ItemStack)
{
Vector3 handPosition = this.getHandPos();
WorldUtility.dropItemStack(worldObj, handPosition, (ItemStack) this.getHeldObject(), false);
}
this.grabbedObject = null;
this.sendGrabItemToClient();
return true;
}
return false;
}
/************************************ Multi Block code *************************************/
@Override
public Vector3 getHandPos()
{
Vector3 position = new Vector3(this);
position.translate(0.5);
position.translate(this.getDeltaHandPosition());
return position;
}
@Override
public Vector3[] getMultiBlockVectors()
{
return new Vector3[] { new Vector3(this).translate(0, 1, 0) };
}
public Vector3 getDeltaHandPosition()
{
// The distance of the position relative to the main position.
double distance = 1f;
Vector3 delta = new Vector3();
// The delta Y of the hand.
delta.y = Math.sin(Math.toRadians(this.actualPitch)) * distance * 2;
// The horizontal delta of the hand.
double dH = Math.cos(Math.toRadians(this.actualPitch)) * distance;
// The delta X and Z.
delta.x = Math.sin(Math.toRadians(-this.actualYaw)) * dH;
delta.z = Math.cos(Math.toRadians(-this.actualYaw)) * dH;
return delta;
}
/************************************ Armbot API methods *************************************/
@Override
public Object getHeldObject()
{
return this.grabbedObject;
}
@Override
public Vector2 getRotation()
{
return new Vector2(this.actualYaw, this.actualPitch);
}
@Override
public boolean grabObject(Object entity)
{
if (this.getHeldObject() == null)
{
if (entity instanceof ItemStack)
{
this.grabbedObject = entity;
this.sendGrabItemToClient();
return true;
}
else if (entity instanceof EntityItem)
{
this.grabbedObject = ((EntityItem) entity).getEntityItem();
((EntityItem) entity).setDead();
this.sendGrabItemToClient();
return true;
}
else if (entity instanceof Entity)
{
this.grabbedObject = entity;
return true;
}
}
return false;
}
@Override
public void setRotation(int yaw, int pitch)
{
if (!this.worldObj.isRemote)
{
this.actualYaw = yaw;
this.actualPitch = pitch;
}
}
@Override
public boolean dropHeldObject()
{
if (this.getHeldObject() != null)
{
if (this.getHeldObject() instanceof ItemStack)
{
Vector3 handPosition = this.getHandPos();
WorldUtility.dropItemStack(worldObj, handPosition, (ItemStack) this.getHeldObject(), false);
}
this.grabbedObject = null;
this.sendGrabItemToClient();
return true;
}
return false;
}
@Override
public boolean moveArmTo(int yaw, int pitch)
{
if (!this.worldObj.isRemote)
{
this.targetYaw = yaw;
this.targetPitch = pitch;
return true;
}
return false;
}
@Override
public boolean isItemValidForSlot(int i, ItemStack itemstack)
{
return itemstack != null && itemstack.itemID == Electrical.itemDisk.itemID;
}
@Override
public boolean moveTo(ForgeDirection direction)
{
if (direction == ForgeDirection.SOUTH)
{
this.targetYaw = 0;
return true;
}
else if (direction == ForgeDirection.EAST)
{
this.targetYaw = 90;
return true;
}
else if (direction == ForgeDirection.NORTH)
{
@Override
public Vector3 getHandPos()
{
Vector3 position = new Vector3(this);
position.translate(0.5);
position.translate(this.getDeltaHandPosition());
return position;
}
this.targetYaw = 180;
return true;
}
else if (direction == ForgeDirection.WEST)
{
this.targetYaw = 270;
return true;
}
return false;
}
public Vector3 getDeltaHandPosition()
{
// The distance of the position relative to the main position.
double distance = 1f;
Vector3 delta = new Vector3();
// The delta Y of the hand.
delta.y = Math.sin(Math.toRadians(this.actualPitch)) * distance * 2;
// The horizontal delta of the hand.
double dH = Math.cos(Math.toRadians(this.actualPitch)) * distance;
// The delta X and Z.
delta.x = Math.sin(Math.toRadians(-this.actualYaw)) * dH;
delta.z = Math.cos(Math.toRadians(-this.actualYaw)) * dH;
return delta;
}
@Override
public IProgram getCurrentProgram()
{
if (this.programHelper == null)
{
this.programHelper = new ProgramHelper(this);
}
if (this.programHelper != null)
{
return this.programHelper.getProgram();
}
return null;
}
@Override
public Vector2 getRotation()
{
return new Vector2(this.actualYaw, this.actualPitch);
}
@Override
public void setCurrentProgram(IProgram program)
{
if (this.programHelper == null)
{
this.programHelper = new ProgramHelper(this);
}
if (this.programHelper != null)
{
this.programHelper.setProgram(program);
}
}
@Override
public void setRotation(int yaw, int pitch)
{
if (!this.worldObj.isRemote)
{
this.actualYaw = yaw;
this.actualPitch = pitch;
}
}
@Override
public boolean clear(Object object)
{
if (this.grabbedObject != null && this.grabbedObject.equals(object))
{
this.grabbedObject = null;
return true;
}
return false;
}
@Override
public boolean moveArmTo(int yaw, int pitch)
{
if (!this.worldObj.isRemote)
{
this.targetYaw = yaw;
this.targetPitch = pitch;
return true;
}
return false;
}
@Override
public boolean moveTo(ForgeDirection direction)
{
if (direction == ForgeDirection.SOUTH)
{
this.targetYaw = 0;
return true;
}
else if (direction == ForgeDirection.EAST)
{
this.targetYaw = 90;
return true;
}
else if (direction == ForgeDirection.NORTH)
{
this.targetYaw = 180;
return true;
}
else if (direction == ForgeDirection.WEST)
{
this.targetYaw = 270;
return true;
}
return false;
}
@Override
public IProgram getCurrentProgram()
{
if (this.programHelper == null)
{
this.programHelper = new ProgramHelper(this);
}
if (this.programHelper != null)
{
return this.programHelper.getProgram();
}
return null;
}
@Override
public void setCurrentProgram(IProgram program)
{
if (this.programHelper == null)
{
this.programHelper = new ProgramHelper(this);
}
if (this.programHelper != null)
{
this.programHelper.setProgram(program);
}
}
@Override
public boolean clear(Object object)
{
if (this.grabbedObject != null && this.grabbedObject.equals(object))
{
this.grabbedObject = null;
return true;
}
return false;
}
@Override
public Pair<World, Vector3> getLocation()
{
return this.location;
}
@Override
public Pair<World, Vector3> getLocation()
{
return this.location;
}
}

View file

@ -12,222 +12,232 @@ import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.IFluidHandler;
import resonantinduction.api.IReadOut;
import resonantinduction.api.fluid.IDrain;
import resonantinduction.core.prefab.tile.TileEnergyMachine;
import resonantinduction.core.prefab.tile.TileMachine;
import resonantinduction.core.tilenetwork.ITileConnector;
import universalelectricity.api.energy.EnergyStorageHandler;
import universalelectricity.api.vector.Vector3;
import calclavia.lib.prefab.tile.IRotatable;
import calclavia.lib.utility.FluidUtility;
import com.builtbroken.common.Pair;
public class TilePump extends TileEnergyMachine implements IReadOut, ITileConnector
public class TilePump extends TileMachine implements IReadOut, ITileConnector, IRotatable
{
private int currentWorldEdits, MAX_WORLD_EDITS_PER_PROCESS;
private int currentWorldEdits, MAX_WORLD_EDITS_PER_PROCESS;
public long ENERGY_PER_DRAIN = 50;
public long ENERGY_PER_DRAIN = 50;
private LiquidPathFinder pathLiquid;
private Vector3 lastDrainOrigin;
private LiquidPathFinder pathLiquid;
private Vector3 lastDrainOrigin;
public int rotation = 0;
public int rotation = 0;
public TilePump()
{
this(10, 50, 5);
}
public TilePump()
{
this(10, 50, 5);
}
/**
* @param wattTick - cost in watts per tick to run the tile
* @param wattDrain - cost in watts to drain or fill one block
* @param maxEdits - max world edits per update (1/2 second)
*/
public TilePump(long wattTick, long wattDrain, int maxEdits)
{
// Power calculation for max power (worldEdits * watts per edit) + (watts per tick * one
// second)
super(wattTick, (maxEdits * wattDrain) + (wattTick * 20));
this.MAX_WORLD_EDITS_PER_PROCESS = maxEdits;
this.ENERGY_PER_DRAIN = wattDrain;
}
/** @param wattTick - cost in watts per tick to run the tile
* @param wattDrain - cost in watts to drain or fill one block
* @param maxEdits - max world edits per update (1/2 second) */
public TilePump(long wattTick, long wattDrain, int maxEdits)
{
this.energy = new EnergyStorageHandler((maxEdits * wattDrain) + (wattTick * 20));
this.MAX_WORLD_EDITS_PER_PROCESS = maxEdits;
this.ENERGY_PER_DRAIN = wattDrain;
}
/** Liquid path finder used by this tile. Retrieve the path finder if using IDrain block */
public LiquidPathFinder getLiquidFinder()
{
if (pathLiquid == null)
{
pathLiquid = new LiquidPathFinder(this.worldObj, 100, 20);
}
return pathLiquid;
}
/** Liquid path finder used by this tile. Retrieve the path finder if using IDrain block */
public LiquidPathFinder getLiquidFinder()
{
if (pathLiquid == null)
{
pathLiquid = new LiquidPathFinder(this.worldObj, 100, 20);
}
return pathLiquid;
}
@Override
public void updateEntity()
{
super.updateEntity();
@Override
public void updateEntity()
{
super.updateEntity();
if (this.ticks % 10 == 0)
{
this.currentWorldEdits = 0;
if (this.ticks % 10 == 0)
{
this.currentWorldEdits = 0;
if (this.isFunctioning())
{
this.rotation = Math.max(Math.min(this.rotation + 1, 7), 0);
if (this.isFunctioning())
{
this.rotation = Math.max(Math.min(this.rotation + 1, 7), 0);
if (!this.worldObj.isRemote)
{
Pair<World, Vector3> pair = this.getDrainOrigin();
if (pair != null && pair.left() != null && pair.right() != null)
{
this.drainAroundArea(pair.left(), pair.right(), 3);
}
}
}
if (!this.worldObj.isRemote)
{
Pair<World, Vector3> pair = this.getDrainOrigin();
if (pair != null && pair.left() != null && pair.right() != null)
{
this.drainAroundArea(pair.left(), pair.right(), 3);
}
}
}
}
}
}
}
/** Gets the origin the path finder starts on */
public Pair<World, Vector3> getDrainOrigin()
{
// TODO change this to lower by the amount of air between the pump and bottom
return new Pair<World, Vector3>(this.worldObj, new Vector3(this).modifyPositionFromSide(ForgeDirection.DOWN));
}
/** Gets the origin the path finder starts on */
public Pair<World, Vector3> getDrainOrigin()
{
// TODO change this to lower by the amount of air between the pump and bottom
return new Pair<World, Vector3>(this.worldObj, new Vector3(this).modifyPositionFromSide(ForgeDirection.DOWN));
}
/**
* Drains an area starting at the given location
*
* @param world - world to drain in, most cases will be the TileEntities world
* @param loc - origin to start the path finder with. If this is an instance of IDrain this
* method will act different
*/
public void drainAroundArea(World world, Vector3 vec, int update)
{
Vector3 origin = vec.clone();
if (origin == null)
{
return;
}
/** Drains an area starting at the given location
*
* @param world - world to drain in, most cases will be the TileEntities world
* @param loc - origin to start the path finder with. If this is an instance of IDrain this
* method will act different */
public void drainAroundArea(World world, Vector3 vec, int update)
{
Vector3 origin = vec.clone();
if (origin == null)
{
return;
}
/* Update last drain origin to prevent failed path finding */
if (this.lastDrainOrigin == null || !this.lastDrainOrigin.equals(origin))
{
this.lastDrainOrigin = origin.clone();
this.getLiquidFinder().reset();
}
/* Update last drain origin to prevent failed path finding */
if (this.lastDrainOrigin == null || !this.lastDrainOrigin.equals(origin))
{
this.lastDrainOrigin = origin.clone();
this.getLiquidFinder().reset();
}
TileEntity drain = vec.clone().getTileEntity(world);
TileEntity entity = null;
TileEntity drain = vec.clone().getTileEntity(world);
TileEntity entity = null;
Set<Vector3> drainList = null;
if (drain instanceof IDrain)
{
if (!((IDrain) drain).canDrain(((IDrain) drain).getDirection()))
{
return;
}
origin = vec.modifyPositionFromSide(((IDrain) drain).getDirection());
entity = origin.getTileEntity(world);
if (entity instanceof IFluidHandler)
{
FluidStack draStack = ((IFluidHandler) entity).drain(ForgeDirection.UP, MAX_WORLD_EDITS_PER_PROCESS * FluidContainerRegistry.BUCKET_VOLUME, false);
Set<Vector3> drainList = null;
if (drain instanceof IDrain)
{
if (!((IDrain) drain).canDrain(((IDrain) drain).getDirection()))
{
return;
}
origin = vec.modifyPositionFromSide(((IDrain) drain).getDirection());
entity = origin.getTileEntity(world);
if (entity instanceof IFluidHandler)
{
FluidStack draStack = ((IFluidHandler) entity).drain(ForgeDirection.UP, MAX_WORLD_EDITS_PER_PROCESS * FluidContainerRegistry.BUCKET_VOLUME, false);
if (draStack != null && FluidUtility.fillTanksAllSides(worldObj, new Vector3(this), draStack, false, ForgeDirection.DOWN) > 0)
{
((IFluidHandler) entity).drain(ForgeDirection.UP, FluidUtility.fillTanksAllSides(worldObj, new Vector3(this), draStack, true, ForgeDirection.DOWN), true);
if (draStack != null && FluidUtility.fillTanksAllSides(worldObj, new Vector3(this), draStack, false, ForgeDirection.DOWN) > 0)
{
((IFluidHandler) entity).drain(ForgeDirection.UP, FluidUtility.fillTanksAllSides(worldObj, new Vector3(this), draStack, true, ForgeDirection.DOWN), true);
}
return;// TODO check why return is here
}
else
{
drainList = ((IDrain) drain).getFluidList();
}
}
}
return;// TODO check why return is here
}
else
{
drainList = ((IDrain) drain).getFluidList();
}
}
if (drainList == null)
{
if (this.getLiquidFinder().results.size() < MAX_WORLD_EDITS_PER_PROCESS + 10)
{
this.getLiquidFinder().setWorld(world).refresh().start(origin, MAX_WORLD_EDITS_PER_PROCESS, false);
}
drainList = this.getLiquidFinder().refresh().results;
}
if (drainList == null)
{
if (this.getLiquidFinder().results.size() < MAX_WORLD_EDITS_PER_PROCESS + 10)
{
this.getLiquidFinder().setWorld(world).refresh().start(origin, MAX_WORLD_EDITS_PER_PROCESS, false);
}
drainList = this.getLiquidFinder().refresh().results;
}
if (entity == null && drainList != null && drainList.size() > 0)
{
// System.out.println("StartPump>>DrainArea>>Targets>" +
// this.getLiquidFinder().results.size());
if (entity == null && drainList != null && drainList.size() > 0)
{
// System.out.println("StartPump>>DrainArea>>Targets>" +
// this.getLiquidFinder().results.size());
Iterator<Vector3> fluidList = drainList.iterator();
Iterator<Vector3> fluidList = drainList.iterator();
while (fluidList.hasNext() && this.consumePower(ENERGY_PER_DRAIN, false))
{
if (this.currentWorldEdits >= MAX_WORLD_EDITS_PER_PROCESS)
{
break;
}
while (fluidList.hasNext() && this.consumePower(ENERGY_PER_DRAIN, false))
{
if (this.currentWorldEdits >= MAX_WORLD_EDITS_PER_PROCESS)
{
break;
}
Vector3 drainLocation = fluidList.next();
FluidStack drainStack = FluidUtility.drainBlock(world, drainLocation, false, 3);
// System.out.println("StartPump>>DrainArea>>Draining>>NextFluidBlock>" +
// (drainStack == null ? "Null" : drainStack.amount + "mb of " +
// drainStack.getFluid().getName()));
Vector3 drainLocation = fluidList.next();
FluidStack drainStack = FluidUtility.drainBlock(world, drainLocation, false, 3);
// System.out.println("StartPump>>DrainArea>>Draining>>NextFluidBlock>" +
// (drainStack == null ? "Null" : drainStack.amount + "mb of " +
// drainStack.getFluid().getName()));
// int fillV = FluidHelper.fillTanksAllSides(worldObj, new Vector3(this),
// drainStack, false, ForgeDirection.DOWN);
// System.out.println("StartPump>>DrainArea>>Draining>>NextFluidBlock>Filled>" +
// fillV + "mb");
// int fillV = FluidHelper.fillTanksAllSides(worldObj, new Vector3(this),
// drainStack, false, ForgeDirection.DOWN);
// System.out.println("StartPump>>DrainArea>>Draining>>NextFluidBlock>Filled>" +
// fillV + "mb");
if (drainStack != null && this.fill(drainStack, false) >= drainStack.amount && this.consumePower(ENERGY_PER_DRAIN, true))
{
// System.out.println("StartPump>>DrainArea>>Draining>>Fluid>" +
// drainLocation.toString());
/* REMOVE BLOCK */
FluidUtility.drainBlock(this.worldObj, drainLocation, true, update);
this.fill(drainStack, true);
this.currentWorldEdits++;
fluidList.remove();
if (drainStack != null && this.fill(drainStack, false) >= drainStack.amount && this.consumePower(ENERGY_PER_DRAIN, true))
{
// System.out.println("StartPump>>DrainArea>>Draining>>Fluid>" +
// drainLocation.toString());
/* REMOVE BLOCK */
FluidUtility.drainBlock(this.worldObj, drainLocation, true, update);
this.fill(drainStack, true);
this.currentWorldEdits++;
fluidList.remove();
if (drain instanceof IDrain)
{
((IDrain) drain).onUse(drainLocation);
}
}
}
}
}
if (drain instanceof IDrain)
{
((IDrain) drain).onUse(drainLocation);
}
}
}
}
}
public int fill(FluidStack stack, boolean doFill)
{
return FluidUtility.fillTanksAllSides(worldObj, new Vector3(this), stack, doFill, ForgeDirection.DOWN);
}
public int fill(FluidStack stack, boolean doFill)
{
return FluidUtility.fillTanksAllSides(worldObj, new Vector3(this), stack, doFill, ForgeDirection.DOWN);
}
@Override
public boolean canFunction()
{
return super.canFunction() && worldObj.isBlockIndirectlyGettingPowered(xCoord, yCoord, zCoord);
}
@Override
public boolean canFunction()
{
return super.canFunction() && worldObj.isBlockIndirectlyGettingPowered(xCoord, yCoord, zCoord);
}
@Override
public String getMeterReading(EntityPlayer user, ForgeDirection side, EnumTools tool)
{
if (tool == EnumTools.PIPE_GUAGE)
{
return "Source Blocks: " + this.getLiquidFinder().results.size();
}
return null;
}
@Override
public String getMeterReading(EntityPlayer user, ForgeDirection side, EnumTools tool)
{
if (tool == EnumTools.PIPE_GUAGE)
{
return "Source Blocks: " + this.getLiquidFinder().results.size();
}
return null;
}
@Override
public boolean canConnect(ForgeDirection direction)
{
return direction != ForgeDirection.DOWN;
}
@Override
public boolean canConnect(ForgeDirection direction)
{
return direction != ForgeDirection.DOWN;
}
@Override
public boolean canTileConnect(Connection type, ForgeDirection direction)
{
return direction != ForgeDirection.DOWN;
}
@Override
public boolean canTileConnect(Connection type, ForgeDirection direction)
{
return direction != ForgeDirection.DOWN;
}
@Override
public ForgeDirection getDirection()
{
// TODO Auto-generated method stub
return null;
}
@Override
public void setDirection(ForgeDirection direection)
{
// TODO Auto-generated method stub
}
}