| 1 | package net.minecraft.client.gui.screens.inventory;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.platform.cursor.CursorTypes;
|
| 4 | import net.fabricmc.api.EnvType;
|
| 5 | import net.fabricmc.api.Environment;
|
| 6 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 7 | import net.minecraft.client.renderer.RenderPipelines;
|
| 8 | import net.minecraft.network.chat.Component;
|
| 9 | import net.minecraft.resources.Identifier;
|
| 10 | import net.minecraft.sounds.SoundEvents;
|
| 11 | import net.minecraft.world.entity.player.Inventory;
|
| 12 | import net.minecraft.world.entity.player.Player;
|
| 13 | import net.minecraft.world.inventory.ContainerInput;
|
| 14 | import net.minecraft.world.inventory.CrafterMenu;
|
| 15 | import net.minecraft.world.inventory.CrafterSlot;
|
| 16 | import net.minecraft.world.inventory.Slot;
|
| 17 | import net.minecraft.world.item.ItemStack;
|
| 18 |
|
| 19 | @Environment(EnvType.CLIENT)
|
| 20 | public class CrafterScreen extends AbstractContainerScreen<CrafterMenu> {
|
| 21 | private static final Identifier DISABLED_SLOT_LOCATION_SPRITE = Identifier.withDefaultNamespace("container/crafter/disabled_slot");
|
| 22 | private static final Identifier POWERED_REDSTONE_LOCATION_SPRITE = Identifier.withDefaultNamespace("container/crafter/powered_redstone");
|
| 23 | private static final Identifier UNPOWERED_REDSTONE_LOCATION_SPRITE = Identifier.withDefaultNamespace("container/crafter/unpowered_redstone");
|
| 24 | private static final Identifier CONTAINER_LOCATION = Identifier.withDefaultNamespace("textures/gui/container/crafter.png");
|
| 25 | private static final Component DISABLED_SLOT_TOOLTIP = Component.translatable("gui.togglable_slot");
|
| 26 | private final Player player;
|
| 27 |
|
| 28 | public CrafterScreen(final CrafterMenu menu, final Inventory inventory, final Component title) {
|
| 29 | super(menu, inventory, title);
|
| 30 | this.player = inventory.player;
|
| 31 | }
|
| 32 |
|
| 33 | @Override
|
| 34 | protected void init() {
|
| 35 | super.init();
|
| 36 | this.titleLabelX = (this.imageWidth - this.font.width(this.title)) / 2;
|
| 37 | }
|
| 38 |
|
| 39 | @Override
|
| 40 | protected void slotClicked(final Slot slot, final int slotId, final int buttonNum, final ContainerInput containerInput) {
|
| 41 | if (slot instanceof CrafterSlot && !slot.hasItem() && !this.player.isSpectator()) {
|
| 42 | switch (containerInput) {
|
| 43 | case PICKUP:
|
| 44 | if (this.menu.isSlotDisabled(slotId)) {
|
| 45 | this.enableSlot(slotId);
|
| 46 | } else if (this.menu.getCarried().isEmpty()) {
|
| 47 | this.disableSlot(slotId);
|
| 48 | }
|
| 49 | break;
|
| 50 | case SWAP:
|
| 51 | ItemStack playerInventoryItem = this.player.getInventory().getItem(buttonNum);
|
| 52 | if (this.menu.isSlotDisabled(slotId) && !playerInventoryItem.isEmpty()) {
|
| 53 | this.enableSlot(slotId);
|
| 54 | }
|
| 55 | }
|
| 56 | }
|
| 57 |
|
| 58 | super.slotClicked(slot, slotId, buttonNum, containerInput);
|
| 59 | }
|
| 60 |
|
| 61 | private void enableSlot(final int slotId) {
|
| 62 | this.updateSlotState(slotId, true);
|
| 63 | }
|
| 64 |
|
| 65 | private void disableSlot(final int slotId) {
|
| 66 | this.updateSlotState(slotId, false);
|
| 67 | }
|
| 68 |
|
| 69 | private void updateSlotState(final int slotId, final boolean enabled) {
|
| 70 | this.menu.setSlotState(slotId, enabled);
|
| 71 | super.handleSlotStateChanged(slotId, this.menu.containerId, enabled);
|
| 72 | float pitch = enabled ? 1.0F : 0.75F;
|
| 73 | this.player.playSound(SoundEvents.UI_BUTTON_CLICK.value(), 0.4F, pitch);
|
| 74 | }
|
| 75 |
|
| 76 | @Override
|
| 77 | public void extractSlot(final GuiGraphicsExtractor graphics, final Slot slot, final int mouseX, final int mouseY) {
|
| 78 | if (slot instanceof CrafterSlot crafterSlot) {
|
| 79 | if (this.menu.isSlotDisabled(slot.index)) {
|
| 80 | this.extractDisabledSlot(graphics, crafterSlot);
|
| 81 | } else {
|
| 82 | super.extractSlot(graphics, slot, mouseX, mouseY);
|
| 83 | }
|
| 84 |
|
| 85 | int x0 = this.leftPos + crafterSlot.x - 2;
|
| 86 | int y0 = this.topPos + crafterSlot.y - 2;
|
| 87 | if (mouseX > x0 && mouseY > y0 && mouseX < x0 + 19 && mouseY < y0 + 19) {
|
| 88 | graphics.requestCursor(CursorTypes.POINTING_HAND);
|
| 89 | }
|
| 90 | } else {
|
| 91 | super.extractSlot(graphics, slot, mouseX, mouseY);
|
| 92 | }
|
| 93 | }
|
| 94 |
|
| 95 | private void extractDisabledSlot(final GuiGraphicsExtractor graphics, final CrafterSlot cs) {
|
| 96 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, DISABLED_SLOT_LOCATION_SPRITE, cs.x - 1, cs.y - 1, 18, 18);
|
| 97 | }
|
| 98 |
|
| 99 | @Override
|
| 100 | public void extractRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 101 | super.extractRenderState(graphics, mouseX, mouseY, a);
|
| 102 | this.extractRedstone(graphics);
|
| 103 | if (this.hoveredSlot instanceof CrafterSlot
|
| 104 | && !this.menu.isSlotDisabled(this.hoveredSlot.index)
|
| 105 | && this.menu.getCarried().isEmpty()
|
| 106 | && !this.hoveredSlot.hasItem()
|
| 107 | && !this.player.isSpectator()) {
|
| 108 | graphics.setTooltipForNextFrame(this.font, DISABLED_SLOT_TOOLTIP, mouseX, mouseY);
|
| 109 | }
|
| 110 | }
|
| 111 |
|
| 112 | private void extractRedstone(final GuiGraphicsExtractor graphics) {
|
| 113 | int xo = this.width / 2 + 9;
|
| 114 | int yo = this.height / 2 - 48;
|
| 115 | Identifier redstoneArrowTexture;
|
| 116 | if (this.menu.isPowered()) {
|
| 117 | redstoneArrowTexture = POWERED_REDSTONE_LOCATION_SPRITE;
|
| 118 | } else {
|
| 119 | redstoneArrowTexture = UNPOWERED_REDSTONE_LOCATION_SPRITE;
|
| 120 | }
|
| 121 |
|
| 122 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, redstoneArrowTexture, xo, yo, 16, 16);
|
| 123 | }
|
| 124 |
|
| 125 | @Override
|
| 126 | public void extractBackground(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 127 | super.extractBackground(graphics, mouseX, mouseY, a);
|
| 128 | int xo = (this.width - this.imageWidth) / 2;
|
| 129 | int yo = (this.height - this.imageHeight) / 2;
|
| 130 | graphics.blit(RenderPipelines.GUI_TEXTURED, CONTAINER_LOCATION, xo, yo, 0.0F, 0.0F, this.imageWidth, this.imageHeight, 256, 256);
|
| 131 | }
|
| 132 | }
|
| 133 |
|