| 1 | package net.minecraft.client.gui.components;
|
| 2 |
|
| 3 | import com.google.common.collect.Maps;
|
| 4 | import java.util.Map;
|
| 5 | import java.util.UUID;
|
| 6 | import net.fabricmc.api.EnvType;
|
| 7 | import net.fabricmc.api.Environment;
|
| 8 | import net.minecraft.client.Minecraft;
|
| 9 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 10 | import net.minecraft.client.renderer.RenderPipelines;
|
| 11 | import net.minecraft.network.chat.Component;
|
| 12 | import net.minecraft.network.protocol.game.ClientboundBossEventPacket;
|
| 13 | import net.minecraft.network.protocol.game.ClientboundBossEventPacket.Handler;
|
| 14 | import net.minecraft.resources.Identifier;
|
| 15 | import net.minecraft.util.Mth;
|
| 16 | import net.minecraft.util.profiling.Profiler;
|
| 17 | import net.minecraft.util.profiling.ProfilerFiller;
|
| 18 | import net.minecraft.world.BossEvent;
|
| 19 | import net.minecraft.world.BossEvent.BossBarColor;
|
| 20 | import net.minecraft.world.BossEvent.BossBarOverlay;
|
| 21 |
|
| 22 | @Environment(EnvType.CLIENT)
|
| 23 | public class BossHealthOverlay {
|
| 24 | private static final int BAR_WIDTH = 182;
|
| 25 | private static final int BAR_HEIGHT = 5;
|
| 26 | private static final Identifier[] BAR_BACKGROUND_SPRITES = new Identifier[]{
|
| 27 | Identifier.withDefaultNamespace("boss_bar/pink_background"),
|
| 28 | Identifier.withDefaultNamespace("boss_bar/blue_background"),
|
| 29 | Identifier.withDefaultNamespace("boss_bar/red_background"),
|
| 30 | Identifier.withDefaultNamespace("boss_bar/green_background"),
|
| 31 | Identifier.withDefaultNamespace("boss_bar/yellow_background"),
|
| 32 | Identifier.withDefaultNamespace("boss_bar/purple_background"),
|
| 33 | Identifier.withDefaultNamespace("boss_bar/white_background")
|
| 34 | };
|
| 35 | private static final Identifier[] BAR_PROGRESS_SPRITES = new Identifier[]{
|
| 36 | Identifier.withDefaultNamespace("boss_bar/pink_progress"),
|
| 37 | Identifier.withDefaultNamespace("boss_bar/blue_progress"),
|
| 38 | Identifier.withDefaultNamespace("boss_bar/red_progress"),
|
| 39 | Identifier.withDefaultNamespace("boss_bar/green_progress"),
|
| 40 | Identifier.withDefaultNamespace("boss_bar/yellow_progress"),
|
| 41 | Identifier.withDefaultNamespace("boss_bar/purple_progress"),
|
| 42 | Identifier.withDefaultNamespace("boss_bar/white_progress")
|
| 43 | };
|
| 44 | private static final Identifier[] OVERLAY_BACKGROUND_SPRITES = new Identifier[]{
|
| 45 | Identifier.withDefaultNamespace("boss_bar/notched_6_background"),
|
| 46 | Identifier.withDefaultNamespace("boss_bar/notched_10_background"),
|
| 47 | Identifier.withDefaultNamespace("boss_bar/notched_12_background"),
|
| 48 | Identifier.withDefaultNamespace("boss_bar/notched_20_background")
|
| 49 | };
|
| 50 | private static final Identifier[] OVERLAY_PROGRESS_SPRITES = new Identifier[]{
|
| 51 | Identifier.withDefaultNamespace("boss_bar/notched_6_progress"),
|
| 52 | Identifier.withDefaultNamespace("boss_bar/notched_10_progress"),
|
| 53 | Identifier.withDefaultNamespace("boss_bar/notched_12_progress"),
|
| 54 | Identifier.withDefaultNamespace("boss_bar/notched_20_progress")
|
| 55 | };
|
| 56 | private final Minecraft minecraft;
|
| 57 | private final Map<UUID, LerpingBossEvent> events = Maps.newLinkedHashMap();
|
| 58 |
|
| 59 | public BossHealthOverlay(final Minecraft minecraft) {
|
| 60 | this.minecraft = minecraft;
|
| 61 | }
|
| 62 |
|
| 63 | public void extractRenderState(final GuiGraphicsExtractor graphics) {
|
| 64 | if (!this.events.isEmpty()) {
|
| 65 | graphics.nextStratum();
|
| 66 | ProfilerFiller profiler = Profiler.get();
|
| 67 | profiler.push("bossHealth");
|
| 68 | int screenWidth = graphics.guiWidth();
|
| 69 | int yOffset = 12;
|
| 70 |
|
| 71 | for (LerpingBossEvent event : this.events.values()) {
|
| 72 | int xLeft = screenWidth / 2 - 91;
|
| 73 | int yo = yOffset;
|
| 74 | this.extractBar(graphics, xLeft, yo, event);
|
| 75 | Component msg = event.getName();
|
| 76 | int width = this.minecraft.font.width(msg);
|
| 77 | int x = screenWidth / 2 - width / 2;
|
| 78 | int y = yo - 9;
|
| 79 | graphics.text(this.minecraft.font, msg, x, y, -1);
|
| 80 | yOffset += 10 + 9;
|
| 81 | if (yOffset >= graphics.guiHeight() / 3) {
|
| 82 | break;
|
| 83 | }
|
| 84 | }
|
| 85 |
|
| 86 | profiler.pop();
|
| 87 | }
|
| 88 | }
|
| 89 |
|
| 90 | private void extractBar(final GuiGraphicsExtractor graphics, final int x, final int y, final BossEvent event) {
|
| 91 | this.extractBar(graphics, x, y, event, 182, BAR_BACKGROUND_SPRITES, OVERLAY_BACKGROUND_SPRITES);
|
| 92 | int width = Mth.lerpDiscrete(event.getProgress(), 0, 182);
|
| 93 | if (width > 0) {
|
| 94 | this.extractBar(graphics, x, y, event, width, BAR_PROGRESS_SPRITES, OVERLAY_PROGRESS_SPRITES);
|
| 95 | }
|
| 96 | }
|
| 97 |
|
| 98 | private void extractBar(
|
| 99 | final GuiGraphicsExtractor graphics,
|
| 100 | final int x,
|
| 101 | final int y,
|
| 102 | final BossEvent event,
|
| 103 | final int width,
|
| 104 | final Identifier[] sprites,
|
| 105 | final Identifier[] overlaySprites
|
| 106 | ) {
|
| 107 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, sprites[event.getColor().ordinal()], 182, 5, 0, 0, x, y, width, 5);
|
| 108 | if (event.getOverlay() != BossBarOverlay.PROGRESS) {
|
| 109 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, overlaySprites[event.getOverlay().ordinal() - 1], 182, 5, 0, 0, x, y, width, 5);
|
| 110 | }
|
| 111 | }
|
| 112 |
|
| 113 | public void update(final ClientboundBossEventPacket packet) {
|
| 114 | packet.dispatch(
|
| 115 | new Handler() {
|
| 116 | @Override
|
| 117 | public void add(
|
| 118 | final UUID id,
|
| 119 | final Component name,
|
| 120 | final float progress,
|
| 121 | final BossBarColor color,
|
| 122 | final BossBarOverlay overlay,
|
| 123 | final boolean darkenScreen,
|
| 124 | final boolean playMusic,
|
| 125 | final boolean createWorldFog
|
| 126 | ) {
|
| 127 | BossHealthOverlay.this.events.put(id, new LerpingBossEvent(id, name, progress, color, overlay, darkenScreen, playMusic, createWorldFog));
|
| 128 | }
|
| 129 |
|
| 130 | @Override
|
| 131 | public void remove(final UUID id) {
|
| 132 | BossHealthOverlay.this.events.remove(id);
|
| 133 | }
|
| 134 |
|
| 135 | @Override
|
| 136 | public void updateProgress(final UUID id, final float progress) {
|
| 137 | BossHealthOverlay.this.events.get(id).setProgress(progress);
|
| 138 | }
|
| 139 |
|
| 140 | @Override
|
| 141 | public void updateName(final UUID id, final Component name) {
|
| 142 | BossHealthOverlay.this.events.get(id).setName(name);
|
| 143 | }
|
| 144 |
|
| 145 | @Override
|
| 146 | public void updateStyle(final UUID id, final BossBarColor color, final BossBarOverlay overlay) {
|
| 147 | LerpingBossEvent event = BossHealthOverlay.this.events.get(id);
|
| 148 | event.setColor(color);
|
| 149 | event.setOverlay(overlay);
|
| 150 | }
|
| 151 |
|
| 152 | @Override
|
| 153 | public void updateProperties(final UUID id, final boolean darkenScreen, final boolean playMusic, final boolean createWorldFog) {
|
| 154 | LerpingBossEvent event = BossHealthOverlay.this.events.get(id);
|
| 155 | event.setDarkenScreen(darkenScreen);
|
| 156 | event.setPlayBossMusic(playMusic);
|
| 157 | event.setCreateWorldFog(createWorldFog);
|
| 158 | }
|
| 159 | }
|
| 160 | );
|
| 161 | }
|
| 162 |
|
| 163 | public void reset() {
|
| 164 | this.events.clear();
|
| 165 | }
|
| 166 |
|
| 167 | public boolean shouldPlayMusic() {
|
| 168 | if (!this.events.isEmpty()) {
|
| 169 | for (BossEvent event : this.events.values()) {
|
| 170 | if (event.shouldPlayBossMusic()) {
|
| 171 | return true;
|
| 172 | }
|
| 173 | }
|
| 174 | }
|
| 175 |
|
| 176 | return false;
|
| 177 | }
|
| 178 |
|
| 179 | public boolean shouldDarkenScreen() {
|
| 180 | if (!this.events.isEmpty()) {
|
| 181 | for (BossEvent event : this.events.values()) {
|
| 182 | if (event.shouldDarkenScreen()) {
|
| 183 | return true;
|
| 184 | }
|
| 185 | }
|
| 186 | }
|
| 187 |
|
| 188 | return false;
|
| 189 | }
|
| 190 |
|
| 191 | public boolean shouldCreateWorldFog() {
|
| 192 | if (!this.events.isEmpty()) {
|
| 193 | for (BossEvent event : this.events.values()) {
|
| 194 | if (event.shouldCreateWorldFog()) {
|
| 195 | return true;
|
| 196 | }
|
| 197 | }
|
| 198 | }
|
| 199 |
|
| 200 | return false;
|
| 201 | }
|
| 202 | }
|
| 203 |
|