Jump to content

[1.18.1] Try to craft something if button clicked


MrOlegTitov

Recommended Posts

Hello all. I've started building a custom workbench, but have run into a problem. According to my idea, a person needs to press a special button to create a thing. But I don't know how to track its pressing from container class. I tried using ContainerData class, but it seems you can't change its values from Screen class. What can I do to fix this issue?

?

Container Class:

package net.xale.satiscraft.container;

import net.minecraft.client.Minecraft;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.world.Container;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.*;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.ItemStackHandler;
import net.minecraftforge.items.SlotItemHandler;
import net.xale.satiscraft.block.entity.WorkbenchBlockEntity;
import net.xale.satiscraft.container.syncdata.WorkbenchContainerData;
import net.xale.satiscraft.crafting.WorkbenchRecipes;
import net.xale.satiscraft.init.BlockInit;
import net.xale.satiscraft.init.ContainerInit;

public class WorkbenchContainer extends AbstractContainerMenu {
    private final ContainerLevelAccess containerAccess;
    public final ContainerData data;

    // Client Constructor
    public WorkbenchContainer(int id, Inventory playerInv) {
        this(id, playerInv, new ItemStackHandler(27), BlockPos.ZERO, new SimpleContainerData(1));
    }

    // Server constructor
    public WorkbenchContainer(int id, Inventory playerInv, IItemHandler slots, BlockPos pos, ContainerData data) {
        super(ContainerInit.WORKBENCH.get(), id);
        this.containerAccess = ContainerLevelAccess.create(playerInv.player.level, pos);
        this.data = data;

        final int slotSizePlus2 = 18, startX = 8, startY = 84, hotbarY = 142;
        for (int column = 0; column < 9; column++){
            for (int row = 0; row < 3; row++){
                addSlot(new Slot(playerInv, 9 + row * 9 + column, startX + column * slotSizePlus2, startY + row * slotSizePlus2));
            }

            addSlot(new Slot(playerInv, column, startX + column * slotSizePlus2, hotbarY));
        }

        addSlot(new SlotItemHandler(slots, 0, 30, 35));
        addSlot(new SlotItemHandler(slots, 1, 48, 35));
        addSlot(new SlotItemHandler(slots, 2, 66, 35));
        addSlot(new SlotItemHandler(slots, 3, 125, 35));

        addDataSlots(data);
    }

    @Override
    public void broadcastChanges() {
        if (this.data.get(0) == 1){
            ItemStack result = WorkbenchRecipes.getInstance().getCraftingResult(this.getSlot(36).getItem(), this.getSlot(37).getItem(), this.getSlot(38).getItem());
            if (result != ItemStack.EMPTY){
                this.getSlot(36).getItem().shrink(1);
                this.getSlot(37).getItem().shrink(1);
                this.getSlot(38).getItem().shrink(1);
                this.getSlot(39).safeInsert(result, 1);
                this.data.set(0, 0);
            }
        }
    }

    @Override
    public ItemStack quickMoveStack(Player player, int index) {
        var retStack = ItemStack.EMPTY;
        final Slot slot = getSlot(index);
        if (slot.hasItem()) {
            final ItemStack item = slot.getItem();
            retStack = item.copy();
            if (index < 27) {
                if (!moveItemStackTo(item, 27, this.slots.size(), true))
                    return ItemStack.EMPTY;
            } else if (!moveItemStackTo(item, 0, 27, false))
                return ItemStack.EMPTY;

            if (item.isEmpty()) {
                slot.set(ItemStack.EMPTY);
            } else {
                slot.setChanged();
            }
        }

        return retStack;
    }

    @Override
    public boolean stillValid(Player player) {
        return stillValid(this.containerAccess, player, BlockInit.WORKBENCH.get());
    }

    public static MenuConstructor getServerContainer(WorkbenchBlockEntity workbenchBlock, BlockPos pos) {
        return (id, playerInv, player) -> new WorkbenchContainer(id, playerInv, workbenchBlock.inventory, pos,
                new WorkbenchContainerData(workbenchBlock, 1));
    }
}

Screen Class:

package net.xale.satiscraft.screen;

import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraftforge.client.gui.widget.ExtendedButton;
import net.xale.satiscraft.SatisCraft;
import net.xale.satiscraft.block.entity.WorkbenchBlockEntity;
import net.xale.satiscraft.container.WorkbenchContainer;
import net.xale.satiscraft.crafting.WorkbenchRecipe;
import net.xale.satiscraft.crafting.WorkbenchRecipes;

public class WorkbenchScreen extends AbstractContainerScreen<WorkbenchContainer> {
    private static final ResourceLocation GUI_TEXTURE = new ResourceLocation(SatisCraft.MOD_ID, "textures/gui/workbench.png");
    private ExtendedButton craftbutton;

    public WorkbenchScreen(WorkbenchContainer container, Inventory playerInventory, Component title){
        super(container, playerInventory, title);
        this.leftPos = 0;
        this.topPos = 0;
        this.imageWidth = 176;
        this.imageHeight = 166;
    }

    @Override
    protected void renderBg(PoseStack stack, float mouseX, int mouseY, int partialTicks) {
        RenderSystem.setShader(GameRenderer::getPositionColorTexShader);
        RenderSystem.setShaderColor(1.0f, 1.0f, 1.0f, 1.0f);
        RenderSystem.setShaderTexture(0, GUI_TEXTURE);
        blit(stack, this.leftPos, this.topPos, 0, 0, this.imageWidth, this.imageHeight);
    }

    @Override
    protected void renderLabels(PoseStack stack, int mouseX, int mouseY){
        drawString(stack, font, title, this.leftPos, this.topPos + 2, 0x404040);
        drawString(stack, font, playerInventoryTitle, this.leftPos + 8, this.topPos + 80, 0x404040);
    }

    @Override
    protected void init(){
        super.init();
        this.craftbutton = this.addRenderableWidget(new ExtendedButton(leftPos, topPos, 16, 16, new TextComponent("Craft!"), btn -> {
            if (this.menu.data.get(0) == 0){
                this.menu.data.set(0, 1);
                Minecraft.getInstance().player.displayClientMessage(new TextComponent(String.valueOf(this.menu.data.get(0))), false);
            }
        }));
    }
}

Custom Container Data Class:

package net.xale.satiscraft.container.syncdata;

import net.minecraft.client.Minecraft;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.world.inventory.ContainerData;
import net.minecraft.world.inventory.SimpleContainerData;
import net.xale.satiscraft.block.entity.WorkbenchBlockEntity;
import net.xale.satiscraft.container.WorkbenchContainer;

public class WorkbenchContainerData extends SimpleContainerData {
    private final WorkbenchBlockEntity blockEntity;
    public WorkbenchContainerData(WorkbenchBlockEntity be, int amount){
        super(amount);
        this.blockEntity = be;
    }

    @Override
    public int get(int key) {
        return switch (key){
            case 0 -> this.blockEntity.canCraft;
            default -> throw new UnsupportedOperationException("There is no value corresponding to key: '" + key +
                    "' in '" + this.blockEntity + "'");
        };
    }

    @Override
    public void set(int key, int value) {
        Minecraft.getInstance().player.displayClientMessage(new TextComponent("Trying to set!"), false);
        switch (key){
            case 0:
                Minecraft.getInstance().player.displayClientMessage(new TextComponent("Setting 0"), false);
                this.blockEntity.canCraft = 1;
                break;
            default:
                throw new UnsupportedOperationException("There is no value corresponding to key: '" + key + "' in '"
                        + this.blockEntity + "'");

        }
    }
}

 

Edited by MrOlegTitov
wrong title
Link to comment
Share on other sites

  • MrOlegTitov changed the title to [1.18.1] Try to craft something if button clicked
15 hours ago, diesieben07 said:

You can call Minecraft#gameMode.handleInventoryButtonClick from your screen. It will then call clickMenuButton on your Menu class on the server.

I've got a bug, when I exit the gui and reopen it, the event stops working when the button is pressed. What can this be connected to?

Link to comment
Share on other sites

7 minutes ago, diesieben07 said:

Show updated code.

Workbench Screen class:

package net.xale.satiscraft.screen;

import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraftforge.client.gui.widget.ExtendedButton;
import net.xale.satiscraft.SatisCraft;
import net.xale.satiscraft.block.entity.WorkbenchBlockEntity;
import net.xale.satiscraft.container.WorkbenchContainer;
import net.xale.satiscraft.crafting.WorkbenchRecipe;
import net.xale.satiscraft.crafting.WorkbenchRecipes;

public class WorkbenchScreen extends AbstractContainerScreen<WorkbenchContainer> {
    private static final ResourceLocation GUI_TEXTURE = new ResourceLocation(SatisCraft.MOD_ID, "textures/gui/workbench.png");
    private ExtendedButton craftbutton;

    public WorkbenchScreen(WorkbenchContainer container, Inventory playerInventory, Component title){
        super(container, playerInventory, title);
        this.leftPos = 0;
        this.topPos = 0;
        this.imageWidth = 176;
        this.imageHeight = 166;
    }

    @Override
    protected void renderBg(PoseStack stack, float mouseX, int mouseY, int partialTicks) {
        renderBackground(stack);
        bindTexture();

        blit(stack, this.leftPos, this.topPos, 0, 0, this.imageWidth, this.imageHeight);
    }

    @Override
    protected void renderLabels(PoseStack stack, int mouseX, int mouseY){

    }

    @Override
    public void render(PoseStack stack, int mouseX, int mouseY, float partialTicks) {
        super.render(stack, mouseX, mouseY, partialTicks);

        this.font.draw(stack, this.title, this.leftPos + 20, this.topPos + 5, 0xFFFFFF);
        this.font.draw(stack, this.playerInventoryTitle, this.leftPos + 5, this.topPos + 73, 0x404040);
    }

    @Override
    protected void init(){
        super.init();
        this.craftbutton = this.addRenderableWidget(new ExtendedButton(150, 35, 40, 20, new TextComponent("Craft!"), btn -> {
            Minecraft.getInstance().gameMode.handleInventoryButtonClick(1, 0);
        }));
    }

    public static void bindTexture(){
        RenderSystem.setShader(GameRenderer::getPositionColorTexShader);
        RenderSystem.setShaderColor(1.0f, 1.0f, 1.0f, 1.0f);
        RenderSystem.setShaderTexture(0, GUI_TEXTURE);
    }
}

WorkbenchContainer class:

package net.xale.satiscraft.container;

import net.minecraft.client.Minecraft;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.world.Container;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.*;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.ItemStackHandler;
import net.minecraftforge.items.SlotItemHandler;
import net.xale.satiscraft.block.entity.WorkbenchBlockEntity;
import net.xale.satiscraft.crafting.WorkbenchRecipes;
import net.xale.satiscraft.init.BlockInit;
import net.xale.satiscraft.init.ContainerInit;

public class WorkbenchContainer extends AbstractContainerMenu {
    private final ContainerLevelAccess containerAccess;

    // Client Constructor
    public WorkbenchContainer(int id, Inventory playerInv) {
        this(id, playerInv, new ItemStackHandler(27), BlockPos.ZERO);
    }

    // Server constructor
    public WorkbenchContainer(int id, Inventory playerInv, IItemHandler slots, BlockPos pos) {
        super(ContainerInit.WORKBENCH.get(), id);
        this.containerAccess = ContainerLevelAccess.create(playerInv.player.level, pos);

        final int slotSizePlus2 = 18, startX = 8, startY = 84, hotbarY = 142;
        for (int column = 0; column < 9; column++) {
            for (int row = 0; row < 3; row++) {
                addSlot(new Slot(playerInv, 9 + row * 9 + column, startX + column * slotSizePlus2, startY + row * slotSizePlus2));
            }

            addSlot(new Slot(playerInv, column, startX + column * slotSizePlus2, hotbarY));
        }

        addSlot(new SlotItemHandler(slots, 0, 30, 35));
        addSlot(new SlotItemHandler(slots, 1, 48, 35));
        addSlot(new SlotItemHandler(slots, 2, 66, 35));
        addSlot(new SlotItemHandler(slots, 3, 125, 35));
    }

    @Override
    public boolean clickMenuButton(Player clickedBy, int buttonId) {
        super.clickMenuButton(clickedBy, buttonId);
        if (buttonId == 0) {
            ItemStack result = WorkbenchRecipes.getInstance().getCraftingResult(this.getSlot(36).getItem(), this.getSlot(37).getItem(), this.getSlot(38).getItem());
            if (result != ItemStack.EMPTY && !this.getSlot(39).hasItem()) {
                this.getSlot(36).getItem().shrink(1);
                this.getSlot(37).getItem().shrink(1);
                this.getSlot(38).getItem().shrink(1);
                this.getSlot(39).safeInsert(result);
            }
        }

        return true;
    }

    @Override
    public ItemStack quickMoveStack(Player player, int index) {
        var retStack = ItemStack.EMPTY;
        final Slot slot = getSlot(index);
        if (slot.hasItem()) {
            final ItemStack item = slot.getItem();
            retStack = item.copy();
            if (index < 27) {
                if (!moveItemStackTo(item, 27, this.slots.size(), true))
                    return ItemStack.EMPTY;
            } else if (!moveItemStackTo(item, 0, 27, false))
                return ItemStack.EMPTY;

            if (item.isEmpty()) {
                slot.set(ItemStack.EMPTY);
            } else {
                slot.setChanged();
            }
        }

        return retStack;
    }

    @Override
    public boolean stillValid(Player player) {
        return stillValid(this.containerAccess, player, BlockInit.WORKBENCH.get());
    }

    public static MenuConstructor getServerContainer(WorkbenchBlockEntity workbenchBlock, BlockPos pos) {
        return (id, playerInv, player) -> new WorkbenchContainer(id, playerInv, workbenchBlock.inventory, pos);
    }
}

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.