ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/common/tile/TileEntityCompost.java

113 lines
3.9 KiB
Java
Raw Normal View History

package de.ellpeck.actuallyadditions.common.tile;
import de.ellpeck.actuallyadditions.api.ActuallyAdditionsAPI;
import de.ellpeck.actuallyadditions.common.util.ItemStackHandlerAA.IAcceptor;
import de.ellpeck.actuallyadditions.common.util.ItemStackHandlerAA.IRemover;
import de.ellpeck.actuallyadditions.common.util.ItemUtil;
import de.ellpeck.actuallyadditions.common.util.StackUtil;
import net.minecraft.block.state.IBlockState;
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
public class TileEntityCompost extends TileEntityInventoryBase {
2017-10-31 03:19:48 +01:00
public static final int COMPOST_TIME_TICKS = 3000;
protected int conversionTime;
protected CompostRecipe recipe;
public TileEntityCompost() {
2015-04-19 01:50:02 +02:00
super(1, "compost");
}
public static CompostRecipe getRecipeForInput(ItemStack input) {
if (StackUtil.isValid(input)) {
for (CompostRecipe recipe : ActuallyAdditionsAPI.COMPOST_RECIPES) {
if (recipe.matches(input)) return recipe;
2016-06-01 00:39:35 +02:00
}
}
return null;
}
@Override
public void writeSyncableNBT(NBTTagCompound compound, NBTType type) {
super.writeSyncableNBT(compound, type);
if (type != NBTType.SAVE_BLOCK) {
compound.setInteger("ConversionTime", this.conversionTime);
}
}
@Override
public boolean shouldSyncSlots() {
return true;
}
@Override
public void readSyncableNBT(NBTTagCompound compound, NBTType type) {
super.readSyncableNBT(compound, type);
if (type != NBTType.SAVE_BLOCK) {
this.conversionTime = compound.getInteger("ConversionTime");
}
if (type == NBTType.SYNC) this.world.markBlockRangeForRenderUpdate(this.pos, this.pos.up());
}
@Override
public void updateEntity() {
super.updateEntity();
if (!this.world.isRemote) {
2015-03-19 21:27:56 +01:00
boolean theFlag = this.conversionTime > 0;
2019-02-27 19:53:05 +01:00
ItemStack input = this.inv.getStackInSlot(0);
if (StackUtil.isValid(input)) {
2019-02-27 19:53:05 +01:00
if (this.recipe == null || !this.recipe.matches(input)) this.recipe = getRecipeForInput(input);
if (this.recipe != null) {
this.conversionTime++;
if (this.conversionTime >= COMPOST_TIME_TICKS) {
2019-02-27 19:53:05 +01:00
ItemStack output = this.recipe.getOutput().copy();
output.setCount(input.getCount());
this.inv.setStackInSlot(0, output);
this.conversionTime = 0;
this.markDirty();
}
} else {
this.conversionTime = 0;
}
}
if (theFlag != this.conversionTime > 0) {
2015-03-19 21:27:56 +01:00
this.markDirty();
}
}
}
@Override
2018-08-10 05:04:07 +02:00
public IAcceptor getAcceptor() {
return (slot, stack, automation) -> getRecipeForInput(stack) != null;
}
@Override
2018-08-10 05:04:07 +02:00
public IRemover getRemover() {
2019-02-27 19:53:05 +01:00
return (slot, automation) -> getRecipeForInput(this.inv.getStackInSlot(slot)) == null;
}
public IBlockState getCurrentDisplay() {
2019-02-27 19:53:05 +01:00
ItemStack input = this.inv.getStackInSlot(0);
CompostRecipe displayRecipe = this.recipe;
if (displayRecipe == null || !displayRecipe.matches(input)) displayRecipe = getRecipeForInput(input);
if (displayRecipe == null) for (CompostRecipe r : ActuallyAdditionsAPI.COMPOST_RECIPES) {
if (ItemUtil.areItemsEqual(input, r.getOutput(), true)) return r.getOutputDisplay();
else if (r.getInput().apply(input)) return r.getInputDisplay();
}
2018-08-10 05:04:07 +02:00
if (displayRecipe != null) return displayRecipe.getInputDisplay();
return Blocks.AIR.getDefaultState();
}
public float getHeight() {
2019-02-27 19:53:05 +01:00
ItemStack input = this.inv.getStackInSlot(0);
if (input.isEmpty()) return 0;
return (float) input.getCount() / input.getMaxStackSize();
}
}