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

275 lines
8.8 KiB
Java
Raw Normal View History

package WayofTime.bloodmagic.tile;
2018-02-06 22:17:06 -08:00
import WayofTime.bloodmagic.api.impl.BloodMagicAPI;
import WayofTime.bloodmagic.api.impl.recipe.RecipeTartaricForge;
import WayofTime.bloodmagic.util.Constants;
import WayofTime.bloodmagic.soul.EnumDemonWillType;
import WayofTime.bloodmagic.soul.IDemonWill;
import WayofTime.bloodmagic.soul.IDemonWillConduit;
import WayofTime.bloodmagic.soul.IDemonWillGem;
import WayofTime.bloodmagic.demonAura.WorldDemonWillHandler;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.ITickable;
2018-02-07 18:46:18 -08:00
import net.minecraftforge.items.ItemHandlerHelper;
import java.util.ArrayList;
import java.util.List;
2017-08-15 21:30:48 -07: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;
2017-08-15 21:30:48 -07:00
public TileSoulForge() {
super(6, "soulForge");
}
@Override
2017-08-15 21:30:48 -07:00
public void deserialize(NBTTagCompound tag) {
super.deserialize(tag);
burnTime = tag.getInteger(Constants.NBT.SOUL_FORGE_BURN);
}
@Override
2017-08-15 21:30:48 -07:00
public NBTTagCompound serialize(NBTTagCompound tag) {
super.serialize(tag);
tag.setInteger(Constants.NBT.SOUL_FORGE_BURN, burnTime);
return tag;
}
@Override
2017-08-15 21:30:48 -07:00
public void update() {
if (!getWorld().isRemote) {
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);
2017-08-15 21:30:48 -07:00
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
2017-08-15 21:30:48 -07:00
if (filled > 0) {
2016-03-07 06:39:37 -05:00
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
}
}
}
}
2017-08-15 21:30:48 -07:00
if (!hasSoulGemOrSoul()) {
2016-03-07 06:39:37 -05:00
burnTime = 0;
return;
}
double soulsInGem = getWill(EnumDemonWillType.DEFAULT);
List<ItemStack> inputList = new ArrayList<ItemStack>();
2018-02-06 22:17:06 -08:00
for (int i = 0; i < 4; i++)
if (!getStackInSlot(i).isEmpty())
inputList.add(getStackInSlot(i));
2018-02-06 22:17:06 -08:00
RecipeTartaricForge recipe = BloodMagicAPI.INSTANCE.getRecipeRegistrar().getTartaricForge(inputList);
2017-08-15 21:30:48 -07:00
if (recipe != null && (soulsInGem >= recipe.getMinimumSouls() || burnTime > 0)) {
if (canCraft(recipe)) {
burnTime++;
2017-08-15 21:30:48 -07:00
if (burnTime == ticksRequired) {
if (!getWorld().isRemote) {
2018-02-06 22:17:06 -08:00
double requiredSouls = recipe.getSoulDrain();
2017-08-15 21:30:48 -07:00
if (requiredSouls > 0) {
if (!getWorld().isRemote && soulsInGem >= recipe.getMinimumSouls()) {
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;
2017-08-15 21:30:48 -07:00
} else if (burnTime > ticksRequired + 10) {
burnTime = 0;
}
2017-08-15 21:30:48 -07:00
} else {
burnTime = 0;
}
2017-08-15 21:30:48 -07:00
} else {
2016-03-07 06:39:37 -05:00
burnTime = 0;
}
}
2017-08-15 21:30:48 -07:00
public double getProgressForGui() {
return ((double) burnTime) / ticksRequired;
}
2018-02-06 22:17:06 -08:00
private boolean canCraft(RecipeTartaricForge recipe) {
if (recipe == null)
return false;
ItemStack currentOutputStack = getStackInSlot(outputSlot);
2018-02-07 18:46:18 -08:00
if (recipe.getOutput().isEmpty())
return false;
2016-12-12 19:56:36 -08:00
if (currentOutputStack.isEmpty())
return true;
2018-02-07 18:46:18 -08:00
if (!currentOutputStack.isItemEqual(recipe.getOutput()))
return false;
2018-02-07 18:46:18 -08:00
int result = currentOutputStack.getCount() + recipe.getOutput().getCount();
return result <= getInventoryStackLimit() && result <= currentOutputStack.getMaxStackSize();
}
2018-02-06 22:17:06 -08:00
public void craftItem(RecipeTartaricForge recipe) {
2017-08-15 21:30:48 -07:00
if (this.canCraft(recipe)) {
ItemStack currentOutputStack = getStackInSlot(outputSlot);
2017-08-15 21:30:48 -07:00
if (currentOutputStack.isEmpty()) {
2018-02-07 18:46:18 -08:00
setInventorySlotContents(outputSlot, recipe.getOutput().copy());
} else if (ItemHandlerHelper.canItemStacksStack(currentOutputStack, recipe.getOutput())) {
currentOutputStack.grow(recipe.getOutput().getCount());
}
consumeInventory();
}
}
2017-08-15 21:30:48 -07:00
public boolean hasSoulGemOrSoul() {
ItemStack soulStack = getStackInSlot(soulSlot);
2017-08-15 21:30:48 -07:00
if (!soulStack.isEmpty()) {
if (soulStack.getItem() instanceof IDemonWill || soulStack.getItem() instanceof IDemonWillGem) {
return true;
}
}
return false;
}
2017-08-15 21:30:48 -07:00
public double getWill(EnumDemonWillType type) {
ItemStack soulStack = getStackInSlot(soulSlot);
2017-08-15 21:30:48 -07:00
if (soulStack != null) {
if (soulStack.getItem() instanceof IDemonWill && ((IDemonWill) soulStack.getItem()).getType(soulStack) == type) {
IDemonWill soul = (IDemonWill) soulStack.getItem();
return soul.getWill(type, soulStack);
}
2017-08-15 21:30:48 -07:00
if (soulStack.getItem() instanceof IDemonWillGem) {
IDemonWillGem soul = (IDemonWillGem) soulStack.getItem();
return soul.getWill(type, soulStack);
}
}
return 0;
}
2017-08-15 21:30:48 -07:00
public double consumeSouls(EnumDemonWillType type, double requested) {
ItemStack soulStack = getStackInSlot(soulSlot);
2017-08-15 21:30:48 -07:00
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);
2017-08-15 21:30:48 -07:00
if (soul.getWill(type, soulStack) <= 0) {
2017-02-19 16:06:29 -08:00
setInventorySlotContents(soulSlot, ItemStack.EMPTY);
}
return souls;
}
2017-08-15 21:30:48 -07:00
if (soulStack.getItem() instanceof IDemonWillGem) {
IDemonWillGem soul = (IDemonWillGem) soulStack.getItem();
return soul.drainWill(type, soulStack, requested, true);
}
}
return 0;
}
2017-08-15 21:30:48 -07:00
public void consumeInventory() {
for (int i = 0; i < 4; i++) {
ItemStack inputStack = getStackInSlot(i);
2017-08-15 21:30:48 -07: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);
2017-08-15 21:30:48 -07:00
if (inputStack.isEmpty()) {
2016-12-12 19:56:36 -08:00
setInventorySlotContents(i, ItemStack.EMPTY);
}
}
}
}
2016-02-18 12:11:29 -05:00
@Override
2017-08-15 21:30:48 -07:00
public int getWeight() {
2016-02-18 12:11:29 -05:00
return 50;
}
@Override
2017-08-15 21:30:48 -07:00
public double fillDemonWill(EnumDemonWillType type, double amount, boolean doFill) {
if (amount <= 0) {
2016-02-18 12:11:29 -05:00
return 0;
}
2017-08-15 21:30:48 -07:00
if (!canFill(type)) {
2016-02-18 12:11:29 -05:00
return 0;
}
ItemStack stack = this.getStackInSlot(soulSlot);
2017-08-15 21:30:48 -07: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
2017-08-15 21:30:48 -07:00
public double drainDemonWill(EnumDemonWillType type, double amount, boolean doDrain) {
2016-02-18 12:11:29 -05:00
ItemStack stack = this.getStackInSlot(soulSlot);
2017-08-15 21:30:48 -07: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);
2017-08-15 21:30:48 -07:00
if (current < drained) {
2016-02-18 12:11:29 -05:00
drained = current;
}
2017-08-15 21:30:48 -07:00
if (doDrain) {
drained = willGem.drainWill(type, stack, drained, true);
2016-02-18 12:11:29 -05:00
}
return drained;
}
@Override
2017-08-15 21:30:48 -07:00
public boolean canFill(EnumDemonWillType type) {
2016-03-29 16:34:39 -04:00
return true;
2016-02-18 12:11:29 -05:00
}
@Override
2017-08-15 21:30:48 -07:00
public boolean canDrain(EnumDemonWillType type) {
2016-03-29 16:34:39 -04:00
return true;
2016-02-18 12:11:29 -05:00
}
@Override
2017-08-15 21:30:48 -07:00
public double getCurrentWill(EnumDemonWillType type) {
2016-02-18 12:11:29 -05:00
return 0;
}
}