CustomBossEvent.java

net.minecraft.server.bossevents.CustomBossEvent

信息

  • 全限定名:net.minecraft.server.bossevents.CustomBossEvent
  • 类型:public class
  • 包:net.minecraft.server.bossevents
  • 源码路径:src/main/java/net/minecraft/server/bossevents/CustomBossEvent.java
  • 起始行号:L20
  • 继承:ServerBossEvent
  • 职责:

    TODO

字段/常量

  • DEFAULT_MAX

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

      TODO

  • customId

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

      TODO

  • players

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

      TODO

  • value

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

      TODO

  • max

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

      TODO

  • dirtyCallback

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

      TODO

内部类/嵌套类型

  • net.minecraft.server.bossevents.CustomBossEvent.Packed
    • 类型: record
    • 修饰符: public
    • 源码定位: L194
    • 说明:

      TODO

构造器

public CustomBossEvent(UUID id, Identifier customId, Component name, Runnable dirtyCallback) @ L28

  • 构造器名:CustomBossEvent
  • 源码定位:L28
  • 修饰符:public

参数:

  • id: UUID
  • customId: Identifier
  • name: Component
  • dirtyCallback: Runnable

说明:

TODO

方法

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

public Identifier customId() @ L35

  • 方法名:customId
  • 源码定位:L35
  • 返回类型:Identifier
  • 修饰符:public

参数:

说明:

TODO

public void addPlayer(ServerPlayer player) @ L39

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

参数:

  • player: ServerPlayer

说明:

TODO

public void removePlayer(ServerPlayer player) @ L47

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

参数:

  • player: ServerPlayer

说明:

TODO

public void removeAllPlayers() @ L55

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

参数:

说明:

TODO

public int value() @ L64

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

参数:

说明:

TODO

public int max() @ L68

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

参数:

说明:

TODO

public void setValue(int value) @ L72

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

参数:

  • value: int

说明:

TODO

public void setMax(int max) @ L78

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

参数:

  • max: int

说明:

TODO

public final Component getDisplayName() @ L84

  • 方法名:getDisplayName
  • 源码定位:L84
  • 返回类型:Component
  • 修饰符:public final

参数:

说明:

TODO

public boolean setPlayers(Collection<ServerPlayer> players) @ L93

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

参数:

  • players: Collection

说明:

TODO

public static CustomBossEvent load(UUID id, Identifier customId, CustomBossEvent.Packed packed, Runnable setDirty) @ L150

  • 方法名:load
  • 源码定位:L150
  • 返回类型:CustomBossEvent
  • 修饰符:public static

参数:

  • id: UUID
  • customId: Identifier
  • packed: CustomBossEvent.Packed
  • setDirty: Runnable

说明:

TODO

public CustomBossEvent.Packed pack() @ L164

  • 方法名:pack
  • 源码定位:L164
  • 返回类型:CustomBossEvent.Packed
  • 修饰符:public

参数:

说明:

TODO

public void onPlayerConnect(ServerPlayer player) @ L179

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

参数:

  • player: ServerPlayer

说明:

TODO

public void onPlayerDisconnect(ServerPlayer player) @ L185

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

参数:

  • player: ServerPlayer

说明:

TODO

public void setDirty() @ L189

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

参数:

说明:

TODO

代码

public class CustomBossEvent extends ServerBossEvent {
    private static final int DEFAULT_MAX = 100;
    private final Identifier customId;
    private final Set<UUID> players = Sets.newHashSet();
    private int value;
    private int max = 100;
    private final Runnable dirtyCallback;
 
    public CustomBossEvent(UUID id, Identifier customId, Component name, Runnable dirtyCallback) {
        super(id, name, BossEvent.BossBarColor.WHITE, BossEvent.BossBarOverlay.PROGRESS);
        this.dirtyCallback = dirtyCallback;
        this.customId = customId;
        this.setProgress(0.0F);
    }
 
    public Identifier customId() {
        return this.customId;
    }
 
    @Override
    public void addPlayer(ServerPlayer player) {
        super.addPlayer(player);
        if (this.players.add(player.getUUID())) {
            this.setDirty();
        }
    }
 
    @Override
    public void removePlayer(ServerPlayer player) {
        super.removePlayer(player);
        if (this.players.remove(player.getUUID())) {
            this.setDirty();
        }
    }
 
    @Override
    public void removeAllPlayers() {
        super.removeAllPlayers();
        if (!this.players.isEmpty()) {
            this.players.clear();
            this.setDirty();
        }
    }
 
    public int value() {
        return this.value;
    }
 
    public int max() {
        return this.max;
    }
 
    public void setValue(int value) {
        this.value = value;
        this.setProgress(Mth.clamp((float)value / this.max, 0.0F, 1.0F));
        this.setDirty();
    }
 
    public void setMax(int max) {
        this.max = max;
        this.setProgress(Mth.clamp((float)this.value / max, 0.0F, 1.0F));
        this.setDirty();
    }
 
    public final Component getDisplayName() {
        return ComponentUtils.wrapInSquareBrackets(this.getName())
            .withStyle(
                s -> s.withColor(this.getColor().getFormatting())
                    .withHoverEvent(new HoverEvent.ShowText(Component.literal(this.customId().toString())))
                    .withInsertion(this.customId().toString())
            );
    }
 
    public boolean setPlayers(Collection<ServerPlayer> players) {
        Set<UUID> toRemove = Sets.newHashSet();
        Set<ServerPlayer> toAdd = Sets.newHashSet();
 
        for (UUID uuid : this.players) {
            boolean found = false;
 
            for (ServerPlayer player : players) {
                if (player.getUUID().equals(uuid)) {
                    found = true;
                    break;
                }
            }
 
            if (!found) {
                toRemove.add(uuid);
            }
        }
 
        for (ServerPlayer playerx : players) {
            boolean found = false;
 
            for (UUID uuid : this.players) {
                if (playerx.getUUID().equals(uuid)) {
                    found = true;
                    break;
                }
            }
 
            if (!found) {
                toAdd.add(playerx);
            }
        }
 
        for (UUID uuidx : toRemove) {
            for (ServerPlayer playerx : this.getPlayers()) {
                if (playerx.getUUID().equals(uuidx)) {
                    this.removePlayer(playerx);
                    break;
                }
            }
 
            this.players.remove(uuidx);
        }
 
        for (ServerPlayer playerxx : toAdd) {
            this.addPlayer(playerxx);
        }
 
        boolean playersChanged = !toRemove.isEmpty() || !toAdd.isEmpty();
        if (playersChanged) {
            this.setDirty();
        }
 
        return playersChanged;
    }
 
    public static CustomBossEvent load(UUID id, Identifier customId, CustomBossEvent.Packed packed, Runnable setDirty) {
        CustomBossEvent event = new CustomBossEvent(id, customId, packed.name, setDirty);
        event.setVisible(packed.visible);
        event.setValue(packed.value);
        event.setMax(packed.max);
        event.setColor(packed.color);
        event.setOverlay(packed.overlay);
        event.setDarkenScreen(packed.darkenScreen);
        event.setPlayBossMusic(packed.playBossMusic);
        event.setCreateWorldFog(packed.createWorldFog);
        event.players.addAll(packed.players);
        return event;
    }
 
    public CustomBossEvent.Packed pack() {
        return new CustomBossEvent.Packed(
            this.getName(),
            this.isVisible(),
            this.value(),
            this.max(),
            this.getColor(),
            this.getOverlay(),
            this.shouldDarkenScreen(),
            this.shouldPlayBossMusic(),
            this.shouldCreateWorldFog(),
            Set.copyOf(this.players)
        );
    }
 
    public void onPlayerConnect(ServerPlayer player) {
        if (this.players.contains(player.getUUID())) {
            this.addPlayer(player);
        }
    }
 
    public void onPlayerDisconnect(ServerPlayer player) {
        super.removePlayer(player);
    }
 
    @Override
    public void setDirty() {
        this.dirtyCallback.run();
    }
 
    public record Packed(
        Component name,
        boolean visible,
        int value,
        int max,
        BossEvent.BossBarColor color,
        BossEvent.BossBarOverlay overlay,
        boolean darkenScreen,
        boolean playBossMusic,
        boolean createWorldFog,
        Set<UUID> players
    ) {
        public static final Codec<CustomBossEvent.Packed> CODEC = RecordCodecBuilder.create(
            i -> i.group(
                    ComponentSerialization.CODEC.fieldOf("Name").forGetter(CustomBossEvent.Packed::name),
                    Codec.BOOL.optionalFieldOf("Visible", false).forGetter(CustomBossEvent.Packed::visible),
                    Codec.INT.optionalFieldOf("Value", 0).forGetter(CustomBossEvent.Packed::value),
                    Codec.INT.optionalFieldOf("Max", 100).forGetter(CustomBossEvent.Packed::max),
                    BossEvent.BossBarColor.CODEC.optionalFieldOf("Color", BossEvent.BossBarColor.WHITE).forGetter(CustomBossEvent.Packed::color),
                    BossEvent.BossBarOverlay.CODEC.optionalFieldOf("Overlay", BossEvent.BossBarOverlay.PROGRESS).forGetter(CustomBossEvent.Packed::overlay),
                    Codec.BOOL.optionalFieldOf("DarkenScreen", false).forGetter(CustomBossEvent.Packed::darkenScreen),
                    Codec.BOOL.optionalFieldOf("PlayBossMusic", false).forGetter(CustomBossEvent.Packed::playBossMusic),
                    Codec.BOOL.optionalFieldOf("CreateWorldFog", false).forGetter(CustomBossEvent.Packed::createWorldFog),
                    UUIDUtil.CODEC_SET.optionalFieldOf("Players", Set.of()).forGetter(CustomBossEvent.Packed::players)
                )
                .apply(i, CustomBossEvent.Packed::new)
        );
    }
}

引用的其他类

  • Component

    • 引用位置: 参数/方法调用/返回值
    • 关联成员: Component.literal()
  • ComponentUtils

    • 引用位置: 方法调用
    • 关联成员: ComponentUtils.wrapInSquareBrackets()
  • HoverEvent

    • 引用位置: 方法调用/构造调用
    • 关联成员: HoverEvent.ShowText(), ShowText()
  • Identifier

    • 引用位置: 参数/字段/返回值
  • ServerBossEvent

    • 引用位置: 继承
  • ServerPlayer

    • 引用位置: 参数
  • Mth

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