SimpleReloadInstance.java

net.minecraft.server.packs.resources.SimpleReloadInstance

信息

  • 全限定名:net.minecraft.server.packs.resources.SimpleReloadInstance
  • 类型:public class
  • 包:net.minecraft.server.packs.resources
  • 源码路径:src/main/java/net/minecraft/server/packs/resources/SimpleReloadInstance.java
  • 起始行号:L16
  • 实现:ReloadInstance
  • 职责:

    TODO

字段/常量

  • PREPARATION_PROGRESS_WEIGHT

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

      TODO

  • EXTRA_RELOAD_PROGRESS_WEIGHT

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

      TODO

  • LISTENER_PROGRESS_WEIGHT

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

      TODO

  • allPreparations

    • 类型: CompletableFuture<Unit>
    • 修饰符: private final
    • 源码定位: L20
    • 说明:

      TODO

  • allDone

    • 类型: CompletableFuture<List<S>>
    • 修饰符: private
    • 源码定位: L21
    • 说明:

      TODO

  • preparingListeners

    • 类型: Set<PreparableReloadListener>
    • 修饰符: private final
    • 源码定位: L22
    • 说明:

      TODO

  • listenerCount

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

      TODO

  • startedTasks

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

      TODO

  • finishedTasks

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

      TODO

  • startedReloads

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

      TODO

  • finishedReloads

    • 类型: AtomicInteger
    • 修饰符: private final
    • 源码定位: L27
    • 说明:

      TODO

内部类/嵌套类型

  • net.minecraft.server.packs.resources.SimpleReloadInstance.StateFactory
    • 类型: interface
    • 修饰符: protected
    • 源码定位: L148
    • 说明:

      TODO

构造器

protected SimpleReloadInstance(List<PreparableReloadListener> listeners) @ L41

  • 构造器名:SimpleReloadInstance
  • 源码定位:L41
  • 修饰符:protected

参数:

  • listeners: List

说明:

TODO

方法

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

public static ReloadInstance of(ResourceManager resourceManager, List<PreparableReloadListener> listeners, Executor taskExecutor, Executor mainThreadExecutor, CompletableFuture<Unit> initialTask) @ L29

  • 方法名:of
  • 源码定位:L29
  • 返回类型:ReloadInstance
  • 修饰符:public static

参数:

  • resourceManager: ResourceManager
  • listeners: List
  • taskExecutor: Executor
  • mainThreadExecutor: Executor
  • initialTask: CompletableFuture

说明:

TODO

protected void startTasks(Executor taskExecutor, Executor mainThreadExecutor, ResourceManager resourceManager, List<PreparableReloadListener> listeners, SimpleReloadInstance.StateFactory<S> stateFactory, CompletableFuture<?> initialTask) @ L46

  • 方法名:startTasks
  • 源码定位:L46
  • 返回类型:void
  • 修饰符:protected

参数:

  • taskExecutor: Executor
  • mainThreadExecutor: Executor
  • resourceManager: ResourceManager
  • listeners: List
  • stateFactory: SimpleReloadInstance.StateFactory
  • initialTask: CompletableFuture<?>

说明:

TODO

protected CompletableFuture<List<S>> prepareTasks(Executor taskExecutor, Executor mainThreadExecutor, ResourceManager resourceManager, List<PreparableReloadListener> listeners, SimpleReloadInstance.StateFactory<S> stateFactory, CompletableFuture<?> initialTask) @ L57

  • 方法名:prepareTasks
  • 源码定位:L57
  • 返回类型:CompletableFuture<List>
  • 修饰符:protected

参数:

  • taskExecutor: Executor
  • mainThreadExecutor: Executor
  • resourceManager: ResourceManager
  • listeners: List
  • stateFactory: SimpleReloadInstance.StateFactory
  • initialTask: CompletableFuture<?>

说明:

TODO

private PreparableReloadListener.PreparationBarrier createBarrierForListener(PreparableReloadListener listener, CompletableFuture<?> previousBarrier, Executor mainThreadExecutor) @ L96

  • 方法名:createBarrierForListener
  • 源码定位:L96
  • 返回类型:PreparableReloadListener.PreparationBarrier
  • 修饰符:private

参数:

  • listener: PreparableReloadListener
  • previousBarrier: CompletableFuture<?>
  • mainThreadExecutor: Executor

说明:

TODO

public CompletableFuture<?> done() @ L117

  • 方法名:done
  • 源码定位:L117
  • 返回类型:CompletableFuture<?>
  • 修饰符:public

参数:

说明:

TODO

public float getActualProgress() @ L122

  • 方法名:getActualProgress
  • 源码定位:L122
  • 返回类型:float
  • 修饰符:public

参数:

说明:

TODO

private static int weightProgress(int preparationTasks, int reloadTasks, int listeners) @ L130

  • 方法名:weightProgress
  • 源码定位:L130
  • 返回类型:int
  • 修饰符:private static

参数:

  • preparationTasks: int
  • reloadTasks: int
  • listeners: int

说明:

TODO

public static ReloadInstance create(ResourceManager resourceManager, List<PreparableReloadListener> listeners, Executor backgroundExecutor, Executor mainThreadExecutor, CompletableFuture<Unit> initialTask, boolean enableProfiling) @ L134

  • 方法名:create
  • 源码定位:L134
  • 返回类型:ReloadInstance
  • 修饰符:public static

参数:

  • resourceManager: ResourceManager
  • listeners: List
  • backgroundExecutor: Executor
  • mainThreadExecutor: Executor
  • initialTask: CompletableFuture
  • enableProfiling: boolean

说明:

TODO

代码

public class SimpleReloadInstance<S> implements ReloadInstance {
    private static final int PREPARATION_PROGRESS_WEIGHT = 2;
    private static final int EXTRA_RELOAD_PROGRESS_WEIGHT = 2;
    private static final int LISTENER_PROGRESS_WEIGHT = 1;
    private final CompletableFuture<Unit> allPreparations = new CompletableFuture<>();
    private @Nullable CompletableFuture<List<S>> allDone;
    private final Set<PreparableReloadListener> preparingListeners;
    private final int listenerCount;
    private final AtomicInteger startedTasks = new AtomicInteger();
    private final AtomicInteger finishedTasks = new AtomicInteger();
    private final AtomicInteger startedReloads = new AtomicInteger();
    private final AtomicInteger finishedReloads = new AtomicInteger();
 
    public static ReloadInstance of(
        ResourceManager resourceManager,
        List<PreparableReloadListener> listeners,
        Executor taskExecutor,
        Executor mainThreadExecutor,
        CompletableFuture<Unit> initialTask
    ) {
        SimpleReloadInstance<Void> result = new SimpleReloadInstance<>(listeners);
        result.startTasks(taskExecutor, mainThreadExecutor, resourceManager, listeners, SimpleReloadInstance.StateFactory.SIMPLE, initialTask);
        return result;
    }
 
    protected SimpleReloadInstance(List<PreparableReloadListener> listeners) {
        this.listenerCount = listeners.size();
        this.preparingListeners = new HashSet<>(listeners);
    }
 
    protected void startTasks(
        Executor taskExecutor,
        Executor mainThreadExecutor,
        ResourceManager resourceManager,
        List<PreparableReloadListener> listeners,
        SimpleReloadInstance.StateFactory<S> stateFactory,
        CompletableFuture<?> initialTask
    ) {
        this.allDone = this.prepareTasks(taskExecutor, mainThreadExecutor, resourceManager, listeners, stateFactory, initialTask);
    }
 
    protected CompletableFuture<List<S>> prepareTasks(
        Executor taskExecutor,
        Executor mainThreadExecutor,
        ResourceManager resourceManager,
        List<PreparableReloadListener> listeners,
        SimpleReloadInstance.StateFactory<S> stateFactory,
        CompletableFuture<?> initialTask
    ) {
        Executor countingTaskExecutor = r -> {
            this.startedTasks.incrementAndGet();
            taskExecutor.execute(() -> {
                r.run();
                this.finishedTasks.incrementAndGet();
            });
        };
        Executor countingReloadExecutor = r -> {
            this.startedReloads.incrementAndGet();
            mainThreadExecutor.execute(() -> {
                r.run();
                this.finishedReloads.incrementAndGet();
            });
        };
        this.startedTasks.incrementAndGet();
        initialTask.thenRun(this.finishedTasks::incrementAndGet);
        PreparableReloadListener.SharedState sharedState = new PreparableReloadListener.SharedState(resourceManager);
        listeners.forEach(listenerx -> listenerx.prepareSharedState(sharedState));
        CompletableFuture<?> barrier = initialTask;
        List<CompletableFuture<S>> allSteps = new ArrayList<>();
 
        for (PreparableReloadListener listener : listeners) {
            PreparableReloadListener.PreparationBarrier barrierForCurrentTask = this.createBarrierForListener(listener, barrier, mainThreadExecutor);
            CompletableFuture<S> state = stateFactory.create(sharedState, barrierForCurrentTask, listener, countingTaskExecutor, countingReloadExecutor);
            allSteps.add(state);
            barrier = state;
        }
 
        return Util.sequenceFailFast(allSteps);
    }
 
    private PreparableReloadListener.PreparationBarrier createBarrierForListener(
        PreparableReloadListener listener, CompletableFuture<?> previousBarrier, Executor mainThreadExecutor
    ) {
        return new PreparableReloadListener.PreparationBarrier() {
            {
                Objects.requireNonNull(SimpleReloadInstance.this);
            }
 
            @Override
            public <T> CompletableFuture<T> wait(T t) {
                mainThreadExecutor.execute(() -> {
                    SimpleReloadInstance.this.preparingListeners.remove(listener);
                    if (SimpleReloadInstance.this.preparingListeners.isEmpty()) {
                        SimpleReloadInstance.this.allPreparations.complete(Unit.INSTANCE);
                    }
                });
                return SimpleReloadInstance.this.allPreparations.thenCombine((CompletionStage<? extends T>)previousBarrier, (v1, v2) -> t);
            }
        };
    }
 
    @Override
    public CompletableFuture<?> done() {
        return Objects.requireNonNull(this.allDone, "not started");
    }
 
    @Override
    public float getActualProgress() {
        int preparationsDone = this.listenerCount - this.preparingListeners.size();
        float doneCount = weightProgress(this.finishedTasks.get(), this.finishedReloads.get(), preparationsDone);
        float totalCount = weightProgress(this.startedTasks.get(), this.startedReloads.get(), this.listenerCount);
        return doneCount / totalCount;
    }
 
    private static int weightProgress(int preparationTasks, int reloadTasks, int listeners) {
        return preparationTasks * 2 + reloadTasks * 2 + listeners * 1;
    }
 
    public static ReloadInstance create(
        ResourceManager resourceManager,
        List<PreparableReloadListener> listeners,
        Executor backgroundExecutor,
        Executor mainThreadExecutor,
        CompletableFuture<Unit> initialTask,
        boolean enableProfiling
    ) {
        return enableProfiling
            ? ProfiledReloadInstance.of(resourceManager, listeners, backgroundExecutor, mainThreadExecutor, initialTask)
            : of(resourceManager, listeners, backgroundExecutor, mainThreadExecutor, initialTask);
    }
 
    @FunctionalInterface
    protected interface StateFactory<S> {
        SimpleReloadInstance.StateFactory<Void> SIMPLE = (currentReload, previousStep, listener, taskExecutor, reloadExecutor) -> listener.reload(
            currentReload, taskExecutor, previousStep, reloadExecutor
        );
 
        CompletableFuture<S> create(
            PreparableReloadListener.SharedState sharedState,
            PreparableReloadListener.PreparationBarrier previousStep,
            PreparableReloadListener listener,
            Executor taskExecutor,
            Executor reloadExecutor
        );
    }
}

引用的其他类

  • PreparableReloadListener

    • 引用位置: 参数/字段/方法调用/构造调用/返回值
    • 关联成员: PreparableReloadListener.PreparationBarrier(), PreparableReloadListener.SharedState(), PreparationBarrier(), SharedState()
  • ProfiledReloadInstance

    • 引用位置: 方法调用
    • 关联成员: ProfiledReloadInstance.of()
  • ReloadInstance

    • 引用位置: 实现/返回值
  • ResourceManager

    • 引用位置: 参数
  • Unit

    • 引用位置: 参数/字段
  • Util

    • 引用位置: 方法调用
    • 关联成员: Util.sequenceFailFast()