* Add javadocs to many methods * Added built-in ways to generate BundledModules and documented them * Add Eclipse launch files to .gitignore and fix 11 warnings * Remove many unused imports * Remove some dead and unused code around app loading * Allow extensions to have client-side rules in their SettingsManager Also change tabs to spaces when those changed * Remove unnecessary classloader param in BundledModule * Change max tickwarp duration to Integer.MAX_VALUE * Move RulePrinter into the source so it can be managed by the IDE * Properly reset hopperCounters when changing worlds Also change tabs to spaces again and minor tweaks to javadocs * Run mergedocs even if Full.md was modified * Update src/main/java/carpet/commands/TickCommand.java Co-authored-by: gnembon <41132274+gnembon@users.noreply.github.com>
132 lines
3.4 KiB
Java
132 lines
3.4 KiB
Java
package carpet.script.value;
|
|
|
|
import carpet.script.Context;
|
|
import carpet.script.Expression;
|
|
import carpet.script.Tokenizer;
|
|
import carpet.script.exception.ExitStatement;
|
|
import carpet.script.exception.ExpressionException;
|
|
import carpet.script.exception.InternalExpressionException;
|
|
import net.minecraft.nbt.Tag;
|
|
|
|
import java.util.List;
|
|
import java.util.concurrent.CompletableFuture;
|
|
import java.util.concurrent.ExecutionException;
|
|
import java.util.concurrent.ExecutorService;
|
|
|
|
public class ThreadValue extends Value
|
|
{
|
|
private final CompletableFuture<Value> taskFuture;
|
|
private final long id;
|
|
private static long sequence = 0L;
|
|
|
|
public ThreadValue(Value pool, FunctionValue function, Expression expr, Tokenizer.Token token, Context ctx, List<Value> args)
|
|
{
|
|
id = sequence++;
|
|
ExecutorService executor = ctx.host.getExecutor(pool);
|
|
if (executor == null)
|
|
{
|
|
// app is shutting down - no more threads can be spawned.
|
|
taskFuture = CompletableFuture.completedFuture(Value.NULL);
|
|
}
|
|
else
|
|
{
|
|
taskFuture = CompletableFuture.supplyAsync(
|
|
() ->
|
|
{
|
|
try
|
|
{
|
|
return function.lazyEval(ctx, Context.NONE, expr, token, FunctionValue.lazify(args)).evalValue(ctx);
|
|
}
|
|
catch (ExitStatement exit)
|
|
{
|
|
// app stopped
|
|
return exit.retval;
|
|
}
|
|
catch (ExpressionException exc)
|
|
{
|
|
ctx.host.handleExpressionException("Thread failed", exc);
|
|
return Value.NULL;
|
|
}
|
|
|
|
},
|
|
ctx.host.getExecutor(pool)
|
|
);
|
|
}
|
|
Thread.yield();
|
|
}
|
|
|
|
@Override
|
|
public String getString()
|
|
{
|
|
return taskFuture.getNow(Value.NULL).getString();
|
|
}
|
|
|
|
public Value getValue()
|
|
{
|
|
return taskFuture.getNow(Value.NULL);
|
|
}
|
|
|
|
@Override
|
|
public boolean getBoolean()
|
|
{
|
|
return taskFuture.getNow(Value.NULL).getBoolean();
|
|
}
|
|
|
|
public Value join()
|
|
{
|
|
try
|
|
{
|
|
return taskFuture.get();
|
|
}
|
|
catch (ExitStatement exit)
|
|
{
|
|
taskFuture.complete(exit.retval);
|
|
return exit.retval;
|
|
}
|
|
catch (InterruptedException | ExecutionException e)
|
|
{
|
|
return Value.NULL;
|
|
}
|
|
}
|
|
|
|
public boolean isFinished()
|
|
{
|
|
return taskFuture.isDone();
|
|
}
|
|
|
|
@Override
|
|
public boolean equals(Object o)
|
|
{
|
|
if (!(o instanceof ThreadValue))
|
|
return false;
|
|
return ((ThreadValue) o).id == this.id;
|
|
}
|
|
|
|
@Override
|
|
public int compareTo(Value o)
|
|
{
|
|
if (!(o instanceof ThreadValue))
|
|
throw new InternalExpressionException("Cannot compare tasks to other types");
|
|
return (int) (this.id - ((ThreadValue) o).id);
|
|
}
|
|
|
|
@Override
|
|
public int hashCode()
|
|
{
|
|
return Long.hashCode(id);
|
|
}
|
|
|
|
@Override
|
|
public Tag toTag(boolean force)
|
|
{
|
|
if (!force) throw new NBTSerializableValue.IncompatibleTypeException(this);
|
|
return getValue().toTag(true);
|
|
}
|
|
|
|
@Override
|
|
public String getTypeString()
|
|
{
|
|
return "task";
|
|
}
|
|
}
|