| 1 | package net.minecraft.client.gui.screens.inventory;
|
| 2 |
|
| 3 | import java.util.List;
|
| 4 | import java.util.Optional;
|
| 5 | import net.fabricmc.api.EnvType;
|
| 6 | import net.fabricmc.api.Environment;
|
| 7 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 8 | import net.minecraft.client.renderer.RenderPipelines;
|
| 9 | import net.minecraft.client.renderer.entity.layers.HumanoidArmorLayer;
|
| 10 | import net.minecraft.client.renderer.entity.state.ArmorStandRenderState;
|
| 11 | import net.minecraft.client.renderer.item.ItemModelResolver;
|
| 12 | import net.minecraft.core.component.DataComponents;
|
| 13 | import net.minecraft.network.chat.Component;
|
| 14 | import net.minecraft.resources.Identifier;
|
| 15 | import net.minecraft.world.entity.EntityTypes;
|
| 16 | import net.minecraft.world.entity.EquipmentSlot;
|
| 17 | import net.minecraft.world.entity.player.Inventory;
|
| 18 | import net.minecraft.world.inventory.AbstractContainerMenu;
|
| 19 | import net.minecraft.world.inventory.SmithingMenu;
|
| 20 | import net.minecraft.world.item.ItemDisplayContext;
|
| 21 | import net.minecraft.world.item.ItemStack;
|
| 22 | import net.minecraft.world.item.SmithingTemplateItem;
|
| 23 | import net.minecraft.world.item.equipment.Equippable;
|
| 24 | import org.joml.Quaternionf;
|
| 25 | import org.joml.Quaternionfc;
|
| 26 | import org.joml.Vector3f;
|
| 27 | import org.joml.Vector3fc;
|
| 28 |
|
| 29 | @Environment(EnvType.CLIENT)
|
| 30 | public class SmithingScreen extends ItemCombinerScreen<SmithingMenu> {
|
| 31 | private static final Identifier ERROR_SPRITE = Identifier.withDefaultNamespace("container/smithing/error");
|
| 32 | private static final Identifier EMPTY_SLOT_SMITHING_TEMPLATE_ARMOR_TRIM = Identifier.withDefaultNamespace("container/slot/smithing_template_armor_trim");
|
| 33 | private static final Identifier EMPTY_SLOT_SMITHING_TEMPLATE_NETHERITE_UPGRADE = Identifier.withDefaultNamespace(
|
| 34 | "container/slot/smithing_template_netherite_upgrade"
|
| 35 | );
|
| 36 | private static final Component MISSING_TEMPLATE_TOOLTIP = Component.translatable("container.upgrade.missing_template_tooltip");
|
| 37 | private static final Component ERROR_TOOLTIP = Component.translatable("container.upgrade.error_tooltip");
|
| 38 | private static final List<Identifier> EMPTY_SLOT_SMITHING_TEMPLATES = List.of(
|
| 39 | EMPTY_SLOT_SMITHING_TEMPLATE_ARMOR_TRIM, EMPTY_SLOT_SMITHING_TEMPLATE_NETHERITE_UPGRADE
|
| 40 | );
|
| 41 | private static final int TITLE_LABEL_X = 44;
|
| 42 | private static final int TITLE_LABEL_Y = 15;
|
| 43 | private static final int ERROR_ICON_WIDTH = 28;
|
| 44 | private static final int ERROR_ICON_HEIGHT = 21;
|
| 45 | private static final int ERROR_ICON_X = 65;
|
| 46 | private static final int ERROR_ICON_Y = 46;
|
| 47 | private static final int TOOLTIP_WIDTH = 115;
|
| 48 | private static final int ARMOR_STAND_Y_ROT = 210;
|
| 49 | private static final int ARMOR_STAND_X_ROT = 25;
|
| 50 | private static final Vector3fc ARMOR_STAND_TRANSLATION = new Vector3f(0.0F, 1.0F, 0.0F);
|
| 51 | private static final Quaternionfc ARMOR_STAND_ANGLE = new Quaternionf().rotationXYZ(0.43633232F, 0.0F, (float) Math.PI);
|
| 52 | private static final int ARMOR_STAND_SCALE = 25;
|
| 53 | private static final int ARMOR_STAND_LEFT = 121;
|
| 54 | private static final int ARMOR_STAND_TOP = 20;
|
| 55 | private static final int ARMOR_STAND_RIGHT = 161;
|
| 56 | private static final int ARMOR_STAND_BOTTOM = 80;
|
| 57 | private final CyclingSlotBackground templateIcon = new CyclingSlotBackground(0);
|
| 58 | private final CyclingSlotBackground baseIcon = new CyclingSlotBackground(1);
|
| 59 | private final CyclingSlotBackground additionalIcon = new CyclingSlotBackground(2);
|
| 60 | private final ArmorStandRenderState armorStandPreview = new ArmorStandRenderState();
|
| 61 |
|
| 62 | public SmithingScreen(final SmithingMenu menu, final Inventory inventory, final Component title) {
|
| 63 | super(menu, inventory, title, Identifier.withDefaultNamespace("textures/gui/container/smithing.png"));
|
| 64 | this.titleLabelX = 44;
|
| 65 | this.titleLabelY = 15;
|
| 66 | this.armorStandPreview.entityType = EntityTypes.ARMOR_STAND;
|
| 67 | this.armorStandPreview.showBasePlate = false;
|
| 68 | this.armorStandPreview.showArms = true;
|
| 69 | this.armorStandPreview.xRot = 25.0F;
|
| 70 | this.armorStandPreview.bodyRot = 210.0F;
|
| 71 | }
|
| 72 |
|
| 73 | @Override
|
| 74 | protected void subInit() {
|
| 75 | this.updateArmorStandPreview(this.menu.getSlot(3).getItem());
|
| 76 | }
|
| 77 |
|
| 78 | @Override
|
| 79 | public void containerTick() {
|
| 80 | super.containerTick();
|
| 81 | Optional<SmithingTemplateItem> template = this.getTemplateItem();
|
| 82 | this.templateIcon.tick(EMPTY_SLOT_SMITHING_TEMPLATES);
|
| 83 | this.baseIcon.tick(template.map(SmithingTemplateItem::getBaseSlotEmptyIcons).orElse(List.of()));
|
| 84 | this.additionalIcon.tick(template.map(SmithingTemplateItem::getAdditionalSlotEmptyIcons).orElse(List.of()));
|
| 85 | }
|
| 86 |
|
| 87 | private Optional<SmithingTemplateItem> getTemplateItem() {
|
| 88 | ItemStack templateSlotItem = this.menu.getSlot(0).getItem();
|
| 89 | return !templateSlotItem.isEmpty() && templateSlotItem.getItem() instanceof SmithingTemplateItem templateItem ? Optional.of(templateItem) : Optional.empty();
|
| 90 | }
|
| 91 |
|
| 92 | @Override
|
| 93 | public void extractRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 94 | super.extractRenderState(graphics, mouseX, mouseY, a);
|
| 95 | this.extractOnboardingTooltips(graphics, mouseX, mouseY);
|
| 96 | }
|
| 97 |
|
| 98 | @Override
|
| 99 | public void extractBackground(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 100 | super.extractBackground(graphics, mouseX, mouseY, a);
|
| 101 | this.templateIcon.extractRenderState(this.menu, graphics, a, this.leftPos, this.topPos);
|
| 102 | this.baseIcon.extractRenderState(this.menu, graphics, a, this.leftPos, this.topPos);
|
| 103 | this.additionalIcon.extractRenderState(this.menu, graphics, a, this.leftPos, this.topPos);
|
| 104 | int x0 = this.leftPos + 121;
|
| 105 | int y0 = this.topPos + 20;
|
| 106 | int x1 = this.leftPos + 161;
|
| 107 | int y1 = this.topPos + 80;
|
| 108 | graphics.entity(this.armorStandPreview, 25.0F, ARMOR_STAND_TRANSLATION, ARMOR_STAND_ANGLE, null, x0, y0, x1, y1);
|
| 109 | }
|
| 110 |
|
| 111 | @Override
|
| 112 | public void slotChanged(final AbstractContainerMenu container, final int slotIndex, final ItemStack itemStack) {
|
| 113 | if (slotIndex == 3) {
|
| 114 | this.updateArmorStandPreview(itemStack);
|
| 115 | }
|
| 116 | }
|
| 117 |
|
| 118 | private void updateArmorStandPreview(final ItemStack itemStack) {
|
| 119 | this.armorStandPreview.leftHandItemStack = ItemStack.EMPTY;
|
| 120 | this.armorStandPreview.leftHandItemState.clear();
|
| 121 | this.armorStandPreview.headEquipment = ItemStack.EMPTY;
|
| 122 | this.armorStandPreview.headItem.clear();
|
| 123 | this.armorStandPreview.chestEquipment = ItemStack.EMPTY;
|
| 124 | this.armorStandPreview.legsEquipment = ItemStack.EMPTY;
|
| 125 | this.armorStandPreview.feetEquipment = ItemStack.EMPTY;
|
| 126 | if (!itemStack.isEmpty()) {
|
| 127 | Equippable equippable = itemStack.get(DataComponents.EQUIPPABLE);
|
| 128 | EquipmentSlot slot = equippable != null ? equippable.slot() : null;
|
| 129 | ItemModelResolver itemModelResolver = this.minecraft.getItemModelResolver();
|
| 130 | switch (slot) {
|
| 131 | case HEAD:
|
| 132 | if (HumanoidArmorLayer.shouldRender(itemStack, EquipmentSlot.HEAD)) {
|
| 133 | this.armorStandPreview.headEquipment = itemStack.copy();
|
| 134 | } else {
|
| 135 | itemModelResolver.updateForTopItem(this.armorStandPreview.headItem, itemStack, ItemDisplayContext.HEAD, this.minecraft.level, null, 0);
|
| 136 | }
|
| 137 | break;
|
| 138 | case CHEST:
|
| 139 | this.armorStandPreview.chestEquipment = itemStack.copy();
|
| 140 | break;
|
| 141 | case LEGS:
|
| 142 | this.armorStandPreview.legsEquipment = itemStack.copy();
|
| 143 | break;
|
| 144 | case FEET:
|
| 145 | this.armorStandPreview.feetEquipment = itemStack.copy();
|
| 146 | break;
|
| 147 | case null:
|
| 148 | default:
|
| 149 | this.armorStandPreview.leftHandItemStack = itemStack.copy();
|
| 150 | itemModelResolver.updateForTopItem(
|
| 151 | this.armorStandPreview.leftHandItemState, itemStack, ItemDisplayContext.THIRD_PERSON_LEFT_HAND, this.minecraft.level, null, 0
|
| 152 | );
|
| 153 | }
|
| 154 | }
|
| 155 | }
|
| 156 |
|
| 157 | @Override
|
| 158 | protected void extractErrorIcon(final GuiGraphicsExtractor graphics, final int xo, final int yo) {
|
| 159 | if (this.hasRecipeError()) {
|
| 160 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, ERROR_SPRITE, xo + 65, yo + 46, 28, 21);
|
| 161 | }
|
| 162 | }
|
| 163 |
|
| 164 | private void extractOnboardingTooltips(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY) {
|
| 165 | Optional<Component> tooltip = Optional.empty();
|
| 166 | if (this.hasRecipeError() && this.isHovering(65, 46, 28, 21, mouseX, mouseY)) {
|
| 167 | tooltip = Optional.of(ERROR_TOOLTIP);
|
| 168 | }
|
| 169 |
|
| 170 | if (this.hoveredSlot != null) {
|
| 171 | ItemStack template = this.menu.getSlot(0).getItem();
|
| 172 | ItemStack hoveredStack = this.hoveredSlot.getItem();
|
| 173 | if (template.isEmpty()) {
|
| 174 | if (this.hoveredSlot.index == 0) {
|
| 175 | tooltip = Optional.of(MISSING_TEMPLATE_TOOLTIP);
|
| 176 | }
|
| 177 | } else if (template.getItem() instanceof SmithingTemplateItem templateItem && hoveredStack.isEmpty()) {
|
| 178 | if (this.hoveredSlot.index == 1) {
|
| 179 | tooltip = Optional.of(templateItem.getBaseSlotDescription());
|
| 180 | } else if (this.hoveredSlot.index == 2) {
|
| 181 | tooltip = Optional.of(templateItem.getAdditionSlotDescription());
|
| 182 | }
|
| 183 | }
|
| 184 | }
|
| 185 |
|
| 186 | tooltip.ifPresent(component -> graphics.setTooltipForNextFrame(this.font, this.font.split(component, 115), mouseX, mouseY));
|
| 187 | }
|
| 188 |
|
| 189 | private boolean hasRecipeError() {
|
| 190 | return this.menu.hasRecipeError();
|
| 191 | }
|
| 192 | }
|
| 193 |
|