Lots more moved
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* This file is part of Applied Energistics 2.
|
||||
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
|
||||
*
|
||||
* Applied Energistics 2 is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Applied Energistics 2 is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
|
||||
*/
|
||||
|
||||
package appeng.crafting;
|
||||
|
||||
import appeng.api.storage.data.IAEItemStack;
|
||||
import net.minecraft.util.registry.Registry;
|
||||
|
||||
public class CraftBranchFailure extends Exception {
|
||||
|
||||
private static final long serialVersionUID = 654603652836724823L;
|
||||
|
||||
private final IAEItemStack missing;
|
||||
|
||||
public CraftBranchFailure(final IAEItemStack what, final long howMany) {
|
||||
super("Failed: " + Registry.ITEM.getId(what.getItem()) + " x " + howMany);
|
||||
this.missing = what.copy();
|
||||
this.missing.setStackSize(howMany);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* This file is part of Applied Energistics 2.
|
||||
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
|
||||
*
|
||||
* Applied Energistics 2 is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Applied Energistics 2 is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
|
||||
*/
|
||||
|
||||
package appeng.crafting;
|
||||
|
||||
import appeng.api.storage.data.IAEItemStack;
|
||||
|
||||
public class CraftingCalculationFailure extends RuntimeException {
|
||||
|
||||
private static final long serialVersionUID = 654603652836724823L;
|
||||
|
||||
private final IAEItemStack missing;
|
||||
|
||||
public CraftingCalculationFailure(final IAEItemStack what, final long howMany) {
|
||||
super("this should have been caught!");
|
||||
this.missing = what.copy();
|
||||
this.missing.setStackSize(howMany);
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,345 @@
|
||||
/*
|
||||
* This file is part of Applied Energistics 2.
|
||||
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
|
||||
*
|
||||
* Applied Energistics 2 is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Applied Energistics 2 is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
|
||||
*/
|
||||
|
||||
package appeng.crafting;
|
||||
|
||||
// FIXME FABRIC DUMMY
|
||||
public class CraftingJob {
|
||||
public boolean simulateFor(int simTime) {
|
||||
return false;
|
||||
import java.util.HashMap;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import com.google.common.base.Stopwatch;
|
||||
|
||||
import net.minecraft.entity.player.PlayerEntity;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
import appeng.api.AEApi;
|
||||
import appeng.api.config.Actionable;
|
||||
import appeng.api.networking.IGrid;
|
||||
import appeng.api.networking.IGridHost;
|
||||
import appeng.api.networking.IGridNode;
|
||||
import appeng.api.networking.crafting.ICraftingCallback;
|
||||
import appeng.api.networking.crafting.ICraftingGrid;
|
||||
import appeng.api.networking.crafting.ICraftingJob;
|
||||
import appeng.api.networking.crafting.ICraftingPatternDetails;
|
||||
import appeng.api.networking.security.IActionHost;
|
||||
import appeng.api.networking.security.IActionSource;
|
||||
import appeng.api.networking.storage.IStorageGrid;
|
||||
import appeng.api.storage.channels.IItemStorageChannel;
|
||||
import appeng.api.storage.data.IAEItemStack;
|
||||
import appeng.api.storage.data.IItemList;
|
||||
import appeng.api.util.DimensionalCoord;
|
||||
import appeng.core.AELog;
|
||||
import appeng.hooks.TickHandler;
|
||||
|
||||
public class CraftingJob implements Runnable, ICraftingJob {
|
||||
private static final String LOG_CRAFTING_JOB = "CraftingJob (%s) issued by %s requesting [%s] using %s bytes took %s ms";
|
||||
private static final String LOG_MACHINE_SOURCE_DETAILS = "Machine[object=%s, %s]";
|
||||
|
||||
private final MECraftingInventory original;
|
||||
private final World world;
|
||||
private final IItemList<IAEItemStack> crafting = AEApi.instance().storage()
|
||||
.getStorageChannel(IItemStorageChannel.class).createList();
|
||||
private final IItemList<IAEItemStack> missing = AEApi.instance().storage()
|
||||
.getStorageChannel(IItemStorageChannel.class).createList();
|
||||
private final HashMap<String, TwoIntegers> opsAndMultiplier = new HashMap<>();
|
||||
private final Object monitor = new Object();
|
||||
private final Stopwatch watch = Stopwatch.createUnstarted();
|
||||
private CraftingTreeNode tree;
|
||||
private final IAEItemStack output;
|
||||
private boolean simulate = false;
|
||||
private MECraftingInventory availableCheck;
|
||||
private long bytes = 0;
|
||||
private final IActionSource actionSrc;
|
||||
private final ICraftingCallback callback;
|
||||
private boolean running = false;
|
||||
private boolean done = false;
|
||||
private int time = 5;
|
||||
private int incTime = Integer.MAX_VALUE;
|
||||
|
||||
private World wrapWorld(final World w) {
|
||||
return w;
|
||||
}
|
||||
|
||||
public CraftingJob(final World w, final IGrid grid, final IActionSource actionSrc, final IAEItemStack what,
|
||||
final ICraftingCallback callback) {
|
||||
this.world = this.wrapWorld(w);
|
||||
this.output = what.copy();
|
||||
this.actionSrc = actionSrc;
|
||||
|
||||
this.callback = callback;
|
||||
final ICraftingGrid cc = grid.getCache(ICraftingGrid.class);
|
||||
final IStorageGrid sg = grid.getCache(IStorageGrid.class);
|
||||
this.original = new MECraftingInventory(
|
||||
sg.getInventory(AEApi.instance().storage().getStorageChannel(IItemStorageChannel.class)), actionSrc,
|
||||
false, false, false);
|
||||
|
||||
this.setTree(this.getCraftingTree(cc, what));
|
||||
this.availableCheck = null;
|
||||
}
|
||||
|
||||
private CraftingTreeNode getCraftingTree(final ICraftingGrid cc, final IAEItemStack what) {
|
||||
return new CraftingTreeNode(cc, this, what, null, -1, 0);
|
||||
}
|
||||
|
||||
void refund(final IAEItemStack o) {
|
||||
this.availableCheck.injectItems(o, Actionable.MODULATE, this.actionSrc);
|
||||
}
|
||||
|
||||
IAEItemStack checkUse(final IAEItemStack available) {
|
||||
return this.availableCheck.extractItems(available, Actionable.MODULATE, this.actionSrc);
|
||||
}
|
||||
|
||||
public void writeToNBT(final CompoundTag out) {
|
||||
|
||||
}
|
||||
|
||||
void addTask(IAEItemStack what, final long crafts, final ICraftingPatternDetails details, final int depth) {
|
||||
if (crafts > 0) {
|
||||
what = what.copy();
|
||||
what.setStackSize(what.getStackSize() * crafts);
|
||||
this.crafting.add(what);
|
||||
}
|
||||
}
|
||||
|
||||
void addMissing(IAEItemStack what) {
|
||||
what = what.copy();
|
||||
this.missing.add(what);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
try {
|
||||
TickHandler.INSTANCE.registerCraftingSimulation(this.world, this);
|
||||
this.handlePausing();
|
||||
|
||||
final Stopwatch timer = Stopwatch.createStarted();
|
||||
|
||||
final MECraftingInventory craftingInventory = new MECraftingInventory(this.original, true, false, true);
|
||||
craftingInventory.ignore(this.output);
|
||||
|
||||
this.availableCheck = new MECraftingInventory(this.original, false, false, false);
|
||||
this.getTree().request(craftingInventory, this.output.getStackSize(), this.actionSrc);
|
||||
this.getTree().dive(this);
|
||||
|
||||
for (final String s : this.opsAndMultiplier.keySet()) {
|
||||
final TwoIntegers ti = this.opsAndMultiplier.get(s);
|
||||
AELog.crafting(s + " * " + ti.times + " = " + (ti.perOp * ti.times));
|
||||
}
|
||||
|
||||
this.logCraftingJob("real", timer);
|
||||
// if ( mode == Actionable.MODULATE )
|
||||
// craftingInventory.moveItemsToStorage( storage );
|
||||
} catch (final CraftBranchFailure e) {
|
||||
this.simulate = true;
|
||||
|
||||
try {
|
||||
final Stopwatch timer = Stopwatch.createStarted();
|
||||
final MECraftingInventory craftingInventory = new MECraftingInventory(this.original, true, false,
|
||||
true);
|
||||
craftingInventory.ignore(this.output);
|
||||
|
||||
this.availableCheck = new MECraftingInventory(this.original, false, false, false);
|
||||
|
||||
this.getTree().setSimulate();
|
||||
this.getTree().request(craftingInventory, this.output.getStackSize(), this.actionSrc);
|
||||
this.getTree().dive(this);
|
||||
|
||||
for (final String s : this.opsAndMultiplier.keySet()) {
|
||||
final TwoIntegers ti = this.opsAndMultiplier.get(s);
|
||||
AELog.crafting(s + " * " + ti.times + " = " + (ti.perOp * ti.times));
|
||||
}
|
||||
|
||||
this.logCraftingJob("simulate", timer);
|
||||
} catch (final CraftBranchFailure e1) {
|
||||
AELog.debug(e1);
|
||||
} catch (final CraftingCalculationFailure f) {
|
||||
AELog.debug(f);
|
||||
} catch (final InterruptedException e1) {
|
||||
AELog.crafting("Crafting calculation canceled.");
|
||||
this.finish();
|
||||
return;
|
||||
}
|
||||
} catch (final CraftingCalculationFailure f) {
|
||||
AELog.debug(f);
|
||||
} catch (final InterruptedException e1) {
|
||||
AELog.crafting("Crafting calculation canceled.");
|
||||
this.finish();
|
||||
return;
|
||||
}
|
||||
|
||||
AELog.craftingDebug("crafting job now done");
|
||||
} catch (final Throwable t) {
|
||||
this.finish();
|
||||
throw new IllegalStateException(t);
|
||||
}
|
||||
|
||||
this.finish();
|
||||
}
|
||||
|
||||
void handlePausing() throws InterruptedException {
|
||||
if (this.incTime > 100) {
|
||||
this.incTime = 0;
|
||||
|
||||
synchronized (this.monitor) {
|
||||
if (this.watch.elapsed(TimeUnit.MICROSECONDS) > this.time) {
|
||||
this.running = false;
|
||||
this.watch.stop();
|
||||
this.monitor.notify();
|
||||
}
|
||||
|
||||
if (!this.running) {
|
||||
AELog.craftingDebug("crafting job will now sleep");
|
||||
|
||||
while (!this.running) {
|
||||
this.monitor.wait();
|
||||
}
|
||||
|
||||
AELog.craftingDebug("crafting job now active");
|
||||
}
|
||||
}
|
||||
|
||||
if (Thread.interrupted()) {
|
||||
throw new InterruptedException();
|
||||
}
|
||||
}
|
||||
this.incTime++;
|
||||
}
|
||||
|
||||
private void finish() {
|
||||
if (this.callback != null) {
|
||||
this.callback.calculationComplete(this);
|
||||
}
|
||||
|
||||
this.availableCheck = null;
|
||||
|
||||
synchronized (this.monitor) {
|
||||
this.running = false;
|
||||
this.done = true;
|
||||
this.monitor.notify();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSimulation() {
|
||||
return this.simulate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getByteTotal() {
|
||||
return this.bytes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void populatePlan(final IItemList<IAEItemStack> plan) {
|
||||
if (this.getTree() != null) {
|
||||
this.getTree().getPlan(plan);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public IAEItemStack getOutput() {
|
||||
return this.output;
|
||||
}
|
||||
|
||||
public boolean isDone() {
|
||||
return this.done;
|
||||
}
|
||||
|
||||
World getWorld() {
|
||||
return this.world;
|
||||
}
|
||||
|
||||
/**
|
||||
* returns true if this needs more simulation.
|
||||
*
|
||||
* @param milli milliseconds of simulation
|
||||
*
|
||||
* @return true if this needs more simulation
|
||||
*/
|
||||
public boolean simulateFor(final int milli) {
|
||||
this.time = milli;
|
||||
|
||||
synchronized (this.monitor) {
|
||||
if (this.done) {
|
||||
return false;
|
||||
}
|
||||
|
||||
this.watch.reset();
|
||||
this.watch.start();
|
||||
this.running = true;
|
||||
|
||||
AELog.craftingDebug("main thread is now going to sleep");
|
||||
|
||||
this.monitor.notify();
|
||||
|
||||
while (this.running) {
|
||||
try {
|
||||
this.monitor.wait();
|
||||
} catch (final InterruptedException ignored) {
|
||||
}
|
||||
}
|
||||
|
||||
AELog.craftingDebug("main thread is now active");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void addBytes(final long crafts) {
|
||||
this.bytes += crafts;
|
||||
}
|
||||
|
||||
public CraftingTreeNode getTree() {
|
||||
return this.tree;
|
||||
}
|
||||
|
||||
private void setTree(final CraftingTreeNode tree) {
|
||||
this.tree = tree;
|
||||
}
|
||||
|
||||
private void logCraftingJob(String type, Stopwatch timer) {
|
||||
if (AELog.isCraftingLogEnabled()) {
|
||||
final String itemToOutput = this.output.toString();
|
||||
final long elapsedTime = timer.elapsed(TimeUnit.MILLISECONDS);
|
||||
final String actionSource;
|
||||
|
||||
if (this.actionSrc.player().isPresent()) {
|
||||
final PlayerEntity player = this.actionSrc.player().get();
|
||||
|
||||
actionSource = player.toString();
|
||||
} else if (this.actionSrc.machine().isPresent()) {
|
||||
final IActionHost machineSource = this.actionSrc.machine().get();
|
||||
final IGridNode actionableNode = machineSource.getActionableNode();
|
||||
final IGridHost machine = actionableNode.getMachine();
|
||||
final DimensionalCoord location = actionableNode.getGridBlock().getLocation();
|
||||
|
||||
actionSource = String.format(LOG_MACHINE_SOURCE_DETAILS, machine, location);
|
||||
} else {
|
||||
actionSource = "[unknown source]";
|
||||
}
|
||||
|
||||
AELog.crafting(LOG_CRAFTING_JOB, type, actionSource, itemToOutput, this.bytes, elapsedTime);
|
||||
}
|
||||
}
|
||||
|
||||
private static class TwoIntegers {
|
||||
private final long perOp = 0;
|
||||
private final long times = 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,182 @@
|
||||
/*
|
||||
* This file is part of Applied Energistics 2.
|
||||
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
|
||||
*
|
||||
* Applied Energistics 2 is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Applied Energistics 2 is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
|
||||
*/
|
||||
|
||||
package appeng.crafting;
|
||||
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
|
||||
import appeng.api.config.Actionable;
|
||||
import appeng.api.networking.crafting.ICraftingCPU;
|
||||
import appeng.api.networking.crafting.ICraftingLink;
|
||||
import appeng.api.networking.crafting.ICraftingRequester;
|
||||
import appeng.api.storage.data.IAEItemStack;
|
||||
|
||||
public class CraftingLink implements ICraftingLink {
|
||||
|
||||
private final ICraftingRequester req;
|
||||
private final ICraftingCPU cpu;
|
||||
private final String CraftID;
|
||||
private final boolean standalone;
|
||||
private boolean canceled = false;
|
||||
private boolean done = false;
|
||||
private CraftingLinkNexus tie;
|
||||
|
||||
public CraftingLink(final CompoundTag data, final ICraftingRequester req) {
|
||||
this.CraftID = data.getString("CraftID");
|
||||
this.setCanceled(data.getBoolean("canceled"));
|
||||
this.setDone(data.getBoolean("done"));
|
||||
this.standalone = data.getBoolean("standalone");
|
||||
|
||||
if (!data.contains("req") || !data.getBoolean("req")) {
|
||||
throw new IllegalStateException("Invalid Crafting Link for Object");
|
||||
}
|
||||
|
||||
this.req = req;
|
||||
this.cpu = null;
|
||||
}
|
||||
|
||||
public CraftingLink(final CompoundTag data, final ICraftingCPU cpu) {
|
||||
this.CraftID = data.getString("CraftID");
|
||||
this.setCanceled(data.getBoolean("canceled"));
|
||||
this.setDone(data.getBoolean("done"));
|
||||
this.standalone = data.getBoolean("standalone");
|
||||
|
||||
if (!data.contains("req") || data.getBoolean("req")) {
|
||||
throw new IllegalStateException("Invalid Crafting Link for Object");
|
||||
}
|
||||
|
||||
this.cpu = cpu;
|
||||
this.req = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCanceled() {
|
||||
if (this.canceled) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (this.done) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (this.tie == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return this.tie.isCanceled();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDone() {
|
||||
if (this.done) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (this.canceled) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (this.tie == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return this.tie.isDone();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cancel() {
|
||||
if (this.done) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.setCanceled(true);
|
||||
|
||||
if (this.tie != null) {
|
||||
this.tie.cancel();
|
||||
}
|
||||
|
||||
this.tie = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isStandalone() {
|
||||
return this.standalone;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToNBT(final CompoundTag tag) {
|
||||
tag.putString("CraftID", this.CraftID);
|
||||
tag.putBoolean("canceled", this.isCanceled());
|
||||
tag.putBoolean("done", this.isDone());
|
||||
tag.putBoolean("standalone", this.standalone);
|
||||
tag.putBoolean("req", this.getRequester() != null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCraftingID() {
|
||||
return this.CraftID;
|
||||
}
|
||||
|
||||
public void setNexus(final CraftingLinkNexus n) {
|
||||
if (this.tie != null) {
|
||||
this.tie.remove(this);
|
||||
}
|
||||
|
||||
if (this.isCanceled() && n != null) {
|
||||
n.cancel();
|
||||
this.tie = null;
|
||||
return;
|
||||
}
|
||||
|
||||
this.tie = n;
|
||||
|
||||
if (n != null) {
|
||||
n.add(this);
|
||||
}
|
||||
}
|
||||
|
||||
public IAEItemStack injectItems(final IAEItemStack input, final Actionable mode) {
|
||||
if (this.tie == null || this.tie.getRequest() == null || this.tie.getRequest().getRequester() == null) {
|
||||
return input;
|
||||
}
|
||||
|
||||
return this.tie.getRequest().getRequester().injectCraftedItems(this.tie.getRequest(), input, mode);
|
||||
}
|
||||
|
||||
public void markDone() {
|
||||
if (this.tie != null) {
|
||||
this.tie.markDone();
|
||||
}
|
||||
}
|
||||
|
||||
void setCanceled(final boolean canceled) {
|
||||
this.canceled = canceled;
|
||||
}
|
||||
|
||||
ICraftingRequester getRequester() {
|
||||
return this.req;
|
||||
}
|
||||
|
||||
ICraftingCPU getCpu() {
|
||||
return this.cpu;
|
||||
}
|
||||
|
||||
void setDone(final boolean done) {
|
||||
this.done = done;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
/*
|
||||
* This file is part of Applied Energistics 2.
|
||||
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
|
||||
*
|
||||
* Applied Energistics 2 is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Applied Energistics 2 is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
|
||||
*/
|
||||
|
||||
package appeng.crafting;
|
||||
|
||||
import appeng.api.networking.IGrid;
|
||||
import appeng.api.networking.IGridHost;
|
||||
import appeng.me.cache.CraftingGridCache;
|
||||
|
||||
public class CraftingLinkNexus {
|
||||
|
||||
private final String craftID;
|
||||
private boolean canceled = false;
|
||||
private boolean done = false;
|
||||
private int tickOfDeath = 0;
|
||||
private CraftingLink req;
|
||||
private CraftingLink cpu;
|
||||
|
||||
public CraftingLinkNexus(final String craftID) {
|
||||
this.craftID = craftID;
|
||||
}
|
||||
|
||||
public boolean isDead(final IGrid g, final CraftingGridCache craftingGridCache) {
|
||||
if (this.canceled || this.done) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (this.getRequest() == null || this.cpu == null) {
|
||||
this.tickOfDeath++;
|
||||
} else {
|
||||
final boolean hasCpu = craftingGridCache.hasCpu(this.cpu.getCpu());
|
||||
final boolean hasMachine = this.getRequest().getRequester().getActionableNode().getGrid() == g;
|
||||
|
||||
if (hasCpu && hasMachine) {
|
||||
this.tickOfDeath = 0;
|
||||
} else {
|
||||
this.tickOfDeath += 60;
|
||||
}
|
||||
}
|
||||
|
||||
if (this.tickOfDeath > 60) {
|
||||
this.cancel();
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void cancel() {
|
||||
this.canceled = true;
|
||||
|
||||
if (this.getRequest() != null) {
|
||||
this.getRequest().setCanceled(true);
|
||||
if (this.getRequest().getRequester() != null) {
|
||||
this.getRequest().getRequester().jobStateChange(this.getRequest());
|
||||
}
|
||||
}
|
||||
|
||||
if (this.cpu != null) {
|
||||
this.cpu.setCanceled(true);
|
||||
}
|
||||
}
|
||||
|
||||
void remove(final CraftingLink craftingLink) {
|
||||
if (this.getRequest() == craftingLink) {
|
||||
this.setRequest(null);
|
||||
} else if (this.cpu == craftingLink) {
|
||||
this.cpu = null;
|
||||
}
|
||||
}
|
||||
|
||||
void add(final CraftingLink craftingLink) {
|
||||
if (craftingLink.getCpu() != null) {
|
||||
this.cpu = craftingLink;
|
||||
} else if (craftingLink.getRequester() != null) {
|
||||
this.setRequest(craftingLink);
|
||||
}
|
||||
}
|
||||
|
||||
boolean isCanceled() {
|
||||
return this.canceled;
|
||||
}
|
||||
|
||||
boolean isDone() {
|
||||
return this.done;
|
||||
}
|
||||
|
||||
void markDone() {
|
||||
this.done = true;
|
||||
|
||||
if (this.getRequest() != null) {
|
||||
this.getRequest().setDone(true);
|
||||
if (this.getRequest().getRequester() != null) {
|
||||
this.getRequest().getRequester().jobStateChange(this.getRequest());
|
||||
}
|
||||
}
|
||||
|
||||
if (this.cpu != null) {
|
||||
this.cpu.setDone(true);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isMachine(final IGridHost machine) {
|
||||
return this.getRequest() == machine;
|
||||
}
|
||||
|
||||
public void removeNode() {
|
||||
if (this.getRequest() != null) {
|
||||
this.getRequest().setNexus(null);
|
||||
}
|
||||
|
||||
this.setRequest(null);
|
||||
this.tickOfDeath = 0;
|
||||
}
|
||||
|
||||
public CraftingLink getRequest() {
|
||||
return this.req;
|
||||
}
|
||||
|
||||
public void setRequest(final CraftingLink req) {
|
||||
this.req = req;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,345 @@
|
||||
/*
|
||||
* This file is part of Applied Energistics 2.
|
||||
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
|
||||
*
|
||||
* Applied Energistics 2 is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Applied Energistics 2 is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
|
||||
*/
|
||||
|
||||
package appeng.crafting;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
import net.minecraft.world.World;
|
||||
|
||||
import appeng.api.AEApi;
|
||||
import appeng.api.config.Actionable;
|
||||
import appeng.api.config.FuzzyMode;
|
||||
import appeng.api.networking.crafting.ICraftingGrid;
|
||||
import appeng.api.networking.crafting.ICraftingPatternDetails;
|
||||
import appeng.api.networking.security.IActionSource;
|
||||
import appeng.api.storage.channels.IItemStorageChannel;
|
||||
import appeng.api.storage.data.IAEItemStack;
|
||||
import appeng.api.storage.data.IItemList;
|
||||
import appeng.me.cluster.implementations.CraftingCPUCluster;
|
||||
|
||||
public class CraftingTreeNode {
|
||||
|
||||
// what slot!
|
||||
private final int slot;
|
||||
private final CraftingJob job;
|
||||
private final IItemList<IAEItemStack> used = AEApi.instance().storage().getStorageChannel(IItemStorageChannel.class)
|
||||
.createList();
|
||||
// parent node.
|
||||
private final CraftingTreeProcess parent;
|
||||
private final World world;
|
||||
// what item is this?
|
||||
private final IAEItemStack what;
|
||||
// what are the crafting patterns for this?
|
||||
private final ArrayList<CraftingTreeProcess> nodes = new ArrayList<>();
|
||||
private int bytes = 0;
|
||||
private boolean canEmit = false;
|
||||
private long missing = 0;
|
||||
private long howManyEmitted = 0;
|
||||
private boolean exhausted = false;
|
||||
|
||||
private boolean sim;
|
||||
|
||||
public CraftingTreeNode(final ICraftingGrid cc, final CraftingJob job, final IAEItemStack wat,
|
||||
final CraftingTreeProcess par, final int slot, final int depth) {
|
||||
this.what = wat;
|
||||
this.parent = par;
|
||||
this.slot = slot;
|
||||
this.world = job.getWorld();
|
||||
this.job = job;
|
||||
this.sim = false;
|
||||
|
||||
this.canEmit = cc.canEmitFor(this.what);
|
||||
|
||||
if (this.canEmit) {
|
||||
return; // if you can emit for something, you can't make it with patterns.
|
||||
}
|
||||
|
||||
for (final ICraftingPatternDetails details : cc.getCraftingFor(this.what,
|
||||
this.parent == null ? null : this.parent.details, slot, this.world))// in
|
||||
// order.
|
||||
{
|
||||
if (this.parent == null || this.parent.notRecursive(details)) {
|
||||
this.nodes.add(new CraftingTreeProcess(cc, job, details, this, depth + 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
boolean notRecursive(final ICraftingPatternDetails details) {
|
||||
IAEItemStack[] o = details.getCondensedOutputs();
|
||||
|
||||
for (final IAEItemStack i : o) {
|
||||
if (i.equals(this.what)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
o = details.getCondensedInputs();
|
||||
|
||||
for (final IAEItemStack i : o) {
|
||||
if (i.equals(this.what)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (this.parent == null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return this.parent.notRecursive(details);
|
||||
}
|
||||
|
||||
IAEItemStack request(final MECraftingInventory inv, long l, final IActionSource src)
|
||||
throws CraftBranchFailure, InterruptedException {
|
||||
this.job.handlePausing();
|
||||
|
||||
final List<IAEItemStack> thingsUsed = new ArrayList<>();
|
||||
|
||||
this.what.setStackSize(l);
|
||||
if (this.getSlot() >= 0 && this.parent != null && this.parent.details.isCraftable()) {
|
||||
final Collection<IAEItemStack> itemList;
|
||||
final IItemList<IAEItemStack> inventoryList = inv.getItemList();
|
||||
|
||||
if (this.parent.details.canSubstitute()) {
|
||||
itemList = inventoryList.findFuzzy(this.what, FuzzyMode.IGNORE_ALL);
|
||||
} else {
|
||||
itemList = Lists.newArrayList();
|
||||
|
||||
final IAEItemStack item = inventoryList.findPrecise(this.what);
|
||||
|
||||
if (item != null) {
|
||||
itemList.add(item);
|
||||
}
|
||||
}
|
||||
|
||||
for (IAEItemStack fuzz : itemList) {
|
||||
if (this.parent.details.isValidItemForSlot(this.getSlot(),
|
||||
fuzz.copy().setStackSize(1).createItemStack(), this.world)) {
|
||||
fuzz = fuzz.copy();
|
||||
fuzz.setStackSize(l);
|
||||
|
||||
final IAEItemStack available = inv.extractItems(fuzz, Actionable.MODULATE, src);
|
||||
|
||||
if (available != null) {
|
||||
if (!this.exhausted) {
|
||||
final IAEItemStack is = this.job.checkUse(available);
|
||||
|
||||
if (is != null) {
|
||||
thingsUsed.add(is.copy());
|
||||
this.used.add(is);
|
||||
}
|
||||
}
|
||||
|
||||
this.bytes += available.getStackSize();
|
||||
l -= available.getStackSize();
|
||||
|
||||
if (l == 0) {
|
||||
return available;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
final IAEItemStack available = inv.extractItems(this.what, Actionable.MODULATE, src);
|
||||
|
||||
if (available != null) {
|
||||
if (!this.exhausted) {
|
||||
final IAEItemStack is = this.job.checkUse(available);
|
||||
|
||||
if (is != null) {
|
||||
thingsUsed.add(is.copy());
|
||||
this.used.add(is);
|
||||
}
|
||||
}
|
||||
|
||||
this.bytes += available.getStackSize();
|
||||
l -= available.getStackSize();
|
||||
|
||||
if (l == 0) {
|
||||
return available;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (this.canEmit) {
|
||||
final IAEItemStack wat = this.what.copy();
|
||||
wat.setStackSize(l);
|
||||
|
||||
this.howManyEmitted = wat.getStackSize();
|
||||
this.bytes += wat.getStackSize();
|
||||
|
||||
return wat;
|
||||
}
|
||||
|
||||
this.exhausted = true;
|
||||
|
||||
if (this.nodes.size() == 1) {
|
||||
final CraftingTreeProcess pro = this.nodes.get(0);
|
||||
|
||||
while (pro.possible && l > 0) {
|
||||
final IAEItemStack madeWhat = pro.getAmountCrafted(this.what);
|
||||
|
||||
pro.request(inv, pro.getTimes(l, madeWhat.getStackSize()), src);
|
||||
|
||||
madeWhat.setStackSize(l);
|
||||
|
||||
final IAEItemStack available = inv.extractItems(madeWhat, Actionable.MODULATE, src);
|
||||
|
||||
if (available != null) {
|
||||
this.bytes += available.getStackSize();
|
||||
l -= available.getStackSize();
|
||||
|
||||
if (l <= 0) {
|
||||
return available;
|
||||
}
|
||||
} else {
|
||||
pro.possible = false; // ;P
|
||||
}
|
||||
}
|
||||
} else if (this.nodes.size() > 1) {
|
||||
for (final CraftingTreeProcess pro : this.nodes) {
|
||||
try {
|
||||
while (pro.possible && l > 0) {
|
||||
final MECraftingInventory subInv = new MECraftingInventory(inv, true, true, true);
|
||||
pro.request(subInv, 1, src);
|
||||
|
||||
this.what.setStackSize(l);
|
||||
final IAEItemStack available = subInv.extractItems(this.what, Actionable.MODULATE, src);
|
||||
|
||||
if (available != null) {
|
||||
if (!subInv.commit(src)) {
|
||||
throw new CraftBranchFailure(this.what, l);
|
||||
}
|
||||
|
||||
this.bytes += available.getStackSize();
|
||||
l -= available.getStackSize();
|
||||
|
||||
if (l <= 0) {
|
||||
return available;
|
||||
}
|
||||
} else {
|
||||
pro.possible = false; // ;P
|
||||
}
|
||||
}
|
||||
} catch (final CraftBranchFailure fail) {
|
||||
pro.possible = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (this.sim) {
|
||||
this.missing += l;
|
||||
this.bytes += l;
|
||||
final IAEItemStack rv = this.what.copy();
|
||||
rv.setStackSize(l);
|
||||
return rv;
|
||||
}
|
||||
|
||||
for (final IAEItemStack o : thingsUsed) {
|
||||
this.job.refund(o.copy());
|
||||
o.setStackSize(-o.getStackSize());
|
||||
this.used.add(o);
|
||||
}
|
||||
|
||||
throw new CraftBranchFailure(this.what, l);
|
||||
}
|
||||
|
||||
void dive(final CraftingJob job) {
|
||||
if (this.missing > 0) {
|
||||
job.addMissing(this.getStack(this.missing));
|
||||
}
|
||||
// missing = 0;
|
||||
|
||||
job.addBytes(8 + this.bytes);
|
||||
|
||||
for (final CraftingTreeProcess pro : this.nodes) {
|
||||
pro.dive(job);
|
||||
}
|
||||
}
|
||||
|
||||
IAEItemStack getStack(final long size) {
|
||||
final IAEItemStack is = this.what.copy();
|
||||
is.setStackSize(size);
|
||||
return is;
|
||||
}
|
||||
|
||||
void setSimulate() {
|
||||
this.sim = true;
|
||||
this.missing = 0;
|
||||
this.bytes = 0;
|
||||
this.used.resetStatus();
|
||||
this.exhausted = false;
|
||||
|
||||
for (final CraftingTreeProcess pro : this.nodes) {
|
||||
pro.setSimulate();
|
||||
}
|
||||
}
|
||||
|
||||
public void setJob(final MECraftingInventory storage, final CraftingCPUCluster craftingCPUCluster,
|
||||
final IActionSource src) throws CraftBranchFailure {
|
||||
for (final IAEItemStack i : this.used) {
|
||||
final IAEItemStack ex = storage.extractItems(i, Actionable.MODULATE, src);
|
||||
|
||||
if (ex == null || ex.getStackSize() != i.getStackSize()) {
|
||||
throw new CraftBranchFailure(i, i.getStackSize());
|
||||
}
|
||||
|
||||
craftingCPUCluster.addStorage(ex);
|
||||
}
|
||||
|
||||
if (this.howManyEmitted > 0) {
|
||||
final IAEItemStack i = this.what.copy().reset();
|
||||
i.setStackSize(this.howManyEmitted);
|
||||
craftingCPUCluster.addEmitable(i);
|
||||
}
|
||||
|
||||
for (final CraftingTreeProcess pro : this.nodes) {
|
||||
pro.setJob(storage, craftingCPUCluster, src);
|
||||
}
|
||||
}
|
||||
|
||||
void getPlan(final IItemList<IAEItemStack> plan) {
|
||||
if (this.missing > 0) {
|
||||
final IAEItemStack o = this.what.copy();
|
||||
o.setStackSize(this.missing);
|
||||
plan.add(o);
|
||||
}
|
||||
|
||||
if (this.howManyEmitted > 0) {
|
||||
final IAEItemStack i = this.what.copy();
|
||||
i.setCountRequestable(this.howManyEmitted);
|
||||
plan.addRequestable(i);
|
||||
}
|
||||
|
||||
for (final IAEItemStack i : this.used) {
|
||||
plan.add(i.copy());
|
||||
}
|
||||
|
||||
for (final CraftingTreeProcess pro : this.nodes) {
|
||||
pro.getPlan(plan);
|
||||
}
|
||||
}
|
||||
|
||||
int getSlot() {
|
||||
return this.slot;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,279 @@
|
||||
/*
|
||||
* This file is part of Applied Energistics 2.
|
||||
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
|
||||
*
|
||||
* Applied Energistics 2 is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Applied Energistics 2 is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
|
||||
*/
|
||||
|
||||
package appeng.crafting;
|
||||
|
||||
import appeng.api.AEApi;
|
||||
import appeng.api.config.Actionable;
|
||||
import appeng.api.networking.crafting.ICraftingGrid;
|
||||
import appeng.api.networking.crafting.ICraftingPatternDetails;
|
||||
import appeng.api.networking.security.IActionSource;
|
||||
import appeng.api.storage.channels.IItemStorageChannel;
|
||||
import appeng.api.storage.data.IAEItemStack;
|
||||
import appeng.api.storage.data.IItemList;
|
||||
import appeng.container.ContainerNull;
|
||||
import appeng.me.cluster.implementations.CraftingCPUCluster;
|
||||
import appeng.util.Platform;
|
||||
import net.minecraft.inventory.CraftingInventory;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
public class CraftingTreeProcess {
|
||||
|
||||
private final CraftingTreeNode parent;
|
||||
final ICraftingPatternDetails details;
|
||||
private final CraftingJob job;
|
||||
private final Map<CraftingTreeNode, Long> nodes = new HashMap<>();
|
||||
private final int depth;
|
||||
boolean possible = true;
|
||||
private World world;
|
||||
private long crafts = 0;
|
||||
private boolean containerItems;
|
||||
private boolean limitQty;
|
||||
private boolean fullSimulation;
|
||||
private long bytes = 0;
|
||||
|
||||
public CraftingTreeProcess(final ICraftingGrid cc, final CraftingJob job, final ICraftingPatternDetails details,
|
||||
final CraftingTreeNode craftingTreeNode, final int depth) {
|
||||
this.parent = craftingTreeNode;
|
||||
this.details = details;
|
||||
this.job = job;
|
||||
this.depth = depth;
|
||||
final World world = job.getWorld();
|
||||
|
||||
if (details.isCraftable()) {
|
||||
final IAEItemStack[] list = details.getInputs();
|
||||
|
||||
final CraftingInventory ic = new CraftingInventory(new ContainerNull(), 3, 3);
|
||||
final IAEItemStack[] is = details.getInputs();
|
||||
for (int x = 0; x < ic.size(); x++) {
|
||||
ic.setStack(x, is[x] == null ? ItemStack.EMPTY : is[x].createItemStack());
|
||||
}
|
||||
|
||||
// FIXME FABRIC Hooks don't exist
|
||||
// FIXME FABRIC BasicEventHooks.firePlayerCraftingEvent((PlayerEntity) FakePlayer.getOrCreate((ServerWorld) world),
|
||||
// FIXME FABRIC details.getOutput(ic, world), ic);
|
||||
|
||||
for (int x = 0; x < ic.size(); x++) {
|
||||
final ItemStack g = ic.getStack(x);
|
||||
if (!g.isEmpty() && g.getCount() > 1) {
|
||||
this.fullSimulation = true;
|
||||
}
|
||||
}
|
||||
|
||||
for (final IAEItemStack part : details.getCondensedInputs()) {
|
||||
final ItemStack g = part.createItemStack();
|
||||
|
||||
boolean isAnInput = false;
|
||||
for (final IAEItemStack a : details.getCondensedOutputs()) {
|
||||
if (!g.isEmpty() && a != null && a.equals(g)) {
|
||||
isAnInput = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (isAnInput) {
|
||||
this.limitQty = true;
|
||||
}
|
||||
|
||||
if (g.getItem().hasRecipeRemainder()) {
|
||||
this.limitQty = this.containerItems = true;
|
||||
}
|
||||
}
|
||||
|
||||
final boolean complicated = false;
|
||||
|
||||
if (this.containerItems || complicated) {
|
||||
for (int x = 0; x < list.length; x++) {
|
||||
final IAEItemStack part = list[x];
|
||||
if (part != null) {
|
||||
this.nodes.put(new CraftingTreeNode(cc, job, part.copy(), this, x, depth + 1),
|
||||
part.getStackSize());
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// this is minor different then below, this slot uses the pattern, but kinda
|
||||
// fudges it.
|
||||
for (final IAEItemStack part : details.getCondensedInputs()) {
|
||||
for (int x = 0; x < list.length; x++) {
|
||||
final IAEItemStack comparePart = list[x];
|
||||
if (part != null && part.equals(comparePart)) {
|
||||
// use the first slot...
|
||||
this.nodes.put(new CraftingTreeNode(cc, job, part.copy(), this, x, depth + 1),
|
||||
part.getStackSize());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (final IAEItemStack part : details.getCondensedInputs()) {
|
||||
final ItemStack g = part.createItemStack();
|
||||
|
||||
boolean isAnInput = false;
|
||||
for (final IAEItemStack a : details.getCondensedOutputs()) {
|
||||
if (!g.isEmpty() && a != null && a.equals(g)) {
|
||||
isAnInput = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (isAnInput) {
|
||||
this.limitQty = true;
|
||||
}
|
||||
}
|
||||
|
||||
for (final IAEItemStack part : details.getCondensedInputs()) {
|
||||
this.nodes.put(new CraftingTreeNode(cc, job, part.copy(), this, -1, depth + 1), part.getStackSize());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
boolean notRecursive(final ICraftingPatternDetails details) {
|
||||
return this.parent == null || this.parent.notRecursive(details);
|
||||
}
|
||||
|
||||
long getTimes(final long remaining, final long stackSize) {
|
||||
if (this.limitQty || this.fullSimulation) {
|
||||
return 1;
|
||||
}
|
||||
return (remaining / stackSize) + (remaining % stackSize != 0 ? 1 : 0);
|
||||
}
|
||||
|
||||
void request(final MECraftingInventory inv, final long i, final IActionSource src)
|
||||
throws CraftBranchFailure, InterruptedException {
|
||||
this.job.handlePausing();
|
||||
|
||||
if (this.fullSimulation) {
|
||||
final CraftingInventory ic = new CraftingInventory(new ContainerNull(), 3, 3);
|
||||
|
||||
for (final Entry<CraftingTreeNode, Long> entry : this.nodes.entrySet()) {
|
||||
final IAEItemStack item = entry.getKey().getStack(entry.getValue());
|
||||
final IAEItemStack stack = entry.getKey().request(inv, item.getStackSize(), src);
|
||||
|
||||
ic.setStack(entry.getKey().getSlot(), stack.createItemStack());
|
||||
}
|
||||
|
||||
// FIXME FABRIC Hook doesn't exist
|
||||
// FIXME FABRIC BasicEventHooks.firePlayerCraftingEvent((PlayerEntity) FakePlayer.getOrCreate((ServerWorld) this.world),
|
||||
// FIXME FABRIC this.details.getOutput(ic, this.world), ic);
|
||||
|
||||
for (int x = 0; x < ic.size(); x++) {
|
||||
ItemStack is = ic.getStack(x);
|
||||
is = Platform.getRecipeRemainder(is);
|
||||
|
||||
final IAEItemStack o = AEApi.instance().storage().getStorageChannel(IItemStorageChannel.class)
|
||||
.createStack(is);
|
||||
if (o != null) {
|
||||
this.bytes++;
|
||||
inv.injectItems(o, Actionable.MODULATE, src);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// request and remove inputs...
|
||||
for (final Entry<CraftingTreeNode, Long> entry : this.nodes.entrySet()) {
|
||||
final IAEItemStack item = entry.getKey().getStack(entry.getValue());
|
||||
final IAEItemStack stack = entry.getKey().request(inv, item.getStackSize() * i, src);
|
||||
|
||||
if (this.containerItems) {
|
||||
final ItemStack is = Platform.getRecipeRemainder(stack.createItemStack());
|
||||
final IAEItemStack o = AEApi.instance().storage().getStorageChannel(IItemStorageChannel.class)
|
||||
.createStack(is);
|
||||
if (o != null) {
|
||||
this.bytes++;
|
||||
inv.injectItems(o, Actionable.MODULATE, src);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// assume its possible.
|
||||
|
||||
// add crafting results..
|
||||
for (final IAEItemStack out : this.details.getCondensedOutputs()) {
|
||||
final IAEItemStack o = out.copy();
|
||||
o.setStackSize(o.getStackSize() * i);
|
||||
inv.injectItems(o, Actionable.MODULATE, src);
|
||||
}
|
||||
|
||||
this.crafts += i;
|
||||
}
|
||||
|
||||
void dive(final CraftingJob job) {
|
||||
job.addTask(this.getAmountCrafted(this.parent.getStack(1)), this.crafts, this.details, this.depth);
|
||||
for (final CraftingTreeNode pro : this.nodes.keySet()) {
|
||||
pro.dive(job);
|
||||
}
|
||||
|
||||
job.addBytes(8 + this.crafts + this.bytes);
|
||||
}
|
||||
|
||||
IAEItemStack getAmountCrafted(IAEItemStack what2) {
|
||||
for (final IAEItemStack is : this.details.getCondensedOutputs()) {
|
||||
if (is.equals(what2)) {
|
||||
what2 = what2.copy();
|
||||
what2.setStackSize(is.getStackSize());
|
||||
return what2;
|
||||
}
|
||||
}
|
||||
|
||||
// more fuzzy!
|
||||
for (final IAEItemStack is : this.details.getCondensedOutputs()) {
|
||||
if (is.getItem() == what2.getItem()
|
||||
&& (is.getItem().isDamageable() || is.getItemDamage() == what2.getItemDamage())) {
|
||||
what2 = is.copy();
|
||||
what2.setStackSize(is.getStackSize());
|
||||
return what2;
|
||||
}
|
||||
}
|
||||
|
||||
throw new IllegalStateException("Crafting Tree construction failed.");
|
||||
}
|
||||
|
||||
void setSimulate() {
|
||||
this.crafts = 0;
|
||||
this.bytes = 0;
|
||||
|
||||
for (final CraftingTreeNode pro : this.nodes.keySet()) {
|
||||
pro.setSimulate();
|
||||
}
|
||||
}
|
||||
|
||||
void setJob(final MECraftingInventory storage, final CraftingCPUCluster craftingCPUCluster, final IActionSource src)
|
||||
throws CraftBranchFailure {
|
||||
craftingCPUCluster.addCrafting(this.details, this.crafts);
|
||||
|
||||
for (final CraftingTreeNode pro : this.nodes.keySet()) {
|
||||
pro.setJob(storage, craftingCPUCluster, src);
|
||||
}
|
||||
}
|
||||
|
||||
void getPlan(final IItemList<IAEItemStack> plan) {
|
||||
for (IAEItemStack i : this.details.getOutputs()) {
|
||||
i = i.copy();
|
||||
i.setCountRequestable(i.getStackSize() * this.crafts);
|
||||
plan.addRequestable(i);
|
||||
}
|
||||
|
||||
for (final CraftingTreeNode pro : this.nodes.keySet()) {
|
||||
pro.getPlan(plan);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* This file is part of Applied Energistics 2.
|
||||
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
|
||||
*
|
||||
* Applied Energistics 2 is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Applied Energistics 2 is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
|
||||
*/
|
||||
|
||||
package appeng.crafting;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.Set;
|
||||
|
||||
import appeng.api.networking.crafting.ICraftingWatcher;
|
||||
import appeng.api.networking.crafting.ICraftingWatcherHost;
|
||||
import appeng.api.storage.data.IAEStack;
|
||||
import appeng.me.cache.CraftingGridCache;
|
||||
|
||||
/**
|
||||
* Maintain my interests, and a global watch list, they should always be fully
|
||||
* synchronized.
|
||||
*/
|
||||
public class CraftingWatcher implements ICraftingWatcher {
|
||||
|
||||
private final CraftingGridCache gsc;
|
||||
private final ICraftingWatcherHost host;
|
||||
private final Set<IAEStack> myInterests = new HashSet<>();
|
||||
|
||||
public CraftingWatcher(final CraftingGridCache cache, final ICraftingWatcherHost host) {
|
||||
this.gsc = cache;
|
||||
this.host = host;
|
||||
}
|
||||
|
||||
public ICraftingWatcherHost getHost() {
|
||||
return this.host;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean add(final IAEStack e) {
|
||||
if (this.myInterests.contains(e)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return this.myInterests.add(e.copy()) && this.gsc.getInterestManager().put(e, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean remove(final IAEStack o) {
|
||||
return this.myInterests.remove(o) && this.gsc.getInterestManager().remove(o, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reset() {
|
||||
final Iterator<IAEStack> i = this.myInterests.iterator();
|
||||
|
||||
while (i.hasNext()) {
|
||||
this.gsc.getInterestManager().remove(i.next(), this);
|
||||
i.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,294 @@
|
||||
/*
|
||||
* This file is part of Applied Energistics 2.
|
||||
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
|
||||
*
|
||||
* Applied Energistics 2 is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Applied Energistics 2 is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
|
||||
*/
|
||||
|
||||
package appeng.crafting;
|
||||
|
||||
import appeng.api.AEApi;
|
||||
import appeng.api.config.Actionable;
|
||||
import appeng.api.networking.security.IActionSource;
|
||||
import appeng.api.storage.IMEInventory;
|
||||
import appeng.api.storage.IMEMonitor;
|
||||
import appeng.api.storage.IStorageChannel;
|
||||
import appeng.api.storage.channels.IItemStorageChannel;
|
||||
import appeng.api.storage.data.IAEItemStack;
|
||||
import appeng.api.storage.data.IItemList;
|
||||
import appeng.util.inv.ItemListIgnoreCrafting;
|
||||
|
||||
public class MECraftingInventory implements IMEInventory<IAEItemStack> {
|
||||
|
||||
private final MECraftingInventory par;
|
||||
|
||||
private final IMEInventory<IAEItemStack> target;
|
||||
private final IItemList<IAEItemStack> localCache;
|
||||
|
||||
private final boolean logExtracted;
|
||||
private final IItemList<IAEItemStack> extractedCache;
|
||||
|
||||
private final boolean logInjections;
|
||||
private final IItemList<IAEItemStack> injectedCache;
|
||||
|
||||
private final boolean logMissing;
|
||||
private final IItemList<IAEItemStack> missingCache;
|
||||
|
||||
public MECraftingInventory() {
|
||||
this.localCache = new ItemListIgnoreCrafting<>(
|
||||
AEApi.instance().storage().getStorageChannel(IItemStorageChannel.class).createList());
|
||||
this.extractedCache = null;
|
||||
this.injectedCache = null;
|
||||
this.missingCache = null;
|
||||
this.logExtracted = false;
|
||||
this.logInjections = false;
|
||||
this.logMissing = false;
|
||||
this.target = null;
|
||||
this.par = null;
|
||||
}
|
||||
|
||||
public MECraftingInventory(final MECraftingInventory parent) {
|
||||
this.target = parent;
|
||||
this.logExtracted = parent.logExtracted;
|
||||
this.logInjections = parent.logInjections;
|
||||
this.logMissing = parent.logMissing;
|
||||
|
||||
if (this.logMissing) {
|
||||
this.missingCache = AEApi.instance().storage().getStorageChannel(IItemStorageChannel.class).createList();
|
||||
} else {
|
||||
this.missingCache = null;
|
||||
}
|
||||
|
||||
if (this.logExtracted) {
|
||||
this.extractedCache = AEApi.instance().storage().getStorageChannel(IItemStorageChannel.class).createList();
|
||||
} else {
|
||||
this.extractedCache = null;
|
||||
}
|
||||
|
||||
if (this.logInjections) {
|
||||
this.injectedCache = AEApi.instance().storage().getStorageChannel(IItemStorageChannel.class).createList();
|
||||
} else {
|
||||
this.injectedCache = null;
|
||||
}
|
||||
|
||||
this.localCache = this.target.getAvailableItems(new ItemListIgnoreCrafting<>(
|
||||
AEApi.instance().storage().getStorageChannel(IItemStorageChannel.class).createList()));
|
||||
|
||||
this.par = parent;
|
||||
}
|
||||
|
||||
public MECraftingInventory(final IMEMonitor<IAEItemStack> target, final IActionSource src,
|
||||
final boolean logExtracted, final boolean logInjections, final boolean logMissing) {
|
||||
this.target = target;
|
||||
this.logExtracted = logExtracted;
|
||||
this.logInjections = logInjections;
|
||||
this.logMissing = logMissing;
|
||||
|
||||
if (logMissing) {
|
||||
this.missingCache = AEApi.instance().storage().getStorageChannel(IItemStorageChannel.class).createList();
|
||||
} else {
|
||||
this.missingCache = null;
|
||||
}
|
||||
|
||||
if (logExtracted) {
|
||||
this.extractedCache = AEApi.instance().storage().getStorageChannel(IItemStorageChannel.class).createList();
|
||||
} else {
|
||||
this.extractedCache = null;
|
||||
}
|
||||
|
||||
if (logInjections) {
|
||||
this.injectedCache = AEApi.instance().storage().getStorageChannel(IItemStorageChannel.class).createList();
|
||||
} else {
|
||||
this.injectedCache = null;
|
||||
}
|
||||
|
||||
this.localCache = new ItemListIgnoreCrafting<>(
|
||||
AEApi.instance().storage().getStorageChannel(IItemStorageChannel.class).createList());
|
||||
for (final IAEItemStack is : target.getStorageList()) {
|
||||
this.localCache.add(target.extractItems(is, Actionable.SIMULATE, src));
|
||||
}
|
||||
|
||||
this.par = null;
|
||||
}
|
||||
|
||||
public MECraftingInventory(final IMEInventory<IAEItemStack> target, final boolean logExtracted,
|
||||
final boolean logInjections, final boolean logMissing) {
|
||||
this.target = target;
|
||||
this.logExtracted = logExtracted;
|
||||
this.logInjections = logInjections;
|
||||
this.logMissing = logMissing;
|
||||
|
||||
if (logMissing) {
|
||||
this.missingCache = AEApi.instance().storage().getStorageChannel(IItemStorageChannel.class).createList();
|
||||
} else {
|
||||
this.missingCache = null;
|
||||
}
|
||||
|
||||
if (logExtracted) {
|
||||
this.extractedCache = AEApi.instance().storage().getStorageChannel(IItemStorageChannel.class).createList();
|
||||
} else {
|
||||
this.extractedCache = null;
|
||||
}
|
||||
|
||||
if (logInjections) {
|
||||
this.injectedCache = AEApi.instance().storage().getStorageChannel(IItemStorageChannel.class).createList();
|
||||
} else {
|
||||
this.injectedCache = null;
|
||||
}
|
||||
|
||||
this.localCache = target.getAvailableItems(
|
||||
AEApi.instance().storage().getStorageChannel(IItemStorageChannel.class).createList());
|
||||
this.par = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IAEItemStack injectItems(final IAEItemStack input, final Actionable mode, final IActionSource src) {
|
||||
if (input == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (mode == Actionable.MODULATE) {
|
||||
if (this.logInjections) {
|
||||
this.injectedCache.add(input);
|
||||
}
|
||||
this.localCache.add(input);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IAEItemStack extractItems(final IAEItemStack request, final Actionable mode, final IActionSource src) {
|
||||
if (request == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final IAEItemStack list = this.localCache.findPrecise(request);
|
||||
if (list == null || list.getStackSize() == 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (list.getStackSize() >= request.getStackSize()) {
|
||||
if (mode == Actionable.MODULATE) {
|
||||
list.decStackSize(request.getStackSize());
|
||||
if (this.logExtracted) {
|
||||
this.extractedCache.add(request);
|
||||
}
|
||||
}
|
||||
|
||||
return request;
|
||||
}
|
||||
|
||||
final IAEItemStack ret = request.copy();
|
||||
ret.setStackSize(list.getStackSize());
|
||||
|
||||
if (mode == Actionable.MODULATE) {
|
||||
list.reset();
|
||||
if (this.logExtracted) {
|
||||
this.extractedCache.add(ret);
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IItemList<IAEItemStack> getAvailableItems(final IItemList<IAEItemStack> out) {
|
||||
for (final IAEItemStack is : this.localCache) {
|
||||
out.add(is);
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IStorageChannel getChannel() {
|
||||
return AEApi.instance().storage().getStorageChannel(IItemStorageChannel.class);
|
||||
}
|
||||
|
||||
public IItemList<IAEItemStack> getItemList() {
|
||||
return this.localCache;
|
||||
}
|
||||
|
||||
public boolean commit(final IActionSource src) {
|
||||
final IItemList<IAEItemStack> added = AEApi.instance().storage().getStorageChannel(IItemStorageChannel.class)
|
||||
.createList();
|
||||
final IItemList<IAEItemStack> pulled = AEApi.instance().storage().getStorageChannel(IItemStorageChannel.class)
|
||||
.createList();
|
||||
boolean failed = false;
|
||||
|
||||
if (this.logInjections) {
|
||||
for (final IAEItemStack inject : this.injectedCache) {
|
||||
IAEItemStack result = null;
|
||||
added.add(result = this.target.injectItems(inject, Actionable.MODULATE, src));
|
||||
|
||||
if (result != null) {
|
||||
failed = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (failed) {
|
||||
for (final IAEItemStack is : added) {
|
||||
this.target.extractItems(is, Actionable.MODULATE, src);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (this.logExtracted) {
|
||||
for (final IAEItemStack extra : this.extractedCache) {
|
||||
IAEItemStack result = null;
|
||||
pulled.add(result = this.target.extractItems(extra, Actionable.MODULATE, src));
|
||||
|
||||
if (result == null || result.getStackSize() != extra.getStackSize()) {
|
||||
failed = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (failed) {
|
||||
for (final IAEItemStack is : added) {
|
||||
this.target.extractItems(is, Actionable.MODULATE, src);
|
||||
}
|
||||
|
||||
for (final IAEItemStack is : pulled) {
|
||||
this.target.injectItems(is, Actionable.MODULATE, src);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (this.logMissing && this.par != null) {
|
||||
for (final IAEItemStack extra : this.missingCache) {
|
||||
this.par.addMissing(extra);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private void addMissing(final IAEItemStack extra) {
|
||||
this.missingCache.add(extra);
|
||||
}
|
||||
|
||||
void ignore(final IAEItemStack what) {
|
||||
final IAEItemStack list = this.localCache.findPrecise(what);
|
||||
if (list != null) {
|
||||
list.setStackSize(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user