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

151 lines
4.9 KiB
Java
Raw Normal View History

package WayofTime.bloodmagic.tile;
import WayofTime.bloodmagic.api.impl.BloodMagicAPI;
import WayofTime.bloodmagic.api.impl.recipe.RecipeAlchemyArray;
import WayofTime.bloodmagic.apibutnotreally.Constants;
import WayofTime.bloodmagic.apibutnotreally.alchemyCrafting.AlchemyArrayEffect;
import WayofTime.bloodmagic.apibutnotreally.alchemyCrafting.AlchemyArrayEffectCraftingNew;
import WayofTime.bloodmagic.apibutnotreally.iface.IAlchemyArray;
import WayofTime.bloodmagic.apibutnotreally.registry.AlchemyArrayRecipeRegistry;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.Entity;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ITickable;
2017-08-15 21:30:48 -07:00
public class TileAlchemyArray extends TileInventory implements ITickable, IAlchemyArray {
public boolean isActive = false;
public int activeCounter = 0;
2017-08-15 21:30:48 -07:00
public EnumFacing rotation = EnumFacing.HORIZONTALS[0];
;
private String key = "empty";
private AlchemyArrayEffect arrayEffect;
2017-08-15 21:30:48 -07:00
public TileAlchemyArray() {
super(2, "alchemyArray");
}
2017-08-15 21:30:48 -07:00
public void onEntityCollidedWithBlock(IBlockState state, Entity entity) {
if (arrayEffect != null) {
2016-12-12 19:56:36 -08:00
arrayEffect.onEntityCollidedWithBlock(this, getWorld(), pos, state, entity);
}
}
@Override
2017-08-15 21:30:48 -07:00
public void deserialize(NBTTagCompound tagCompound) {
super.deserialize(tagCompound);
this.isActive = tagCompound.getBoolean("isActive");
this.activeCounter = tagCompound.getInteger("activeCounter");
this.key = tagCompound.getString("key");
this.rotation = EnumFacing.HORIZONTALS[tagCompound.getInteger(Constants.NBT.DIRECTION)];
NBTTagCompound arrayTag = tagCompound.getCompoundTag("arrayTag");
arrayEffect = AlchemyArrayRecipeRegistry.getAlchemyArrayEffect(key);
2017-08-15 21:30:48 -07:00
if (arrayEffect != null) {
arrayEffect.readFromNBT(arrayTag);
}
}
@Override
2017-08-15 21:30:48 -07:00
public NBTTagCompound serialize(NBTTagCompound tagCompound) {
super.serialize(tagCompound);
tagCompound.setBoolean("isActive", isActive);
tagCompound.setInteger("activeCounter", activeCounter);
tagCompound.setString("key", "".equals(key) ? "empty" : key);
tagCompound.setInteger(Constants.NBT.DIRECTION, rotation.getHorizontalIndex());
NBTTagCompound arrayTag = new NBTTagCompound();
2017-08-15 21:30:48 -07:00
if (arrayEffect != null) {
arrayEffect.writeToNBT(arrayTag);
}
tagCompound.setTag("arrayTag", arrayTag);
return tagCompound;
}
@Override
2017-08-15 21:30:48 -07:00
public int getInventoryStackLimit() {
return 1;
}
@Override
2017-08-15 21:30:48 -07:00
public void update() {
if (isActive && attemptCraft()) {
activeCounter++;
2017-08-15 21:30:48 -07:00
} else {
isActive = false;
activeCounter = 0;
arrayEffect = null;
key = "empty";
}
}
/**
* This occurs when the block is destroyed.
*/
@Override
2017-08-15 21:30:48 -07:00
public void dropItems() {
super.dropItems();
2017-08-15 21:30:48 -07:00
if (arrayEffect != null) {
}
}
2017-08-15 21:30:48 -07:00
public boolean attemptCraft() {
AlchemyArrayEffect effect = AlchemyArrayRecipeRegistry.getAlchemyArrayEffect(this.getStackInSlot(0), this.getStackInSlot(1));
2017-08-15 21:30:48 -07:00
if (effect != null) {
if (arrayEffect == null) {
arrayEffect = effect;
key = effect.getKey();
2017-08-15 21:30:48 -07:00
} else {
String effectKey = effect.getKey();
2017-08-15 21:30:48 -07:00
if (effectKey.equals(key)) {
//Good! Moving on.
2017-08-15 21:30:48 -07:00
} else {
//Something has changed, therefore we have to move our stuffs.
//TODO: Add an AlchemyArrayEffect.onBreak(); ?
arrayEffect = effect;
key = effect.getKey();
}
}
2017-08-15 21:30:48 -07:00
} else {
RecipeAlchemyArray recipe = BloodMagicAPI.INSTANCE.getRecipeRegistrar().getAlchemyArray(getStackInSlot(0), getStackInSlot(1));
if (recipe == null)
return false;
AlchemyArrayEffect newEffect = new AlchemyArrayEffectCraftingNew(recipe);
if (arrayEffect == null) {
arrayEffect = newEffect;
key = newEffect.key;
} else if (!newEffect.key.equals(key)) {
arrayEffect = newEffect;
key = newEffect.key;
}
}
2017-08-15 21:30:48 -07:00
if (arrayEffect != null) {
isActive = true;
2017-08-15 21:30:48 -07:00
if (arrayEffect.update(this, this.activeCounter)) {
this.decrStackSize(0, 1);
this.decrStackSize(1, 1);
2016-12-12 19:56:36 -08:00
this.getWorld().setBlockToAir(getPos());
}
return true;
}
return false;
}
@Override
public EnumFacing getRotation() {
return rotation;
}
public void setRotation(EnumFacing rotation) {
this.rotation = rotation;
}
}