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

169 lines
5.3 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;
2018-08-10 05:04:07 +02:00
import java.util.ArrayList;
import java.util.List;
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;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
2016-09-14 21:42:03 +02:00
import net.minecraft.item.ItemFood;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
2016-09-14 21:42:03 +02:00
import net.minecraft.util.EnumFacing;
import net.minecraftforge.common.IPlantable;
2016-11-26 08:58:42 +01:00
import net.minecraftforge.energy.IEnergyStorage;
2016-09-14 21:42:03 +02:00
2018-08-10 05:04:07 +02:00
public class TileEntityBioReactor extends TileEntityInventoryBase implements 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;
2018-08-10 05:04:07 +02:00
public TileEntityBioReactor() {
2016-09-14 21:42:03 +02:00
super(8, "bioReactor");
}
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();
2018-08-10 05:04:07 +02:00
if (isValid(item)) {
2016-10-31 19:05:29 +01:00
return true;
2018-08-10 05:04:07 +02:00
} else if (item instanceof ItemBlock) { 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) {
2016-10-31 19:05:29 +01:00
return o instanceof IPlantable || o instanceof IGrowable || o instanceof ItemFood;
}
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
2018-08-10 05:04:07 +02:00
public void writeSyncableNBT(NBTTagCompound compound, NBTType type) {
2016-09-14 21:42:03 +02:00
super.writeSyncableNBT(compound, type);
this.storage.writeToNBT(compound);
compound.setInteger("BurnTime", this.burnTime);
compound.setInteger("MaxBurnTime", this.maxBurnTime);
compound.setInteger("ProducePerTick", this.producePerTick);
}
@Override
2018-08-10 05:04:07 +02:00
public void readSyncableNBT(NBTTagCompound compound, NBTType type) {
2016-09-14 21:42:03 +02:00
super.readSyncableNBT(compound, type);
this.storage.readFromNBT(compound);
this.burnTime = compound.getInteger("BurnTime");
this.maxBurnTime = compound.getInteger("MaxBurnTime");
this.producePerTick = compound.getInteger("ProducePerTick");
}
@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
2018-08-10 05:04:07 +02:00
public EnumFacing[] getEnergyShareSides() {
2016-09-14 21:42:03 +02:00
return EnumFacing.values();
}
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
2018-08-10 05:04:07 +02:00
public IEnergyStorage getEnergyStorage(EnumFacing facing) {
2016-11-26 08:58:42 +01:00
return this.storage;
}
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
}
2016-09-14 21:42:03 +02:00
}