Jump to content

GUI Copying Items to Other Slots


Hipposgrumm

Recommended Posts

I am trying to create a custom crafting table sort of thing, and I encountered this problem where if I put an item in one slot, it would copy it to the rest, same with removing items.

 

Here is the only logical culprit:

Spoiler
// Imports Ommitted

public class EggingTableGUI extends AbstractContainerMenu {
    public static final int RESULT_SLOT = 10;
    public static final int MAIN_INGREDIENT_SLOT = 0;
    private static final int CRAFT_SLOT_START = 1;
    private static final int CRAFT_SLOT_END = 9;
    private static final int INV_SLOT_START = 11;
    private static final int INV_SLOT_END = 38;
    private static final int USE_ROW_SLOT_START = 38;
    private static final int USE_ROW_SLOT_END = 47;
    private final CraftingContainer craftSlots = new CraftingContainer(this, 10, 1); // Maybe it's here
    private final ResultContainer resultSlot = new ResultContainer();
    private final ContainerLevelAccess access;
    private final Player player;

    public EggingTableGUI(int id, Inventory inv, net.minecraft.network.FriendlyByteBuf extraData) {this(id, inv, ContainerLevelAccess.NULL);}

    public EggingTableGUI(int id, Inventory inv, ContainerLevelAccess access) {
        super(GUIRegisterer.EGGING_GUI.get(), id);
        this.access = access;
        this.player = inv.player;
        addPlayerInventory(inv);
        addPlayerHotbar(inv);
        this.addSlot(new ResultSlot(inv.player, this.craftSlots, this.resultSlot, 0, 140, 40));
        this.addSlot(new Slot(this.craftSlots,0,96,40));
        for (int i=1;i<10;i++) { // Might be here
            for (int j=0;j<37;j+=18) {
                for (int l=0;l<37;l+=18) {
                    this.addSlot(new Slot(this.craftSlots, i, 16 + l, 22 + j));
                }
            }
        }
    }

    protected static void slotChangedCraftingGrid(AbstractContainerMenu menu, Level level, Player player, CraftingContainer craftslots, ResultContainer resultslot) { // Might be here
        if (!level.isClientSide) {
            ServerPlayer serverplayer = (ServerPlayer)player;
            ItemStack itemstack = ItemStack.EMPTY;
            Optional<CraftingRecipe> optional = level.getServer().getRecipeManager().getRecipeFor(RecipeType.CRAFTING, craftslots, level);
            if (optional.isPresent()) {
                CraftingRecipe craftingrecipe = optional.get();
                if (resultslot.setRecipeUsed(level, serverplayer, craftingrecipe)) {
                    itemstack = craftingrecipe.assemble(craftslots);
                }
            }

            resultslot.setItem(0, itemstack);
            menu.setRemoteSlot(0, itemstack);
            serverplayer.connection.send(new ClientboundContainerSetSlotPacket(menu.containerId, menu.incrementStateId(), 0, itemstack));
        }
    }

    public void slotsChanged(Container block) { // Might be here
        this.access.execute((level, pos) -> {
            slotChangedCraftingGrid(this, level, this.player, this.craftSlots, this.resultSlot);
        });
    }

    public void fillCraftSlotsStackedContents(StackedContents itemstack) {
        this.craftSlots.fillStackedContents(itemstack);
    }

    public void clearCraftingContent() {
        this.craftSlots.clearContent();
        this.resultSlot.clearContent();
    }

    public ItemStack quickMoveStack(Player player, int _slot) { // Might be here
        ItemStack itemstack = ItemStack.EMPTY;
        Slot slot = this.slots.get(_slot);
        if (slot != null && slot.hasItem()) {
            ItemStack itemstack1 = slot.getItem();
            itemstack = itemstack1.copy();
            if (_slot == 0) {
                this.access.execute((level, pos) -> {
                    itemstack1.getItem().onCraftedBy(itemstack1, level, player);
                });
                if (!this.moveItemStackTo(itemstack1, 10, 46, true)) {
                    return ItemStack.EMPTY;
                }

                slot.onQuickCraft(itemstack1, itemstack);
            } else if (_slot >= 10 && _slot < 46) {
                if (!this.moveItemStackTo(itemstack1, 1, 10, false)) {
                    if (_slot < 37) {
                        if (!this.moveItemStackTo(itemstack1, 37, 46, false)) {
                            return ItemStack.EMPTY;
                        }
                    } else if (!this.moveItemStackTo(itemstack1, 10, 37, false)) {
                        return ItemStack.EMPTY;
                    }
                }
            } else if (!this.moveItemStackTo(itemstack1, 10, 46, false)) {
                return ItemStack.EMPTY;
            }

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

            if (itemstack1.getCount() == itemstack.getCount()) {
                return ItemStack.EMPTY;
            }

            slot.onTake(player, itemstack1);
            if (_slot == 0) {
                player.drop(itemstack1, false);
            }
        }

        return itemstack;
    }

    public boolean canTakeItemForPickAll(ItemStack stack, Slot _slot) { // Probably not here but maybe
        return _slot.container != this.resultSlot && super.canTakeItemForPickAll(stack, _slot);
    }

    public void removed(Player player) {
        super.removed(player);
        this.access.execute((p_39371_, p_39372_) -> {
            this.clearContainer(player, this.craftSlots);
        });
    }

    public boolean stillValid(Player player) {
        return stillValid(this.access, player, Main.EGGING_TABLE.get());
    }

    public int getResultSlotIndex() {
        return RESULT_SLOT;
    }

    public int getGridWidth() {
        return this.craftSlots.getWidth();
    }
	
  	// This became redundant a while ago.
    public int getGridHeight() {
        return this.craftSlots.getHeight();
    }

    public int getSize() {
        return 11;
    }

    //public RecipeBookType getRecipeBookType() {return RecipeBookType.CRAFTING;}

    public boolean shouldMoveToInventory(int slot) {return slot != this.getResultSlotIndex();}

    private void addPlayerInventory(Inventory playerInventory) {
        for (int i = 0; i < 3; ++i) {
            for (int l = 0; l < 9; ++l) {
                this.addSlot(new Slot(playerInventory, l + i * 9 + 9, 8 + l * 18, 84 + i * 18));
            }
        }
    }

    private void addPlayerHotbar(Inventory playerInventory) {
        for (int i = 0; i < 9; ++i) {
            this.addSlot(new Slot(playerInventory, i, 8 + i * 18, 142));
        }
    }
}

 

 

I'm not good at modding, but at least I can read a crash report (well enough). That's something, right?

Link to comment
Share on other sites

This doesn't really look like a question for this forum.

It looks like you copied some random code from other places without understanding what it does.

Then when it didn't work, you decided to ask us to fix it for you.

We don't write your mod for you. Nor do we provide free code review.

 

Some observations:

       addPlayerInventory(inv); // slots 0 to 27 are the player inventory
        addPlayerHotbar(inv); // slots 28 to 36 are the hot bar
        // slot 37 is the result slot
        this.addSlot(new ResultSlot(inv.player, this.craftSlots, this.resultSlot, 0, 140, 40));
        // Slot 38 is the 0th crafting slot
        this.addSlot(new Slot(this.craftSlots,0,96,40));
        // this is a mess, you have 9 copies of 1 to 9 of the crafting slots so 9 x 9 = 81 or slots 39 to 119
        for (int i=1;i<10;i++) { // Might be here
            for (int j=0;j<37;j+=18) {
                for (int l=0;l<37;l+=18) {
                    this.addSlot(new Slot(this.craftSlots, i, 16 + l, 22 + j));
                }
// snip

            itemstack = itemstack1.copy();
            // Here you are treating slot 0 as the result slot, but your result slot is 37
            if (_slot == 0) {
                this.access.execute((level, pos) -> {
                    itemstack1.getItem().onCraftedBy(itemstack1, level, player);
                });
                // Here you are treating slots 10 to 45 as a group, but it doesn't match any group you defined in your constructor
                if (!this.moveItemStackTo(itemstack1, 10, 46, true)) {
                    return ItemStack.EMPTY;
                }

etc.

 

Edited by warjort

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Link to comment
Share on other sites

Quote

addPlayerInventory(inv); // slots 0 to 27 are the player inventory

There's an off-by-one in my calculations. 🙂 

The player inventory is slots 0 to 26 since it is 3x9 so everything else needs adjusting by one.

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

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.