pick 97420a31d The big reformat of 2020
This commit is contained in:
@@ -18,7 +18,6 @@
|
||||
|
||||
package appeng.crafting;
|
||||
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@@ -47,361 +46,300 @@ 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]";
|
||||
|
||||
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 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;
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
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.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;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
IAEItemStack checkUse( final IAEItemStack available )
|
||||
{
|
||||
return this.availableCheck.extractItems( available, Actionable.MODULATE, this.actionSrc );
|
||||
}
|
||||
public void writeToNBT(final CompoundNBT out) {
|
||||
|
||||
public void writeToNBT( final CompoundNBT 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 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);
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
try
|
||||
{
|
||||
try
|
||||
{
|
||||
TickHandler.INSTANCE.registerCraftingSimulation( this.world, this );
|
||||
this.handlePausing();
|
||||
final Stopwatch timer = Stopwatch.createStarted();
|
||||
|
||||
final Stopwatch timer = Stopwatch.createStarted();
|
||||
final MECraftingInventory craftingInventory = new MECraftingInventory(this.original, true, false, true);
|
||||
craftingInventory.ignore(this.output);
|
||||
|
||||
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);
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
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);
|
||||
|
||||
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.availableCheck = new MECraftingInventory( this.original, false, false, false );
|
||||
this.getTree().setSimulate();
|
||||
this.getTree().request(craftingInventory, this.output.getStackSize(), this.actionSrc);
|
||||
this.getTree().dive(this);
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
AELog.craftingDebug( "crafting job now done" );
|
||||
}
|
||||
catch( final Throwable t )
|
||||
{
|
||||
this.finish();
|
||||
throw new IllegalStateException( t );
|
||||
}
|
||||
this.finish();
|
||||
}
|
||||
|
||||
this.finish();
|
||||
}
|
||||
void handlePausing() throws InterruptedException {
|
||||
if (this.incTime > 100) {
|
||||
this.incTime = 0;
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
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");
|
||||
|
||||
if( !this.running )
|
||||
{
|
||||
AELog.craftingDebug( "crafting job will now sleep" );
|
||||
while (!this.running) {
|
||||
this.monitor.wait();
|
||||
}
|
||||
|
||||
while( !this.running )
|
||||
{
|
||||
this.monitor.wait();
|
||||
}
|
||||
AELog.craftingDebug("crafting job now active");
|
||||
}
|
||||
}
|
||||
|
||||
AELog.craftingDebug( "crafting job now active" );
|
||||
}
|
||||
}
|
||||
if (Thread.interrupted()) {
|
||||
throw new InterruptedException();
|
||||
}
|
||||
}
|
||||
this.incTime++;
|
||||
}
|
||||
|
||||
if( Thread.interrupted() )
|
||||
{
|
||||
throw new InterruptedException();
|
||||
}
|
||||
}
|
||||
this.incTime++;
|
||||
}
|
||||
private void finish() {
|
||||
if (this.callback != null) {
|
||||
this.callback.calculationComplete(this);
|
||||
}
|
||||
|
||||
private void finish()
|
||||
{
|
||||
if( this.callback != null )
|
||||
{
|
||||
this.callback.calculationComplete( this );
|
||||
}
|
||||
this.availableCheck = null;
|
||||
|
||||
this.availableCheck = null;
|
||||
synchronized (this.monitor) {
|
||||
this.running = false;
|
||||
this.done = true;
|
||||
this.monitor.notify();
|
||||
}
|
||||
}
|
||||
|
||||
synchronized( this.monitor )
|
||||
{
|
||||
this.running = false;
|
||||
this.done = true;
|
||||
this.monitor.notify();
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public boolean isSimulation() {
|
||||
return this.simulate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSimulation()
|
||||
{
|
||||
return this.simulate;
|
||||
}
|
||||
@Override
|
||||
public long getByteTotal() {
|
||||
return this.bytes;
|
||||
}
|
||||
|
||||
@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 void populatePlan( final IItemList<IAEItemStack> plan )
|
||||
{
|
||||
if( this.getTree() != null )
|
||||
{
|
||||
this.getTree().getPlan( plan );
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public IAEItemStack getOutput() {
|
||||
return this.output;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IAEItemStack getOutput()
|
||||
{
|
||||
return this.output;
|
||||
}
|
||||
public boolean isDone() {
|
||||
return this.done;
|
||||
}
|
||||
|
||||
public boolean isDone()
|
||||
{
|
||||
return this.done;
|
||||
}
|
||||
World getWorld() {
|
||||
return this.world;
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
synchronized( this.monitor )
|
||||
{
|
||||
if( this.done )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
this.watch.reset();
|
||||
this.watch.start();
|
||||
this.running = true;
|
||||
|
||||
this.watch.reset();
|
||||
this.watch.start();
|
||||
this.running = true;
|
||||
AELog.craftingDebug("main thread is now going to sleep");
|
||||
|
||||
AELog.craftingDebug( "main thread is now going to sleep" );
|
||||
this.monitor.notify();
|
||||
|
||||
this.monitor.notify();
|
||||
while (this.running) {
|
||||
try {
|
||||
this.monitor.wait();
|
||||
} catch (final InterruptedException ignored) {
|
||||
}
|
||||
}
|
||||
|
||||
while( this.running )
|
||||
{
|
||||
try
|
||||
{
|
||||
this.monitor.wait();
|
||||
}
|
||||
catch( final InterruptedException ignored )
|
||||
{
|
||||
}
|
||||
}
|
||||
AELog.craftingDebug("main thread is now active");
|
||||
}
|
||||
|
||||
AELog.craftingDebug( "main thread is now active" );
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
void addBytes(final long crafts) {
|
||||
this.bytes += crafts;
|
||||
}
|
||||
|
||||
void addBytes( final long crafts )
|
||||
{
|
||||
this.bytes += crafts;
|
||||
}
|
||||
public CraftingTreeNode getTree() {
|
||||
return this.tree;
|
||||
}
|
||||
|
||||
public CraftingTreeNode getTree()
|
||||
{
|
||||
return this.tree;
|
||||
}
|
||||
private void setTree(final CraftingTreeNode tree) {
|
||||
this.tree = 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;
|
||||
|
||||
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();
|
||||
|
||||
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 = 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]";
|
||||
}
|
||||
|
||||
actionSource = String.format( LOG_MACHINE_SOURCE_DETAILS, machine, location );
|
||||
}
|
||||
else
|
||||
{
|
||||
actionSource = "[unknown source]";
|
||||
}
|
||||
AELog.crafting(LOG_CRAFTING_JOB, type, actionSource, itemToOutput, this.bytes, elapsedTime);
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
private static class TwoIntegers {
|
||||
private final long perOp = 0;
|
||||
private final long times = 0;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user