BloodMagic/src/main/java/WayofTime/bloodmagic/tile/TileSoulForge.java

336 lines
9.4 KiB
Java
Raw Normal View History

package WayofTime.bloodmagic.tile;
import WayofTime.bloodmagic.api.Constants;
import WayofTime.bloodmagic.api.recipe.TartaricForgeRecipe;
import WayofTime.bloodmagic.api.registry.TartaricForgeRecipeRegistry;
2016-02-18 12:11:29 -05:00
import WayofTime.bloodmagic.api.soul.EnumDemonWillType;
import WayofTime.bloodmagic.api.soul.IDemonWill;
2016-02-18 12:11:29 -05:00
import WayofTime.bloodmagic.api.soul.IDemonWillConduit;
import WayofTime.bloodmagic.api.soul.IDemonWillGem;
import WayofTime.bloodmagic.demonAura.WorldDemonWillHandler;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.ITickable;
import java.util.ArrayList;
import java.util.List;
2016-02-18 12:11:29 -05:00
public class TileSoulForge extends TileInventory implements ITickable, IDemonWillConduit
{
public static final int ticksRequired = 100;
public static final double worldWillTransferRate = 1;
public static final int soulSlot = 4;
public static final int outputSlot = 5;
//Input slots are from 0 to 3.
public int burnTime = 0;
public TileSoulForge()
{
super(6, "soulForge");
}
@Override
public void deserialize(NBTTagCompound tag)
{
super.deserialize(tag);
burnTime = tag.getInteger(Constants.NBT.SOUL_FORGE_BURN);
}
@Override
public NBTTagCompound serialize(NBTTagCompound tag)
{
super.serialize(tag);
tag.setInteger(Constants.NBT.SOUL_FORGE_BURN, burnTime);
return tag;
}
@Override
public void update()
{
2016-12-12 19:56:36 -08:00
if (!getWorld().isRemote)
{
2016-03-07 06:39:37 -05:00
for (EnumDemonWillType type : EnumDemonWillType.values())
{
2016-12-12 19:56:36 -08:00
double willInWorld = WorldDemonWillHandler.getCurrentWill(getWorld(), pos, type);
2016-03-07 06:39:37 -05:00
double filled = Math.min(willInWorld, worldWillTransferRate);
if (filled > 0)
{
2016-03-07 06:39:37 -05:00
filled = this.fillDemonWill(type, filled, false);
2016-12-12 19:56:36 -08:00
filled = WorldDemonWillHandler.drainWill(getWorld(), pos, type, filled, false);
2016-03-07 06:39:37 -05:00
if (filled > 0)
{
this.fillDemonWill(type, filled, true);
2016-12-12 19:56:36 -08:00
WorldDemonWillHandler.drainWill(getWorld(), pos, type, filled, true);
2016-03-07 06:39:37 -05:00
}
}
}
}
2016-03-07 06:39:37 -05:00
if (!hasSoulGemOrSoul())
{
burnTime = 0;
return;
}
double soulsInGem = getWill(EnumDemonWillType.DEFAULT);
List<ItemStack> inputList = new ArrayList<ItemStack>();
for (int i = 0; i < 4; i++)
{
2016-12-12 19:56:36 -08:00
if (!getStackInSlot(i).isEmpty())
{
inputList.add(getStackInSlot(i));
}
}
TartaricForgeRecipe recipe = TartaricForgeRecipeRegistry.getMatchingRecipe(inputList, getWorld(), getPos());
if (recipe != null && (soulsInGem >= recipe.getMinimumSouls() || burnTime > 0))
{
if (canCraft(recipe))
{
burnTime++;
if (burnTime == ticksRequired)
{
2016-12-12 19:56:36 -08:00
if (!getWorld().isRemote)
{
double requiredSouls = recipe.getSoulsDrained();
if (requiredSouls > 0)
{
2016-12-12 19:56:36 -08:00
if (!getWorld().isRemote && soulsInGem >= recipe.getMinimumSouls())
2016-03-07 06:39:37 -05:00
{
consumeSouls(EnumDemonWillType.DEFAULT, requiredSouls);
2016-03-07 06:39:37 -05:00
}
}
2016-12-12 19:56:36 -08:00
if (!getWorld().isRemote && soulsInGem >= recipe.getMinimumSouls())
2016-03-07 06:39:37 -05:00
craftItem(recipe);
}
burnTime = 0;
} else if (burnTime > ticksRequired + 10)
{
burnTime = 0;
}
} else
{
burnTime = 0;
}
2016-03-07 06:39:37 -05:00
} else
{
burnTime = 0;
}
}
public double getProgressForGui()
{
return ((double) burnTime) / ticksRequired;
}
private boolean canCraft(TartaricForgeRecipe recipe)
{
if (recipe == null)
{
return false;
}
ItemStack outputStack = recipe.getRecipeOutput();
ItemStack currentOutputStack = getStackInSlot(outputSlot);
2016-12-12 19:56:36 -08:00
if (outputStack.isEmpty())
return false;
2016-12-12 19:56:36 -08:00
if (currentOutputStack.isEmpty())
return true;
if (!currentOutputStack.isItemEqual(outputStack))
return false;
2016-12-12 19:56:36 -08:00
int result = currentOutputStack.getCount() + outputStack.getCount();
return result <= getInventoryStackLimit() && result <= currentOutputStack.getMaxStackSize();
}
public void craftItem(TartaricForgeRecipe recipe)
{
if (this.canCraft(recipe))
{
ItemStack outputStack = recipe.getRecipeOutput();
ItemStack currentOutputStack = getStackInSlot(outputSlot);
2016-12-12 19:56:36 -08:00
if (currentOutputStack.isEmpty())
{
setInventorySlotContents(outputSlot, outputStack);
} else if (currentOutputStack.getItem() == currentOutputStack.getItem())
{
2016-12-12 19:56:36 -08:00
currentOutputStack.grow(outputStack.getCount());
}
consumeInventory();
}
}
public boolean hasSoulGemOrSoul()
{
ItemStack soulStack = getStackInSlot(soulSlot);
2016-12-12 19:56:36 -08:00
if (!soulStack.isEmpty())
{
if (soulStack.getItem() instanceof IDemonWill || soulStack.getItem() instanceof IDemonWillGem)
{
return true;
}
}
return false;
}
public double getWill(EnumDemonWillType type)
{
ItemStack soulStack = getStackInSlot(soulSlot);
if (soulStack != null)
{
if (soulStack.getItem() instanceof IDemonWill && ((IDemonWill) soulStack.getItem()).getType(soulStack) == type)
{
IDemonWill soul = (IDemonWill) soulStack.getItem();
return soul.getWill(type, soulStack);
}
if (soulStack.getItem() instanceof IDemonWillGem)
{
IDemonWillGem soul = (IDemonWillGem) soulStack.getItem();
return soul.getWill(type, soulStack);
}
}
return 0;
}
public double consumeSouls(EnumDemonWillType type, double requested)
{
ItemStack soulStack = getStackInSlot(soulSlot);
if (soulStack != null)
{
if (soulStack.getItem() instanceof IDemonWill && ((IDemonWill) soulStack.getItem()).getType(soulStack) == type)
{
IDemonWill soul = (IDemonWill) soulStack.getItem();
double souls = soul.drainWill(type, soulStack, requested);
if (soul.getWill(type, soulStack) <= 0)
{
setInventorySlotContents(soulSlot, null);
}
return souls;
}
if (soulStack.getItem() instanceof IDemonWillGem)
{
IDemonWillGem soul = (IDemonWillGem) soulStack.getItem();
return soul.drainWill(type, soulStack, requested, true);
}
}
return 0;
}
public void consumeInventory()
{
for (int i = 0; i < 4; i++)
{
ItemStack inputStack = getStackInSlot(i);
2016-12-12 19:56:36 -08:00
if (!inputStack.isEmpty())
{
if (inputStack.getItem().hasContainerItem(inputStack))
{
setInventorySlotContents(i, inputStack.getItem().getContainerItem(inputStack));
continue;
}
2016-12-12 19:56:36 -08:00
inputStack.shrink(1);
if (inputStack.isEmpty())
{
2016-12-12 19:56:36 -08:00
setInventorySlotContents(i, ItemStack.EMPTY);
}
}
}
}
2016-02-18 12:11:29 -05:00
@Override
public int getWeight()
{
return 50;
}
@Override
public double fillDemonWill(EnumDemonWillType type, double amount, boolean doFill)
{
if (amount <= 0)
{
return 0;
}
if (!canFill(type))
{
return 0;
}
ItemStack stack = this.getStackInSlot(soulSlot);
2016-12-12 19:56:36 -08:00
if (stack.isEmpty() || !(stack.getItem() instanceof IDemonWillGem))
2016-02-18 12:11:29 -05:00
{
return 0;
}
IDemonWillGem willGem = (IDemonWillGem) stack.getItem();
2016-12-12 19:56:36 -08:00
return willGem.fillWill(type, stack, amount, doFill);
2016-02-18 12:11:29 -05:00
}
@Override
public double drainDemonWill(EnumDemonWillType type, double amount, boolean doDrain)
{
ItemStack stack = this.getStackInSlot(soulSlot);
2016-12-12 19:56:36 -08:00
if (stack.isEmpty() || !(stack.getItem() instanceof IDemonWillGem))
2016-02-18 12:11:29 -05:00
{
return 0;
}
IDemonWillGem willGem = (IDemonWillGem) stack.getItem();
double drained = amount;
double current = willGem.getWill(type, stack);
2016-02-18 12:11:29 -05:00
if (current < drained)
{
drained = current;
}
if (doDrain)
{
drained = willGem.drainWill(type, stack, drained, true);
2016-02-18 12:11:29 -05:00
}
return drained;
}
@Override
public boolean canFill(EnumDemonWillType type)
{
2016-03-29 16:34:39 -04:00
return true;
2016-02-18 12:11:29 -05:00
}
@Override
public boolean canDrain(EnumDemonWillType type)
{
2016-03-29 16:34:39 -04:00
return true;
2016-02-18 12:11:29 -05:00
}
@Override
public double getCurrentWill(EnumDemonWillType type)
{
return 0;
}
}