Files
Applied-Energistics-2-Unoff…/src/main/java/appeng/helpers/ReshuffleTask.java
T

396 lines
15 KiB
Java

package appeng.helpers;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraftforge.common.util.Constants.NBT;
import appeng.api.config.AccessRestriction;
import appeng.api.config.Actionable;
import appeng.api.config.ReshufflePhase;
import appeng.api.networking.security.MachineSource;
import appeng.api.networking.security.ReshuffleActionSource;
import appeng.api.networking.storage.IStorageGrid;
import appeng.api.storage.IMEInventoryHandler;
import appeng.api.storage.IMEMonitor;
import appeng.api.storage.IMENetworkInventory;
import appeng.api.storage.data.IAEStack;
import appeng.api.storage.data.IAEStackType;
import appeng.api.storage.data.IItemList;
import appeng.core.AELog;
import appeng.me.cache.NetworkMonitor;
import appeng.me.storage.NetworkInventoryHandler;
import appeng.util.AEStackTypeFilter;
import appeng.util.IterationCounter;
import appeng.util.Platform;
import appeng.util.item.IAEStackList;
public class ReshuffleTask {
// slowdown because working too fast, nobody gonna believe that real working
public static final long stacks_per_tick = 8;
private final ReshuffleActionSource src;
private final MachineSource rollbackSrc;
private final IStorageGrid sg;
private final AEStackTypeFilter typeFilters;
private final boolean includeSubnets;
private final boolean insertOrder;
private ReshufflePhase phase = ReshufflePhase.IDLE;
private int extractedTypes, injectedTypes = 0;
private long startTime, endTime;
private double extractedItems, injectedItems = 0;
private final IItemList<IAEStack<?>> extracted = new IAEStackList();
private final IItemList<IAEStack<?>> cantExtract = new IAEStackList();
private final IItemList<IAEStack<?>> cantInject;
private final IItemList<IAEStack<?>> beforeSnapshot = new IAEStackList();
private final IItemList<IAEStack<?>> afterSnapshot = new IAEStackList();
private final IItemList<IAEStack<?>> stackLookup = new IAEStackList();
private final List<PendingInjection> injectQueue = new ArrayList<>();
private final Map<IAEStack<?>, PendingInjection> injectLookup = new HashMap<>();
private Iterator<Iterator<IMEInventoryHandler>> netTypesIterator = null;
private Iterator<IMEInventoryHandler> typeInvHandlersIterator = null;
private deepDig deepInspector = null;
private Iterator<PendingInjection> injectIterator = null;
private static class PendingInjection {
private IAEStack<?> stack;
private final List<SourceContribution> sources = new ArrayList<>();
private PendingInjection(IAEStack<?> stack) {
this.stack = stack;
}
}
private static class SourceContribution {
private final IMEInventoryHandler source;
private final long amount;
private SourceContribution(IMEInventoryHandler source, long amount) {
this.source = source;
this.amount = amount;
}
}
private static class deepDig {
private final Iterator<IMEInventoryHandler> firstLayer;
private deepDig nextLayer = null;
deepDig(Iterator<IMEInventoryHandler> firstLayer) {
this.firstLayer = firstLayer;
}
public boolean hasNextLayer() {
if (this.nextLayer == null) return false;
if (this.nextLayer.nextLayer == null && !this.nextLayer.firstLayer.hasNext()) {
this.nextLayer = null;
}
return this.nextLayer != null;
}
}
public ReshuffleTask(AEStackTypeFilter typeFilters, IStorageGrid sg, IItemList<IAEStack<?>> cantInject,
ReshuffleActionSource src, boolean includeSubnets, boolean insertOrder) {
this.src = src;
this.rollbackSrc = new MachineSource(src.via);
this.sg = sg;
this.cantInject = cantInject;
this.typeFilters = typeFilters;
this.includeSubnets = includeSubnets;
this.insertOrder = insertOrder;
}
public void initialize() {
this.startTime = System.currentTimeMillis();
this.setupIterator();
this.phase = ReshufflePhase.BEFORE_SNAPSHOT;
}
private boolean snapshotBefore() {
return this.handlerProcessor(this.beforeSnapshot, false);
}
private boolean snapshotAfter() {
return this.handlerProcessor(this.afterSnapshot, false);
}
private void setupIterator() {
final List<Iterator<IMEInventoryHandler>> temp = new ArrayList<>();
if (this.netTypesIterator == null) {
for (IAEStackType<?> type : this.typeFilters.getEnabledTypes()) {
if (!(this.sg.getMEMonitor(type) instanceof NetworkMonitor<?>nm)) continue;
if (!(nm.getHandler() instanceof NetworkInventoryHandler nih)) continue;
if (this.netTypesIterator == null) temp.add(nih.getHandlers().iterator());
}
this.netTypesIterator = temp.iterator();
} else {
this.resetIterators();
this.setupIterator();
}
}
private void resetIterators() {
this.netTypesIterator = null;
this.typeInvHandlersIterator = null;
this.deepInspector = null;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
private boolean handlerProcessor(final IItemList<IAEStack<?>> target, final boolean extract) {
if (this.typeInvHandlersIterator == null) {
if (this.netTypesIterator.hasNext()) {
this.typeInvHandlersIterator = this.netTypesIterator.next();
} else return true;
}
final Iterator<IMEInventoryHandler> i;
deepDig currentDeep = this.deepInspector;
if (currentDeep != null) {
while (currentDeep.hasNextLayer()) currentDeep = currentDeep.nextLayer;
if (currentDeep.firstLayer.hasNext()) i = currentDeep.firstLayer;
else i = this.typeInvHandlersIterator;
} else {
i = this.typeInvHandlersIterator;
}
while (i.hasNext()) {
final IMEInventoryHandler cellHandler = i.next();
if (cellHandler == null || cellHandler.isAutoCraftingInventory()
|| extract && !cellHandler.getReshuffleAccess().hasPermission(AccessRestriction.READ))
continue;
final IAEStackType<?> currentType = cellHandler.getStackType();
final IMENetworkInventory<?> subnet = cellHandler.getExternalNetworkInventory();
if (subnet instanceof NetworkInventoryHandler nextNetwork) {
if (!includeSubnets) continue;
if (currentDeep == null) this.deepInspector = new deepDig(nextNetwork.getHandlers().iterator());
else currentDeep.nextLayer = new deepDig(nextNetwork.getHandlers().iterator());
return false;
}
cellHandler.getAvailableItems(currentType.createList(), IterationCounter.fetchNewId()).forEach(o -> {
if (o instanceof IAEStack<?>aes) {
aes = aes.copy();
if (extract) {
final IAEStack<?> extracted = cellHandler.extractItems(aes, Actionable.MODULATE, this.src);
if (extracted == null) {
this.cantExtract.add(aes);
} else {
if (extracted.getStackSize() != aes.getStackSize()) {
aes.decStackSize(extracted.getStackSize());
this.cantExtract.add(aes);
}
this.extractedItems += extracted.getStackSize();
target.add(extracted);
PendingInjection pending = this.injectLookup.get(extracted);
if (pending == null) {
pending = new PendingInjection(target.findPrecise(extracted));
this.injectLookup.put(pending.stack, pending);
this.injectQueue.add(pending);
}
pending.sources.add(new SourceContribution(cellHandler, extracted.getStackSize()));
}
this.extractedTypes = this.extracted.size();
} else {
this.stackLookup.add(aes);
target.add(aes);
}
}
});
return false;
}
this.typeInvHandlersIterator = null;
return false;
}
private void toQueueList() {
this.injectLookup.clear();
final Iterator<IAEStack<?>> i = this.extracted.iterator();
while (i.hasNext()) {
i.next();
i.remove();
}
this.injectQueue.sort(
Comparator.comparingLong(
pending -> this.insertOrder ? -pending.stack.getStackSize() : pending.stack.getStackSize()));
}
public void processNextBatch() {
switch (this.phase) {
case BEFORE_SNAPSHOT -> {
if (this.snapshotBefore()) {
this.setupIterator();
this.phase = ReshufflePhase.EXTRACTION;
}
}
case EXTRACTION -> {
if (this.handlerProcessor(this.extracted, true)) {
this.setupIterator();
this.toQueueList();
this.phase = ReshufflePhase.INJECTION;
}
}
case INJECTION -> {
int operations = 0;
if (this.injectIterator == null) this.injectIterator = this.injectQueue.iterator();
while (this.injectIterator.hasNext()) {
final PendingInjection pending = this.injectIterator.next();
final IAEStack<?> aes = pending.stack;
final IMEMonitor monitor = this.sg.getMEMonitor(aes.getStackType());
final IAEStack<?> res = monitor == null ? aes
: monitor.injectItems(aes, Actionable.MODULATE, this.src);
if (res != null) {
final long rejectedItems = res.getStackSize();
pending.stack = res;
this.returnPendingItem(pending);
this.injectedItems -= rejectedItems;
if (rejectedItems == aes.getStackSize()) this.injectedTypes -= 1;
} else {
pending.stack = null;
}
this.injectedTypes += 1;
this.injectedItems += aes.getStackSize();
this.injectIterator.remove();
operations++;
if (operations == stacks_per_tick) return;
}
this.injectIterator = null;
this.phase = ReshufflePhase.AFTER_SNAPSHOT;
}
case AFTER_SNAPSHOT -> {
if (this.snapshotAfter()) {
this.setupIterator();
this.finalizeReport();
}
}
}
}
public void cancel() {
this.phase = ReshufflePhase.CANCEL;
this.returnPendingItems(this.injectQueue.iterator());
this.injectLookup.clear();
}
public void nbt(NBTTagCompound tag) {
final NBTTagList tagListInjectQueue = new NBTTagList();
this.injectQueue.forEach(
pending -> tagListInjectQueue.appendTag(Platform.writeStackNBT(pending.stack, new NBTTagCompound())));
tag.setTag("injectQueue", tagListInjectQueue);
}
public static void nbtLoad(NBTTagCompound tag, IItemList<IAEStack<?>> list) {
Platform.readAEStackListNBT(tag.getTagList("extracted", NBT.TAG_COMPOUND)).forEach(list::add);
NBTTagList tagList = tag.getTagList("injectQueue", NBT.TAG_COMPOUND);
for (int i = 0; i < tagList.tagCount(); i++) list.add(Platform.readStackNBT(tagList.getCompoundTagAt(i)));
}
public void error() {
this.phase = ReshufflePhase.ERROR;
for (PendingInjection pending : this.injectQueue) {
if (pending.stack != null) this.cantInject.add(pending.stack);
}
this.injectQueue.clear();
this.injectLookup.clear();
this.injectIterator = null;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
private void returnPendingItems(Iterator<PendingInjection> i) {
while (i.hasNext()) {
final PendingInjection pending = i.next();
this.returnPendingItem(pending);
i.remove();
}
}
@SuppressWarnings({ "unchecked", "rawtypes" })
private void returnPendingItem(PendingInjection pending) {
long remaining = pending.stack.getStackSize();
for (SourceContribution source : pending.sources) {
if (remaining == 0) break;
final long amount = Math.min(remaining, source.amount);
final IAEStack<?> stack = pending.stack.copy().setStackSize(amount);
IAEStack<?> rejected = stack;
try {
rejected = source.source.injectItems(stack, Actionable.MODULATE, this.rollbackSrc);
} catch (Exception e) {
AELog.error(e, "Failed to restore a storage reshuffle stack to its source");
}
if (rejected != null) this.cantInject.add(rejected);
remaining -= amount;
}
if (remaining > 0) {
this.cantInject.add(pending.stack.copy().setStackSize(remaining));
}
pending.stack = null;
}
private void finalizeReport() {
this.phase = ReshufflePhase.DONE;
this.endTime = System.currentTimeMillis();
}
public ReshuffleReport getReport() {
return new ReshuffleReport(
this.typeFilters,
this.includeSubnets,
this.phase,
this.extractedTypes,
this.injectedTypes,
this.startTime,
this.endTime,
this.extractedItems,
this.injectedItems,
this.cantExtract,
this.cantInject,
this.beforeSnapshot,
this.afterSnapshot,
this.stackLookup);
}
public boolean isRunning() {
return this.phase == ReshufflePhase.EXTRACTION || this.phase == ReshufflePhase.INJECTION
|| this.phase == ReshufflePhase.AFTER_SNAPSHOT
|| this.phase == ReshufflePhase.BEFORE_SNAPSHOT;
}
}