Created a new prefab TileEntityMultiblock class and merged all Dynamic Tank multiblock-specific code into it.

Should make it much easier to create the new Steam Boiler and Steam Turbine!
This commit is contained in:
Aidan C. Brady 2014-08-22 16:17:16 -04:00
parent 55e70d5f5c
commit 8cae029ed7
8 changed files with 300 additions and 205 deletions

View file

@ -138,6 +138,7 @@ import mekanism.common.tile.TileEntityLaser;
import mekanism.common.tile.TileEntityLaserAmplifier;
import mekanism.common.tile.TileEntityLogisticalSorter;
import mekanism.common.tile.TileEntityMetallurgicInfuser;
import mekanism.common.tile.TileEntityMultiblock;
import mekanism.common.tile.TileEntityObsidianTNT;
import mekanism.common.tile.TileEntityOsmiumCompressor;
import mekanism.common.tile.TileEntityPRC;
@ -157,7 +158,6 @@ import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import net.minecraftforge.client.MinecraftForgeClient;
import net.minecraftforge.common.config.Configuration;
import net.minecraftforge.common.util.ForgeDirection;
import cpw.mods.fml.client.FMLClientHandler;
import cpw.mods.fml.client.registry.ClientRegistry;
@ -491,7 +491,7 @@ public class ClientProxy extends CommonProxy
}
@Override
public void doTankAnimation(TileEntityDynamicTank tileEntity)
public void doTankAnimation(TileEntityMultiblock<?> tileEntity)
{
new ThreadTankSparkle(tileEntity).start();
}

View file

@ -6,7 +6,7 @@ import java.util.Set;
import mekanism.api.Coord4D;
import mekanism.api.MekanismConfig.general;
import mekanism.common.tile.TileEntityDynamicTank;
import mekanism.common.tile.TileEntityMultiblock;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
@ -16,13 +16,13 @@ import cpw.mods.fml.relauncher.SideOnly;
@SideOnly(Side.CLIENT)
public class ThreadTankSparkle extends Thread
{
public TileEntityDynamicTank pointer;
public TileEntityMultiblock<?> pointer;
public Random random = new Random();
public Set<TileEntity> iteratedNodes = new HashSet<TileEntity>();
public ThreadTankSparkle(TileEntityDynamicTank tileEntity)
public ThreadTankSparkle(TileEntityMultiblock<?> tileEntity)
{
pointer = tileEntity;
}
@ -40,7 +40,7 @@ public class ThreadTankSparkle extends Thread
} catch(Exception e) {}
}
public void loop(TileEntityDynamicTank tileEntity)
public void loop(TileEntityMultiblock<?> tileEntity)
{
World world = pointer.getWorldObj();
@ -83,9 +83,9 @@ public class ThreadTankSparkle extends Thread
{
TileEntity tile = Coord4D.get(tileEntity).getFromSide(side).getTileEntity(pointer.getWorldObj());
if(tile instanceof TileEntityDynamicTank && !iteratedNodes.contains(tile))
if(tile instanceof TileEntityMultiblock && tile.getClass() == pointer.getClass() && !iteratedNodes.contains(tile))
{
loop((TileEntityDynamicTank)tile);
loop((TileEntityMultiblock<?>)tile);
}
}
}

View file

@ -73,6 +73,7 @@ import mekanism.common.tile.TileEntityLaser;
import mekanism.common.tile.TileEntityLaserAmplifier;
import mekanism.common.tile.TileEntityLogisticalSorter;
import mekanism.common.tile.TileEntityMetallurgicInfuser;
import mekanism.common.tile.TileEntityMultiblock;
import mekanism.common.tile.TileEntityObsidianTNT;
import mekanism.common.tile.TileEntityOsmiumCompressor;
import mekanism.common.tile.TileEntityPRC;
@ -312,7 +313,7 @@ public class CommonProxy
/**
* Does the Dynamic Tank creation animation, starting from the rendering block.
*/
public void doTankAnimation(TileEntityDynamicTank tileEntity) {}
public void doTankAnimation(TileEntityMultiblock<?> tileEntity) {}
/**
* Get the actual interface for a GUI. Client-only.

View file

@ -10,5 +10,11 @@ public class SynchronizedMatrixData extends SynchronizedData<SynchronizedTankDat
public double electricityStored;
public double heat;
public int capacitors;
public int outputters;
public int coolants;
}

View file

@ -18,14 +18,14 @@ public class SynchronizedTankData extends SynchronizedData<SynchronizedTankData>
public ItemStack[] inventory = new ItemStack[2];
public boolean didTick;
public boolean hasRenderer;
public Coord4D renderLocation;
public Set<ValveData> valves = new HashSet<ValveData>();
@Override
public ItemStack[] getInventory()
{
return inventory;
}
public static class ValveData
{
public ForgeDirection side;

View file

@ -4,8 +4,7 @@ import java.util.HashSet;
import java.util.Set;
import mekanism.api.Coord4D;
import mekanism.common.content.tank.SynchronizedTankData;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.item.ItemStack;
public abstract class SynchronizedData<T>
{
@ -21,6 +20,17 @@ public abstract class SynchronizedData<T>
public int inventoryID;
public boolean didTick;
public boolean hasRenderer;
public Coord4D renderLocation;
public ItemStack[] getInventory()
{
return null;
}
@Override
public int hashCode()
{

View file

@ -11,46 +11,27 @@ import mekanism.api.Range4D;
import mekanism.common.Mekanism;
import mekanism.common.base.IFluidContainerManager;
import mekanism.common.content.tank.SynchronizedTankData;
import mekanism.common.content.tank.TankUpdateProtocol;
import mekanism.common.content.tank.SynchronizedTankData.ValveData;
import mekanism.common.content.tank.TankUpdateProtocol;
import mekanism.common.multiblock.IMultiblock;
import mekanism.common.multiblock.SynchronizedData;
import mekanism.common.multiblock.MultiblockManager;
import mekanism.common.network.PacketTileEntity.TileEntityMessage;
import mekanism.common.util.FluidContainerUtils;
import mekanism.common.util.FluidContainerUtils.ContainerEditMode;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.util.AxisAlignedBB;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.fluids.FluidContainerRegistry;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.IFluidContainerItem;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
public class TileEntityDynamicTank extends TileEntityContainerBlock implements IFluidContainerManager, IMultiblock<SynchronizedTankData>
public class TileEntityDynamicTank extends TileEntityMultiblock<SynchronizedTankData> implements IFluidContainerManager, IMultiblock<SynchronizedTankData>
{
/** The tank data for this structure. */
public SynchronizedTankData structure;
/** Whether or not to send this tank's structure in the next update packet. */
public boolean sendStructure;
/** This tank's previous "has structure" state. */
public boolean prevStructure;
/** Whether or not this tank has it's structure, for the client side mechanics. */
public boolean clientHasStructure;
/** A client-sided and server-sided map of valves on this tank's structure, used on the client for rendering fluids. */
public Map<ValveData, Integer> valveViewing = new HashMap<ValveData, Integer>();
/** The capacity this tank has on the client-side. */
public int clientCapacity;
/** Whether or not this tank segment is rendering the structure. */
public boolean isRendering;
public float prevScale;
public TileEntityDynamicTank()
@ -64,29 +45,13 @@ public class TileEntityDynamicTank extends TileEntityContainerBlock implements I
inventory = new ItemStack[2];
}
public void update()
{
if(!worldObj.isRemote && (structure == null || !structure.didTick))
{
new TankUpdateProtocol(this).updateTanks();
if(structure != null)
{
structure.didTick = true;
}
}
}
@Override
public void onUpdate()
{
super.onUpdate();
if(worldObj.isRemote)
{
if(structure == null)
{
structure = new SynchronizedTankData();
}
if(structure != null && clientHasStructure && isRendering)
{
for(ValveData data : valveViewing.keySet())
@ -97,11 +62,6 @@ public class TileEntityDynamicTank extends TileEntityContainerBlock implements I
}
}
if(!prevStructure)
{
Mekanism.proxy.doTankAnimation(this);
}
float targetScale = (float)(structure.fluidStored != null ? structure.fluidStored.amount : 0)/clientCapacity;
if(Math.abs(prevScale - targetScale) > 0.01)
@ -110,8 +70,6 @@ public class TileEntityDynamicTank extends TileEntityContainerBlock implements I
}
}
prevStructure = clientHasStructure;
if(!clientHasStructure || !isRendering)
{
for(ValveData data : valveViewing.keySet())
@ -128,59 +86,10 @@ public class TileEntityDynamicTank extends TileEntityContainerBlock implements I
}
}
if(playersUsing.size() > 0 && ((worldObj.isRemote && !clientHasStructure) || (!worldObj.isRemote && structure == null)))
{
for(EntityPlayer player : playersUsing)
{
player.closeScreen();
}
}
if(!worldObj.isRemote)
{
if(structure == null)
{
isRendering = false;
}
if(structure == null && ticker == 5)
{
update();
}
if(prevStructure != (structure != null))
{
if(structure != null && !structure.hasRenderer)
{
structure.hasRenderer = true;
isRendering = true;
sendStructure = true;
}
for(ForgeDirection side : ForgeDirection.VALID_DIRECTIONS)
{
Coord4D obj = Coord4D.get(this).getFromSide(side);
if(!obj.isAirBlock(worldObj) && !(obj.getTileEntity(worldObj) instanceof TileEntityDynamicTank))
{
obj.getBlock(worldObj).onNeighborChange(worldObj, obj.xCoord, obj.yCoord, obj.zCoord, xCoord, yCoord, zCoord);
}
}
Mekanism.packetHandler.sendToReceivers(new TileEntityMessage(Coord4D.get(this), getNetworkedData(new ArrayList())), new Range4D(Coord4D.get(this)));
}
prevStructure = structure != null;
if(structure != null)
{
structure.didTick = false;
if(structure.inventoryID != -1)
{
Mekanism.tankManager.updateCache(this);
}
manageInventory();
}
}
@ -346,14 +255,23 @@ public class TileEntityDynamicTank extends TileEntityContainerBlock implements I
}
}
@Override
public SynchronizedTankData getNewStructure()
{
return new SynchronizedTankData();
}
@Override
public MultiblockManager<SynchronizedTankData> getManager()
{
return Mekanism.tankManager;
}
@Override
public ArrayList getNetworkedData(ArrayList data)
{
super.getNetworkedData(data);
data.add(isRendering);
data.add(structure != null);
if(structure != null)
{
data.add(structure.volume*TankUpdateProtocol.FLUID_PER_TANK);
@ -372,22 +290,6 @@ public class TileEntityDynamicTank extends TileEntityContainerBlock implements I
if(structure != null && isRendering)
{
if(sendStructure)
{
sendStructure = false;
data.add(true);
data.add(structure.volHeight);
data.add(structure.volWidth);
data.add(structure.volLength);
structure.renderLocation.write(data);
}
else {
data.add(false);
}
data.add(structure.valves.size());
for(ValveData valveData : structure.valves)
@ -407,14 +309,6 @@ public class TileEntityDynamicTank extends TileEntityContainerBlock implements I
{
super.handlePacketData(dataStream);
if(structure == null)
{
structure = new SynchronizedTankData();
}
isRendering = dataStream.readBoolean();
clientHasStructure = dataStream.readBoolean();
if(clientHasStructure)
{
clientCapacity = dataStream.readInt();
@ -431,15 +325,6 @@ public class TileEntityDynamicTank extends TileEntityContainerBlock implements I
if(clientHasStructure && isRendering)
{
if(dataStream.readBoolean())
{
structure.volHeight = dataStream.readInt();
structure.volWidth = dataStream.readInt();
structure.volLength = dataStream.readInt();
structure.renderLocation = Coord4D.read(dataStream);
}
int size = dataStream.readInt();
for(int i = 0; i < size; i++)
@ -474,22 +359,6 @@ public class TileEntityDynamicTank extends TileEntityContainerBlock implements I
}
}
public void sendPacketToRenderer()
{
if(structure != null)
{
for(Coord4D obj : structure.locations)
{
TileEntityDynamicTank tileEntity = (TileEntityDynamicTank)obj.getTileEntity(worldObj);
if(tileEntity != null && tileEntity.isRendering)
{
Mekanism.packetHandler.sendToReceivers(new TileEntityMessage(Coord4D.get(tileEntity), tileEntity.getNetworkedData(new ArrayList())), new Range4D(Coord4D.get(tileEntity)));
}
}
}
}
public int getScaledFluidLevel(int i)
{
if(clientCapacity == 0 || structure.fluidStored == null)
@ -500,33 +369,6 @@ public class TileEntityDynamicTank extends TileEntityContainerBlock implements I
return structure.fluidStored.amount*i / clientCapacity;
}
@Override
public ItemStack getStackInSlot(int slotID)
{
return structure != null ? structure.inventory[slotID] : null;
}
@Override
public void setInventorySlotContents(int slotID, ItemStack itemstack)
{
if(structure != null)
{
structure.inventory[slotID] = itemstack;
if(itemstack != null && itemstack.stackSize > getInventoryStackLimit())
{
itemstack.stackSize = getInventoryStackLimit();
}
}
}
@Override
@SideOnly(Side.CLIENT)
public AxisAlignedBB getRenderBoundingBox()
{
return INFINITE_EXTENT_AABB;
}
@Override
public ContainerEditMode getContainerEditMode()
{
@ -548,16 +390,4 @@ public class TileEntityDynamicTank extends TileEntityContainerBlock implements I
structure.editMode = mode;
}
@Override
public boolean handleInventory()
{
return false;
}
@Override
public SynchronizedData<SynchronizedTankData> getSynchronizedData()
{
return structure;
}
}

View file

@ -0,0 +1,248 @@
package mekanism.common.tile;
import io.netty.buffer.ByteBuf;
import java.util.ArrayList;
import mekanism.api.Coord4D;
import mekanism.api.Range4D;
import mekanism.common.Mekanism;
import mekanism.common.content.tank.TankUpdateProtocol;
import mekanism.common.multiblock.IMultiblock;
import mekanism.common.multiblock.MultiblockManager;
import mekanism.common.multiblock.SynchronizedData;
import mekanism.common.network.PacketTileEntity.TileEntityMessage;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.util.AxisAlignedBB;
import net.minecraftforge.common.util.ForgeDirection;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
public abstract class TileEntityMultiblock<T> extends TileEntityContainerBlock implements IMultiblock<T>
{
/** The tank data for this structure. */
public T structure;
/** Whether or not to send this tank's structure in the next update packet. */
public boolean sendStructure;
/** This tank's previous "has structure" state. */
public boolean prevStructure;
/** Whether or not this tank has it's structure, for the client side mechanics. */
public boolean clientHasStructure;
/** Whether or not this tank segment is rendering the structure. */
public boolean isRendering;
public TileEntityMultiblock(String name)
{
super(name);
}
@Override
public void onUpdate()
{
if(worldObj.isRemote)
{
if(structure == null)
{
structure = getNewStructure();
}
if(structure != null && clientHasStructure && isRendering)
{
if(!prevStructure)
{
Mekanism.proxy.doTankAnimation(this);
}
}
prevStructure = clientHasStructure;
}
if(playersUsing.size() > 0 && ((worldObj.isRemote && !clientHasStructure) || (!worldObj.isRemote && structure == null)))
{
for(EntityPlayer player : playersUsing)
{
player.closeScreen();
}
}
if(!worldObj.isRemote)
{
if(structure == null)
{
isRendering = false;
}
if(structure == null && ticker == 5)
{
update();
}
if(prevStructure != (structure != null))
{
if(structure != null && !getSynchronizedData().hasRenderer)
{
getSynchronizedData().hasRenderer = true;
isRendering = true;
sendStructure = true;
}
for(ForgeDirection side : ForgeDirection.VALID_DIRECTIONS)
{
Coord4D obj = Coord4D.get(this).getFromSide(side);
if(!obj.isAirBlock(worldObj) && (obj.getTileEntity(worldObj) == null || obj.getTileEntity(worldObj).getClass() != getClass()))
{
obj.getBlock(worldObj).onNeighborChange(worldObj, obj.xCoord, obj.yCoord, obj.zCoord, xCoord, yCoord, zCoord);
}
}
Mekanism.packetHandler.sendToReceivers(new TileEntityMessage(Coord4D.get(this), getNetworkedData(new ArrayList())), new Range4D(Coord4D.get(this)));
}
prevStructure = structure != null;
if(structure != null)
{
getSynchronizedData().didTick = false;
if(getSynchronizedData().inventoryID != -1)
{
getManager().updateCache(this);
}
}
}
}
public void update()
{
if(!worldObj.isRemote && (structure == null || !getSynchronizedData().didTick))
{
new TankUpdateProtocol(this).updateTanks();
if(structure != null)
{
getSynchronizedData().didTick = true;
}
}
}
public void sendPacketToRenderer()
{
if(structure != null)
{
for(Coord4D obj : getSynchronizedData().locations)
{
TileEntityDynamicTank tileEntity = (TileEntityDynamicTank)obj.getTileEntity(worldObj);
if(tileEntity != null && tileEntity.isRendering)
{
Mekanism.packetHandler.sendToReceivers(new TileEntityMessage(Coord4D.get(tileEntity), tileEntity.getNetworkedData(new ArrayList())), new Range4D(Coord4D.get(tileEntity)));
}
}
}
}
protected abstract T getNewStructure();
protected abstract MultiblockManager<T> getManager();
@Override
public ArrayList getNetworkedData(ArrayList data)
{
super.getNetworkedData(data);
data.add(isRendering);
data.add(structure != null);
if(structure != null && isRendering)
{
if(sendStructure)
{
sendStructure = false;
data.add(true);
data.add(getSynchronizedData().volHeight);
data.add(getSynchronizedData().volWidth);
data.add(getSynchronizedData().volLength);
getSynchronizedData().renderLocation.write(data);
}
else {
data.add(false);
}
}
return data;
}
@Override
public void handlePacketData(ByteBuf dataStream)
{
super.handlePacketData(dataStream);
if(structure == null)
{
structure = getNewStructure();
}
isRendering = dataStream.readBoolean();
clientHasStructure = dataStream.readBoolean();
if(clientHasStructure && isRendering)
{
if(dataStream.readBoolean())
{
getSynchronizedData().volHeight = dataStream.readInt();
getSynchronizedData().volWidth = dataStream.readInt();
getSynchronizedData().volLength = dataStream.readInt();
getSynchronizedData().renderLocation = Coord4D.read(dataStream);
}
}
}
@Override
public ItemStack getStackInSlot(int slotID)
{
return structure != null && getSynchronizedData().getInventory() != null ? getSynchronizedData().getInventory()[slotID] : null;
}
@Override
public void setInventorySlotContents(int slotID, ItemStack itemstack)
{
if(structure != null && getSynchronizedData().getInventory() != null)
{
getSynchronizedData().getInventory()[slotID] = itemstack;
if(itemstack != null && itemstack.stackSize > getInventoryStackLimit())
{
itemstack.stackSize = getInventoryStackLimit();
}
}
}
@Override
@SideOnly(Side.CLIENT)
public AxisAlignedBB getRenderBoundingBox()
{
return INFINITE_EXTENT_AABB;
}
@Override
public boolean handleInventory()
{
return false;
}
@Override
public SynchronizedData<T> getSynchronizedData()
{
return (SynchronizedData<T>)structure;
}
}