2015-12-23 15:20:26 -05:00
|
|
|
package WayofTime.bloodmagic.tile;
|
|
|
|
|
2018-02-11 11:40:13 -08:00
|
|
|
import WayofTime.bloodmagic.api.impl.BloodMagicAPI;
|
|
|
|
import WayofTime.bloodmagic.api.impl.recipe.RecipeAlchemyArray;
|
2018-02-05 17:04:38 -08:00
|
|
|
import WayofTime.bloodmagic.apibutnotreally.Constants;
|
|
|
|
import WayofTime.bloodmagic.apibutnotreally.alchemyCrafting.AlchemyArrayEffect;
|
2018-02-11 11:40:13 -08:00
|
|
|
import WayofTime.bloodmagic.apibutnotreally.alchemyCrafting.AlchemyArrayEffectCraftingNew;
|
2018-02-05 17:04:38 -08:00
|
|
|
import WayofTime.bloodmagic.apibutnotreally.iface.IAlchemyArray;
|
|
|
|
import WayofTime.bloodmagic.apibutnotreally.registry.AlchemyArrayRecipeRegistry;
|
2016-06-28 13:35:42 -04:00
|
|
|
import net.minecraft.block.state.IBlockState;
|
|
|
|
import net.minecraft.entity.Entity;
|
2015-12-23 18:45:47 -05:00
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
2016-06-29 07:43:04 -04:00
|
|
|
import net.minecraft.util.EnumFacing;
|
2015-12-23 18:45:47 -05:00
|
|
|
import net.minecraft.util.ITickable;
|
2015-12-23 15:20:26 -05:00
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
public class TileAlchemyArray extends TileInventory implements ITickable, IAlchemyArray {
|
2015-12-30 15:34:40 -05:00
|
|
|
public boolean isActive = false;
|
|
|
|
public int activeCounter = 0;
|
2017-08-15 21:30:48 -07:00
|
|
|
public EnumFacing rotation = EnumFacing.HORIZONTALS[0];
|
|
|
|
;
|
2016-06-29 07:43:04 -04:00
|
|
|
|
2016-06-27 15:07:00 -04:00
|
|
|
private String key = "empty";
|
2016-06-27 11:21:37 -04:00
|
|
|
private AlchemyArrayEffect arrayEffect;
|
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
public TileAlchemyArray() {
|
2015-12-30 15:34:40 -05:00
|
|
|
super(2, "alchemyArray");
|
|
|
|
}
|
2015-12-23 20:19:06 -05:00
|
|
|
|
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);
|
2016-06-28 13:35:42 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-30 15:34:40 -05:00
|
|
|
@Override
|
2017-08-15 21:30:48 -07:00
|
|
|
public void deserialize(NBTTagCompound tagCompound) {
|
2016-09-07 17:46:06 -07:00
|
|
|
super.deserialize(tagCompound);
|
2015-12-30 15:34:40 -05:00
|
|
|
this.isActive = tagCompound.getBoolean("isActive");
|
|
|
|
this.activeCounter = tagCompound.getInteger("activeCounter");
|
2016-06-27 11:21:37 -04:00
|
|
|
this.key = tagCompound.getString("key");
|
2016-06-29 07:43:04 -04:00
|
|
|
this.rotation = EnumFacing.HORIZONTALS[tagCompound.getInteger(Constants.NBT.DIRECTION)];
|
2016-06-27 11:21:37 -04:00
|
|
|
|
|
|
|
NBTTagCompound arrayTag = tagCompound.getCompoundTag("arrayTag");
|
|
|
|
arrayEffect = AlchemyArrayRecipeRegistry.getAlchemyArrayEffect(key);
|
2017-08-15 21:30:48 -07:00
|
|
|
if (arrayEffect != null) {
|
2016-06-27 11:21:37 -04:00
|
|
|
arrayEffect.readFromNBT(arrayTag);
|
|
|
|
}
|
2015-12-30 15:34:40 -05:00
|
|
|
}
|
2015-12-23 18:45:47 -05:00
|
|
|
|
2015-12-30 15:34:40 -05:00
|
|
|
@Override
|
2017-08-15 21:30:48 -07:00
|
|
|
public NBTTagCompound serialize(NBTTagCompound tagCompound) {
|
2016-09-07 17:46:06 -07:00
|
|
|
super.serialize(tagCompound);
|
2015-12-30 15:34:40 -05:00
|
|
|
tagCompound.setBoolean("isActive", isActive);
|
|
|
|
tagCompound.setInteger("activeCounter", activeCounter);
|
2016-06-28 07:26:16 -04:00
|
|
|
tagCompound.setString("key", "".equals(key) ? "empty" : key);
|
2016-06-29 07:43:04 -04:00
|
|
|
tagCompound.setInteger(Constants.NBT.DIRECTION, rotation.getHorizontalIndex());
|
2016-06-27 11:21:37 -04:00
|
|
|
|
|
|
|
NBTTagCompound arrayTag = new NBTTagCompound();
|
2017-08-15 21:30:48 -07:00
|
|
|
if (arrayEffect != null) {
|
2016-06-27 11:21:37 -04:00
|
|
|
arrayEffect.writeToNBT(arrayTag);
|
|
|
|
}
|
|
|
|
tagCompound.setTag("arrayTag", arrayTag);
|
|
|
|
|
2016-05-19 17:43:33 -07:00
|
|
|
return tagCompound;
|
2015-12-30 15:34:40 -05:00
|
|
|
}
|
2015-12-23 18:45:47 -05:00
|
|
|
|
2016-02-08 14:41:58 -08:00
|
|
|
@Override
|
2017-08-15 21:30:48 -07:00
|
|
|
public int getInventoryStackLimit() {
|
2016-02-08 14:41:58 -08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-12-30 15:34:40 -05:00
|
|
|
@Override
|
2017-08-15 21:30:48 -07:00
|
|
|
public void update() {
|
|
|
|
if (isActive && attemptCraft()) {
|
2015-12-30 15:34:40 -05:00
|
|
|
activeCounter++;
|
2017-08-15 21:30:48 -07:00
|
|
|
} else {
|
2015-12-30 15:34:40 -05:00
|
|
|
isActive = false;
|
|
|
|
activeCounter = 0;
|
2016-06-27 11:21:37 -04:00
|
|
|
arrayEffect = null;
|
2016-06-27 15:07:00 -04:00
|
|
|
key = "empty";
|
2015-12-30 15:34:40 -05:00
|
|
|
}
|
|
|
|
}
|
2015-12-23 20:19:06 -05:00
|
|
|
|
2016-06-27 20:01:34 -04:00
|
|
|
/**
|
|
|
|
* This occurs when the block is destroyed.
|
|
|
|
*/
|
|
|
|
@Override
|
2017-08-15 21:30:48 -07:00
|
|
|
public void dropItems() {
|
2016-06-27 20:01:34 -04:00
|
|
|
super.dropItems();
|
2017-08-15 21:30:48 -07:00
|
|
|
if (arrayEffect != null) {
|
2016-06-27 20:01:34 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
public boolean attemptCraft() {
|
2015-12-30 15:34:40 -05:00
|
|
|
AlchemyArrayEffect effect = AlchemyArrayRecipeRegistry.getAlchemyArrayEffect(this.getStackInSlot(0), this.getStackInSlot(1));
|
2017-08-15 21:30:48 -07:00
|
|
|
if (effect != null) {
|
|
|
|
if (arrayEffect == null) {
|
2016-06-27 11:21:37 -04:00
|
|
|
arrayEffect = effect;
|
|
|
|
key = effect.getKey();
|
2017-08-15 21:30:48 -07:00
|
|
|
} else {
|
2016-06-27 11:21:37 -04:00
|
|
|
String effectKey = effect.getKey();
|
2017-08-15 21:30:48 -07:00
|
|
|
if (effectKey.equals(key)) {
|
2016-06-27 11:21:37 -04:00
|
|
|
//Good! Moving on.
|
2017-08-15 21:30:48 -07:00
|
|
|
} else {
|
2016-06-27 11:21:37 -04:00
|
|
|
//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 {
|
2018-02-11 11:40:13 -08:00
|
|
|
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;
|
|
|
|
}
|
2016-06-27 11:21:37 -04:00
|
|
|
}
|
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
if (arrayEffect != null) {
|
2015-12-30 15:34:40 -05:00
|
|
|
isActive = true;
|
2015-12-23 20:19:06 -05:00
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
if (arrayEffect.update(this, this.activeCounter)) {
|
2015-12-30 15:34:40 -05:00
|
|
|
this.decrStackSize(0, 1);
|
|
|
|
this.decrStackSize(1, 1);
|
2016-12-12 19:56:36 -08:00
|
|
|
this.getWorld().setBlockToAir(getPos());
|
2015-12-30 15:34:40 -05:00
|
|
|
}
|
2015-12-23 20:19:06 -05:00
|
|
|
|
2015-12-30 15:34:40 -05:00
|
|
|
return true;
|
|
|
|
}
|
2015-12-23 20:19:06 -05:00
|
|
|
|
2015-12-30 15:34:40 -05:00
|
|
|
return false;
|
|
|
|
}
|
2017-08-14 20:53:42 -07:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public EnumFacing getRotation() {
|
|
|
|
return rotation;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setRotation(EnumFacing rotation) {
|
|
|
|
this.rotation = rotation;
|
|
|
|
}
|
2015-12-23 15:20:26 -05:00
|
|
|
}
|