2015-08-29 14:33:25 +02:00
|
|
|
/*
|
2016-05-16 22:52:27 +02:00
|
|
|
* This file ("TileEntityCompost.java") is part of the Actually Additions mod for Minecraft.
|
2015-08-29 14:33:25 +02:00
|
|
|
* It is created and owned by Ellpeck and distributed
|
|
|
|
* under the Actually Additions License to be found at
|
2016-05-16 22:52:27 +02:00
|
|
|
* http://ellpeck.de/actaddlicense
|
2015-08-29 14:33:25 +02:00
|
|
|
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
|
|
|
|
*
|
2017-01-01 16:23:26 +01:00
|
|
|
* © 2015-2017 Ellpeck
|
2015-08-29 14:33:25 +02:00
|
|
|
*/
|
|
|
|
|
2016-01-05 04:47:35 +01:00
|
|
|
package de.ellpeck.actuallyadditions.mod.tile;
|
2015-02-09 17:25:05 +01:00
|
|
|
|
2016-05-20 17:55:33 +02:00
|
|
|
import de.ellpeck.actuallyadditions.api.ActuallyAdditionsAPI;
|
|
|
|
import de.ellpeck.actuallyadditions.api.recipe.CompostRecipe;
|
2018-08-10 05:04:07 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IAcceptor;
|
|
|
|
import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IRemover;
|
2018-06-24 02:44:47 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.ItemUtil;
|
2016-11-16 16:59:00 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
|
2021-02-26 22:15:48 +01:00
|
|
|
import net.minecraft.block.state.BlockState;
|
2018-06-24 02:44:47 +02:00
|
|
|
import net.minecraft.init.Blocks;
|
2015-02-09 17:25:05 +01:00
|
|
|
import net.minecraft.item.ItemStack;
|
2021-02-26 22:15:48 +01:00
|
|
|
import net.minecraft.nbt.CompoundNBT;
|
2015-02-09 17:25:05 +01:00
|
|
|
|
2018-06-24 02:44:47 +02:00
|
|
|
public class TileEntityCompost extends TileEntityInventoryBase {
|
2015-02-09 17:25:05 +01:00
|
|
|
|
2017-10-31 03:19:48 +01:00
|
|
|
public static final int COMPOST_TIME_TICKS = 3000;
|
|
|
|
|
2018-06-24 02:44:47 +02:00
|
|
|
protected int conversionTime;
|
|
|
|
protected CompostRecipe recipe;
|
2015-11-28 19:02:01 +01:00
|
|
|
|
2018-06-24 02:44:47 +02:00
|
|
|
public TileEntityCompost() {
|
2015-04-19 01:50:02 +02:00
|
|
|
super(1, "compost");
|
2015-02-09 17:25:05 +01:00
|
|
|
}
|
|
|
|
|
2018-06-24 02:44:47 +02:00
|
|
|
public static CompostRecipe getRecipeForInput(ItemStack input) {
|
|
|
|
if (StackUtil.isValid(input)) {
|
|
|
|
for (CompostRecipe recipe : ActuallyAdditionsAPI.COMPOST_RECIPES) {
|
2021-02-26 22:15:48 +01:00
|
|
|
if (recipe.matches(input)) {
|
|
|
|
return recipe;
|
|
|
|
}
|
2016-06-01 00:39:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-02-01 20:32:49 +01:00
|
|
|
@Override
|
2021-02-26 22:15:48 +01:00
|
|
|
public void writeSyncableNBT(CompoundNBT compound, NBTType type) {
|
2016-07-02 15:01:46 +02:00
|
|
|
super.writeSyncableNBT(compound, type);
|
2018-06-24 02:44:47 +02:00
|
|
|
if (type != NBTType.SAVE_BLOCK) {
|
2016-07-02 15:01:46 +02:00
|
|
|
compound.setInteger("ConversionTime", this.conversionTime);
|
|
|
|
}
|
2016-02-01 20:32:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-06-24 02:44:47 +02:00
|
|
|
public boolean shouldSyncSlots() {
|
2016-02-01 20:32:49 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-02-26 22:15:48 +01:00
|
|
|
public void readSyncableNBT(CompoundNBT compound, NBTType type) {
|
2016-07-02 15:01:46 +02:00
|
|
|
super.readSyncableNBT(compound, type);
|
2018-06-24 02:44:47 +02:00
|
|
|
if (type != NBTType.SAVE_BLOCK) {
|
2016-07-02 15:01:46 +02:00
|
|
|
this.conversionTime = compound.getInteger("ConversionTime");
|
|
|
|
}
|
2021-02-26 22:15:48 +01:00
|
|
|
if (type == NBTType.SYNC) {
|
|
|
|
this.world.markBlockRangeForRenderUpdate(this.pos, this.pos.up());
|
|
|
|
}
|
2016-02-01 20:32:49 +01:00
|
|
|
}
|
|
|
|
|
2015-02-20 22:45:33 +01:00
|
|
|
@Override
|
2018-06-24 02:44:47 +02:00
|
|
|
public void updateEntity() {
|
2015-11-18 23:11:24 +01:00
|
|
|
super.updateEntity();
|
2018-06-24 02:44:47 +02:00
|
|
|
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);
|
2018-06-24 02:44:47 +02:00
|
|
|
if (StackUtil.isValid(input)) {
|
2021-02-26 22:15:48 +01:00
|
|
|
if (this.recipe == null || !this.recipe.matches(input)) {
|
|
|
|
this.recipe = getRecipeForInput(input);
|
|
|
|
}
|
2019-02-27 19:53:05 +01:00
|
|
|
if (this.recipe != null) {
|
2016-05-20 17:55:33 +02:00
|
|
|
this.conversionTime++;
|
2018-06-24 02:44:47 +02:00
|
|
|
if (this.conversionTime >= COMPOST_TIME_TICKS) {
|
2019-02-27 19:53:05 +01:00
|
|
|
ItemStack output = this.recipe.getOutput().copy();
|
2018-06-23 01:39:30 +02:00
|
|
|
output.setCount(input.getCount());
|
|
|
|
this.inv.setStackInSlot(0, output);
|
2016-05-20 17:55:33 +02:00
|
|
|
this.conversionTime = 0;
|
|
|
|
this.markDirty();
|
|
|
|
}
|
2018-06-24 02:44:47 +02:00
|
|
|
} else {
|
2015-02-09 17:25:05 +01:00
|
|
|
this.conversionTime = 0;
|
|
|
|
}
|
|
|
|
}
|
2016-05-20 17:55:33 +02:00
|
|
|
|
2018-06-24 02:44:47 +02:00
|
|
|
if (theFlag != this.conversionTime > 0) {
|
2015-03-19 21:27:56 +01:00
|
|
|
this.markDirty();
|
|
|
|
}
|
2015-02-09 17:25:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-07 02:23:31 +01:00
|
|
|
@Override
|
2018-08-10 05:04:07 +02:00
|
|
|
public IAcceptor getAcceptor() {
|
|
|
|
return (slot, stack, automation) -> getRecipeForInput(stack) != null;
|
2015-03-07 02:23:31 +01:00
|
|
|
}
|
|
|
|
|
2016-06-03 22:16:23 +02:00
|
|
|
@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;
|
2016-05-20 17:55:33 +02:00
|
|
|
}
|
2018-06-24 02:44:47 +02:00
|
|
|
|
2021-02-26 22:15:48 +01:00
|
|
|
public BlockState getCurrentDisplay() {
|
2019-02-27 19:53:05 +01:00
|
|
|
ItemStack input = this.inv.getStackInSlot(0);
|
|
|
|
CompostRecipe displayRecipe = this.recipe;
|
2021-02-26 22:15:48 +01:00
|
|
|
if (displayRecipe == null || !displayRecipe.matches(input)) {
|
|
|
|
displayRecipe = getRecipeForInput(input);
|
|
|
|
}
|
2018-06-24 02:44:47 +02:00
|
|
|
|
2021-02-26 22:15:48 +01:00
|
|
|
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-06-24 02:44:47 +02:00
|
|
|
}
|
|
|
|
|
2021-02-26 22:15:48 +01:00
|
|
|
if (displayRecipe != null) {
|
|
|
|
return displayRecipe.getInputDisplay();
|
|
|
|
}
|
2018-06-24 02:44:47 +02:00
|
|
|
return Blocks.AIR.getDefaultState();
|
|
|
|
}
|
|
|
|
|
|
|
|
public float getHeight() {
|
2019-02-27 19:53:05 +01:00
|
|
|
ItemStack input = this.inv.getStackInSlot(0);
|
2021-02-26 22:15:48 +01:00
|
|
|
if (input.isEmpty()) {
|
|
|
|
return 0;
|
|
|
|
}
|
2018-06-24 02:44:47 +02:00
|
|
|
return (float) input.getCount() / input.getMaxStackSize();
|
|
|
|
}
|
2015-02-09 17:25:05 +01:00
|
|
|
}
|