|
| 1 | +package com.mrbysco.armorposer.client.gui; |
| 2 | + |
| 3 | +import com.mrbysco.armorposer.client.GlowHandler; |
| 4 | +import com.mrbysco.armorposer.client.gui.widgets.ArmorGlowWidget; |
| 5 | +import net.minecraft.client.Minecraft; |
| 6 | +import net.minecraft.client.gui.Font; |
| 7 | +import net.minecraft.client.gui.GuiGraphics; |
| 8 | +import net.minecraft.client.gui.components.Button; |
| 9 | +import net.minecraft.client.gui.components.ObjectSelectionList; |
| 10 | +import net.minecraft.client.gui.screens.Screen; |
| 11 | +import net.minecraft.commands.arguments.EntityAnchorArgument; |
| 12 | +import net.minecraft.network.chat.Component; |
| 13 | +import net.minecraft.world.entity.EntitySelector; |
| 14 | +import net.minecraft.world.entity.decoration.ArmorStand; |
| 15 | + |
| 16 | +import java.util.Collections; |
| 17 | +import java.util.List; |
| 18 | +import java.util.function.Consumer; |
| 19 | +import java.util.function.Function; |
| 20 | +import java.util.stream.Collectors; |
| 21 | + |
| 22 | +public class ArmorGlowScreen extends Screen { |
| 23 | + private static final int PADDING = 6; |
| 24 | + |
| 25 | + private ArmorGlowWidget armorListWidget; |
| 26 | + private ArmorGlowWidget.ListEntry selected = null; |
| 27 | + private List<ArmorStand> armorStands; |
| 28 | + private Button locateButton; |
| 29 | + private Button modifyButton; |
| 30 | + |
| 31 | + public ArmorStandScreen parentScreen; |
| 32 | + |
| 33 | + public ArmorGlowScreen(ArmorStandScreen parent) { |
| 34 | + super(Component.translatable("armorposer.gui.armor_list.list")); |
| 35 | + this.parentScreen = parent; |
| 36 | + |
| 37 | + this.minecraft = Minecraft.getInstance(); |
| 38 | + |
| 39 | + //Add the armor stands to the list |
| 40 | + if (minecraft.player == null) |
| 41 | + this.onClose(); |
| 42 | + |
| 43 | + List<ArmorStand> armorStands = minecraft.level.getEntitiesOfClass(ArmorStand.class, |
| 44 | + minecraft.player.getBoundingBox().inflate(30.0D), EntitySelector.LIVING_ENTITY_STILL_ALIVE).stream().collect(Collectors.toList()); |
| 45 | + //Sort the list based on how far the armor stand is from the player |
| 46 | + armorStands.sort((armorStand, armorStand2) -> { |
| 47 | + double distance1 = armorStand.distanceToSqr(minecraft.player); |
| 48 | + double distance2 = armorStand2.distanceToSqr(minecraft.player); |
| 49 | + return Double.compare(distance1, distance2); |
| 50 | + }); |
| 51 | + this.armorStands = Collections.unmodifiableList(armorStands); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public boolean isPauseScreen() { |
| 56 | + return false; |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + protected void init() { |
| 61 | + int centerWidth = this.width / 2; |
| 62 | + int listWidth = this.width / 4 + 20; |
| 63 | + int structureWidth = this.width - listWidth - (PADDING * 3); |
| 64 | + int closeButtonWidth = Math.min(structureWidth, 160); |
| 65 | + int y = this.height - 20 - PADDING; |
| 66 | + this.addRenderableWidget(Button.builder(Component.translatable("gui.cancel"), b -> ArmorGlowScreen.this.onClose()) |
| 67 | + .bounds(centerWidth - (closeButtonWidth / 2) + PADDING, y, closeButtonWidth, 20).build()); |
| 68 | + |
| 69 | + y -= 18 + PADDING; |
| 70 | + int buttonWidth = (closeButtonWidth / 2) - 1; |
| 71 | + this.addRenderableWidget(this.locateButton = Button.builder(Component.translatable("armorposer.gui.armor_list.locate"), b -> { |
| 72 | + if (selected != null && minecraft.player != null) { |
| 73 | + GlowHandler.startGlowing(this.selected.getArmorStand().getUUID()); |
| 74 | + minecraft.player.lookAt(EntityAnchorArgument.Anchor.EYES, selected.getArmorStand().position()); |
| 75 | + } |
| 76 | + }).bounds(centerWidth - (closeButtonWidth / 2) + PADDING, y, buttonWidth, 20).build()); |
| 77 | + this.addRenderableWidget(this.modifyButton = Button.builder(Component.translatable("armorposer.gui.armor_list.modify"), b -> { |
| 78 | + if (selected != null && minecraft.player != null) { |
| 79 | + minecraft.setScreen(new ArmorStandScreen(selected.getArmorStand())); |
| 80 | + } |
| 81 | + }).bounds(centerWidth - (closeButtonWidth / 2) + PADDING + buttonWidth + 2, y, buttonWidth, 20).build()); |
| 82 | + |
| 83 | + int fullButtonHeight = PADDING + 20 + PADDING; |
| 84 | + this.armorListWidget = new ArmorGlowWidget(this, Component.translatable("armorposer.gui.armor_list.list"), listWidth, fullButtonHeight, 14 - getScreenFont().lineHeight); |
| 85 | + this.armorListWidget.setX(0); |
| 86 | + this.armorListWidget.setY(10); |
| 87 | + this.armorListWidget.setHeight(this.height); |
| 88 | + |
| 89 | + addWidget(armorListWidget); |
| 90 | + |
| 91 | + updateCache(); |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public void tick() { |
| 96 | + armorListWidget.setSelected(selected); |
| 97 | + } |
| 98 | + |
| 99 | + public <T extends ObjectSelectionList.Entry<T>> void buildPositionList(Consumer<T> ListViewConsumer, Function<ArmorStand, T> newEntry) { |
| 100 | + armorStands.forEach(stand -> ListViewConsumer.accept(newEntry.apply(stand))); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public void render(GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTicks) { |
| 105 | + this.armorListWidget.render(guiGraphics, mouseX, mouseY, partialTicks); |
| 106 | + super.render(guiGraphics, mouseX, mouseY, partialTicks); |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public boolean keyPressed(int keyCode, int scanCode, int modifiers) { |
| 111 | + return super.keyPressed(keyCode, scanCode, modifiers); |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public void renderBackground(GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTicks) { |
| 116 | + //Nope |
| 117 | + } |
| 118 | + |
| 119 | + public void setSelected(ArmorGlowWidget.ListEntry entry) { |
| 120 | + this.selected = entry == this.selected ? null : entry; |
| 121 | + updateCache(); |
| 122 | + } |
| 123 | + |
| 124 | + private void updateCache() { |
| 125 | + this.locateButton.active = selected != null; |
| 126 | + this.modifyButton.active = selected != null; |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Clear the search field when right-clicked on it |
| 131 | + */ |
| 132 | + @Override |
| 133 | + public boolean mouseClicked(double mouseX, double mouseY, int button) { |
| 134 | + return super.mouseClicked(mouseX, mouseY, button); |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + public void resize(Minecraft mc, int width, int height) { |
| 139 | + ArmorGlowWidget.ListEntry selected = this.selected; |
| 140 | + this.init(mc, width, height); |
| 141 | + this.selected = selected; |
| 142 | + updateCache(); |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + public void onClose() { |
| 147 | + this.minecraft.setScreen(parentScreen); |
| 148 | + } |
| 149 | + |
| 150 | + public Minecraft getScreenMinecraft() { |
| 151 | + return this.minecraft; |
| 152 | + } |
| 153 | + |
| 154 | + public Font getScreenFont() { |
| 155 | + return this.font; |
| 156 | + } |
| 157 | +} |
0 commit comments