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
|
|
|
|
*
|
|
|
|
* © 2015-2016 Ellpeck
|
|
|
|
*/
|
|
|
|
|
|
|
|
package de.ellpeck.actuallyadditions.mod.tile;
|
|
|
|
|
|
|
|
import cofh.api.energy.EnergyStorage;
|
2016-11-16 16:59:00 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
|
2016-09-14 21:48:29 +02:00
|
|
|
import net.minecraft.block.Block;
|
2016-09-14 21:42:03 +02:00
|
|
|
import net.minecraft.block.IGrowable;
|
|
|
|
import net.minecraft.item.Item;
|
2016-09-14 21:48:29 +02:00
|
|
|
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.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
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
public class TileEntityBioReactor extends TileEntityInventoryBase implements ISharingEnergyProvider{
|
|
|
|
|
2016-11-23 18:10:55 +01:00
|
|
|
public final CustomEnergyStorage storage = new CustomEnergyStorage(200000, 800);
|
2016-09-14 21:42:03 +02:00
|
|
|
|
|
|
|
public int burnTime;
|
|
|
|
public int maxBurnTime;
|
|
|
|
public int producePerTick;
|
|
|
|
|
|
|
|
private int lastBurnTime;
|
|
|
|
private int lastProducePerTick;
|
|
|
|
|
|
|
|
public TileEntityBioReactor(){
|
|
|
|
super(8, "bioReactor");
|
|
|
|
}
|
|
|
|
|
2016-10-31 19:05:29 +01:00
|
|
|
public static boolean isValidItem(ItemStack stack){
|
2016-11-16 16:59:00 +01:00
|
|
|
if(StackUtil.isValid(stack)){
|
2016-10-31 19:05:29 +01:00
|
|
|
Item item = stack.getItem();
|
|
|
|
if(isValid(item)){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if(item instanceof ItemBlock){
|
|
|
|
return isValid(Block.getBlockFromItem(item));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static boolean isValid(Object o){
|
|
|
|
return o instanceof IPlantable || o instanceof IGrowable || o instanceof ItemFood;
|
|
|
|
}
|
|
|
|
|
2016-09-14 21:42:03 +02:00
|
|
|
@Override
|
|
|
|
public void updateEntity(){
|
|
|
|
super.updateEntity();
|
|
|
|
|
|
|
|
if(this.burnTime <= 0){
|
|
|
|
List<Item> types = null;
|
|
|
|
|
|
|
|
if(this.storage.getEnergyStored() < this.storage.getMaxEnergyStored()){
|
2016-11-20 20:13:26 +01:00
|
|
|
for(int i = 0; i < this.slots.size(); i++){
|
|
|
|
ItemStack stack = this.slots.get(i);
|
2016-11-16 16:59:00 +01:00
|
|
|
if(StackUtil.isValid(stack)){
|
2016-09-14 21:42:03 +02:00
|
|
|
Item item = stack.getItem();
|
2016-09-14 21:48:29 +02:00
|
|
|
if(isValidItem(stack) && (types == null || !types.contains(item))){
|
2016-09-14 21:42:03 +02:00
|
|
|
if(types == null){
|
|
|
|
types = new ArrayList<Item>();
|
|
|
|
}
|
|
|
|
types.add(item);
|
|
|
|
|
2016-11-20 20:13:26 +01:00
|
|
|
this.slots.set(i, StackUtil.addStackSize(stack, -1));
|
2016-09-14 21:42:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.markDirty();
|
|
|
|
}
|
|
|
|
|
|
|
|
if(types != null && !types.isEmpty()){
|
|
|
|
int amount = types.size();
|
|
|
|
this.producePerTick = (int)Math.pow(amount*2, 2);
|
|
|
|
|
|
|
|
this.maxBurnTime = 200-(int)Math.pow(1.8, amount);
|
|
|
|
this.burnTime = this.maxBurnTime;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
this.burnTime = 0;
|
|
|
|
this.maxBurnTime = 0;
|
|
|
|
this.producePerTick = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
this.burnTime--;
|
2016-11-23 18:10:55 +01:00
|
|
|
this.storage.receiveEnergyInternal(this.producePerTick, false);
|
2016-09-14 21:42:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if((this.lastBurnTime != this.burnTime || this.lastProducePerTick != this.producePerTick) && this.sendUpdateWithInterval()){
|
|
|
|
this.lastBurnTime = this.burnTime;
|
|
|
|
this.lastProducePerTick = this.producePerTick;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void writeSyncableNBT(NBTTagCompound compound, NBTType type){
|
|
|
|
super.writeSyncableNBT(compound, type);
|
|
|
|
|
|
|
|
this.storage.writeToNBT(compound);
|
|
|
|
compound.setInteger("BurnTime", this.burnTime);
|
|
|
|
compound.setInteger("MaxBurnTime", this.maxBurnTime);
|
|
|
|
compound.setInteger("ProducePerTick", this.producePerTick);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void readSyncableNBT(NBTTagCompound compound, NBTType type){
|
|
|
|
super.readSyncableNBT(compound, type);
|
|
|
|
|
|
|
|
this.storage.readFromNBT(compound);
|
|
|
|
this.burnTime = compound.getInteger("BurnTime");
|
|
|
|
this.maxBurnTime = compound.getInteger("MaxBurnTime");
|
|
|
|
this.producePerTick = compound.getInteger("ProducePerTick");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canInsertItem(int index, ItemStack stack, EnumFacing direction){
|
|
|
|
return this.isItemValidForSlot(index, stack);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canExtractItem(int index, ItemStack stack, EnumFacing direction){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isItemValidForSlot(int index, ItemStack stack){
|
2016-09-14 21:48:29 +02:00
|
|
|
return isValidItem(stack);
|
2016-09-14 21:42:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int extractEnergy(EnumFacing from, int maxExtract, boolean simulate){
|
|
|
|
return this.storage.extractEnergy(maxExtract, simulate);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getEnergyStored(EnumFacing from){
|
|
|
|
return this.storage.getEnergyStored();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getMaxEnergyStored(EnumFacing from){
|
|
|
|
return this.storage.getMaxEnergyStored();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canConnectEnergy(EnumFacing from){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getEnergyToSplitShare(){
|
|
|
|
return this.storage.getEnergyStored();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean doesShareEnergy(){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public EnumFacing[] getEnergyShareSides(){
|
|
|
|
return EnumFacing.values();
|
|
|
|
}
|
2016-11-26 08:58:42 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public IEnergyStorage getEnergyStorage(EnumFacing facing){
|
|
|
|
return this.storage;
|
|
|
|
}
|
2016-09-14 21:42:03 +02:00
|
|
|
}
|