Got basic battery multiblock structure connecting

This commit is contained in:
Calclavia 2014-01-03 23:42:07 +08:00
parent e19a8b9712
commit 59bf99c105
4 changed files with 98 additions and 142 deletions

View file

@ -1,96 +1,48 @@
package resonantinduction.battery;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;
import net.minecraft.item.ItemStack;
import universalelectricity.api.energy.EnergyStorageHandler;
import universalelectricity.api.net.IConnector;
import calclavia.lib.multiblock.structure.Structure;
public class BatteryStructure extends EnergyStorageHandler
public class BatteryStructure extends Structure<TileBattery>
{
public BatteryStructure(TileBattery battery)
public void redistribute()
{
super(TileBattery.STORAGE);
this.battery.add(battery);
long totalEnergy = 0;
for (TileBattery battery : this.get())
{
totalEnergy += battery.getEnergy(null);
}
long totalPerBattery = totalEnergy / this.get().size();
long totalPerBatteryRemainder = totalPerBattery + totalEnergy % this.get().size();
TileBattery firstNode = this.getFirstNode();
for (TileBattery battery : this.get())
{
if (battery == firstNode)
{
battery.setEnergy(null, totalPerBatteryRemainder);
}
else
{
battery.setEnergy(null, totalPerBattery);
}
}
}
public Set<TileBattery> battery = new LinkedHashSet<TileBattery>();
public int length;
public int width;
public int height;
public ItemStack tempStack;
public boolean isMultiblock;
public boolean didTick;
public boolean wroteNBT;
public int getVolume()
{
return this.battery.size();
}
/*
* @Override
* public int hashCode()
* {
* int code = 1;
* code = 31 * locations.hashCode();
* code = 31 * length;
* code = 31 * width;
* code = 31 * height;
* return code;
* }
*/
@Override
public boolean equals(Object obj)
public Structure getNew()
{
if (!(obj instanceof BatteryStructure))
{
return false;
}
BatteryStructure data = (BatteryStructure) obj;
if (!data.battery.equals(battery))
{
return false;
}
if (data.length != length || data.width != width || data.height != height)
{
return false;
}
return true;
return new BatteryStructure();
}
public void merge(TileBattery tile)
@Override
protected void refreshNode(TileBattery node)
{
// Merge structure.
long energyToMerge = tile.structure.getEnergy();
long capacityToMerge = tile.structure.getEnergyCapacity();
this.battery.addAll(tile.structure.battery);
tile.structure.battery.clear();
this.resetReferences();
this.setCapacity(capacityToMerge);
this.receiveEnergy(energyToMerge, true);
}
public void resetReferences()
{
Iterator<TileBattery> it = this.battery.iterator();
while (it.hasNext())
{
TileBattery tile = it.next();
tile.structure = this;
}
node.setNetwork(this);
}
}

View file

@ -34,7 +34,7 @@ public class BlockBattery extends BlockBase implements ITileEntityProvider
{
if (!world.isRemote)
{
System.out.println(((TileBattery) world.getBlockTileEntity(x, y, z)).structure.hashCode());
System.out.println(((TileBattery) world.getBlockTileEntity(x, y, z)).getNetwork().hashCode());
}
return true;
}

View file

@ -93,14 +93,7 @@ public class RenderBattery extends TileEntitySpecialRenderer
GL11.glTranslated(x + 0.5, y + 1.5, z + 0.5);
GL11.glRotatef(180F, 0.0F, 0.0F, 1.0F);
if (((TileBattery) t).structure.isMultiblock)
{
this.bindTexture(TEXTURE_MULTI);
}
else
{
this.bindTexture(TEXTURE);
}
this.bindTexture(TEXTURE);
MODEL.render(0.0625f);
GL11.glPopMatrix();

View file

@ -1,6 +1,3 @@
/**
*
*/
package resonantinduction.battery;
import java.util.ArrayList;
@ -8,13 +5,15 @@ import java.util.HashSet;
import java.util.Set;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.ForgeDirection;
import resonantinduction.ResonantInduction;
import universalelectricity.api.UniversalClass;
import universalelectricity.api.energy.EnergyStorageHandler;
import universalelectricity.api.energy.IEnergyContainer;
import universalelectricity.api.energy.IEnergyInterface;
import universalelectricity.api.net.IConnector;
import universalelectricity.api.vector.Vector3;
import calclavia.lib.network.IPacketReceiver;
import calclavia.lib.network.IPacketSender;
import calclavia.lib.prefab.tile.TileElectrical;
@ -29,13 +28,13 @@ import cpw.mods.fml.common.network.PacketDispatcher;
* @author Calclavia
*/
@UniversalClass
public class TileBattery extends TileElectrical implements IPacketSender, IPacketReceiver, IEnergyInterface, IEnergyContainer
public class TileBattery extends TileElectrical implements IConnector<BatteryStructure>, IPacketSender, IPacketReceiver, IEnergyInterface, IEnergyContainer
{
public static final long STORAGE = 100000000;
public Set<EntityPlayer> playersUsing = new HashSet<EntityPlayer>();
private BatteryStructure structure;
public BatteryStructure structure = new BatteryStructure(this);
public Set<EntityPlayer> playersUsing = new HashSet<EntityPlayer>();
public float clientEnergy;
public int clientCells;
@ -56,17 +55,16 @@ public class TileBattery extends TileElectrical implements IPacketSender, IPacke
{
if (!this.worldObj.isRemote)
{
/*
* for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS)
* {
* TileEntity tile = new
* Vector3(this).modifyPositionFromSide(dir).getTileEntity(this.worldObj);
* if (tile instanceof TileBattery)
* {
* this.structure.merge((TileBattery) tile);
* }
* }
*/
for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS)
{
TileEntity tile = new Vector3(this).modifyPositionFromSide(dir).getTileEntity(this.worldObj);
if (tile instanceof TileBattery)
{
this.getNetwork().merge(((TileBattery) tile).getNetwork());
}
}
}
}
@ -77,59 +75,29 @@ public class TileBattery extends TileElectrical implements IPacketSender, IPacke
if (!this.worldObj.isRemote)
{
if (this.getNetwork().getFirstNode() == this)
{
this.getNetwork().redistribute();
}
this.produce();
}
}
public float getTransferThreshhold()
{
return this.structure.getVolume() * 50;
}
public void updateClient()
{
PacketDispatcher.sendPacketToAllPlayers(ResonantInduction.PACKET_TILE.getPacket(this, getPacketData(0).toArray()));
}
@Override
public void readFromNBT(NBTTagCompound nbt)
{
super.readFromNBT(nbt);
this.structure.readFromNBT(nbt);
}
@Override
public void writeToNBT(NBTTagCompound nbt)
{
super.writeToNBT(nbt);
if (!structure.wroteNBT)
{
this.structure.writeToNBT(nbt);
structure.wroteNBT = true;
}
}
@Override
public void onReceivePacket(ByteArrayDataInput data, EntityPlayer player, Object... extra)
{
structure.isMultiblock = data.readBoolean();
structure.height = data.readInt();
structure.length = data.readInt();
structure.width = data.readInt();
}
@Override
public ArrayList getPacketData(int type)
{
ArrayList data = new ArrayList();
data.add(structure.isMultiblock);
data.add(structure.height);
data.add(structure.length);
data.add(structure.width);
return data;
}
@ -138,4 +106,47 @@ public class TileBattery extends TileElectrical implements IPacketSender, IPacke
{
return true;
}
@Override
public BatteryStructure getNetwork()
{
if (this.structure == null)
{
this.structure = new BatteryStructure();
this.structure.add(this);
}
return this.structure;
}
@Override
public void setNetwork(BatteryStructure structure)
{
this.structure = structure;
}
@Override
public Object[] getConnections()
{
Object[] connections = new Object[6];
for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS)
{
TileEntity tile = new Vector3(this).modifyPositionFromSide(dir).getTileEntity(this.worldObj);
if (tile instanceof TileBattery)
{
connections[dir.ordinal()] = tile;
}
}
return connections;
}
@Override
public void invalidate()
{
this.getNetwork().split(this);
super.invalidate();
}
}