2015-08-29 14:33:25 +02:00
|
|
|
/*
|
2016-05-16 22:52:27 +02:00
|
|
|
* This file ("TileEntityCanolaPress.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-05-20 22:39:43 +02:00
|
|
|
|
2022-01-07 00:16:37 +01:00
|
|
|
import de.ellpeck.actuallyadditions.api.ActuallyAdditionsAPI;
|
2021-08-31 00:44:39 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.blocks.ActuallyBlocks;
|
2022-01-07 00:16:37 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.crafting.PressingRecipe;
|
|
|
|
import de.ellpeck.actuallyadditions.mod.crafting.SingleItem;
|
|
|
|
import de.ellpeck.actuallyadditions.mod.fluids.OutputOnlyFluidTank;
|
2021-02-28 12:33:01 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.inventory.ContainerCanolaPress;
|
2018-08-10 05:04:07 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IAcceptor;
|
|
|
|
import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IRemover;
|
2016-11-16 16:59:00 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
|
2024-03-02 21:23:08 +01:00
|
|
|
import net.minecraft.core.BlockPos;
|
|
|
|
import net.minecraft.core.Direction;
|
|
|
|
import net.minecraft.nbt.CompoundTag;
|
|
|
|
import net.minecraft.network.chat.Component;
|
|
|
|
import net.minecraft.world.MenuProvider;
|
|
|
|
import net.minecraft.world.entity.player.Inventory;
|
|
|
|
import net.minecraft.world.entity.player.Player;
|
|
|
|
import net.minecraft.world.inventory.AbstractContainerMenu;
|
|
|
|
import net.minecraft.world.item.ItemStack;
|
|
|
|
import net.minecraft.world.level.Level;
|
|
|
|
import net.minecraft.world.level.block.entity.BlockEntity;
|
|
|
|
import net.minecraft.world.level.block.state.BlockState;
|
2021-02-27 16:33:00 +01:00
|
|
|
import net.minecraftforge.api.distmarker.Dist;
|
|
|
|
import net.minecraftforge.api.distmarker.OnlyIn;
|
|
|
|
import net.minecraftforge.common.util.LazyOptional;
|
2016-11-26 08:58:42 +01:00
|
|
|
import net.minecraftforge.energy.IEnergyStorage;
|
2016-11-26 21:32:27 +01:00
|
|
|
import net.minecraftforge.fluids.FluidStack;
|
2024-03-03 01:20:53 +01:00
|
|
|
import net.minecraftforge.fluids.FluidType;
|
2021-02-27 16:33:00 +01:00
|
|
|
import net.minecraftforge.fluids.capability.IFluidHandler;
|
2016-05-19 20:05:12 +02:00
|
|
|
|
2021-02-28 12:33:01 +01:00
|
|
|
import javax.annotation.Nullable;
|
2022-01-07 00:16:37 +01:00
|
|
|
import java.util.Optional;
|
2021-02-28 12:33:01 +01:00
|
|
|
|
2024-03-02 21:23:08 +01:00
|
|
|
public class TileEntityCanolaPress extends TileEntityInventoryBase implements MenuProvider, ISharingFluidHandler {
|
2015-05-20 22:39:43 +02:00
|
|
|
|
2015-12-01 19:48:09 +01:00
|
|
|
public static final int ENERGY_USE = 35;
|
|
|
|
private static final int TIME = 30;
|
2016-11-26 20:43:50 +01:00
|
|
|
public final CustomEnergyStorage storage = new CustomEnergyStorage(40000, 100, 0);
|
2021-02-27 16:33:00 +01:00
|
|
|
public final LazyOptional<IEnergyStorage> lazyEnergy = LazyOptional.of(() -> this.storage);
|
|
|
|
|
2024-03-03 01:20:53 +01:00
|
|
|
public final OutputOnlyFluidTank tank = new OutputOnlyFluidTank(2 * FluidType.BUCKET_VOLUME);
|
2021-02-27 16:33:00 +01:00
|
|
|
public final LazyOptional<IFluidHandler> lazyFluid = LazyOptional.of(() -> this.tank);
|
|
|
|
|
2015-05-20 22:39:43 +02:00
|
|
|
public int currentProcessTime;
|
2015-10-03 10:16:18 +02:00
|
|
|
private int lastEnergyStored;
|
|
|
|
private int lastTankAmount;
|
2015-07-02 04:21:21 +02:00
|
|
|
private int lastProcessTime;
|
2015-05-20 22:39:43 +02:00
|
|
|
|
2024-03-02 21:23:08 +01:00
|
|
|
public TileEntityCanolaPress(BlockPos pos, BlockState state) {
|
|
|
|
super(ActuallyBlocks.CANOLA_PRESS.getTileEntityType(), pos, state, 1);
|
2021-11-21 21:34:08 +01:00
|
|
|
}
|
|
|
|
|
2021-02-26 22:15:48 +01:00
|
|
|
@OnlyIn(Dist.CLIENT)
|
2018-08-10 05:04:07 +02:00
|
|
|
public int getTankScaled(int i) {
|
|
|
|
return this.tank.getFluidAmount() * i / this.tank.getCapacity();
|
2015-12-01 19:48:09 +01:00
|
|
|
}
|
|
|
|
|
2021-02-26 22:15:48 +01:00
|
|
|
@OnlyIn(Dist.CLIENT)
|
2018-08-10 05:04:07 +02:00
|
|
|
public int getProcessScaled(int i) {
|
|
|
|
return this.currentProcessTime * i / TIME;
|
2015-12-01 19:48:09 +01:00
|
|
|
}
|
|
|
|
|
2021-02-26 22:15:48 +01:00
|
|
|
@OnlyIn(Dist.CLIENT)
|
2018-08-10 05:04:07 +02:00
|
|
|
public int getEnergyScaled(int i) {
|
|
|
|
return this.storage.getEnergyStored() * i / this.storage.getMaxEnergyStored();
|
2015-12-01 19:48:09 +01:00
|
|
|
}
|
|
|
|
|
2016-02-01 20:32:49 +01:00
|
|
|
@Override
|
2024-03-02 21:23:08 +01:00
|
|
|
public void writeSyncableNBT(CompoundTag compound, NBTType type) {
|
2018-08-10 05:04:07 +02:00
|
|
|
if (type != NBTType.SAVE_BLOCK) {
|
2021-02-27 16:33:00 +01:00
|
|
|
compound.putInt("ProcessTime", this.currentProcessTime);
|
2016-07-02 15:01:46 +02:00
|
|
|
}
|
2016-02-01 20:32:49 +01:00
|
|
|
this.storage.writeToNBT(compound);
|
|
|
|
this.tank.writeToNBT(compound);
|
2016-07-02 15:01:46 +02:00
|
|
|
super.writeSyncableNBT(compound, type);
|
2016-02-01 20:32:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2024-03-02 21:23:08 +01:00
|
|
|
public void readSyncableNBT(CompoundTag compound, NBTType type) {
|
2018-08-10 05:04:07 +02:00
|
|
|
if (type != NBTType.SAVE_BLOCK) {
|
2021-02-27 16:33:00 +01:00
|
|
|
this.currentProcessTime = compound.getInt("ProcessTime");
|
2016-07-02 15:01:46 +02:00
|
|
|
}
|
2016-02-01 20:32:49 +01:00
|
|
|
this.storage.readFromNBT(compound);
|
|
|
|
this.tank.readFromNBT(compound);
|
2016-07-02 15:01:46 +02:00
|
|
|
super.readSyncableNBT(compound, type);
|
2016-02-01 20:32:49 +01:00
|
|
|
}
|
|
|
|
|
2024-03-02 21:23:08 +01:00
|
|
|
public static <T extends BlockEntity> void clientTick(Level level, BlockPos pos, BlockState state, T t) {
|
|
|
|
if (t instanceof TileEntityCanolaPress tile) {
|
|
|
|
tile.clientTick();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static <T extends BlockEntity> void serverTick(Level level, BlockPos pos, BlockState state, T t) {
|
|
|
|
if (t instanceof TileEntityCanolaPress tile) {
|
|
|
|
tile.serverTick();
|
|
|
|
|
|
|
|
Optional<PressingRecipe> recipe = getRecipeForInput(tile.inv.getStackInSlot(0));
|
2022-01-07 00:16:37 +01:00
|
|
|
recipe.ifPresent(r -> {
|
2024-03-02 21:23:08 +01:00
|
|
|
if ((r.getOutput().isFluidEqual(tile.tank.getFluid()) || tile.tank.isEmpty()) && r.getOutput().getAmount() <= tile.tank.getCapacity() - tile.tank.getFluidAmount()) {
|
|
|
|
if (tile.storage.getEnergyStored() >= ENERGY_USE) {
|
|
|
|
tile.currentProcessTime++;
|
|
|
|
tile.storage.extractEnergyInternal(ENERGY_USE, false);
|
|
|
|
if (tile.currentProcessTime >= TIME) {
|
|
|
|
tile.currentProcessTime = 0;
|
|
|
|
|
|
|
|
tile.inv.setStackInSlot(0, StackUtil.shrink(tile.inv.getStackInSlot(0), 1));
|
2022-01-07 00:16:37 +01:00
|
|
|
FluidStack produced = r.getOutput().copy();
|
2024-03-02 21:23:08 +01:00
|
|
|
tile.tank.fillInternal(produced, IFluidHandler.FluidAction.EXECUTE);
|
|
|
|
tile.setChanged();
|
2022-01-07 00:16:37 +01:00
|
|
|
}
|
2015-06-13 12:52:22 +02:00
|
|
|
}
|
2015-05-20 22:39:43 +02:00
|
|
|
}
|
2022-01-07 00:16:37 +01:00
|
|
|
});
|
|
|
|
if (!recipe.isPresent())
|
2024-03-02 21:23:08 +01:00
|
|
|
tile.currentProcessTime = 0;
|
2015-07-02 04:21:21 +02:00
|
|
|
|
2024-03-02 21:23:08 +01:00
|
|
|
if ((tile.storage.getEnergyStored() != tile.lastEnergyStored || tile.tank.getFluidAmount() != tile.lastTankAmount | tile.currentProcessTime != tile.lastProcessTime) && tile.sendUpdateWithInterval()) {
|
|
|
|
tile.lastEnergyStored = tile.storage.getEnergyStored();
|
|
|
|
tile.lastProcessTime = tile.currentProcessTime;
|
|
|
|
tile.lastTankAmount = tile.tank.getFluidAmount();
|
2015-07-02 04:21:21 +02:00
|
|
|
}
|
2015-05-20 22:39:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-07 00:16:37 +01:00
|
|
|
public boolean validInput(ItemStack stack) {
|
|
|
|
return getRecipeForInput(stack).isPresent();
|
|
|
|
}
|
|
|
|
|
2022-01-07 01:40:19 +01:00
|
|
|
public static Optional<PressingRecipe> getRecipeForInput(ItemStack stack) {
|
2022-01-07 00:16:37 +01:00
|
|
|
return ActuallyAdditionsAPI.PRESSING_RECIPES.stream().filter(recipe -> recipe.matches(new SingleItem(stack), null)).findFirst();
|
|
|
|
}
|
|
|
|
|
2016-02-01 17:49:55 +01:00
|
|
|
@Override
|
2018-08-10 05:04:07 +02:00
|
|
|
public IAcceptor getAcceptor() {
|
2022-01-07 00:16:37 +01:00
|
|
|
return (slot, stack, automation) -> slot == 0 && validInput(stack);
|
2016-02-01 17:49:55 +01:00
|
|
|
}
|
|
|
|
|
2015-05-20 22:39:43 +02:00
|
|
|
@Override
|
2018-08-10 05:04:07 +02:00
|
|
|
public IRemover getRemover() {
|
|
|
|
return (slot, automation) -> !automation;
|
2015-05-20 22:39:43 +02:00
|
|
|
}
|
|
|
|
|
2016-06-05 02:16:52 +02:00
|
|
|
@Override
|
2021-02-27 16:33:00 +01:00
|
|
|
public LazyOptional<IFluidHandler> getFluidHandler(Direction facing) {
|
|
|
|
return this.lazyFluid;
|
2016-06-05 02:16:52 +02:00
|
|
|
}
|
2016-06-10 21:52:37 +02:00
|
|
|
|
|
|
|
@Override
|
2018-08-10 05:04:07 +02:00
|
|
|
public int getMaxFluidAmountToSplitShare() {
|
2016-08-31 20:16:36 +02:00
|
|
|
return this.tank.getFluidAmount();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-08-10 05:04:07 +02:00
|
|
|
public boolean doesShareFluid() {
|
2016-08-31 20:16:36 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-02-27 13:24:45 +01:00
|
|
|
public Direction[] getFluidShareSides() {
|
|
|
|
return Direction.values();
|
2016-08-31 20:16:36 +02:00
|
|
|
}
|
2016-11-26 08:58:42 +01:00
|
|
|
|
|
|
|
@Override
|
2021-02-27 16:33:00 +01:00
|
|
|
public LazyOptional<IEnergyStorage> getEnergyStorage(Direction facing) {
|
|
|
|
return this.lazyEnergy;
|
2016-11-26 08:58:42 +01:00
|
|
|
}
|
2021-02-28 12:33:01 +01:00
|
|
|
|
|
|
|
@Override
|
2024-03-02 21:23:08 +01:00
|
|
|
public Component getDisplayName() {
|
2024-03-03 01:20:53 +01:00
|
|
|
return Component.translatable("container.actuallyadditions.canola_press");
|
2021-02-28 12:33:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@Override
|
2024-03-02 21:23:08 +01:00
|
|
|
public AbstractContainerMenu createMenu(int windowId, Inventory playerInventory, Player playerEntity) {
|
2021-02-28 12:33:01 +01:00
|
|
|
return new ContainerCanolaPress(windowId, playerInventory, this);
|
|
|
|
}
|
2015-05-20 22:39:43 +02:00
|
|
|
}
|