PrettyPipes/src/main/java/de/ellpeck/prettypipes/terminal/containers/CraftingTerminalGui.java

85 lines
3.4 KiB
Java
Raw Normal View History

2020-05-08 22:58:16 +02:00
package de.ellpeck.prettypipes.terminal.containers;
import com.mojang.blaze3d.platform.InputConstants;
2021-12-02 16:55:04 +01:00
import com.mojang.blaze3d.vertex.PoseStack;
2020-05-08 22:58:16 +02:00
import de.ellpeck.prettypipes.PrettyPipes;
2020-05-09 13:40:46 +02:00
import de.ellpeck.prettypipes.packets.PacketButton;
import de.ellpeck.prettypipes.packets.PacketHandler;
2021-12-02 16:55:04 +01:00
import net.minecraft.client.gui.components.Button;
2022-06-30 16:00:33 +02:00
import net.minecraft.client.gui.screens.Screen;
2021-12-02 16:55:04 +01:00
import net.minecraft.network.chat.Component;
2021-12-02 12:31:04 +01:00
import net.minecraft.resources.ResourceLocation;
2021-12-02 16:55:04 +01:00
import net.minecraft.world.entity.player.Inventory;
2020-05-08 22:58:16 +02:00
public class CraftingTerminalGui extends ItemTerminalGui {
2021-12-02 16:55:04 +01:00
2020-05-08 22:58:16 +02:00
private static final ResourceLocation TEXTURE = new ResourceLocation(PrettyPipes.ID, "textures/gui/crafting_terminal.png");
private Button requestButton;
2020-05-08 22:58:16 +02:00
2021-12-02 16:55:04 +01:00
public CraftingTerminalGui(ItemTerminalContainer screenContainer, Inventory inv, Component titleIn) {
2020-05-08 22:58:16 +02:00
super(screenContainer, inv, titleIn);
2021-12-02 16:55:04 +01:00
this.imageWidth = 256;
2020-05-08 22:58:16 +02:00
}
@Override
protected void init() {
super.init();
2022-06-30 16:00:33 +02:00
this.requestButton = this.addRenderableWidget(new Button(this.leftPos + 8, this.topPos + 100, 50, 20, Component.translatable("info." + PrettyPipes.ID + ".request"), button -> {
2022-06-30 16:00:33 +02:00
var amount = ItemTerminalGui.requestModifier();
// also allow holding backspace instead of alt for people whose alt key is inaccessible (linux?)
2022-06-30 16:00:33 +02:00
var force = Screen.hasAltDown() || InputConstants.isKeyDown(this.minecraft.getWindow().getWindow(), 259) ? 1 : 0;
PacketHandler.sendToServer(new PacketButton(this.menu.tile.getBlockPos(), PacketButton.ButtonResult.CRAFT_TERMINAL_REQUEST, amount, force));
}));
2020-05-09 15:42:09 +02:00
this.tick();
}
@Override
2021-12-02 16:55:04 +01:00
public void containerTick() {
super.containerTick();
var tile = this.getCraftingContainer().getTile();
2020-05-09 14:56:58 +02:00
this.requestButton.active = false;
2021-12-02 16:55:04 +01:00
for (var i = 0; i < tile.craftItems.getSlots(); i++) {
var stack = tile.getRequestedCraftItem(i);
2020-05-09 15:42:09 +02:00
if (!stack.isEmpty() && stack.getCount() < stack.getMaxStackSize()) {
2020-05-09 14:56:58 +02:00
this.requestButton.active = true;
break;
}
}
}
@Override
2021-12-02 16:55:04 +01:00
protected void renderLabels(PoseStack matrix, int mouseX, int mouseY) {
super.renderLabels(matrix, mouseX, mouseY);
2020-05-09 14:56:58 +02:00
2021-12-02 16:55:04 +01:00
var container = this.getCraftingContainer();
var tile = container.getTile();
for (var i = 0; i < tile.ghostItems.getSlots(); i++) {
2020-05-09 14:56:58 +02:00
if (!tile.craftItems.getStackInSlot(i).isEmpty())
continue;
2021-12-02 16:55:04 +01:00
var ghost = tile.ghostItems.getStackInSlot(i);
2020-05-09 14:56:58 +02:00
if (ghost.isEmpty())
continue;
2021-12-02 16:55:04 +01:00
var finalI = i;
var slot = container.slots.stream().filter(s -> s.container == container.craftInventory && s.getSlotIndex() == finalI).findFirst().orElse(null);
2020-05-09 14:56:58 +02:00
if (slot == null)
continue;
2021-12-02 16:55:04 +01:00
this.minecraft.getItemRenderer().renderGuiItem(ghost, slot.x, slot.y);
this.minecraft.getItemRenderer().renderGuiItemDecorations(this.font, ghost, slot.x, slot.y, "0");
2020-05-09 14:56:58 +02:00
}
}
2020-05-08 22:58:16 +02:00
@Override
protected ResourceLocation getTexture() {
2022-06-30 16:00:33 +02:00
return CraftingTerminalGui.TEXTURE;
2020-05-08 22:58:16 +02:00
}
@Override
protected int getXOffset() {
return 65;
}
protected CraftingTerminalContainer getCraftingContainer() {
2021-12-02 16:55:04 +01:00
return (CraftingTerminalContainer) this.menu;
}
2020-05-08 22:58:16 +02:00
}