LOTS of alchemy changes...
This commit is contained in:
parent
64ccc50698
commit
2b749000b6
99 changed files with 7488 additions and 659 deletions
|
@ -0,0 +1,10 @@
|
|||
package WayofTime.alchemicalWizardry.api.alchemy.energy;
|
||||
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
public interface IAlchemyGoggles
|
||||
{
|
||||
public boolean showIngameHUD(World world, ItemStack stack, EntityPlayer player);
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package WayofTime.alchemicalWizardry.api.alchemy.energy;
|
||||
|
||||
import net.minecraftforge.fluids.FluidTankInfo;
|
||||
|
||||
public interface IReagentContainer
|
||||
{
|
||||
public ReagentStack getReagent();
|
||||
|
||||
public int getReagentStackAmount();
|
||||
|
||||
public int getCapacity();
|
||||
|
||||
public int fill(ReagentStack resource, boolean doFill);
|
||||
|
||||
public ReagentStack drain(int maxDrain, boolean doDrain);
|
||||
|
||||
public ReagentContainerInfo getInfo();
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package WayofTime.alchemicalWizardry.api.alchemy.energy;
|
||||
|
||||
import net.minecraftforge.common.util.ForgeDirection;
|
||||
|
||||
public interface IReagentHandler
|
||||
{
|
||||
int fill(ForgeDirection from, ReagentStack resource, boolean doFill);
|
||||
|
||||
ReagentStack drain(ForgeDirection from, ReagentStack resource, boolean doDrain);
|
||||
|
||||
ReagentStack drain(ForgeDirection from, int maxDrain, boolean doDrain);
|
||||
|
||||
boolean canFill(ForgeDirection from, Reagent reagent);
|
||||
|
||||
boolean canDrain(ForgeDirection from, Reagent reagent);
|
||||
|
||||
ReagentContainerInfo[] getContainerInfo(ForgeDirection from);
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package WayofTime.alchemicalWizardry.api.alchemy.energy;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public interface ISegmentedReagentHandler extends IReagentHandler
|
||||
{
|
||||
public int getNumberOfTanks();
|
||||
|
||||
public int getTanksTunedToReagent(Reagent reagent);
|
||||
|
||||
public void setTanksTunedToReagent(Reagent reagent, int total);
|
||||
|
||||
public Map<Reagent, Integer> getAttunedTankMap();
|
||||
}
|
|
@ -0,0 +1,54 @@
|
|||
package WayofTime.alchemicalWizardry.api.alchemy.energy;
|
||||
|
||||
import net.minecraft.item.ItemStack;
|
||||
|
||||
public class Reagent
|
||||
{
|
||||
public final String name;
|
||||
|
||||
public static final int REAGENT_SIZE = 1000;
|
||||
|
||||
private int colourRed = 0;
|
||||
private int colourGreen = 0;
|
||||
private int colourBlue = 0;
|
||||
private int colourIntensity = 255;
|
||||
|
||||
public Reagent(String name)
|
||||
{
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void setColour(int red, int green, int blue, int intensity)
|
||||
{
|
||||
this.colourRed = red;
|
||||
this.colourGreen = green;
|
||||
this.colourBlue = blue;
|
||||
this.colourIntensity = intensity;
|
||||
}
|
||||
|
||||
public int getColourRed()
|
||||
{
|
||||
return colourRed;
|
||||
}
|
||||
|
||||
public int getColourGreen()
|
||||
{
|
||||
return colourGreen;
|
||||
}
|
||||
|
||||
public int getColourBlue()
|
||||
{
|
||||
return colourBlue;
|
||||
}
|
||||
|
||||
public int getColourIntensity()
|
||||
{
|
||||
return colourIntensity;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o)
|
||||
{
|
||||
return o instanceof Reagent ? this == o && name.equals(((Reagent)o).name) : false;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,158 @@
|
|||
package WayofTime.alchemicalWizardry.api.alchemy.energy;
|
||||
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraftforge.fluids.Fluid;
|
||||
import net.minecraftforge.fluids.FluidStack;
|
||||
import net.minecraftforge.fluids.FluidTank;
|
||||
|
||||
public class ReagentContainer implements IReagentContainer
|
||||
{
|
||||
protected ReagentStack reagentStack;
|
||||
protected int capacity;
|
||||
|
||||
public ReagentContainer(int capacity)
|
||||
{
|
||||
this(null, capacity);
|
||||
}
|
||||
|
||||
public ReagentContainer(ReagentStack stack, int capacity)
|
||||
{
|
||||
this.reagentStack = stack;
|
||||
this.capacity = capacity;
|
||||
}
|
||||
|
||||
public ReagentContainer(Reagent reagent, int amount, int capacity)
|
||||
{
|
||||
this(new ReagentStack(reagent, amount), capacity);
|
||||
}
|
||||
|
||||
public static ReagentContainer readFromNBT(NBTTagCompound nbt)
|
||||
{
|
||||
ReagentStack reagent = ReagentStack.loadReagentStackFromNBT(nbt);
|
||||
int capacity = nbt.getInteger("capacity");
|
||||
|
||||
if (reagent != null)
|
||||
{
|
||||
return new ReagentContainer(reagent, capacity);
|
||||
}else
|
||||
{
|
||||
return new ReagentContainer(null, capacity);
|
||||
}
|
||||
}
|
||||
|
||||
public NBTTagCompound writeToNBT(NBTTagCompound nbt)
|
||||
{
|
||||
if (reagentStack != null)
|
||||
{
|
||||
reagentStack.writeToNBT(nbt);
|
||||
}
|
||||
|
||||
nbt.setInteger("capacity", capacity);
|
||||
|
||||
return nbt;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReagentStack getReagent()
|
||||
{
|
||||
return reagentStack;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getReagentStackAmount()
|
||||
{
|
||||
if (reagentStack == null)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
return reagentStack.amount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCapacity()
|
||||
{
|
||||
return capacity;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int fill(ReagentStack resource, boolean doFill)
|
||||
{
|
||||
if (resource == null)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!doFill)
|
||||
{
|
||||
if (reagentStack == null)
|
||||
{
|
||||
return Math.min(capacity, resource.amount);
|
||||
}
|
||||
|
||||
if (!reagentStack.isReagentEqual(resource))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return Math.min(capacity - reagentStack.amount, resource.amount);
|
||||
}
|
||||
|
||||
if (reagentStack == null)
|
||||
{
|
||||
reagentStack = new ReagentStack(resource, Math.min(capacity, resource.amount));
|
||||
|
||||
return reagentStack.amount;
|
||||
}
|
||||
|
||||
if (!reagentStack.isReagentEqual(resource))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
int filled = capacity - reagentStack.amount;
|
||||
|
||||
if (resource.amount < filled)
|
||||
{
|
||||
reagentStack.amount += resource.amount;
|
||||
filled = resource.amount;
|
||||
}
|
||||
else
|
||||
{
|
||||
reagentStack.amount = capacity;
|
||||
}
|
||||
|
||||
return filled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReagentStack drain(int maxDrain, boolean doDrain)
|
||||
{
|
||||
if (reagentStack == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
int drained = maxDrain;
|
||||
if (reagentStack.amount < drained)
|
||||
{
|
||||
drained = reagentStack.amount;
|
||||
}
|
||||
|
||||
ReagentStack stack = new ReagentStack(reagentStack, drained);
|
||||
if (doDrain)
|
||||
{
|
||||
reagentStack.amount -= drained;
|
||||
if (reagentStack.amount <= 0)
|
||||
{
|
||||
reagentStack = null;
|
||||
}
|
||||
}
|
||||
return stack;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReagentContainerInfo getInfo()
|
||||
{
|
||||
return new ReagentContainerInfo(this);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package WayofTime.alchemicalWizardry.api.alchemy.energy;
|
||||
|
||||
import net.minecraftforge.fluids.FluidStack;
|
||||
import net.minecraftforge.fluids.IFluidTank;
|
||||
|
||||
public final class ReagentContainerInfo
|
||||
{
|
||||
public final ReagentStack reagent;
|
||||
public final int capacity;
|
||||
|
||||
public ReagentContainerInfo(ReagentStack reagent, int capacity)
|
||||
{
|
||||
this.reagent = reagent;
|
||||
this.capacity = capacity;
|
||||
}
|
||||
|
||||
public ReagentContainerInfo(IReagentContainer tank)
|
||||
{
|
||||
this.reagent = tank.getReagent();
|
||||
this.capacity = tank.getCapacity();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,191 @@
|
|||
package WayofTime.alchemicalWizardry.api.alchemy.energy;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
|
||||
import net.minecraft.item.ItemStack;
|
||||
import WayofTime.alchemicalWizardry.ModItems;
|
||||
|
||||
public class ReagentRegistry
|
||||
{
|
||||
public static Map<String, Reagent> reagentList = new HashMap();
|
||||
public static Map<ItemStack, ReagentStack> itemToReagentMap = new HashMap();
|
||||
|
||||
public static Reagent sanctusReagent;
|
||||
public static Reagent incendiumReagent;
|
||||
public static Reagent aquasalusReagent;
|
||||
public static Reagent magicalesReagent;
|
||||
public static Reagent aetherReagent;
|
||||
public static Reagent crepitousReagent;
|
||||
public static Reagent crystallosReagent;
|
||||
public static Reagent terraeReagent;
|
||||
public static Reagent tenebraeReagent;
|
||||
|
||||
public static Reagent offensaReagent;
|
||||
public static Reagent praesidiumReagent;
|
||||
public static Reagent orbisTerraeReagent;
|
||||
public static Reagent virtusReagent;
|
||||
public static Reagent reductusReagent;
|
||||
public static Reagent potentiaReagent;
|
||||
|
||||
public static void initReagents()
|
||||
{
|
||||
sanctusReagent = new Reagent("sanctus");
|
||||
incendiumReagent = new Reagent("incendium");
|
||||
aquasalusReagent = new Reagent("aquasalus");
|
||||
magicalesReagent = new Reagent("magicales");
|
||||
aetherReagent = new Reagent("aether");
|
||||
crepitousReagent = new Reagent("crepitous");
|
||||
crystallosReagent = new Reagent("crystallos");
|
||||
terraeReagent = new Reagent("terrae");
|
||||
tenebraeReagent = new Reagent("tenebrae");
|
||||
offensaReagent = new Reagent("offensa");
|
||||
praesidiumReagent = new Reagent("praesidium");
|
||||
orbisTerraeReagent = new Reagent("orbisTerrae");
|
||||
virtusReagent = new Reagent("virtus");
|
||||
reductusReagent = new Reagent("reductus");
|
||||
potentiaReagent = new Reagent("potentia");
|
||||
|
||||
sanctusReagent.setColour(255, 255, 0, 255);
|
||||
incendiumReagent.setColour(255, 0, 0, 255);
|
||||
aquasalusReagent.setColour(47, 0, 196, 255);
|
||||
magicalesReagent.setColour(150, 0, 146, 255);
|
||||
aetherReagent.setColour(105, 223, 86, 255);
|
||||
crepitousReagent.setColour(145, 145, 145, 255);
|
||||
crystallosReagent.setColour(135, 255, 231, 255);
|
||||
terraeReagent.setColour(147, 48, 13, 255);
|
||||
tenebraeReagent.setColour(86, 86, 86, 255);
|
||||
offensaReagent.setColour(126, 0, 0, 255);
|
||||
praesidiumReagent.setColour(135, 135, 135, 255);
|
||||
orbisTerraeReagent.setColour(32, 94, 14, 255);
|
||||
virtusReagent.setColour(180, 0, 0, 255);
|
||||
reductusReagent.setColour(20, 93, 2, 255);
|
||||
potentiaReagent.setColour(64, 81, 208, 255);
|
||||
|
||||
registerReagent("sanctus", sanctusReagent);
|
||||
registerReagent("incendium", incendiumReagent);
|
||||
registerReagent("aquasalus", aquasalusReagent);
|
||||
registerReagent("magicales", magicalesReagent);
|
||||
registerReagent("aether", aetherReagent);
|
||||
registerReagent("crepitous", crepitousReagent);
|
||||
registerReagent("crystallos", crystallosReagent);
|
||||
registerReagent("terrae", terraeReagent);
|
||||
registerReagent("tenebrae", tenebraeReagent);
|
||||
registerReagent("offensa", offensaReagent);
|
||||
registerReagent("praesidium", praesidiumReagent);
|
||||
registerReagent("orbisTerrae", orbisTerraeReagent);
|
||||
registerReagent("virtus", virtusReagent);
|
||||
registerReagent("reductus", reductusReagent);
|
||||
registerReagent("potentia", potentiaReagent);
|
||||
|
||||
ReagentRegistry.registerItemAndReagent(new ItemStack(ModItems.sanctus), new ReagentStack(sanctusReagent, 1000));
|
||||
ReagentRegistry.registerItemAndReagent(new ItemStack(ModItems.incendium), new ReagentStack(incendiumReagent, 1000));
|
||||
ReagentRegistry.registerItemAndReagent(new ItemStack(ModItems.aquasalus), new ReagentStack(aquasalusReagent, 1000));
|
||||
ReagentRegistry.registerItemAndReagent(new ItemStack(ModItems.magicales), new ReagentStack(magicalesReagent, 1000));
|
||||
ReagentRegistry.registerItemAndReagent(new ItemStack(ModItems.aether), new ReagentStack(aetherReagent, 1000));
|
||||
ReagentRegistry.registerItemAndReagent(new ItemStack(ModItems.crepitous), new ReagentStack(crepitousReagent, 1000));
|
||||
ReagentRegistry.registerItemAndReagent(new ItemStack(ModItems.crystallos), new ReagentStack(crystallosReagent, 1000));
|
||||
ReagentRegistry.registerItemAndReagent(new ItemStack(ModItems.terrae), new ReagentStack(terraeReagent, 1000));
|
||||
ReagentRegistry.registerItemAndReagent(new ItemStack(ModItems.tennebrae), new ReagentStack(tenebraeReagent, 1000));
|
||||
ReagentRegistry.registerItemAndReagent(new ItemStack(ModItems.baseAlchemyItems,1,0), new ReagentStack(offensaReagent, 1000));
|
||||
ReagentRegistry.registerItemAndReagent(new ItemStack(ModItems.baseAlchemyItems,1,1), new ReagentStack(praesidiumReagent, 1000));
|
||||
ReagentRegistry.registerItemAndReagent(new ItemStack(ModItems.baseAlchemyItems,1,2), new ReagentStack(orbisTerraeReagent, 1000));
|
||||
ReagentRegistry.registerItemAndReagent(new ItemStack(ModItems.baseAlchemyItems,1,6), new ReagentStack(virtusReagent, 1000));
|
||||
ReagentRegistry.registerItemAndReagent(new ItemStack(ModItems.baseAlchemyItems,1,7), new ReagentStack(reductusReagent, 1000));
|
||||
ReagentRegistry.registerItemAndReagent(new ItemStack(ModItems.baseAlchemyItems,1,8), new ReagentStack(potentiaReagent, 1000));
|
||||
}
|
||||
|
||||
public static boolean registerReagent(String key, Reagent reagent)
|
||||
{
|
||||
if(reagentList.containsKey(key) || reagent == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
reagentList.put(key, reagent);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static Reagent getReagentForKey(String key)
|
||||
{
|
||||
if(reagentList.containsKey(key))
|
||||
{
|
||||
return reagentList.get(key);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static String getKeyForReagent(Reagent reagent)
|
||||
{
|
||||
if(reagentList.containsValue(reagent))
|
||||
{
|
||||
Set<Entry<String, Reagent>> set = reagentList.entrySet();
|
||||
for(Entry<String, Reagent> entry : set)
|
||||
{
|
||||
if(entry.getValue().equals(reagent))
|
||||
{
|
||||
return entry.getKey();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
public static void registerItemAndReagent(ItemStack stack, ReagentStack reagentStack)
|
||||
{
|
||||
itemToReagentMap.put(stack, reagentStack);
|
||||
}
|
||||
|
||||
public static ReagentStack getReagentStackForItem(ItemStack stack)
|
||||
{
|
||||
if(stack == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
for(Entry<ItemStack, ReagentStack> entry : itemToReagentMap.entrySet())
|
||||
{
|
||||
if(entry.getKey() != null && entry.getKey().isItemEqual(stack))
|
||||
{
|
||||
if(entry.getValue() == null)
|
||||
{
|
||||
return null;
|
||||
}else
|
||||
{
|
||||
return entry.getValue().copy();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static ItemStack getItemForReagent(Reagent reagent)
|
||||
{
|
||||
if(reagent == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
for(Entry<ItemStack, ReagentStack> entry : itemToReagentMap.entrySet())
|
||||
{
|
||||
if(entry.getValue() != null && entry.getValue().reagent == reagent)
|
||||
{
|
||||
if(entry.getKey() == null)
|
||||
{
|
||||
return null;
|
||||
}else
|
||||
{
|
||||
return entry.getKey().copy();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,64 @@
|
|||
package WayofTime.alchemicalWizardry.api.alchemy.energy;
|
||||
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraftforge.fluids.FluidStack;
|
||||
|
||||
public class ReagentStack
|
||||
{
|
||||
public Reagent reagent;
|
||||
public int amount;
|
||||
|
||||
public ReagentStack(Reagent reagent, int amount)
|
||||
{
|
||||
this.reagent = reagent;
|
||||
this.amount = amount;
|
||||
}
|
||||
|
||||
public ReagentStack(ReagentStack reagentStack, int amount)
|
||||
{
|
||||
this(reagentStack.reagent,amount);
|
||||
}
|
||||
|
||||
public static ReagentStack loadReagentStackFromNBT(NBTTagCompound tag)
|
||||
{
|
||||
Reagent reagent = ReagentRegistry.getReagentForKey(tag.getString("Reagent"));
|
||||
|
||||
if(reagent == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
int amount = tag.getInteger("amount");
|
||||
ReagentStack stack = new ReagentStack(reagent, amount);
|
||||
|
||||
return stack;
|
||||
}
|
||||
|
||||
public NBTTagCompound writeToNBT(NBTTagCompound tag)
|
||||
{
|
||||
tag.setString("Reagent", ReagentRegistry.getKeyForReagent(this.reagent));
|
||||
tag.setInteger("amount", this.amount);
|
||||
|
||||
return tag;
|
||||
}
|
||||
|
||||
public ReagentStack splitStack(int amount)
|
||||
{
|
||||
ReagentStack copyStack = this.copy();
|
||||
int splitAmount = Math.min(amount, this.amount);
|
||||
copyStack.amount = splitAmount;
|
||||
this.amount -= splitAmount;
|
||||
|
||||
return copyStack;
|
||||
}
|
||||
|
||||
public ReagentStack copy()
|
||||
{
|
||||
return new ReagentStack(this.reagent, this.amount);
|
||||
}
|
||||
|
||||
public boolean isReagentEqual(ReagentStack other)
|
||||
{
|
||||
return other != null && this.reagent == other.reagent;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,68 @@
|
|||
|
||||
package WayofTime.alchemicalWizardry.api.alchemy.energy;
|
||||
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraftforge.common.util.ForgeDirection;
|
||||
import net.minecraftforge.fluids.FluidContainerRegistry;
|
||||
import net.minecraftforge.fluids.IFluidHandler;
|
||||
|
||||
public class TileReagentHandler extends TileEntity implements IReagentHandler
|
||||
{
|
||||
protected ReagentContainer tank = new ReagentContainer(4000);
|
||||
|
||||
@Override
|
||||
public void readFromNBT(NBTTagCompound tag)
|
||||
{
|
||||
super.readFromNBT(tag);
|
||||
tank.readFromNBT(tag);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToNBT(NBTTagCompound tag)
|
||||
{
|
||||
super.writeToNBT(tag);
|
||||
tank.writeToNBT(tag);
|
||||
}
|
||||
|
||||
/* IReagentHandler */
|
||||
@Override
|
||||
public int fill(ForgeDirection from, ReagentStack resource, boolean doFill)
|
||||
{
|
||||
return tank.fill(resource, doFill);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReagentStack drain(ForgeDirection from, ReagentStack resource, boolean doDrain)
|
||||
{
|
||||
if (resource == null || !resource.isReagentEqual(tank.getReagent()))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return tank.drain(resource.amount, doDrain);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReagentStack drain(ForgeDirection from, int maxDrain, boolean doDrain)
|
||||
{
|
||||
return tank.drain(maxDrain, doDrain);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canFill(ForgeDirection from, Reagent reagent)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canDrain(ForgeDirection from, Reagent reagent)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReagentContainerInfo[] getContainerInfo(ForgeDirection from)
|
||||
{
|
||||
return new ReagentContainerInfo[] {tank.getInfo()};
|
||||
}
|
||||
}
|
|
@ -0,0 +1,283 @@
|
|||
|
||||
package WayofTime.alchemicalWizardry.api.alchemy.energy;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.nbt.NBTTagList;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraftforge.common.util.Constants;
|
||||
import net.minecraftforge.common.util.ForgeDirection;
|
||||
|
||||
public class TileSegmentedReagentHandler extends TileEntity implements ISegmentedReagentHandler
|
||||
{
|
||||
protected ReagentContainer[] tanks;
|
||||
protected Map<Reagent, Integer> attunedTankMap;
|
||||
|
||||
public TileSegmentedReagentHandler()
|
||||
{
|
||||
this(1);
|
||||
}
|
||||
|
||||
public TileSegmentedReagentHandler(int numberOfTanks)
|
||||
{
|
||||
this(numberOfTanks, 1000);
|
||||
}
|
||||
|
||||
public TileSegmentedReagentHandler(int numberOfTanks, int tankSize)
|
||||
{
|
||||
super();
|
||||
|
||||
this.attunedTankMap = new HashMap();
|
||||
this.tanks = new ReagentContainer[numberOfTanks];
|
||||
for(int i=0; i<numberOfTanks; i++)
|
||||
{
|
||||
this.tanks[i] = new ReagentContainer(tankSize);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readFromNBT(NBTTagCompound tag)
|
||||
{
|
||||
super.readFromNBT(tag);
|
||||
|
||||
NBTTagList tagList = tag.getTagList("reagentTanks", Constants.NBT.TAG_COMPOUND);
|
||||
|
||||
int size = tagList.tagCount();
|
||||
this.tanks = new ReagentContainer[size];
|
||||
|
||||
for(int i=0; i<size; i++)
|
||||
{
|
||||
NBTTagCompound savedTag = tagList.getCompoundTagAt(i);
|
||||
this.tanks[i] = ReagentContainer.readFromNBT(savedTag);
|
||||
}
|
||||
|
||||
NBTTagList attunedTagList = tag.getTagList("attunedTankMap", Constants.NBT.TAG_COMPOUND);
|
||||
|
||||
for(int i=0; i<attunedTagList.tagCount(); i++)
|
||||
{
|
||||
NBTTagCompound savedTag = attunedTagList.getCompoundTagAt(i);
|
||||
Reagent reagent = ReagentRegistry.getReagentForKey(savedTag.getString("reagent"));
|
||||
this.attunedTankMap.put(reagent, savedTag.getInteger("amount"));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToNBT(NBTTagCompound tag)
|
||||
{
|
||||
super.writeToNBT(tag);
|
||||
|
||||
NBTTagList tagList = 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);
|
||||
}
|
||||
tagList.appendTag(savedTag);
|
||||
}
|
||||
|
||||
tag.setTag("reagentTanks", tagList);
|
||||
|
||||
NBTTagList attunedTagList = new NBTTagList();
|
||||
|
||||
for(Entry<Reagent, Integer> entry : this.attunedTankMap.entrySet())
|
||||
{
|
||||
NBTTagCompound savedTag = new NBTTagCompound();
|
||||
savedTag.setString("reagent", ReagentRegistry.getKeyForReagent(entry.getKey()));
|
||||
savedTag.setInteger("amount", entry.getValue());
|
||||
attunedTagList.appendTag(savedTag);
|
||||
}
|
||||
|
||||
tag.setTag("attunedTankMap", attunedTagList);
|
||||
}
|
||||
|
||||
/* ISegmentedReagentHandler */
|
||||
@Override
|
||||
public int fill(ForgeDirection from, ReagentStack resource, boolean doFill)
|
||||
{
|
||||
int totalFill = 0;
|
||||
|
||||
boolean useTankLimit = !this.attunedTankMap.isEmpty();
|
||||
|
||||
if(resource != null)
|
||||
{
|
||||
int totalTanksFillable = useTankLimit ? this.getTanksTunedToReagent(resource.reagent) : this.tanks.length;
|
||||
int tanksFilled = 0;
|
||||
|
||||
int maxFill = resource.amount;
|
||||
|
||||
for(int i=this.tanks.length-1; i>=0; i--)
|
||||
{
|
||||
ReagentStack remainingStack = resource.copy();
|
||||
remainingStack.amount = maxFill - totalFill;
|
||||
|
||||
boolean doesReagentMatch = tanks[i].getReagent() == null ? false : tanks[i].getReagent().isReagentEqual(remainingStack);
|
||||
|
||||
if(doesReagentMatch)
|
||||
{
|
||||
totalFill += tanks[i].fill(remainingStack, doFill);
|
||||
tanksFilled++;
|
||||
}else
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if(totalFill >= maxFill || tanksFilled >= totalTanksFillable)
|
||||
{
|
||||
return totalFill;
|
||||
}
|
||||
}
|
||||
|
||||
if(tanksFilled >= totalTanksFillable)
|
||||
{
|
||||
return totalFill;
|
||||
}
|
||||
|
||||
for(int i=this.tanks.length-1; i>=0; i--)
|
||||
{
|
||||
ReagentStack remainingStack = resource.copy();
|
||||
remainingStack.amount = maxFill - totalFill;
|
||||
|
||||
boolean isTankEmpty = tanks[i].getReagent() == null;
|
||||
|
||||
if(isTankEmpty)
|
||||
{
|
||||
totalFill += tanks[i].fill(remainingStack, doFill);
|
||||
tanksFilled++;
|
||||
}else
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if(totalFill >= maxFill || tanksFilled >= totalTanksFillable)
|
||||
{
|
||||
return totalFill;
|
||||
}
|
||||
}
|
||||
}
|
||||
return totalFill;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReagentStack drain(ForgeDirection from, ReagentStack resource, boolean doDrain)
|
||||
{
|
||||
if(resource == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
int maxDrain = resource.amount;
|
||||
Reagent reagent = resource.reagent;
|
||||
int drained = 0;
|
||||
|
||||
for(int i=0; i<tanks.length; i++)
|
||||
{
|
||||
if(drained >= maxDrain)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
if(resource.isReagentEqual(tanks[i].getReagent()))
|
||||
{
|
||||
ReagentStack drainStack = tanks[i].drain(maxDrain-drained, doDrain);
|
||||
if(drainStack != null)
|
||||
{
|
||||
drained += drainStack.amount;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new ReagentStack(reagent, drained);
|
||||
}
|
||||
|
||||
/* Only returns the amount from the first available tank */
|
||||
@Override
|
||||
public ReagentStack drain(ForgeDirection from, int maxDrain, boolean doDrain)
|
||||
{
|
||||
for(int i=0; i<tanks.length; i++)
|
||||
{
|
||||
ReagentStack stack = tanks[i].drain(maxDrain, doDrain);
|
||||
if(stack != null)
|
||||
{
|
||||
return stack;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canFill(ForgeDirection from, Reagent reagent)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canDrain(ForgeDirection from, Reagent reagent)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReagentContainerInfo[] getContainerInfo(ForgeDirection from)
|
||||
{
|
||||
ReagentContainerInfo[] info = new ReagentContainerInfo[this.getNumberOfTanks()];
|
||||
for(int i=0; i<this.getNumberOfTanks(); i++)
|
||||
{
|
||||
info[i] = tanks[i].getInfo();
|
||||
}
|
||||
return info;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getNumberOfTanks()
|
||||
{
|
||||
return tanks.length;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getTanksTunedToReagent(Reagent reagent)
|
||||
{
|
||||
if(this.attunedTankMap.containsKey(reagent) && this.attunedTankMap.get(reagent) != null)
|
||||
{
|
||||
return this.attunedTankMap.get(reagent);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setTanksTunedToReagent(Reagent reagent, int total)
|
||||
{
|
||||
if(total == 0 && this.attunedTankMap.containsKey(reagent))
|
||||
{
|
||||
this.attunedTankMap.remove(reagent);
|
||||
return;
|
||||
}
|
||||
|
||||
this.attunedTankMap.put(reagent, new Integer(total));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<Reagent, Integer> getAttunedTankMap()
|
||||
{
|
||||
return this.attunedTankMap;
|
||||
}
|
||||
|
||||
public boolean areTanksEmpty()
|
||||
{
|
||||
for(int i=0; i<this.tanks.length; i++)
|
||||
{
|
||||
if(tanks[i] != null && tanks[i].reagentStack != null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue