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

191 lines
6.2 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;
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;
import net.minecraft.block.Block;
2016-09-14 21:42:03 +02:00
import net.minecraft.block.IGrowable;
2021-02-27 21:24:26 +01:00
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.container.Container;
import net.minecraft.inventory.container.INamedContainerProvider;
2021-02-27 16:33:00 +01:00
import net.minecraft.item.BlockItem;
2016-09-14 21:42:03 +02:00
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
2021-02-26 22:15:48 +01:00
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.Direction;
2021-02-27 21:24:26 +01:00
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.StringTextComponent;
2016-09-14 21:42:03 +02:00
import net.minecraftforge.common.IPlantable;
2021-02-27 16:33:00 +01:00
import net.minecraftforge.common.util.LazyOptional;
2016-11-26 08:58:42 +01:00
import net.minecraftforge.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;
2021-02-27 21:24:26 +01:00
public class TileEntityBioReactor extends TileEntityInventoryBase implements INamedContainerProvider, 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);
2021-02-27 16:33:00 +01:00
public final LazyOptional<IEnergyStorage> lazyEnergy = LazyOptional.of(() -> this.storage);
2016-09-14 21:42:03 +02:00
public int burnTime;
public int maxBurnTime;
public int producePerTick;
private int lastBurnTime;
private int lastProducePerTick;
2018-08-10 05:04:07 +02:00
public TileEntityBioReactor() {
2021-02-27 16:33:00 +01:00
super(ActuallyTiles.BIOREACTOR_TILE.get(), 8);
2016-09-14 21:42:03 +02:00
}
2018-08-10 05:04:07 +02:00
public static boolean isValidItem(ItemStack stack) {
if (StackUtil.isValid(stack)) {
2016-10-31 19:05:29 +01:00
Item item = stack.getItem();
2021-02-27 16:33:00 +01:00
if (item.isFood()) {
2016-10-31 19:05:29 +01:00
return true;
2021-02-27 16:33:00 +01:00
} else if (item instanceof BlockItem) {
2021-02-26 22:15:48 +01:00
return isValid(Block.getBlockFromItem(item));
}
2016-10-31 19:05:29 +01:00
}
return false;
}
2018-08-10 05:04:07 +02:00
private static boolean isValid(Object o) {
2021-02-27 16:33:00 +01:00
return o instanceof IPlantable || o instanceof IGrowable;
2016-10-31 19:05:29 +01:00
}
2016-09-14 21:42:03 +02:00
@Override
2018-08-10 05:04:07 +02:00
public void updateEntity() {
2016-09-14 21:42:03 +02:00
super.updateEntity();
2018-08-10 05:04:07 +02:00
if (this.burnTime <= 0) {
2016-09-14 21:42:03 +02:00
List<Item> types = null;
2018-08-10 05:04:07 +02:00
if (!this.isRedstonePowered && this.storage.getEnergyStored() < this.storage.getMaxEnergyStored()) {
for (int i = 0; i < this.inv.getSlots(); i++) {
ItemStack stack = this.inv.getStackInSlot(i);
2018-08-10 05:04:07 +02:00
if (StackUtil.isValid(stack)) {
2016-09-14 21:42:03 +02:00
Item item = stack.getItem();
2018-08-10 05:04:07 +02:00
if (isValidItem(stack) && (types == null || !types.contains(item))) {
if (types == null) {
2019-02-27 19:53:05 +01:00
types = new ArrayList<>();
2016-09-14 21:42:03 +02:00
}
types.add(item);
this.inv.setStackInSlot(i, StackUtil.shrink(stack, 1));
2016-09-14 21:42:03 +02:00
}
}
}
this.markDirty();
}
2018-08-10 05:04:07 +02:00
if (types != null && !types.isEmpty()) {
2016-09-14 21:42:03 +02:00
int amount = types.size();
2018-08-10 05:04:07 +02:00
this.producePerTick = (int) Math.pow(amount * 2, 2);
2016-09-14 21:42:03 +02:00
2018-08-10 05:04:07 +02:00
this.maxBurnTime = 200 - (int) Math.pow(1.8, amount);
2016-09-14 21:42:03 +02:00
this.burnTime = this.maxBurnTime;
2018-08-10 05:04:07 +02:00
} else {
2016-09-14 21:42:03 +02:00
this.burnTime = 0;
this.maxBurnTime = 0;
this.producePerTick = 0;
}
2018-08-10 05:04:07 +02:00
} else {
2016-09-14 21:42:03 +02:00
this.burnTime--;
this.storage.receiveEnergyInternal(this.producePerTick, false);
2016-09-14 21:42:03 +02:00
}
2018-08-10 05:04:07 +02:00
if ((this.lastBurnTime != this.burnTime || this.lastProducePerTick != this.producePerTick) && this.sendUpdateWithInterval()) {
2016-09-14 21:42:03 +02:00
this.lastBurnTime = this.burnTime;
this.lastProducePerTick = this.producePerTick;
}
}
@Override
2021-02-26 22:15:48 +01:00
public void writeSyncableNBT(CompoundNBT 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
2021-02-26 22:15:48 +01:00
public void readSyncableNBT(CompoundNBT 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
2018-08-10 05:04:07 +02:00
public boolean canShareTo(TileEntity tile) {
return true;
}
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
}
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
public ITextComponent getDisplayName() {
return StringTextComponent.EMPTY;
}
@Nullable
@Override
public Container createMenu(int windowId, PlayerInventory playerInventory, PlayerEntity playerEntity) {
return new ContainerBioReactor(windowId, playerInventory, this);
2021-02-27 21:24:26 +01:00
}
2016-09-14 21:42:03 +02:00
}