mirror of
https://github.com/GTNewHorizons/EnderIO.git
synced 2026-11-19 19:59:58 +00:00
212 lines
5.5 KiB
Java
212 lines
5.5 KiB
Java
package crazypants.enderio.machine.buffer;
|
|
|
|
import net.minecraft.block.Block;
|
|
import net.minecraft.item.ItemStack;
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
import net.minecraft.tileentity.TileEntity;
|
|
import net.minecraftforge.common.util.ForgeDirection;
|
|
import crazypants.enderio.config.Config;
|
|
import crazypants.enderio.machine.AbstractPowerConsumerEntity;
|
|
import crazypants.enderio.machine.IoMode;
|
|
import crazypants.enderio.machine.SlotDefinition;
|
|
import crazypants.enderio.machine.painter.IPaintableTileEntity;
|
|
import crazypants.enderio.machine.painter.PainterUtil;
|
|
import crazypants.enderio.power.PowerDistributor;
|
|
import crazypants.util.BlockCoord;
|
|
|
|
public class TileBuffer extends AbstractPowerConsumerEntity implements IPaintableTileEntity {
|
|
|
|
private Block sourceBlock;
|
|
private int sourceBlockMetadata;
|
|
|
|
private boolean hasPower, hasInventory, isCreative;
|
|
|
|
private PowerDistributor dist;
|
|
|
|
private final int maxOut = Config.powerConduitTierThreeRF;
|
|
|
|
public TileBuffer() {
|
|
super(new SlotDefinition(9, 0, 0));
|
|
}
|
|
|
|
@Override
|
|
public String getMachineName() {
|
|
return BlockItemBuffer.Type.get(this).getUnlocalizedName();
|
|
}
|
|
|
|
@Override
|
|
protected boolean isMachineItemValidForSlot(int i, ItemStack itemstack) {
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public boolean isActive() {
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public float getProgress() {
|
|
return 0; // none
|
|
}
|
|
|
|
@Override
|
|
protected boolean processTasks(boolean redstoneCheckPassed) {
|
|
if (getEnergyStored() <= 0) {
|
|
return false;
|
|
}
|
|
if (dist == null) {
|
|
dist = new PowerDistributor(new BlockCoord(this));
|
|
}
|
|
int transmitted = dist.transmitEnergy(worldObj, Math.min(maxOut, getEnergyStored()));
|
|
setEnergyStored(getEnergyStored() - transmitted);
|
|
return transmitted > 0;
|
|
}
|
|
|
|
@Override
|
|
public void setIoMode(ForgeDirection faceHit, IoMode mode) {
|
|
super.setIoMode(faceHit, mode);
|
|
if (dist != null) {
|
|
dist.neighboursChanged();
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public boolean canInsertItem(int slot, ItemStack var2, int side) {
|
|
return hasInventory() && super.canInsertItem(slot, var2, side) && getIoMode(ForgeDirection.VALID_DIRECTIONS[side]).canRecieveInput();
|
|
}
|
|
|
|
@Override
|
|
public boolean canExtractItem(int slot, ItemStack itemstack, int side) {
|
|
return hasInventory() && super.canExtractItem(slot, itemstack, side) && getIoMode(ForgeDirection.VALID_DIRECTIONS[side]).canOutput();
|
|
}
|
|
|
|
@Override
|
|
public boolean canConnectEnergy(ForgeDirection from) {
|
|
return hasPower;
|
|
}
|
|
|
|
@Override
|
|
public int getMaxEnergyRecieved(ForgeDirection dir) {
|
|
return maxOut;
|
|
}
|
|
|
|
@Override
|
|
public int receiveEnergy(ForgeDirection from, int maxReceive, boolean simulate) {
|
|
return hasPower() ? super.receiveEnergy(from, maxReceive, isCreative() || simulate) : 0;
|
|
}
|
|
|
|
@Override
|
|
protected boolean doPull(ForgeDirection dir) {
|
|
ItemStack[] invCopy = new ItemStack[inventory.length];
|
|
for (int i = 0; i < inventory.length; i++) {
|
|
invCopy[i] = inventory[i] == null ? null : inventory[i].copy();
|
|
}
|
|
|
|
boolean ret = super.doPull(dir);
|
|
|
|
if(isCreative()) {
|
|
inventory = invCopy;
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
@Override
|
|
protected boolean doPush(ForgeDirection dir) {
|
|
|
|
if(worldObj.getTotalWorldTime() % 20 != 0) {
|
|
return false;
|
|
}
|
|
|
|
ItemStack[] invCopy = new ItemStack[inventory.length];
|
|
for (int i = 0; i < inventory.length; i++) {
|
|
invCopy[i] = inventory[i] == null ? null : inventory[i].copy();
|
|
}
|
|
|
|
BlockCoord loc = getLocation().getLocation(dir);
|
|
TileEntity te = worldObj.getTileEntity(loc.x, loc.y, loc.z);
|
|
|
|
boolean ret = super.doPush(dir, te, slotDefinition.minInputSlot, slotDefinition.maxInputSlot);
|
|
|
|
if(isCreative()) {
|
|
inventory = invCopy;
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
@Override
|
|
public void writeCustomNBT(NBTTagCompound nbtRoot) {
|
|
super.writeCustomNBT(nbtRoot);
|
|
nbtRoot.setBoolean("hasInv", hasInventory);
|
|
nbtRoot.setBoolean("hasPower", hasPower);
|
|
nbtRoot.setBoolean("creative", isCreative);
|
|
}
|
|
|
|
@Override
|
|
public void writeCommon(NBTTagCompound nbtRoot) {
|
|
super.writeCommon(nbtRoot);
|
|
PainterUtil.setSourceBlock(nbtRoot, sourceBlock, sourceBlockMetadata);
|
|
}
|
|
|
|
@Override
|
|
public void readCustomNBT(NBTTagCompound nbtRoot) {
|
|
super.readCustomNBT(nbtRoot);
|
|
this.hasInventory = nbtRoot.getBoolean("hasInv");
|
|
this.hasPower = nbtRoot.getBoolean("hasPower");
|
|
this.isCreative = nbtRoot.getBoolean("creative");
|
|
}
|
|
|
|
@Override
|
|
public void readCommon(NBTTagCompound nbtRoot) {
|
|
super.readCommon(nbtRoot);
|
|
this.sourceBlock = PainterUtil.getSourceBlock(nbtRoot);
|
|
this.sourceBlockMetadata = PainterUtil.getSourceBlockMetadata(nbtRoot);
|
|
}
|
|
|
|
@Override
|
|
public void setSourceBlockMetadata(int sourceBlockMetadata) {
|
|
this.sourceBlockMetadata = sourceBlockMetadata;
|
|
}
|
|
|
|
@Override
|
|
public int getSourceBlockMetadata() {
|
|
return sourceBlockMetadata;
|
|
}
|
|
|
|
@Override
|
|
public void setSourceBlock(Block sourceBlock) {
|
|
this.sourceBlock = sourceBlock;
|
|
}
|
|
|
|
@Override
|
|
public Block getSourceBlock() {
|
|
return sourceBlock;
|
|
}
|
|
|
|
public boolean hasInventory() {
|
|
return hasInventory;
|
|
}
|
|
|
|
public void setHasInventory(boolean hasInventory) {
|
|
this.hasInventory = hasInventory;
|
|
}
|
|
|
|
@Override
|
|
public boolean hasPower() {
|
|
return hasPower;
|
|
}
|
|
|
|
public void setHasPower(boolean hasPower) {
|
|
this.hasPower = hasPower;
|
|
}
|
|
|
|
public boolean isCreative() {
|
|
return isCreative;
|
|
}
|
|
|
|
public void setCreative(boolean isCreative) {
|
|
this.isCreative = isCreative;
|
|
}
|
|
}
|