BloodMagic/src/main/java/WayofTime/alchemicalWizardry/common/tileEntity/TEReagentConduit.java

551 lines
18 KiB
Java
Raw Normal View History

package WayofTime.alchemicalWizardry.common.tileEntity;
2015-07-29 08:23:01 -04:00
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
2014-08-25 07:58:39 -04:00
import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.Packet;
import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
2015-07-29 08:23:01 -04:00
import net.minecraft.server.gui.IUpdatePlayerListBox;
import net.minecraft.tileentity.TileEntity;
2015-07-29 08:23:01 -04:00
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumFacing;
2014-08-25 07:58:39 -04:00
import net.minecraftforge.common.util.Constants;
2015-07-29 09:08:46 -04:00
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
2015-07-29 08:23:01 -04:00
import WayofTime.alchemicalWizardry.api.ColourAndCoords;
import WayofTime.alchemicalWizardry.api.Int3;
import WayofTime.alchemicalWizardry.api.alchemy.energy.IReagentHandler;
import WayofTime.alchemicalWizardry.api.alchemy.energy.Reagent;
import WayofTime.alchemicalWizardry.api.alchemy.energy.ReagentContainer;
import WayofTime.alchemicalWizardry.api.alchemy.energy.ReagentRegistry;
import WayofTime.alchemicalWizardry.api.alchemy.energy.ReagentStack;
import WayofTime.alchemicalWizardry.api.alchemy.energy.TileSegmentedReagentHandler;
import WayofTime.alchemicalWizardry.common.entity.projectile.EntityParticleBeam;
import WayofTime.alchemicalWizardry.common.spell.complex.effect.SpellHelper;
2015-07-29 08:23:01 -04:00
public class TEReagentConduit extends TileSegmentedReagentHandler implements IUpdatePlayerListBox
{
2014-10-13 22:33:20 +02:00
public List<ColourAndCoords> destinationList; //These are offsets
public Map<Reagent, List<Int3>> reagentTargetList;
public Map<Reagent, Integer> reagentTankDesignationList;
public int tickRate = 20; //Rate that the reagents are sent
int hasChanged = 0;
public boolean affectedByRedstone = true;
public int maxConnextions = 5;
public int renderCount = 0;
public TEReagentConduit()
{
this(2, 2000);
}
public TEReagentConduit(int numberOfTanks, int size)
{
super(numberOfTanks, size);
2015-07-30 10:21:53 -04:00
destinationList = new LinkedList<ColourAndCoords>();
reagentTargetList = new HashMap<Reagent, List<Int3>>();
reagentTankDesignationList = new HashMap<Reagent, Integer>();
2014-10-13 22:33:20 +02:00
}
public Int3 getColour()
{
int[] redMap = new int[this.tanks.length];
int[] greenMap = new int[this.tanks.length];
int[] blueMap = new int[this.tanks.length];
for (int i = 0; i < this.tanks.length; i++)
{
ReagentContainer container = this.tanks[i];
if (container != null && container.getReagent() != null)
{
Reagent reagent = container.getReagent().reagent;
redMap[i] = reagent.getColourRed();
greenMap[i] = reagent.getColourGreen();
blueMap[i] = reagent.getColourBlue();
}
}
int red = 0;
int green = 0;
int blue = 0;
for (int i = 0; i < this.tanks.length; i++)
{
red += redMap[i];
green += greenMap[i];
blue += blueMap[i];
}
red /= this.tanks.length;
green /= this.tanks.length;
blue /= this.tanks.length;
return new Int3(red, green, blue);
}
@Override
public void writeToNBT(NBTTagCompound tag)
{
super.writeToNBT(tag);
tag.setInteger("hasChanged", hasChanged);
NBTTagList tagList = new NBTTagList();
for (int i = 0; i < destinationList.size(); i++)
{
NBTTagCompound savedTag = new NBTTagCompound();
tagList.appendTag(destinationList.get(i).writeToNBT(savedTag));
}
tag.setTag("destinationList", tagList);
NBTTagList reagentTagList = new NBTTagList(); //TODO
for (Entry<Reagent, List<Int3>> entry : reagentTargetList.entrySet())
{
NBTTagCompound savedTag = new NBTTagCompound();
savedTag.setString("reagent", ReagentRegistry.getKeyForReagent(entry.getKey()));
NBTTagList coordinateTagList = new NBTTagList();
for (Int3 coord : entry.getValue())
{
NBTTagCompound coordinateTag = new NBTTagCompound();
coord.writeToNBT(coordinateTag);
coordinateTagList.appendTag(coordinateTag);
}
savedTag.setTag("coordinateList", coordinateTagList);
reagentTagList.appendTag(savedTag);
}
tag.setTag("reagentTargetList", reagentTagList);
NBTTagList tankDesignationList = new NBTTagList();
for (Entry<Reagent, Integer> entry : this.reagentTankDesignationList.entrySet())
{
NBTTagCompound savedTag = new NBTTagCompound();
savedTag.setString("reagent", ReagentRegistry.getKeyForReagent(entry.getKey()));
savedTag.setInteger("integer", entry.getValue());
tankDesignationList.appendTag(savedTag);
}
tag.setTag("tankDesignationList", tankDesignationList);
}
@Override
public void readFromNBT(NBTTagCompound tag)
{
super.readFromNBT(tag);
hasChanged = tag.getInteger("hasChanged");
NBTTagList tagList = tag.getTagList("destinationList", Constants.NBT.TAG_COMPOUND);
2015-07-30 10:21:53 -04:00
destinationList = new LinkedList<ColourAndCoords>();
2014-10-13 22:33:20 +02:00
for (int i = 0; i < tagList.tagCount(); i++)
{
NBTTagCompound savedTag = tagList.getCompoundTagAt(i);
destinationList.add(ColourAndCoords.readFromNBT(savedTag));
}
2015-07-30 10:21:53 -04:00
reagentTargetList = new HashMap<Reagent, List<Int3>>();
2014-10-13 22:33:20 +02:00
NBTTagList reagentTagList = tag.getTagList("reagentTargetList", Constants.NBT.TAG_COMPOUND);
for (int i = 0; i < reagentTagList.tagCount(); i++)
{
NBTTagCompound savedTag = reagentTagList.getCompoundTagAt(i);
Reagent reagent = ReagentRegistry.getReagentForKey(savedTag.getString("reagent"));
2015-07-30 10:21:53 -04:00
List<Int3> coordList = new LinkedList<Int3>();
2014-10-13 22:33:20 +02:00
NBTTagList coordinateList = savedTag.getTagList("coordinateList", Constants.NBT.TAG_COMPOUND);
for (int j = 0; j < coordinateList.tagCount(); j++)
{
coordList.add(Int3.readFromNBT(coordinateList.getCompoundTagAt(j)));
}
reagentTargetList.put(reagent, coordList);
}
2015-07-30 10:21:53 -04:00
reagentTankDesignationList = new HashMap<Reagent, Integer>();
2014-10-13 22:33:20 +02:00
NBTTagList tankDesignationList = tag.getTagList("tankDesignationList", Constants.NBT.TAG_COMPOUND);
for (int i = 0; i < tankDesignationList.tagCount(); i++)
{
NBTTagCompound savedTag = tankDesignationList.getCompoundTagAt(i);
2015-07-30 10:21:53 -04:00
this.reagentTankDesignationList.put(ReagentRegistry.getReagentForKey(savedTag.getString("reagent")), savedTag.getInteger("integer"));
2014-10-13 22:33:20 +02:00
}
}
public void readClientNBT(NBTTagCompound tag)
{
NBTTagList tagList = tag.getTagList("destinationList", Constants.NBT.TAG_COMPOUND);
2015-07-30 10:21:53 -04:00
destinationList = new LinkedList<ColourAndCoords>();
2014-10-13 22:33:20 +02:00
for (int i = 0; i < tagList.tagCount(); i++)
{
NBTTagCompound savedTag = tagList.getCompoundTagAt(i);
destinationList.add(ColourAndCoords.readFromNBT(savedTag));
}
NBTTagList reagentTagList = tag.getTagList("reagentTanks", Constants.NBT.TAG_COMPOUND);
2014-08-25 07:58:39 -04:00
int size = reagentTagList.tagCount();
this.tanks = new ReagentContainer[size];
2014-10-13 22:33:20 +02:00
for (int i = 0; i < size; i++)
2014-08-25 07:58:39 -04:00
{
2014-10-13 22:33:20 +02:00
NBTTagCompound savedTag = reagentTagList.getCompoundTagAt(i);
this.tanks[i] = ReagentContainer.readFromNBT(savedTag);
2014-08-25 07:58:39 -04:00
}
2014-10-13 22:33:20 +02:00
}
public void writeClientNBT(NBTTagCompound tag)
{
NBTTagList tagList = new NBTTagList();
for (int i = 0; i < destinationList.size(); i++)
2014-08-25 07:58:39 -04:00
{
2014-10-13 22:33:20 +02:00
NBTTagCompound savedTag = new NBTTagCompound();
tagList.appendTag(destinationList.get(i).writeToNBT(savedTag));
2014-08-25 07:58:39 -04:00
}
2014-10-13 22:33:20 +02:00
tag.setTag("destinationList", tagList);
NBTTagList reagentTagList = new NBTTagList();
for (int i = 0; i < this.tanks.length; i++)
{
NBTTagCompound savedTag = new NBTTagCompound();
if (this.tanks[i] != null)
{
this.tanks[i].writeToNBT(savedTag);
}
reagentTagList.appendTag(savedTag);
}
2014-08-25 07:58:39 -04:00
tag.setTag("reagentTanks", reagentTagList);
2014-10-13 22:33:20 +02:00
}
@Override
2015-07-29 08:23:01 -04:00
public void update()
2014-10-13 22:33:20 +02:00
{
if (!worldObj.isRemote)
{
if (hasChanged > 1)
{
hasChanged = 1;
} else if (hasChanged == 1)
{
hasChanged = 0;
}
if (worldObj.getWorldTime() % 100 == 99)
{
this.updateColourList();
}
2015-07-29 08:23:01 -04:00
if (affectedByRedstone && worldObj.isBlockPowered(pos)) //isBlockBeingIndirectlyPowered()
2014-10-13 22:33:20 +02:00
{
return;
}
int totalTransfered = 0;
for (Entry<Reagent, List<Int3>> entry : this.reagentTargetList.entrySet())
{
for (Int3 coord : entry.getValue())
{
if (totalTransfered >= this.tickRate)
{
break;
}
2015-07-29 08:23:01 -04:00
ReagentStack maxDrainAmount = this.drain(EnumFacing.UP, new ReagentStack(entry.getKey(), this.tickRate - totalTransfered), false);
2014-10-13 22:33:20 +02:00
if (maxDrainAmount == null)
{
continue;
}
int amountLeft = maxDrainAmount.amount;
if (amountLeft <= 0)
{
continue;
}
2015-07-29 08:23:01 -04:00
BlockPos newPos = pos.add(coord.xCoord, coord.yCoord, coord.zCoord);
2014-10-13 22:33:20 +02:00
2015-07-29 08:23:01 -04:00
TileEntity tile = worldObj.getTileEntity(newPos);
2014-10-13 22:33:20 +02:00
if (tile instanceof IReagentHandler)
{
2015-07-29 08:23:01 -04:00
int amount = Math.min(((IReagentHandler) tile).fill(EnumFacing.UP, maxDrainAmount, false), amountLeft);
2014-10-13 22:33:20 +02:00
if (amount > 0)
{
totalTransfered += amount;
2015-07-29 08:23:01 -04:00
ReagentStack stack = this.drain(EnumFacing.UP, new ReagentStack(entry.getKey(), amount), true);
((IReagentHandler) tile).fill(EnumFacing.UP, stack, true);
2014-10-13 22:33:20 +02:00
}
}
}
}
} else
{
2015-07-29 08:23:01 -04:00
if (affectedByRedstone && worldObj.isBlockPowered(pos))
2014-10-13 22:33:20 +02:00
{
return;
}
renderCount++;
if (worldObj.getWorldTime() % 100 != 0)
{
return;
}
this.sendPlayerStuffs();
}
}
@SideOnly(Side.CLIENT)
public void sendPlayerStuffs()
{
Minecraft mc = Minecraft.getMinecraft();
EntityPlayer player = mc.thePlayer;
if (SpellHelper.canPlayerSeeAlchemy(player))
{
for (ColourAndCoords colourSet : this.destinationList)
{
2015-07-29 08:23:01 -04:00
BlockPos newPos = pos.add(colourSet.xCoord, colourSet.yCoord, colourSet.zCoord);
if (!(worldObj.getTileEntity(newPos) instanceof IReagentHandler))
2014-10-13 22:33:20 +02:00
{
continue;
}
2015-07-29 08:23:01 -04:00
EntityParticleBeam beam = new EntityParticleBeam(worldObj, pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5);
2014-10-13 22:33:20 +02:00
double velocity = Math.sqrt(Math.pow(colourSet.xCoord, 2) + Math.pow(colourSet.yCoord, 2) + Math.pow(colourSet.zCoord, 2));
double wantedVel = 0.3d;
beam.setVelocity(wantedVel * colourSet.xCoord / velocity, wantedVel * colourSet.yCoord / velocity, wantedVel * colourSet.zCoord / velocity);
beam.setColour(colourSet.colourRed / 255f, colourSet.colourGreen / 255f, colourSet.colourBlue / 255f);
2015-07-29 08:23:01 -04:00
beam.setDestination(pos.getX() + colourSet.xCoord, pos.getY() + colourSet.yCoord, pos.getZ() + colourSet.zCoord);
2014-10-13 22:33:20 +02:00
worldObj.spawnEntityInWorld(beam);
}
}
}
public void updateColourList()
{
if (worldObj.isRemote)
{
return;
}
List<ColourAndCoords> newList = this.compileListForReagentTargets(this.reagentTargetList);
if (newList != null && !newList.equals(destinationList))
{
this.destinationList = newList;
2015-07-29 08:23:01 -04:00
worldObj.markBlockForUpdate(pos);
2014-10-13 22:33:20 +02:00
}
}
public List<ColourAndCoords> compileListForReagentTargets(Map<Reagent, List<Int3>> map)
{
2015-07-30 10:21:53 -04:00
List<ColourAndCoords> list = new LinkedList<ColourAndCoords>();
2014-10-13 22:33:20 +02:00
for (Entry<Reagent, List<Int3>> entry : map.entrySet())
{
if (entry.getValue() != null)
{
Reagent reagent = entry.getKey();
if (reagent == null)
{
continue;
}
List<Int3> coords = entry.getValue();
for (Int3 coord : coords)
{
if (coord == null)
{
continue;
}
list.add(new ColourAndCoords(reagent.getColourRed(), reagent.getColourGreen(), reagent.getColourBlue(), reagent.getColourIntensity(), coord.xCoord, coord.yCoord, coord.zCoord));
}
}
}
return list;
}
public boolean addDestinationViaOffset(int red, int green, int blue, int intensity, int xOffset, int yOffset, int zOffset)
{
if (xOffset == 0 && yOffset == 0 && zOffset == 0)
{
return false;
}
this.destinationList.add(new ColourAndCoords(red, green, blue, intensity, xOffset, yOffset, zOffset));
return true;
}
public boolean addDestinationViaActual(int red, int green, int blue, int intensity, int x, int y, int z)
{
2015-07-29 08:23:01 -04:00
return this.addDestinationViaOffset(red, green, blue, intensity, x - pos.getX(), y - pos.getY(), z - pos.getZ());
2014-10-13 22:33:20 +02:00
}
@Override
public Packet getDescriptionPacket()
{
NBTTagCompound nbttagcompound = new NBTTagCompound();
writeClientNBT(nbttagcompound);
2015-07-29 08:23:01 -04:00
return new S35PacketUpdateTileEntity(pos, 90210, nbttagcompound);
2014-10-13 22:33:20 +02:00
}
@Override
public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity packet)
{
super.onDataPacket(net, packet);
2015-07-29 08:23:01 -04:00
readClientNBT(packet.getNbtCompound());
2014-10-13 22:33:20 +02:00
}
public boolean addReagentDestinationViaOffset(Reagent reagent, int xOffset, int yOffset, int zOffset)
{
int totalConnections = 0;
for (Entry<Reagent, List<Int3>> entry : this.reagentTargetList.entrySet())
{
if (entry.getValue() != null)
{
totalConnections += entry.getValue().size();
}
}
if (totalConnections >= this.maxConnextions)
{
//Send message that it cannot be done? Maybe add a Player instance
return false;
}
if (xOffset == 0 && yOffset == 0 && zOffset == 0)
{
return false;
}
Int3 newCoord = new Int3(xOffset, yOffset, zOffset);
if (this.reagentTargetList.containsKey(reagent))
{
List<Int3> coordList = this.reagentTargetList.get(reagent);
if (coordList == null)
{
2015-07-30 10:21:53 -04:00
List<Int3> newCoordList = new LinkedList<Int3>();
2014-10-13 22:33:20 +02:00
newCoordList.add(newCoord);
this.reagentTargetList.put(reagent, newCoordList);
} else
{
coordList.add(newCoord);
}
return true;
} else
{
2015-07-30 10:21:53 -04:00
List<Int3> newCoordList = new LinkedList<Int3>();
2014-10-13 22:33:20 +02:00
newCoordList.add(newCoord);
this.reagentTargetList.put(reagent, newCoordList);
return true;
}
}
public boolean addReagentDestinationViaActual(Reagent reagent, int x, int y, int z)
{
2015-07-29 08:23:01 -04:00
return (this.addReagentDestinationViaOffset(reagent, x - pos.getX(), y - pos.getY(), z - pos.getZ()));
2014-10-13 22:33:20 +02:00
}
public boolean removeReagentDestinationViaOffset(Reagent reagent, int xOffset, int yOffset, int zOffset)
{
if (this.reagentTargetList.containsKey(reagent))
{
List<Int3> coords = this.reagentTargetList.get(reagent);
if (coords != null)
{
Int3 reference = new Int3(xOffset, yOffset, zOffset);
return coords.remove(reference);
}
}
return false;
}
public boolean removeReagentDestinationViaActual(Reagent reagent, int x, int y, int z)
{
2015-07-29 08:23:01 -04:00
return this.removeReagentDestinationViaOffset(reagent, x - pos.getX(), y - pos.getY(), z - pos.getZ());
2014-10-13 22:33:20 +02:00
}
@Override
2015-07-29 08:23:01 -04:00
public int fill(EnumFacing from, ReagentStack resource, boolean doFill)
2014-10-13 22:33:20 +02:00
{
if (doFill && !worldObj.isRemote)
{
2015-07-29 08:23:01 -04:00
worldObj.markBlockForUpdate(pos);
2014-10-13 22:33:20 +02:00
hasChanged = 2;
}
return super.fill(from, resource, doFill);
}
@Override
2015-07-29 08:23:01 -04:00
public ReagentStack drain(EnumFacing from, ReagentStack resource, boolean doDrain)
2014-10-13 22:33:20 +02:00
{
if (doDrain && !worldObj.isRemote)
{
2015-07-29 08:23:01 -04:00
worldObj.markBlockForUpdate(pos);
2014-10-13 22:33:20 +02:00
hasChanged = 2;
}
return super.drain(from, resource, doDrain);
}
public void removeReagentDestinationViaActual(Reagent reagent, BlockPos pos)
{
this.removeReagentDestinationViaActual(reagent, pos.getX(), pos.getY(), pos.getZ());
}
public boolean addReagentDestinationViaActual(Reagent reagent, BlockPos pos)
{
return addReagentDestinationViaActual(reagent, pos.getX(), pos.getY(), pos.getZ());
}
2014-08-25 07:58:39 -04:00
}