ExecutionContext.java

net.minecraft.commands.execution.ExecutionContext

信息

  • 全限定名:net.minecraft.commands.execution.ExecutionContext
  • 类型:public class
  • 包:net.minecraft.commands.execution
  • 源码路径:src/main/java/net/minecraft/commands/execution/ExecutionContext.java
  • 起始行号:L18
  • 实现:AutoCloseable
  • 职责:

    TODO

字段/常量

  • MAX_QUEUE_DEPTH

    • 类型: int
    • 修饰符: private static final
    • 源码定位: L19
    • 说明:

      TODO

  • LOGGER

    • 类型: Logger
    • 修饰符: private static final
    • 源码定位: L20
    • 说明:

      TODO

  • commandLimit

    • 类型: int
    • 修饰符: private final
    • 源码定位: L21
    • 说明:

      TODO

  • forkLimit

    • 类型: int
    • 修饰符: private final
    • 源码定位: L22
    • 说明:

      TODO

  • profiler

    • 类型: ProfilerFiller
    • 修饰符: private final
    • 源码定位: L23
    • 说明:

      TODO

  • tracer

    • 类型: TraceCallbacks
    • 修饰符: private
    • 源码定位: L24
    • 说明:

      TODO

  • commandQuota

    • 类型: int
    • 修饰符: private
    • 源码定位: L25
    • 说明:

      TODO

  • queueOverflow

    • 类型: boolean
    • 修饰符: private
    • 源码定位: L26
    • 说明:

      TODO

  • commandQueue

    • 类型: Deque<CommandQueueEntry<T>>
    • 修饰符: private final
    • 源码定位: L27
    • 说明:

      TODO

  • newTopCommands

    • 类型: List<CommandQueueEntry<T>>
    • 修饰符: private final
    • 源码定位: L28
    • 说明:

      TODO

  • currentFrameDepth

    • 类型: int
    • 修饰符: private
    • 源码定位: L29
    • 说明:

      TODO

内部类/嵌套类型

构造器

public ExecutionContext(int commandLimit, int forkLimit, ProfilerFiller profiler) @ L31

  • 构造器名:ExecutionContext
  • 源码定位:L31
  • 修饰符:public

参数:

  • commandLimit: int
  • forkLimit: int
  • profiler: ProfilerFiller

说明:

TODO

方法

下面的方法块按源码顺序生成。

private static <T extends ExecutionCommandSource<T>> Frame createTopFrame(ExecutionContext<T> context, CommandResultCallback frameResult) @ L38

  • 方法名:createTopFrame
  • 源码定位:L38
  • 返回类型:<T extends ExecutionCommandSource> Frame
  • 修饰符:private static

参数:

  • context: ExecutionContext
  • frameResult: CommandResultCallback

说明:

TODO

public static <T extends ExecutionCommandSource<T>> void queueInitialFunctionCall(ExecutionContext<T> context, InstantiatedFunction<T> function, T sender, CommandResultCallback functionReturn) @ L47

  • 方法名:queueInitialFunctionCall
  • 源码定位:L47
  • 返回类型:<T extends ExecutionCommandSource> void
  • 修饰符:public static

参数:

  • context: ExecutionContext
  • function: InstantiatedFunction
  • sender: T
  • functionReturn: CommandResultCallback

说明:

TODO

public static <T extends ExecutionCommandSource<T>> void queueInitialCommandExecution(ExecutionContext<T> context, String command, ContextChain<T> executionChain, T sender, CommandResultCallback commandReturn) @ L53

  • 方法名:queueInitialCommandExecution
  • 源码定位:L53
  • 返回类型:<T extends ExecutionCommandSource> void
  • 修饰符:public static

参数:

  • context: ExecutionContext
  • command: String
  • executionChain: ContextChain
  • sender: T
  • commandReturn: CommandResultCallback

说明:

TODO

private void handleQueueOverflow() @ L59

  • 方法名:handleQueueOverflow
  • 源码定位:L59
  • 返回类型:void
  • 修饰符:private

参数:

说明:

TODO

public void queueNext(CommandQueueEntry<T> entry) @ L65

  • 方法名:queueNext
  • 源码定位:L65
  • 返回类型:void
  • 修饰符:public

参数:

  • entry: CommandQueueEntry

说明:

TODO

public void discardAtDepthOrHigher(int depthToDiscard) @ L75

  • 方法名:discardAtDepthOrHigher
  • 源码定位:L75
  • 返回类型:void
  • 修饰符:public

参数:

  • depthToDiscard: int

说明:

TODO

public Frame.FrameControl frameControlForDepth(int depthToDiscard) @ L81

  • 方法名:frameControlForDepth
  • 源码定位:L81
  • 返回类型:Frame.FrameControl
  • 修饰符:public

参数:

  • depthToDiscard: int

说明:

TODO

public void runCommandQueue() @ L85

  • 方法名:runCommandQueue
  • 源码定位:L85
  • 返回类型:void
  • 修饰符:public

参数:

说明:

TODO

private void pushNewCommands() @ L112

  • 方法名:pushNewCommands
  • 源码定位:L112
  • 返回类型:void
  • 修饰符:private

参数:

说明:

TODO

public void tracer(TraceCallbacks tracer) @ L120

  • 方法名:tracer
  • 源码定位:L120
  • 返回类型:void
  • 修饰符:public

参数:

  • tracer: TraceCallbacks

说明:

TODO

public TraceCallbacks tracer() @ L124

  • 方法名:tracer
  • 源码定位:L124
  • 返回类型:TraceCallbacks
  • 修饰符:public

参数:

说明:

TODO

public ProfilerFiller profiler() @ L128

  • 方法名:profiler
  • 源码定位:L128
  • 返回类型:ProfilerFiller
  • 修饰符:public

参数:

说明:

TODO

public int forkLimit() @ L132

  • 方法名:forkLimit
  • 源码定位:L132
  • 返回类型:int
  • 修饰符:public

参数:

说明:

TODO

public void incrementCost() @ L136

  • 方法名:incrementCost
  • 源码定位:L136
  • 返回类型:void
  • 修饰符:public

参数:

说明:

TODO

public void close() @ L140

  • 方法名:close
  • 源码定位:L140
  • 返回类型:void
  • 修饰符:public

参数:

说明:

TODO

代码

public class ExecutionContext<T> implements AutoCloseable {
    private static final int MAX_QUEUE_DEPTH = 10000000;
    private static final Logger LOGGER = LogUtils.getLogger();
    private final int commandLimit;
    private final int forkLimit;
    private final ProfilerFiller profiler;
    private @Nullable TraceCallbacks tracer;
    private int commandQuota;
    private boolean queueOverflow;
    private final Deque<CommandQueueEntry<T>> commandQueue = Queues.newArrayDeque();
    private final List<CommandQueueEntry<T>> newTopCommands = new ObjectArrayList<>();
    private int currentFrameDepth;
 
    public ExecutionContext(int commandLimit, int forkLimit, ProfilerFiller profiler) {
        this.commandLimit = commandLimit;
        this.forkLimit = forkLimit;
        this.profiler = profiler;
        this.commandQuota = commandLimit;
    }
 
    private static <T extends ExecutionCommandSource<T>> Frame createTopFrame(ExecutionContext<T> context, CommandResultCallback frameResult) {
        if (context.currentFrameDepth == 0) {
            return new Frame(0, frameResult, context.commandQueue::clear);
        } else {
            int reentrantFrameDepth = context.currentFrameDepth + 1;
            return new Frame(reentrantFrameDepth, frameResult, context.frameControlForDepth(reentrantFrameDepth));
        }
    }
 
    public static <T extends ExecutionCommandSource<T>> void queueInitialFunctionCall(
        ExecutionContext<T> context, InstantiatedFunction<T> function, T sender, CommandResultCallback functionReturn
    ) {
        context.queueNext(new CommandQueueEntry<>(createTopFrame(context, functionReturn), new CallFunction<>(function, sender.callback(), false).bind(sender)));
    }
 
    public static <T extends ExecutionCommandSource<T>> void queueInitialCommandExecution(
        ExecutionContext<T> context, String command, ContextChain<T> executionChain, T sender, CommandResultCallback commandReturn
    ) {
        context.queueNext(new CommandQueueEntry<>(createTopFrame(context, commandReturn), new BuildContexts.TopLevel<>(command, executionChain, sender)));
    }
 
    private void handleQueueOverflow() {
        this.queueOverflow = true;
        this.newTopCommands.clear();
        this.commandQueue.clear();
    }
 
    public void queueNext(CommandQueueEntry<T> entry) {
        if (this.newTopCommands.size() + this.commandQueue.size() > 10000000) {
            this.handleQueueOverflow();
        }
 
        if (!this.queueOverflow) {
            this.newTopCommands.add(entry);
        }
    }
 
    public void discardAtDepthOrHigher(int depthToDiscard) {
        while (!this.commandQueue.isEmpty() && this.commandQueue.peek().frame().depth() >= depthToDiscard) {
            this.commandQueue.removeFirst();
        }
    }
 
    public Frame.FrameControl frameControlForDepth(int depthToDiscard) {
        return () -> this.discardAtDepthOrHigher(depthToDiscard);
    }
 
    public void runCommandQueue() {
        this.pushNewCommands();
 
        while (true) {
            if (this.commandQuota <= 0) {
                LOGGER.info("Command execution stopped due to limit (executed {} commands)", this.commandLimit);
                break;
            }
 
            CommandQueueEntry<T> command = this.commandQueue.pollFirst();
            if (command == null) {
                return;
            }
 
            this.currentFrameDepth = command.frame().depth();
            command.execute(this);
            if (this.queueOverflow) {
                LOGGER.error("Command execution stopped due to command queue overflow (max {})", 10000000);
                break;
            }
 
            this.pushNewCommands();
        }
 
        this.currentFrameDepth = 0;
    }
 
    private void pushNewCommands() {
        for (int i = this.newTopCommands.size() - 1; i >= 0; i--) {
            this.commandQueue.addFirst(this.newTopCommands.get(i));
        }
 
        this.newTopCommands.clear();
    }
 
    public void tracer(@Nullable TraceCallbacks tracer) {
        this.tracer = tracer;
    }
 
    public @Nullable TraceCallbacks tracer() {
        return this.tracer;
    }
 
    public ProfilerFiller profiler() {
        return this.profiler;
    }
 
    public int forkLimit() {
        return this.forkLimit;
    }
 
    public void incrementCost() {
        this.commandQuota--;
    }
 
    @Override
    public void close() {
        if (this.tracer != null) {
            this.tracer.close();
        }
    }
}

引用的其他类