2016-01-07 18:05:23 -05:00
|
|
|
package WayofTime.bloodmagic.tile;
|
|
|
|
|
2016-01-08 09:12:31 -05:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import net.minecraft.item.ItemStack;
|
2016-01-07 18:05:23 -05:00
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
import net.minecraft.util.ITickable;
|
2016-01-08 10:27:26 -05:00
|
|
|
import WayofTime.bloodmagic.api.Constants;
|
2016-01-08 09:12:31 -05:00
|
|
|
import WayofTime.bloodmagic.api.recipe.SoulForgeRecipe;
|
|
|
|
import WayofTime.bloodmagic.api.registry.SoulForgeRecipeRegistry;
|
|
|
|
import WayofTime.bloodmagic.api.soul.ISoul;
|
|
|
|
import WayofTime.bloodmagic.api.soul.ISoulGem;
|
2016-01-07 18:05:23 -05:00
|
|
|
|
|
|
|
public class TileSoulForge extends TileInventory implements ITickable
|
|
|
|
{
|
2016-01-08 10:27:26 -05:00
|
|
|
public static final int ticksRequired = 100;
|
|
|
|
|
2016-01-08 09:12:31 -05:00
|
|
|
public static final int soulSlot = 4;
|
|
|
|
public static final int outputSlot = 5;
|
|
|
|
|
|
|
|
//Input slots are from 0 to 3.
|
|
|
|
|
2016-01-08 10:27:26 -05:00
|
|
|
public int burnTime = 0;
|
|
|
|
|
2016-01-07 18:05:23 -05:00
|
|
|
public TileSoulForge()
|
|
|
|
{
|
|
|
|
super(6, "soulForge");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-01-08 10:27:26 -05:00
|
|
|
public void readFromNBT(NBTTagCompound tag)
|
2016-01-07 18:05:23 -05:00
|
|
|
{
|
2016-01-08 10:27:26 -05:00
|
|
|
super.readFromNBT(tag);
|
|
|
|
|
|
|
|
burnTime = tag.getInteger(Constants.NBT.SOUL_FORGE_BURN);
|
2016-01-07 18:05:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-01-08 10:27:26 -05:00
|
|
|
public void writeToNBT(NBTTagCompound tag)
|
2016-01-07 18:05:23 -05:00
|
|
|
{
|
2016-01-08 10:27:26 -05:00
|
|
|
super.writeToNBT(tag);
|
|
|
|
|
|
|
|
tag.setInteger(Constants.NBT.SOUL_FORGE_BURN, burnTime);
|
2016-01-07 18:05:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void update()
|
|
|
|
{
|
2016-01-08 09:12:31 -05:00
|
|
|
if (!hasSoulGemOrSoul())
|
|
|
|
{
|
2016-01-08 10:27:26 -05:00
|
|
|
burnTime = 0;
|
2016-01-08 09:12:31 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
double soulsInGem = getSouls();
|
|
|
|
|
|
|
|
List<ItemStack> inputList = new ArrayList<ItemStack>();
|
|
|
|
|
|
|
|
for (int i = 0; i < 4; i++)
|
|
|
|
{
|
|
|
|
if (getStackInSlot(i) != null)
|
|
|
|
{
|
|
|
|
inputList.add(getStackInSlot(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SoulForgeRecipe recipe = SoulForgeRecipeRegistry.getMatchingRecipe(inputList, getWorld(), getPos());
|
2016-01-08 10:27:26 -05:00
|
|
|
if (recipe != null && (soulsInGem >= recipe.getMinimumSouls() || burnTime > 0))
|
2016-01-08 09:12:31 -05:00
|
|
|
{
|
2016-01-08 10:27:26 -05:00
|
|
|
if (canCraft(recipe))
|
2016-01-08 09:12:31 -05:00
|
|
|
{
|
2016-01-08 10:27:26 -05:00
|
|
|
burnTime++;
|
|
|
|
|
|
|
|
if (burnTime == ticksRequired)
|
|
|
|
{
|
|
|
|
if (!worldObj.isRemote)
|
|
|
|
{
|
|
|
|
double requiredSouls = recipe.getSoulsDrained();
|
|
|
|
if (requiredSouls > 0)
|
|
|
|
{
|
|
|
|
consumeSouls(requiredSouls);
|
|
|
|
}
|
|
|
|
|
|
|
|
craftItem(recipe);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
burnTime = 0;
|
|
|
|
} else if (burnTime > ticksRequired + 10)
|
|
|
|
{
|
|
|
|
burnTime = 0;
|
|
|
|
}
|
2016-01-08 09:12:31 -05:00
|
|
|
} else
|
|
|
|
{
|
2016-01-08 10:27:26 -05:00
|
|
|
burnTime = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public double getProgressForGui()
|
|
|
|
{
|
|
|
|
return ((double) burnTime) / ticksRequired;
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean canCraft(SoulForgeRecipe recipe)
|
|
|
|
{
|
|
|
|
if (recipe == null)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-08 09:12:31 -05:00
|
|
|
|
2016-01-08 10:27:26 -05:00
|
|
|
ItemStack outputStack = recipe.getRecipeOutput();
|
|
|
|
ItemStack currentOutputStack = getStackInSlot(outputSlot);
|
|
|
|
if (outputStack == null)
|
|
|
|
return false;
|
|
|
|
if (currentOutputStack == null)
|
|
|
|
return true;
|
|
|
|
if (!currentOutputStack.isItemEqual(outputStack))
|
|
|
|
return false;
|
|
|
|
int result = currentOutputStack.stackSize + outputStack.stackSize;
|
|
|
|
return result <= getInventoryStackLimit() && result <= currentOutputStack.getMaxStackSize();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public void craftItem(SoulForgeRecipe recipe)
|
|
|
|
{
|
|
|
|
if (this.canCraft(recipe))
|
|
|
|
{
|
|
|
|
ItemStack outputStack = recipe.getRecipeOutput();
|
|
|
|
ItemStack currentOutputStack = getStackInSlot(outputSlot);
|
|
|
|
|
|
|
|
if (currentOutputStack == null)
|
|
|
|
{
|
|
|
|
setInventorySlotContents(outputSlot, outputStack);
|
|
|
|
} else if (currentOutputStack.getItem() == currentOutputStack.getItem())
|
|
|
|
{
|
|
|
|
currentOutputStack.stackSize += outputStack.stackSize;
|
2016-01-08 09:12:31 -05:00
|
|
|
}
|
2016-01-08 10:27:26 -05:00
|
|
|
|
|
|
|
consumeInventory();
|
2016-01-08 09:12:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean hasSoulGemOrSoul()
|
|
|
|
{
|
|
|
|
ItemStack soulStack = getStackInSlot(soulSlot);
|
|
|
|
|
|
|
|
if (soulStack != null)
|
|
|
|
{
|
|
|
|
if (soulStack.getItem() instanceof ISoul || soulStack.getItem() instanceof ISoulGem)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public double getSouls()
|
|
|
|
{
|
|
|
|
ItemStack soulStack = getStackInSlot(soulSlot);
|
|
|
|
|
|
|
|
if (soulStack != null)
|
|
|
|
{
|
|
|
|
if (soulStack.getItem() instanceof ISoul)
|
|
|
|
{
|
|
|
|
ISoul soul = (ISoul) soulStack.getItem();
|
|
|
|
return soul.getSouls(soulStack);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (soulStack.getItem() instanceof ISoulGem)
|
|
|
|
{
|
|
|
|
ISoulGem soul = (ISoulGem) soulStack.getItem();
|
|
|
|
return soul.getSouls(soulStack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-01-08 10:27:26 -05:00
|
|
|
public double consumeSouls(double requested)
|
|
|
|
{
|
|
|
|
ItemStack soulStack = getStackInSlot(soulSlot);
|
|
|
|
|
|
|
|
if (soulStack != null)
|
|
|
|
{
|
|
|
|
if (soulStack.getItem() instanceof ISoul)
|
|
|
|
{
|
|
|
|
ISoul soul = (ISoul) soulStack.getItem();
|
|
|
|
double souls = soul.drainSouls(soulStack, requested);
|
|
|
|
if (soul.getSouls(soulStack) <= 0)
|
|
|
|
{
|
|
|
|
setInventorySlotContents(soulSlot, null);
|
|
|
|
}
|
|
|
|
return souls;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (soulStack.getItem() instanceof ISoulGem)
|
|
|
|
{
|
|
|
|
ISoulGem soul = (ISoulGem) soulStack.getItem();
|
|
|
|
return soul.drainSouls(soulStack, requested);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-01-08 09:12:31 -05:00
|
|
|
public void consumeInventory()
|
|
|
|
{
|
|
|
|
for (int i = 0; i < 4; i++)
|
|
|
|
{
|
|
|
|
ItemStack inputStack = getStackInSlot(i);
|
|
|
|
if (inputStack != null)
|
|
|
|
{
|
|
|
|
if (inputStack.getItem().hasContainerItem(inputStack))
|
|
|
|
{
|
|
|
|
setInventorySlotContents(i, inputStack.getItem().getContainerItem(inputStack));
|
|
|
|
continue;
|
|
|
|
}
|
2016-01-07 18:05:23 -05:00
|
|
|
|
2016-01-08 09:12:31 -05:00
|
|
|
inputStack.stackSize--;
|
|
|
|
if (inputStack.stackSize <= 0)
|
|
|
|
{
|
|
|
|
setInventorySlotContents(i, null);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-01-07 18:05:23 -05:00
|
|
|
}
|
|
|
|
}
|