ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityBioReactor.java

199 lines
6.8 KiB
Java
Raw Normal View History

2016-09-14 21:42:03 +02:00
/*
* This file ("TileEntityBioReactor.java") is part of the Actually Additions mod for Minecraft.
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
* http://ellpeck.de/actaddlicense
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2017-01-01 16:23:26 +01:00
* © 2015-2017 Ellpeck
2016-09-14 21:42:03 +02:00
*/
package de.ellpeck.actuallyadditions.mod.tile;
2021-08-31 00:44:39 +02:00
import de.ellpeck.actuallyadditions.mod.blocks.ActuallyBlocks;
import de.ellpeck.actuallyadditions.mod.inventory.ContainerBioReactor;
2018-08-10 05:04:07 +02:00
import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IAcceptor;
import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IRemover;
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.BlockItem;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.BonemealableBlock;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
2024-03-04 20:21:48 +01:00
import net.neoforged.neoforge.common.IPlantable;
import net.neoforged.neoforge.energy.IEnergyStorage;
2016-09-14 21:42:03 +02:00
2021-02-27 21:24:26 +01:00
import javax.annotation.Nullable;
2021-02-26 22:15:48 +01:00
import java.util.ArrayList;
import java.util.List;
2024-03-02 21:23:08 +01:00
public class TileEntityBioReactor extends TileEntityInventoryBase implements MenuProvider, ISharingEnergyProvider {
2016-09-14 21:42:03 +02:00
2016-11-26 20:43:50 +01:00
public final CustomEnergyStorage storage = new CustomEnergyStorage(200000, 0, 800);
2016-09-14 21:42:03 +02:00
public int burnTime;
public int maxBurnTime;
public int producePerTick;
private int lastBurnTime;
private int lastProducePerTick;
2024-03-02 21:23:08 +01:00
public TileEntityBioReactor(BlockPos pos, BlockState state) {
super(ActuallyBlocks.BIOREACTOR.getTileEntityType(), pos, state, 8);
2016-09-14 21:42:03 +02:00
}
2018-08-10 05:04:07 +02:00
public static boolean isValidItem(ItemStack stack) {
2024-03-12 17:00:02 +01:00
if (!stack.isEmpty()) {
2016-10-31 19:05:29 +01:00
Item item = stack.getItem();
if (item.isEdible()) {
2016-10-31 19:05:29 +01:00
return true;
2021-02-27 16:33:00 +01:00
} else if (item instanceof BlockItem) {
return isValid(Block.byItem(item));
2021-02-26 22:15:48 +01:00
}
2016-10-31 19:05:29 +01:00
}
return false;
}
2018-08-10 05:04:07 +02:00
private static boolean isValid(Object o) {
2024-03-02 21:23:08 +01:00
return o instanceof IPlantable || o instanceof BonemealableBlock;
2016-10-31 19:05:29 +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 TileEntityBioReactor tile) {
tile.clientTick();
}
}
public static <T extends BlockEntity> void serverTick(Level level, BlockPos pos, BlockState state, T t) {
if (t instanceof TileEntityBioReactor tile) {
tile.serverTick();
if (tile.burnTime <= 0) {
List<Item> types = null;
if (!tile.isRedstonePowered && tile.storage.getEnergyStored() < tile.storage.getMaxEnergyStored()) {
for (int i = 0; i < tile.inv.getSlots(); i++) {
ItemStack stack = tile.inv.getStackInSlot(i);
2024-03-12 17:00:02 +01:00
if (!stack.isEmpty()) {
2024-03-02 21:23:08 +01:00
Item item = stack.getItem();
if (isValidItem(stack) && (types == null || !types.contains(item))) {
if (types == null) {
types = new ArrayList<>();
}
types.add(item);
2016-09-14 21:42:03 +02:00
2024-03-02 21:23:08 +01:00
tile.inv.setStackInSlot(i, StackUtil.shrink(stack, 1));
}
2016-09-14 21:42:03 +02:00
}
}
2024-03-02 21:23:08 +01:00
tile.setChanged();
}
2016-09-14 21:42:03 +02:00
2024-03-02 21:23:08 +01:00
if (types != null && !types.isEmpty()) {
int amount = types.size();
tile.producePerTick = (int) Math.pow(amount * 2, 2);
2016-09-14 21:42:03 +02:00
2024-03-02 21:23:08 +01:00
tile.maxBurnTime = 200 - (int) Math.pow(1.8, amount);
tile.burnTime = tile.maxBurnTime;
} else {
tile.burnTime = 0;
tile.maxBurnTime = 0;
tile.producePerTick = 0;
}
2018-08-10 05:04:07 +02:00
} else {
2024-03-02 21:23:08 +01:00
tile.burnTime--;
tile.storage.receiveEnergyInternal(tile.producePerTick, false);
2016-09-14 21:42:03 +02:00
}
2024-03-02 21:23:08 +01:00
if ((tile.lastBurnTime != tile.burnTime || tile.lastProducePerTick != tile.producePerTick) && tile.sendUpdateWithInterval()) {
tile.lastBurnTime = tile.burnTime;
tile.lastProducePerTick = tile.producePerTick;
}
2016-09-14 21:42:03 +02:00
}
}
@Override
2024-03-02 21:23:08 +01:00
public void writeSyncableNBT(CompoundTag compound, NBTType type) {
2016-09-14 21:42:03 +02:00
super.writeSyncableNBT(compound, type);
this.storage.writeToNBT(compound);
2021-02-27 16:33:00 +01:00
compound.putInt("BurnTime", this.burnTime);
compound.putInt("MaxBurnTime", this.maxBurnTime);
compound.putInt("ProducePerTick", this.producePerTick);
2016-09-14 21:42:03 +02:00
}
@Override
2024-03-02 21:23:08 +01:00
public void readSyncableNBT(CompoundTag compound, NBTType type) {
2016-09-14 21:42:03 +02:00
super.readSyncableNBT(compound, type);
this.storage.readFromNBT(compound);
2021-02-27 16:33:00 +01:00
this.burnTime = compound.getInt("BurnTime");
this.maxBurnTime = compound.getInt("MaxBurnTime");
this.producePerTick = compound.getInt("ProducePerTick");
2016-09-14 21:42:03 +02:00
}
@Override
2018-08-10 05:04:07 +02:00
public IAcceptor getAcceptor() {
return (slot, stack, automation) -> isValidItem(stack);
2016-09-14 21:42:03 +02:00
}
@Override
2018-08-10 05:04:07 +02:00
public IRemover getRemover() {
2019-07-09 04:51:05 +02:00
return (slot, automation) -> !automation;
2016-09-14 21:42:03 +02:00
}
@Override
2018-08-10 05:04:07 +02:00
public int getEnergyToSplitShare() {
2016-09-14 21:42:03 +02:00
return this.storage.getEnergyStored();
}
@Override
2018-08-10 05:04:07 +02:00
public boolean doesShareEnergy() {
2016-09-14 21:42:03 +02:00
return true;
}
@Override
public Direction[] getEnergyShareSides() {
return Direction.values();
2016-09-14 21:42:03 +02:00
}
2016-11-26 08:58:42 +01:00
@Override
2024-03-02 21:23:08 +01:00
public boolean canShareTo(BlockEntity tile) {
return true;
}
2016-11-26 08:58:42 +01:00
@Override
2024-03-04 20:21:48 +01:00
public IEnergyStorage getEnergyStorage(Direction facing) {
return this.storage;
2016-11-26 08:58:42 +01:00
}
2018-07-28 02:45:50 +02:00
@Override
2018-08-10 05:04:07 +02:00
public int getComparatorStrength() {
2019-02-27 19:53:05 +01:00
float calc = (float) this.storage.getEnergyStored() / (float) this.storage.getMaxEnergyStored() * 15F;
2018-08-10 05:04:07 +02:00
return (int) calc;
2018-07-28 02:45:50 +02:00
}
2021-02-27 21:24:26 +01:00
@Override
2024-03-02 21:23:08 +01:00
public Component getDisplayName() {
2024-03-12 17:00:02 +01:00
return Component.translatable("container.actuallyadditions.bioReactor");
2021-02-27 21:24:26 +01:00
}
@Nullable
@Override
2024-03-02 21:23:08 +01:00
public AbstractContainerMenu createMenu(int windowId, Inventory playerInventory, Player playerEntity) {
return new ContainerBioReactor(windowId, playerInventory, this);
2021-02-27 21:24:26 +01:00
}
2016-09-14 21:42:03 +02:00
}