Jump to content

[1.19.2] Pass data from Menu to Screen


Sweetmimike

Recommended Posts

Hello !

 

I'm in trouble with passing data from a ContainerMenu class to ContainerScreen class. Indeed as you can see just below, I'm using the function "getProgressed()" but this function seems to not having access to this.data as it returns always the same thing : 0. But if I call "this.data.get(0)" in the overrided function "stillValid" from the same class, it returns the correct integer.

 

public class MobFarmMenu extends AbstractContainerMenu {

    /** Clicked entity */
    private final IronMobFarmEntity farmEntity;

    /** Current level */
    private final Level level;

    private final ContainerData data;

    public MobFarmMenu(int pContainerId, Inventory inv, FriendlyByteBuf extraData) {
        this(pContainerId, inv, inv.player.level.getBlockEntity(extraData.readBlockPos()), new SimpleContainerData(2));
    }

    public MobFarmMenu(int pContainerId, Inventory inv, BlockEntity entity, ContainerData data) {
        super(MenuManager.MOB_FARM_MENU.get(), pContainerId);
        checkContainerSize(inv, 1);
        farmEntity = ((IronMobFarmEntity) entity);
        this.level = inv.player.level;
        this.data = data;
        System.out.println("IN CONSTRUCTOR " + data.get(1));

        addPlayerInventory(inv);
        addPlayerHotbar(inv);

        this.farmEntity.getCapability(ForgeCapabilities.ITEM_HANDLER).ifPresent(handler -> {
            this.addSlot(new MobShardSlot(handler, 0, 80, 20));
        });
    }

    public int getProgress() {
        int current = this.data.get(0);
        int max = this.data.get(1);
        int progressBarSize = 41;
       // System.out.println("current : " + current + " || max : " + max  + "|| data" + this.data.getCount());
        System.out.println("DATA " + this.data.get(1));

        return max != 0 ? current * progressBarSize / max : 0;
    }

    // CREDIT GOES TO: diesieben07 | https://github.com/diesieben07/SevenCommons
    // must assign a slot number to each of the slots used by the GUI.
    // For this container, we can see both the tile inventory's slots as well as the player inventory slots and the hotbar.
    // Each time we add a Slot to the container, it automatically increases the slotIndex, which means
    //  0 - 8 = hotbar slots (which will map to the InventoryPlayer slot numbers 0 - 8)
    //  9 - 35 = player inventory slots (which map to the InventoryPlayer slot numbers 9 - 35)
    //  36 - 44 = TileInventory slots, which map to our TileEntity slot numbers 0 - 8)
    private static final int HOTBAR_SLOT_COUNT = 9;
    private static final int PLAYER_INVENTORY_ROW_COUNT = 3;
    private static final int PLAYER_INVENTORY_COLUMN_COUNT = 9;
    private static final int PLAYER_INVENTORY_SLOT_COUNT = PLAYER_INVENTORY_COLUMN_COUNT * PLAYER_INVENTORY_ROW_COUNT;
    private static final int VANILLA_SLOT_COUNT = HOTBAR_SLOT_COUNT + PLAYER_INVENTORY_SLOT_COUNT;
    private static final int VANILLA_FIRST_SLOT_INDEX = 0;
    private static final int TE_INVENTORY_FIRST_SLOT_INDEX = VANILLA_FIRST_SLOT_INDEX + VANILLA_SLOT_COUNT;

    // THIS YOU HAVE TO DEFINE!
    private static final int TE_INVENTORY_SLOT_COUNT = 1;  // must be the number of slots you have!

    @Override
    public ItemStack quickMoveStack(Player playerIn, int index) {
        Slot sourceSlot = slots.get(index);
        if (sourceSlot == null || !sourceSlot.hasItem()) return ItemStack.EMPTY;  //EMPTY_ITEM
        ItemStack sourceStack = sourceSlot.getItem();
        ItemStack copyOfSourceStack = sourceStack.copy();

        // Check if the slot clicked is one of the vanilla container slots
        if (index < VANILLA_FIRST_SLOT_INDEX + VANILLA_SLOT_COUNT) {
            // This is a vanilla container slot so merge the stack into the tile inventory
            if (!moveItemStackTo(sourceStack, TE_INVENTORY_FIRST_SLOT_INDEX, TE_INVENTORY_FIRST_SLOT_INDEX
                    + TE_INVENTORY_SLOT_COUNT, false)) {
                return ItemStack.EMPTY;  // EMPTY_ITEM
            }
        } else if (index < TE_INVENTORY_FIRST_SLOT_INDEX + TE_INVENTORY_SLOT_COUNT) {
            // This is a TE slot so merge the stack into the players inventory
            if (!moveItemStackTo(sourceStack, VANILLA_FIRST_SLOT_INDEX, VANILLA_FIRST_SLOT_INDEX + VANILLA_SLOT_COUNT, false)) {
                return ItemStack.EMPTY;
            }
        } else {
            System.out.println("Invalid slotIndex:" + index);
            return ItemStack.EMPTY;
        }
        // If stack size == 0 (the entire stack was moved) set slot contents to null
        if (sourceStack.getCount() == 0) {
            sourceSlot.set(ItemStack.EMPTY);
        } else {
            sourceSlot.setChanged();
        }
        sourceSlot.onTake(playerIn, sourceStack);
        return copyOfSourceStack;
    }

    @Override
    public boolean stillValid(Player pPlayer) {
        System.out.println("DATA1 " + this.data.get(1));
//        return stillValid(ContainerLevelAccess.create(level, farmEntity.getBlockPos()),
//                pPlayer, BlockManager.IRON_MOB_FARM.get());
        if (!(this.level.getBlockEntity(this.farmEntity.getBlockPos()).getBlockState().getBlock() instanceof IronMobFarm)) {
            return false;
        } else {
            return !(pPlayer.distanceToSqr((double) this.farmEntity.getBlockPos().getX() + 0.5D, (double) this.farmEntity.getBlockPos().getY() + 0.5D, (double) this.farmEntity.getBlockPos().getZ() + 0.5D) > 64.0D);
        }
    }

    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, 51 + i * 18));
            }
        }
    }

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

 

I don't get why the function "getProgressed" can't have access to the right values of to "this.data".

 

 

Link to comment
Share on other sites

You will need to use a DataSlot to actually pass the ContainerData over to the client.
You can do this by just adding this line 

        this.addDataSlots(data);

to the constructor of your menu.

I don't think you need to change anything else.

  • Like 1
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.