mirror of
https://github.com/Thorfusion/Mekanism-Community-Edition.git
synced 2026-11-26 14:52:58 +00:00
* Improve performance of predicted insert. Don't copy stacks nearly as often * Cache what stack is in the slots and update comments * Cleanup up pathfinder code some * Clean up a bit more mainly TileComponentEjector * Fix crash caused by an earlier commit * Minor cleanup * Minor cleanup * Slightly clean up TileEntityBin and fix creative bin auto eject lowering item count * Partially cleanup getpaths, and prep work for better handling of AcceptorData with mulitple TransitResponses * Scratch previous attempted idea of combining responses * Use EnumMap instead of HashMap for keeping track of side data for Ejector and prep for an eventual change to TransitResponse of inserting into more than one slot at a time if the stack would be best split among multiple * Change some more HashMaps to EnumMaps * Make it easier to read component ejector for gas and fluids * Inline getStacksToDest so that it can bail even earlier in cases when destination is full * Don't create a copy of the stack if it won't be inserted AND also fix having broken inserting across multiple slots * Replace a bunch of other HashMap instances with EnumMap where possible * Make a couple sets more generic * Use Int2ObjectMap for int based cache & tidy clamping * ensure EnumMap doesnt throw NPE * FastUtil map changes
292 lines
11 KiB
Java
292 lines
11 KiB
Java
package mekanism.common.tile.component;
|
|
|
|
import io.netty.buffer.ByteBuf;
|
|
import java.util.EnumMap;
|
|
import java.util.EnumSet;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Map.Entry;
|
|
import java.util.Set;
|
|
import mekanism.api.Coord4D;
|
|
import mekanism.api.EnumColor;
|
|
import mekanism.api.TileNetworkList;
|
|
import mekanism.api.gas.GasStack;
|
|
import mekanism.api.gas.GasTank;
|
|
import mekanism.api.transmitters.TransmissionType;
|
|
import mekanism.common.SideData;
|
|
import mekanism.common.base.ILogisticalTransporter;
|
|
import mekanism.common.base.ISideConfiguration;
|
|
import mekanism.common.base.ITankManager;
|
|
import mekanism.common.base.ITileComponent;
|
|
import mekanism.common.capabilities.Capabilities;
|
|
import mekanism.common.content.transporter.TransitRequest;
|
|
import mekanism.common.content.transporter.TransitRequest.TransitResponse;
|
|
import mekanism.common.tile.prefab.TileEntityContainerBlock;
|
|
import mekanism.common.util.CapabilityUtils;
|
|
import mekanism.common.util.GasUtils;
|
|
import mekanism.common.util.InventoryUtils;
|
|
import mekanism.common.util.MekanismUtils;
|
|
import mekanism.common.util.PipeUtils;
|
|
import mekanism.common.util.TransporterUtils;
|
|
import net.minecraft.item.ItemStack;
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
import net.minecraft.tileentity.TileEntity;
|
|
import net.minecraft.util.EnumFacing;
|
|
import net.minecraftforge.fluids.FluidStack;
|
|
import net.minecraftforge.fluids.FluidTank;
|
|
|
|
public class TileComponentEjector implements ITileComponent {
|
|
|
|
private static final int GAS_OUTPUT = 256;
|
|
private static final int FLUID_OUTPUT = 256;
|
|
private TileEntityContainerBlock tileEntity;
|
|
private boolean strictInput;
|
|
private EnumColor outputColor;
|
|
private EnumColor[] inputColors = new EnumColor[]{null, null, null, null, null, null};
|
|
private int tickDelay = 0;
|
|
private Map<TransmissionType, SideData> sideData = new EnumMap<>(TransmissionType.class);
|
|
public Map<TransmissionType, int[]> trackers = new EnumMap<>(TransmissionType.class);
|
|
|
|
public TileComponentEjector(TileEntityContainerBlock tile) {
|
|
tileEntity = tile;
|
|
tile.components.add(this);
|
|
}
|
|
|
|
public TileComponentEjector setOutputData(TransmissionType type, SideData data) {
|
|
sideData.put(type, data);
|
|
trackers.put(type, new int[data.availableSlots.length]);
|
|
return this;
|
|
}
|
|
|
|
public void readFrom(TileComponentEjector ejector) {
|
|
strictInput = ejector.strictInput;
|
|
outputColor = ejector.outputColor;
|
|
inputColors = ejector.inputColors;
|
|
tickDelay = ejector.tickDelay;
|
|
sideData = ejector.sideData;
|
|
}
|
|
|
|
private Set<EnumFacing> getTrackedOutputs(TransmissionType type, int index, Set<EnumFacing> dirs) {
|
|
Set<EnumFacing> sides = EnumSet.noneOf(EnumFacing.class);
|
|
int tracker = trackers.get(type)[index];
|
|
for (int i = tracker + 1; i <= tracker + 6; i++) {
|
|
EnumFacing side = EnumFacing.byIndex(i % 6);
|
|
if (dirs.contains(side)) {
|
|
sides.add(side);
|
|
}
|
|
}
|
|
return sides;
|
|
}
|
|
|
|
@Override
|
|
public void tick() {
|
|
if (tickDelay == 0) {
|
|
if (sideData.get(TransmissionType.ITEM) != null) {
|
|
outputItems();
|
|
}
|
|
} else {
|
|
tickDelay--;
|
|
}
|
|
if (!tileEntity.getWorld().isRemote) {
|
|
eject(TransmissionType.GAS);
|
|
eject(TransmissionType.FLUID);
|
|
}
|
|
}
|
|
|
|
private void eject(TransmissionType type) {
|
|
SideData data = sideData.get(type);
|
|
if (data != null && getEjecting(type)) {
|
|
ITankManager tankManager = (ITankManager) this.tileEntity;
|
|
if (tankManager.getTanks() != null) {
|
|
Set<EnumFacing> outputSides = getOutputSides(type, data);
|
|
if (type == TransmissionType.GAS) {
|
|
ejectGas(outputSides, (GasTank) tankManager.getTanks()[data.availableSlots[0]]);
|
|
} else if (type == TransmissionType.FLUID) {
|
|
ejectFluid(outputSides, (FluidTank) tankManager.getTanks()[data.availableSlots[0]]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void ejectGas(Set<EnumFacing> outputSides, GasTank tank) {
|
|
if (tank.getGas() != null && tank.getStored() > 0) {
|
|
GasStack toEmit = tank.getGas().copy().withAmount(Math.min(GAS_OUTPUT, tank.getStored()));
|
|
int emit = GasUtils.emit(toEmit, tileEntity, outputSides);
|
|
tank.draw(emit, true);
|
|
}
|
|
}
|
|
|
|
private void ejectFluid(Set<EnumFacing> outputSides, FluidTank tank) {
|
|
if (tank.getFluid() != null && tank.getFluidAmount() > 0) {
|
|
FluidStack toEmit = PipeUtils.copy(tank.getFluid(), Math.min(FLUID_OUTPUT, tank.getFluidAmount()));
|
|
int emit = PipeUtils.emit(outputSides, toEmit, tileEntity);
|
|
tank.drain(emit, true);
|
|
}
|
|
}
|
|
|
|
public Set<EnumFacing> getOutputSides(TransmissionType type, SideData data) {
|
|
Set<EnumFacing> outputSides = EnumSet.noneOf(EnumFacing.class);
|
|
TileComponentConfig config = ((ISideConfiguration) tileEntity).getConfig();
|
|
SideConfig sideConfig = config.getConfig(type);
|
|
List<SideData> outputs = config.getOutputs(type);
|
|
EnumFacing[] facings = MekanismUtils.getBaseOrientations(tileEntity.facing);
|
|
for (int i = 0; i < EnumFacing.VALUES.length; i++) {
|
|
EnumFacing side = facings[i];
|
|
if (sideConfig.get(side) == outputs.indexOf(data)) {
|
|
outputSides.add(EnumFacing.VALUES[i]);
|
|
}
|
|
}
|
|
return outputSides;
|
|
}
|
|
|
|
public void outputItems() {
|
|
if (!getEjecting(TransmissionType.ITEM) || tileEntity.getWorld().isRemote) {
|
|
return;
|
|
}
|
|
|
|
SideData data = sideData.get(TransmissionType.ITEM);
|
|
Set<EnumFacing> outputSides = getOutputSides(TransmissionType.ITEM, data);
|
|
for (int index = 0; index < data.availableSlots.length; index++) {
|
|
int slotID = data.availableSlots[index];
|
|
if (tileEntity.getStackInSlot(slotID).isEmpty()) {
|
|
continue;
|
|
}
|
|
|
|
ItemStack stack = tileEntity.getStackInSlot(slotID);
|
|
Coord4D tileCoord = Coord4D.get(tileEntity);
|
|
Set<EnumFacing> outputs = getTrackedOutputs(TransmissionType.ITEM, index, outputSides);
|
|
for (EnumFacing side : outputs) {
|
|
TileEntity tile = tileCoord.offset(side).getTileEntity(tileEntity.getWorld());
|
|
int prevCount = stack.getCount();
|
|
|
|
ILogisticalTransporter capability = CapabilityUtils.getCapability(tile, Capabilities.LOGISTICAL_TRANSPORTER_CAPABILITY, side.getOpposite());
|
|
TransitRequest transitRequest = TransitRequest.getFromStack(stack.copy());
|
|
TransitResponse response;
|
|
if (capability == null) {
|
|
response = InventoryUtils.putStackInInventory(tile, transitRequest, side, false);
|
|
} else {
|
|
response = TransporterUtils.insert(tileEntity, capability, transitRequest, outputColor, true, 0);
|
|
}
|
|
if (!response.isEmpty()) {
|
|
stack.shrink(response.getSendingAmount());
|
|
}
|
|
|
|
if (stack.isEmpty() || prevCount != stack.getCount()) {
|
|
trackers.get(TransmissionType.ITEM)[index] = side.ordinal();
|
|
}
|
|
if (stack.isEmpty()) {
|
|
break;
|
|
}
|
|
}
|
|
|
|
tileEntity.setInventorySlotContents(slotID, stack);
|
|
tileEntity.markDirty();
|
|
}
|
|
tickDelay = 20;
|
|
}
|
|
|
|
public boolean hasStrictInput() {
|
|
return strictInput;
|
|
}
|
|
|
|
public void setStrictInput(boolean strict) {
|
|
strictInput = strict;
|
|
MekanismUtils.saveChunk(tileEntity);
|
|
}
|
|
|
|
public EnumColor getOutputColor() {
|
|
return outputColor;
|
|
}
|
|
|
|
public void setOutputColor(EnumColor color) {
|
|
outputColor = color;
|
|
MekanismUtils.saveChunk(tileEntity);
|
|
}
|
|
|
|
public void setInputColor(EnumFacing side, EnumColor color) {
|
|
inputColors[side.ordinal()] = color;
|
|
MekanismUtils.saveChunk(tileEntity);
|
|
}
|
|
|
|
public EnumColor getInputColor(EnumFacing side) {
|
|
return inputColors[side.ordinal()];
|
|
}
|
|
|
|
@Override
|
|
public void read(NBTTagCompound nbtTags) {
|
|
strictInput = nbtTags.getBoolean("strictInput");
|
|
if (nbtTags.hasKey("ejectColor")) {
|
|
outputColor = readColor(nbtTags.getInteger("ejectColor"));
|
|
}
|
|
for (Entry<TransmissionType, SideData> entry : sideData.entrySet()) {
|
|
TransmissionType type = entry.getKey();
|
|
SideData data = entry.getValue();
|
|
for (int i = 0; i < data.availableSlots.length; i++) {
|
|
trackers.get(type)[i] = nbtTags.getInteger("tracker" + type.getTransmission() + i);
|
|
}
|
|
}
|
|
for (int i = 0; i < 6; i++) {
|
|
if (nbtTags.hasKey("inputColors" + i)) {
|
|
inputColors[i] = readColor(nbtTags.getInteger("inputColors" + i));
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void read(ByteBuf dataStream) {
|
|
strictInput = dataStream.readBoolean();
|
|
outputColor = readColor(dataStream.readInt());
|
|
for (int i = 0; i < 6; i++) {
|
|
inputColors[i] = readColor(dataStream.readInt());
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void write(NBTTagCompound nbtTags) {
|
|
nbtTags.setBoolean("strictInput", strictInput);
|
|
if (outputColor != null) {
|
|
nbtTags.setInteger("ejectColor", getColorIndex(outputColor));
|
|
}
|
|
for (Entry<TransmissionType, SideData> entry : sideData.entrySet()) {
|
|
TransmissionType type = entry.getKey();
|
|
SideData data = entry.getValue();
|
|
for (int i = 0; i < data.availableSlots.length; i++) {
|
|
nbtTags.setInteger("tracker" + type.getTransmission() + i, trackers.get(type)[i]);
|
|
}
|
|
}
|
|
for (int i = 0; i < 6; i++) {
|
|
nbtTags.setInteger("inputColors" + i, getColorIndex(inputColors[i]));
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void write(TileNetworkList data) {
|
|
data.add(strictInput);
|
|
data.add(getColorIndex(outputColor));
|
|
for (int i = 0; i < 6; i++) {
|
|
data.add(getColorIndex(inputColors[i]));
|
|
}
|
|
}
|
|
|
|
private EnumColor readColor(int inputColor) {
|
|
if (inputColor == -1) {
|
|
return null;
|
|
}
|
|
return TransporterUtils.colors.get(inputColor);
|
|
}
|
|
|
|
private int getColorIndex(EnumColor color) {
|
|
if (color == null) {
|
|
return -1;
|
|
}
|
|
return TransporterUtils.colors.indexOf(color);
|
|
}
|
|
|
|
@Override
|
|
public void invalidate() {
|
|
}
|
|
|
|
private boolean getEjecting(TransmissionType type) {
|
|
return ((ISideConfiguration) tileEntity).getConfig().isEjecting(type);
|
|
}
|
|
} |