Stopwatches.java

net.minecraft.world.Stopwatches

信息

  • 全限定名:net.minecraft.world.Stopwatches
  • 类型:public class
  • 包:net.minecraft.world
  • 源码路径:src/main/java/net/minecraft/world/Stopwatches.java
  • 起始行号:L16
  • 继承:SavedData
  • 职责:

    TODO

字段/常量

  • CODEC

    • 类型: Codec<Stopwatches>
    • 修饰符: private static final
    • 源码定位: L17
    • 说明:

      TODO

  • TYPE

    • 类型: SavedDataType<Stopwatches>
    • 修饰符: public static final
    • 源码定位: L21
    • 说明:

      TODO

  • stopwatches

    • 类型: Map<Identifier,Stopwatch>
    • 修饰符: private final
    • 源码定位: L24
    • 说明:

      TODO

内部类/嵌套类型

构造器

private Stopwatches() @ L26

  • 构造器名:Stopwatches
  • 源码定位:L26
  • 修饰符:private

参数:

说明:

TODO

方法

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

private static Stopwatches unpack(Map<Identifier,Long> stopwatches) @ L29

  • 方法名:unpack
  • 源码定位:L29
  • 返回类型:Stopwatches
  • 修饰符:private static

参数:

  • stopwatches: Map<Identifier,Long>

说明:

TODO

private Map<Identifier,Long> pack() @ L36

  • 方法名:pack
  • 源码定位:L36
  • 返回类型:Map<Identifier,Long>
  • 修饰符:private

参数:

说明:

TODO

public Stopwatch get(Identifier id) @ L43

  • 方法名:get
  • 源码定位:L43
  • 返回类型:Stopwatch
  • 修饰符:public

参数:

  • id: Identifier

说明:

TODO

public boolean add(Identifier id, Stopwatch stopwatch) @ L47

  • 方法名:add
  • 源码定位:L47
  • 返回类型:boolean
  • 修饰符:public

参数:

  • id: Identifier
  • stopwatch: Stopwatch

说明:

TODO

public boolean update(Identifier id, UnaryOperator<Stopwatch> update) @ L56

  • 方法名:update
  • 源码定位:L56
  • 返回类型:boolean
  • 修饰符:public

参数:

  • id: Identifier
  • update: UnaryOperator

说明:

TODO

public boolean remove(Identifier id) @ L65

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

参数:

  • id: Identifier

说明:

TODO

public boolean isDirty() @ L74

  • 方法名:isDirty
  • 源码定位:L74
  • 返回类型:boolean
  • 修饰符:public

参数:

说明:

TODO

public List<Identifier> ids() @ L79

  • 方法名:ids
  • 源码定位:L79
  • 返回类型:List
  • 修饰符:public

参数:

说明:

TODO

public static long currentTime() @ L83

  • 方法名:currentTime
  • 源码定位:L83
  • 返回类型:long
  • 修饰符:public static

参数:

说明:

TODO

代码

public class Stopwatches extends SavedData {
    private static final Codec<Stopwatches> CODEC = Codec.unboundedMap(Identifier.CODEC, Codec.LONG)
        .fieldOf("stopwatches")
        .codec()
        .xmap(Stopwatches::unpack, Stopwatches::pack);
    public static final SavedDataType<Stopwatches> TYPE = new SavedDataType<>(
        Identifier.withDefaultNamespace("stopwatches"), Stopwatches::new, CODEC, DataFixTypes.SAVED_DATA_STOPWATCHES
    );
    private final Map<Identifier, Stopwatch> stopwatches = new Object2ObjectOpenHashMap<>();
 
    private Stopwatches() {
    }
 
    private static Stopwatches unpack(Map<Identifier, Long> stopwatches) {
        Stopwatches result = new Stopwatches();
        long currentTime = currentTime();
        stopwatches.forEach((id, accumulatedElapsedTime) -> result.stopwatches.put(id, new Stopwatch(currentTime, accumulatedElapsedTime)));
        return result;
    }
 
    private Map<Identifier, Long> pack() {
        long currentTime = currentTime();
        Map<Identifier, Long> result = new TreeMap<>();
        this.stopwatches.forEach((id, stopwatch) -> result.put(id, stopwatch.elapsedMilliseconds(currentTime)));
        return result;
    }
 
    public @Nullable Stopwatch get(Identifier id) {
        return this.stopwatches.get(id);
    }
 
    public boolean add(Identifier id, Stopwatch stopwatch) {
        if (this.stopwatches.putIfAbsent(id, stopwatch) == null) {
            this.setDirty();
            return true;
        } else {
            return false;
        }
    }
 
    public boolean update(Identifier id, UnaryOperator<Stopwatch> update) {
        if (this.stopwatches.computeIfPresent(id, (key, value) -> update.apply(value)) != null) {
            this.setDirty();
            return true;
        } else {
            return false;
        }
    }
 
    public boolean remove(Identifier id) {
        boolean removed = this.stopwatches.remove(id) != null;
        if (removed) {
            this.setDirty();
        }
 
        return removed;
    }
 
    @Override
    public boolean isDirty() {
        return super.isDirty() || !this.stopwatches.isEmpty();
    }
 
    public List<Identifier> ids() {
        return List.copyOf(this.stopwatches.keySet());
    }
 
    public static long currentTime() {
        return Util.getMillis();
    }
}

引用的其他类

  • Identifier

    • 引用位置: 参数/字段/方法调用/返回值
    • 关联成员: Identifier.withDefaultNamespace()
  • Util

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

    • 引用位置: 参数/字段/构造调用/返回值
    • 关联成员: Stopwatch()
  • SavedData

    • 引用位置: 继承
  • SavedDataType

    • 引用位置: 字段