Jump to content

Recommended Posts

Posted

I have a block with a Menu/Screen and a button.  In the Screen I handle button clicks with this call:

this.minecraft.gameMode.handleInventoryButtonClick((this.menu).containerId, 1);

which will call clickMenuButton in my Menu. I would like the button to perform its task only if a certain condition in the world is met (for example, there is a stone block below it). However, it seems that clickMenuButton is only called on the client side (debug prints only show as coming from the "Render thread" not the "Server thread") where the ContainerLevelAccess given to the Menu constructor is ContainerLevelAccess.NULL. If clickMenuButton is only called on the client side, how can I interact with the Level as part of a button press?

Note that I do not necessarily need to read the Level exactly when the button is pressed, so I do not mind storing whether the world condition is met in a variable and changing it when the block is changed, for example, as an alternate solution.

Posted

Thanks. The other bit I can't work out is that the server needs to know the BlockPos of my block (or some other identifying feature by which it could work that out). I know I could send that in the packet from the client to the server if I had it, but I'm not sure how the Menu can get the position of its block in the first place, again because it doesn't receive a ContainerLevelAccess. How do I find the BlockPos to send across in the packet, or am I taking the wrong approach here?

Posted

if your Menu is bind to a BlockEntity you can get the Position from it pass in the BlockPos into the server side constructor (the client need a dummy BlockPos),
when you then create your Menu in the BE you can use the position which is stored in it.
if you're not using a BlockEntity you need to do the same but set the position when you open the Menu by your self

Posted

Good to know, that is what I thought originally, so I was surprised to see it not show up on the server side.  Your reply got me looking in the right place, so while I was copying the code over to show you, I spotted the error, and I have now solved the issue. If my ContainerLevelAccess evaluation failed, I was returning false (as I do when it fails in other ways, e.g. the slots are empty). However, I should have been returning true, because "false" is telling the client to short-circuit and not bother sending the packet to the server as it has already determined the button will fail. After returning true here, handleInventoryButtonClick actually gets called, so the packet is sent to the server and it works correctly.

I include the code below anyway, in case it helps anyone, or in case someone wants to tell me I'm doing something else horribly wrong.

The Screen:

    @Override
    public boolean mouseClicked(double x, double y, int p_98760_) {
        int xstart = (this.width - this.imageWidth) / 2;
        int ystart = (this.height - this.imageHeight) / 2;

        double xl = x - (double)(xstart + 126);
        double yl = y - (double)(ystart + 7);
        double yl1 = y - (double)(ystart + 61);
        if (xl >= 0.0D && yl >= 0.0D && xl < 36D && yl < 18.0D) {
        // This conditional lets the client side decide not to bother sending the packet to the server if it does not need it
            if (this.menu.clickMenuButton(this.minecraft.player, 1)) {  
                this.minecraft.gameMode.handleInventoryButtonClick((this.menu).containerId, 1);
                return true;
            }
        }
        // 2nd button ...

        return super.mouseClicked(x, y, p_98760_);
    }

The Menu:

    public HarmoniserMenu(int id, Inventory inv) {
        this(id, inv, ContainerLevelAccess.NULL);
        Main.LOGGER.info("Creating harmoniser menu with null level access");  // I see this called from the render thread
    }

    public HarmoniserMenu(int id, Inventory inv, ContainerLevelAccess access) {
        super(ModMenus.HARMONISER.get(), id);
        Main.LOGGER.info("Created harmoniser menu"); // I see this called from both the render and the server thread
        this.access = access;
	// ... slots etc ...
    }

    private int testEval(Level l, BlockPos p) {
        Main.LOGGER.info("Evaluation successful at " + p);  // Never called
        return 0;
    }

    @Override
    public boolean clickMenuButton(Player player, int button) {
        Main.LOGGER.info("Clicked button " + button);  // Shows "Clicked button 1" on Render thread only (!!)
        if(button == 1) { // Debug button
            access.evaluate(this::testEval);
            return false;   // This line was the problem: evaluation fails on client side, but should return true to do on server side
        }
		// ... other button etc	...
    }

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.