Jump to content

[1.14.4] How to open crafting GUI on custom block right click?


Xander402

Recommended Posts

I tried the following:

    @Override
    public boolean onBlockActivated(BlockState state, World world, BlockPos pos,
            PlayerEntity player, Hand hand, BlockRayTraceResult result) {
        player.openContainer(state.getContainer(world, pos));
        return true;
    }

    @Override
    public INamedContainerProvider getContainer(BlockState state, World world, BlockPos pos) {
        return new SimpleNamedContainerProvider((i, playerInventory, playerEntity)
                -> new WorkbenchContainer(0, playerInventory), TEXT_COMPONENT);
    }

This opens a crafting GUI, but does some mess in the inventory when opening - some items disappear, some get moved to other slot or duplicated, and everything returns back after leaving the GUI and clicking on any item in the inventory. What am I doing wrong?

A few bytes about me:

public class Xander402 extends Modder implements IForumMember {
    int javaExperience, moddingExperience;
    LearningWay preferredLearningWay;
    public Xander402() {
        this.javaExperience = BIG;
        this.moddingExperience = NOT_SO_BIG;
        this.preferredLearningWay = LearningWay.through("exampes");
      	super(/*displayName*/"Xander402", /*moddingSince*/"1.9", preferredLearningWay);
    }
    @Override
    public Goal getReasonOfJoining(Forum forum) { return new Goal(() -> { while (true) moddingExperience++; }); }
}

 

Link to comment
Share on other sites

Since I need my own implementation of crafting container, I should be able to set which items can be used in crafting with this block, right?
I tried to initiate the crafting matrix like this (the only difference between this and the vanilla class is the slot class name):

        for (i = 0; i < 3; ++i) {
            for (j = 0; j < 3; ++j) {
                this.addSlot(new FoodSlot(this.inventory, j + i * 3,
                        30 + j * 18, 17 + i * 18));
            }
        }

But the game actually doesn't reject any item even if the FoodSlot class looks like this:

package ...

import ...

public class FoodSlot extends Slot {
    public FoodSlot(IInventory inventory, int p1, int p2, int p3) {
        super(inventory, p1, p2, p3);
    }

    @Override
    public boolean isItemValid(ItemStack item) {
        return false;
    }
}

Instead, it moves the entire content of the matrix back to the inventory after several clicks.

A few bytes about me:

public class Xander402 extends Modder implements IForumMember {
    int javaExperience, moddingExperience;
    LearningWay preferredLearningWay;
    public Xander402() {
        this.javaExperience = BIG;
        this.moddingExperience = NOT_SO_BIG;
        this.preferredLearningWay = LearningWay.through("exampes");
      	super(/*displayName*/"Xander402", /*moddingSince*/"1.9", preferredLearningWay);
    }
    @Override
    public Goal getReasonOfJoining(Forum forum) { return new Goal(() -> { while (true) moddingExperience++; }); }
}

 

Link to comment
Share on other sites

Block:
https://pastebin.com/uHCFPFVq

Container:
https://pastebin.com/PBNvPkbR

Edited by Xander402

A few bytes about me:

public class Xander402 extends Modder implements IForumMember {
    int javaExperience, moddingExperience;
    LearningWay preferredLearningWay;
    public Xander402() {
        this.javaExperience = BIG;
        this.moddingExperience = NOT_SO_BIG;
        this.preferredLearningWay = LearningWay.through("exampes");
      	super(/*displayName*/"Xander402", /*moddingSince*/"1.9", preferredLearningWay);
    }
    @Override
    public Goal getReasonOfJoining(Forum forum) { return new Goal(() -> { while (true) moddingExperience++; }); }
}

 

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.