| 1 | package net.minecraft.client.gui.screens.inventory;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import java.util.List;
|
| 5 | import java.util.Objects;
|
| 6 | import java.util.Optional;
|
| 7 | import net.fabricmc.api.EnvType;
|
| 8 | import net.fabricmc.api.Environment;
|
| 9 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 10 | import net.minecraft.client.gui.Hud;
|
| 11 | import net.minecraft.client.gui.components.AbstractButton;
|
| 12 | import net.minecraft.client.gui.components.AbstractWidget;
|
| 13 | import net.minecraft.client.gui.components.Tooltip;
|
| 14 | import net.minecraft.client.gui.narration.NarrationElementOutput;
|
| 15 | import net.minecraft.client.input.InputWithModifiers;
|
| 16 | import net.minecraft.client.renderer.RenderPipelines;
|
| 17 | import net.minecraft.core.Holder;
|
| 18 | import net.minecraft.network.chat.CommonComponents;
|
| 19 | import net.minecraft.network.chat.Component;
|
| 20 | import net.minecraft.network.chat.MutableComponent;
|
| 21 | import net.minecraft.network.protocol.game.ServerboundSetBeaconPacket;
|
| 22 | import net.minecraft.resources.Identifier;
|
| 23 | import net.minecraft.world.effect.MobEffect;
|
| 24 | import net.minecraft.world.entity.player.Inventory;
|
| 25 | import net.minecraft.world.inventory.AbstractContainerMenu;
|
| 26 | import net.minecraft.world.inventory.BeaconMenu;
|
| 27 | import net.minecraft.world.inventory.ContainerListener;
|
| 28 | import net.minecraft.world.item.ItemStack;
|
| 29 | import net.minecraft.world.item.Items;
|
| 30 | import net.minecraft.world.level.block.entity.BeaconBlockEntity;
|
| 31 | import org.jspecify.annotations.Nullable;
|
| 32 |
|
| 33 | @Environment(EnvType.CLIENT)
|
| 34 | public class BeaconScreen extends AbstractContainerScreen<BeaconMenu> {
|
| 35 | private static final Identifier BEACON_LOCATION = Identifier.withDefaultNamespace("textures/gui/container/beacon.png");
|
| 36 | private static final Identifier BUTTON_DISABLED_SPRITE = Identifier.withDefaultNamespace("container/beacon/button_disabled");
|
| 37 | private static final Identifier BUTTON_SELECTED_SPRITE = Identifier.withDefaultNamespace("container/beacon/button_selected");
|
| 38 | private static final Identifier BUTTON_HIGHLIGHTED_SPRITE = Identifier.withDefaultNamespace("container/beacon/button_highlighted");
|
| 39 | private static final Identifier BUTTON_SPRITE = Identifier.withDefaultNamespace("container/beacon/button");
|
| 40 | private static final Identifier CONFIRM_SPRITE = Identifier.withDefaultNamespace("container/beacon/confirm");
|
| 41 | private static final Identifier CANCEL_SPRITE = Identifier.withDefaultNamespace("container/beacon/cancel");
|
| 42 | private static final Component PRIMARY_EFFECT_LABEL = Component.translatable("block.minecraft.beacon.primary");
|
| 43 | private static final Component SECONDARY_EFFECT_LABEL = Component.translatable("block.minecraft.beacon.secondary");
|
| 44 | private final List<BeaconScreen.BeaconButton> beaconButtons = Lists.newArrayList();
|
| 45 | private @Nullable Holder<MobEffect> primary;
|
| 46 | private @Nullable Holder<MobEffect> secondary;
|
| 47 |
|
| 48 | public BeaconScreen(final BeaconMenu menu, final Inventory inventory, final Component title) {
|
| 49 | super(menu, inventory, title, 230, 219);
|
| 50 | menu.addSlotListener(new ContainerListener() {
|
| 51 | @Override
|
| 52 | public void slotChanged(final AbstractContainerMenu container, final int slotIndex, final ItemStack itemStack) {
|
| 53 | }
|
| 54 |
|
| 55 | @Override
|
| 56 | public void dataChanged(final AbstractContainerMenu container, final int id, final int value) {
|
| 57 | BeaconScreen.this.primary = menu.getPrimaryEffect();
|
| 58 | BeaconScreen.this.secondary = menu.getSecondaryEffect();
|
| 59 | }
|
| 60 | });
|
| 61 | }
|
| 62 |
|
| 63 | private <T extends AbstractWidget & BeaconScreen.BeaconButton> void addBeaconButton(final T beaconButton) {
|
| 64 | this.addRenderableWidget(beaconButton);
|
| 65 | this.beaconButtons.add(beaconButton);
|
| 66 | }
|
| 67 |
|
| 68 | @Override
|
| 69 | protected void init() {
|
| 70 | super.init();
|
| 71 | this.beaconButtons.clear();
|
| 72 |
|
| 73 | for (int tier = 0; tier <= 2; tier++) {
|
| 74 | int count = BeaconBlockEntity.BEACON_EFFECTS.get(tier).size();
|
| 75 | int totalWidth = count * 22 + (count - 1) * 2;
|
| 76 |
|
| 77 | for (int c = 0; c < count; c++) {
|
| 78 | Holder<MobEffect> effect = BeaconBlockEntity.BEACON_EFFECTS.get(tier).get(c);
|
| 79 | BeaconScreen.BeaconPowerButton beaconPowerButton = new BeaconScreen.BeaconPowerButton(
|
| 80 | this.leftPos + 76 + c * 24 - totalWidth / 2, this.topPos + 22 + tier * 25, effect, true, tier
|
| 81 | );
|
| 82 | beaconPowerButton.active = false;
|
| 83 | this.addBeaconButton(beaconPowerButton);
|
| 84 | }
|
| 85 | }
|
| 86 |
|
| 87 | int tier = 3;
|
| 88 | int count = BeaconBlockEntity.BEACON_EFFECTS.get(3).size() + 1;
|
| 89 | int totalWidth = count * 22 + (count - 1) * 2;
|
| 90 |
|
| 91 | for (int c = 0; c < count - 1; c++) {
|
| 92 | Holder<MobEffect> effect = BeaconBlockEntity.BEACON_EFFECTS.get(3).get(c);
|
| 93 | BeaconScreen.BeaconPowerButton beaconPowerButton = new BeaconScreen.BeaconPowerButton(
|
| 94 | this.leftPos + 167 + c * 24 - totalWidth / 2, this.topPos + 47, effect, false, 3
|
| 95 | );
|
| 96 | beaconPowerButton.active = false;
|
| 97 | this.addBeaconButton(beaconPowerButton);
|
| 98 | }
|
| 99 |
|
| 100 | Holder<MobEffect> dummyEffect = BeaconBlockEntity.BEACON_EFFECTS.get(0).get(0);
|
| 101 | BeaconScreen.BeaconPowerButton beaconPowerButton = new BeaconScreen.BeaconUpgradePowerButton(
|
| 102 | this.leftPos + 167 + (count - 1) * 24 - totalWidth / 2, this.topPos + 47, dummyEffect
|
| 103 | );
|
| 104 | beaconPowerButton.visible = false;
|
| 105 | this.addBeaconButton(beaconPowerButton);
|
| 106 | this.addBeaconButton(new BeaconScreen.BeaconConfirmButton(this.leftPos + 164, this.topPos + 107));
|
| 107 | this.addBeaconButton(new BeaconScreen.BeaconCancelButton(this.leftPos + 190, this.topPos + 107));
|
| 108 | }
|
| 109 |
|
| 110 | @Override
|
| 111 | public void containerTick() {
|
| 112 | super.containerTick();
|
| 113 | this.updateButtons();
|
| 114 | }
|
| 115 |
|
| 116 | private void updateButtons() {
|
| 117 | int levels = this.menu.getLevels();
|
| 118 | this.beaconButtons.forEach(b -> b.updateStatus(levels));
|
| 119 | }
|
| 120 |
|
| 121 | @Override
|
| 122 | protected void extractLabels(final GuiGraphicsExtractor graphics, final int xm, final int ym) {
|
| 123 | graphics.centeredText(this.font, PRIMARY_EFFECT_LABEL, 62, 10, -2039584);
|
| 124 | graphics.centeredText(this.font, SECONDARY_EFFECT_LABEL, 169, 10, -2039584);
|
| 125 | }
|
| 126 |
|
| 127 | @Override
|
| 128 | public void extractBackground(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 129 | super.extractBackground(graphics, mouseX, mouseY, a);
|
| 130 | int xo = (this.width - this.imageWidth) / 2;
|
| 131 | int yo = (this.height - this.imageHeight) / 2;
|
| 132 | graphics.blit(RenderPipelines.GUI_TEXTURED, BEACON_LOCATION, xo, yo, 0.0F, 0.0F, this.imageWidth, this.imageHeight, 256, 256);
|
| 133 | graphics.item(new ItemStack(Items.NETHERITE_INGOT), xo + 20, yo + 109);
|
| 134 | graphics.item(new ItemStack(Items.EMERALD), xo + 41, yo + 109);
|
| 135 | graphics.item(new ItemStack(Items.DIAMOND), xo + 41 + 22, yo + 109);
|
| 136 | graphics.item(new ItemStack(Items.GOLD_INGOT), xo + 42 + 44, yo + 109);
|
| 137 | graphics.item(new ItemStack(Items.IRON_INGOT), xo + 42 + 66, yo + 109);
|
| 138 | }
|
| 139 |
|
| 140 | @Environment(EnvType.CLIENT)
|
| 141 | private interface BeaconButton {
|
| 142 | void updateStatus(final int levels);
|
| 143 | }
|
| 144 |
|
| 145 | @Environment(EnvType.CLIENT)
|
| 146 | private class BeaconCancelButton extends BeaconScreen.BeaconSpriteScreenButton {
|
| 147 | public BeaconCancelButton(final int x, final int y) {
|
| 148 | super(x, y, BeaconScreen.CANCEL_SPRITE, CommonComponents.GUI_CANCEL);
|
| 149 | }
|
| 150 |
|
| 151 | @Override
|
| 152 | public void onPress(final InputWithModifiers input) {
|
| 153 | BeaconScreen.this.minecraft.player.closeContainer();
|
| 154 | }
|
| 155 |
|
| 156 | @Override
|
| 157 | public void updateStatus(final int levels) {
|
| 158 | }
|
| 159 | }
|
| 160 |
|
| 161 | @Environment(EnvType.CLIENT)
|
| 162 | private class BeaconConfirmButton extends BeaconScreen.BeaconSpriteScreenButton {
|
| 163 | public BeaconConfirmButton(final int x, final int y) {
|
| 164 | super(x, y, BeaconScreen.CONFIRM_SPRITE, CommonComponents.GUI_DONE);
|
| 165 | }
|
| 166 |
|
| 167 | @Override
|
| 168 | public void onPress(final InputWithModifiers input) {
|
| 169 | BeaconScreen.this.minecraft
|
| 170 | .getConnection()
|
| 171 | .send(new ServerboundSetBeaconPacket(Optional.ofNullable(BeaconScreen.this.primary), Optional.ofNullable(BeaconScreen.this.secondary)));
|
| 172 | BeaconScreen.this.minecraft.player.closeContainer();
|
| 173 | }
|
| 174 |
|
| 175 | @Override
|
| 176 | public void updateStatus(final int levels) {
|
| 177 | this.active = BeaconScreen.this.menu.hasPayment() && BeaconScreen.this.primary != null;
|
| 178 | }
|
| 179 | }
|
| 180 |
|
| 181 | @Environment(EnvType.CLIENT)
|
| 182 | private class BeaconPowerButton extends BeaconScreen.BeaconScreenButton {
|
| 183 | private final boolean isPrimary;
|
| 184 | protected final int tier;
|
| 185 | private Holder<MobEffect> effect;
|
| 186 | private Identifier sprite;
|
| 187 |
|
| 188 | public BeaconPowerButton(final int x, final int y, final Holder<MobEffect> effect, final boolean isPrimary, final int tier) {
|
| 189 | super(x, y);
|
| 190 | this.isPrimary = isPrimary;
|
| 191 | this.tier = tier;
|
| 192 | this.setEffect(effect);
|
| 193 | }
|
| 194 |
|
| 195 | protected void setEffect(final Holder<MobEffect> effect) {
|
| 196 | this.effect = effect;
|
| 197 | this.sprite = Hud.getMobEffectSprite(effect);
|
| 198 | this.setTooltip(Tooltip.create(this.createEffectDescription(effect), null));
|
| 199 | }
|
| 200 |
|
| 201 | protected MutableComponent createEffectDescription(final Holder<MobEffect> effect) {
|
| 202 | return Component.translatable(effect.value().getDescriptionId());
|
| 203 | }
|
| 204 |
|
| 205 | @Override
|
| 206 | public void onPress(final InputWithModifiers input) {
|
| 207 | if (!this.isSelected()) {
|
| 208 | if (this.isPrimary) {
|
| 209 | BeaconScreen.this.primary = this.effect;
|
| 210 | if (!Objects.equals(BeaconScreen.this.secondary, this.effect)) {
|
| 211 | BeaconScreen.this.secondary = null;
|
| 212 | }
|
| 213 | } else {
|
| 214 | BeaconScreen.this.secondary = this.effect;
|
| 215 | }
|
| 216 |
|
| 217 | BeaconScreen.this.updateButtons();
|
| 218 | }
|
| 219 | }
|
| 220 |
|
| 221 | @Override
|
| 222 | protected void extractIcon(final GuiGraphicsExtractor graphics) {
|
| 223 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, this.sprite, this.getX() + 2, this.getY() + 2, 18, 18);
|
| 224 | }
|
| 225 |
|
| 226 | @Override
|
| 227 | public void updateStatus(final int levels) {
|
| 228 | this.active = this.tier < levels;
|
| 229 | this.setSelected(this.effect.equals(this.isPrimary ? BeaconScreen.this.primary : BeaconScreen.this.secondary));
|
| 230 | }
|
| 231 |
|
| 232 | @Override
|
| 233 | protected MutableComponent createNarrationMessage() {
|
| 234 | return this.createEffectDescription(this.effect);
|
| 235 | }
|
| 236 | }
|
| 237 |
|
| 238 | @Environment(EnvType.CLIENT)
|
| 239 | private abstract static class BeaconScreenButton extends AbstractButton implements BeaconScreen.BeaconButton {
|
| 240 | private boolean selected;
|
| 241 |
|
| 242 | protected BeaconScreenButton(final int x, final int y) {
|
| 243 | super(x, y, 22, 22, CommonComponents.EMPTY);
|
| 244 | }
|
| 245 |
|
| 246 | protected BeaconScreenButton(final int x, final int y, final Component component) {
|
| 247 | super(x, y, 22, 22, component);
|
| 248 | }
|
| 249 |
|
| 250 | @Override
|
| 251 | public void extractContents(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 252 | Identifier sprite;
|
| 253 | if (!this.active) {
|
| 254 | sprite = BeaconScreen.BUTTON_DISABLED_SPRITE;
|
| 255 | } else if (this.selected) {
|
| 256 | sprite = BeaconScreen.BUTTON_SELECTED_SPRITE;
|
| 257 | } else if (this.isHoveredOrFocused()) {
|
| 258 | sprite = BeaconScreen.BUTTON_HIGHLIGHTED_SPRITE;
|
| 259 | } else {
|
| 260 | sprite = BeaconScreen.BUTTON_SPRITE;
|
| 261 | }
|
| 262 |
|
| 263 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, sprite, this.getX(), this.getY(), this.width, this.height);
|
| 264 | this.extractIcon(graphics);
|
| 265 | }
|
| 266 |
|
| 267 | protected abstract void extractIcon(final GuiGraphicsExtractor graphics);
|
| 268 |
|
| 269 | public boolean isSelected() {
|
| 270 | return this.selected;
|
| 271 | }
|
| 272 |
|
| 273 | public void setSelected(final boolean selected) {
|
| 274 | this.selected = selected;
|
| 275 | }
|
| 276 |
|
| 277 | @Override
|
| 278 | public void updateWidgetNarration(final NarrationElementOutput output) {
|
| 279 | this.defaultButtonNarrationText(output);
|
| 280 | }
|
| 281 | }
|
| 282 |
|
| 283 | @Environment(EnvType.CLIENT)
|
| 284 | private abstract static class BeaconSpriteScreenButton extends BeaconScreen.BeaconScreenButton {
|
| 285 | private final Identifier sprite;
|
| 286 |
|
| 287 | protected BeaconSpriteScreenButton(final int x, final int y, final Identifier sprite, final Component label) {
|
| 288 | super(x, y, label);
|
| 289 | this.setTooltip(Tooltip.create(label));
|
| 290 | this.sprite = sprite;
|
| 291 | }
|
| 292 |
|
| 293 | @Override
|
| 294 | protected void extractIcon(final GuiGraphicsExtractor graphics) {
|
| 295 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, this.sprite, this.getX() + 2, this.getY() + 2, 18, 18);
|
| 296 | }
|
| 297 | }
|
| 298 |
|
| 299 | @Environment(EnvType.CLIENT)
|
| 300 | private class BeaconUpgradePowerButton extends BeaconScreen.BeaconPowerButton {
|
| 301 | public BeaconUpgradePowerButton(final int x, final int y, final Holder<MobEffect> effect) {
|
| 302 | super(x, y, effect, false, 3);
|
| 303 | }
|
| 304 |
|
| 305 | @Override
|
| 306 | protected MutableComponent createEffectDescription(final Holder<MobEffect> effect) {
|
| 307 | return Component.translatable(effect.value().getDescriptionId()).append(" II");
|
| 308 | }
|
| 309 |
|
| 310 | @Override
|
| 311 | public void updateStatus(final int levels) {
|
| 312 | if (BeaconScreen.this.primary != null) {
|
| 313 | this.visible = true;
|
| 314 | this.setEffect(BeaconScreen.this.primary);
|
| 315 | super.updateStatus(levels);
|
| 316 | } else {
|
| 317 | this.visible = false;
|
| 318 | }
|
| 319 | }
|
| 320 | }
|
| 321 | }
|
| 322 |
|