Jump to content

Help opening Container to Player


Big_Bad_E

Recommended Posts

I just need some clarification with opening a container to the player.

I am trying to make a custom Crafting Table, I registered all my custom recipes in a custom Registry, but now I need to open the Container.

Currently I am calling this:

@Override
    public boolean onBlockActivated(BlockState state, World worldIn, BlockPos pos, PlayerEntity player, Hand handIn, BlockRayTraceResult hit) {
        BlockPos playerPos = player.getPosition();
        double distance = pos.distanceSq(playerPos);
        if(player.isCreative() && distance <= 5 || !player.isCreative() && distance <= 3) {
            ServerPlayerEntity playerEntity = null;
            if (!worldIn.isRemote) {
                for (ServerPlayerEntity entity : worldIn.getServer().getPlayerList().getPlayers())
                    if (entity.getUniqueID().equals(player.getUniqueID())) {
                        playerEntity = entity;
                        break;
                    }
                NetworkHooks.openGui(playerEntity, new CulinaryWorkbenchContainerProvider(pos));
            }
            return false;
        }
        return true;
    }

This goes to my CulinaryWorkbenchContainerProvider:

public class CulinaryWorkbenchContainerProvider implements INamedContainerProvider {
    private BlockPos pos;

    public CulinaryWorkbenchContainerProvider(BlockPos pos) {
        this.pos = pos;
    }

    @Override
    public ITextComponent getDisplayName() {
        return ITextComponent.Serializer.fromJson("culinaryworkbench.name");
    }

    @Nullable
    @Override
    public Container createMenu(int windowID, PlayerInventory playerInventory, PlayerEntity playerEntity) {
        return new CulinaryWorkbenchContainer(windowID, playerInventory, new PacketBuffer(Unpooled.buffer(8, 8)).writeBlockPos(pos));
    }
}

And then my Container:

public CulinaryWorkbenchContainer(int windowId, PlayerInventory playerInventory, PacketBuffer extraData) {
    super(ContainerType.CRAFTING, windowId);
}

Is this the correct usage?

 

It seems that MinecraftServer#getPlayerList() is returning null, how else can I get the ServerPlayerEntity instance?

Edited by Big_Bad_E
Link to comment
Share on other sites

6 hours ago, diesieben07 said:

Why...? What even?

You already have the player as a parameter...

 

Assuming you are in 1.14.2 (you didn't specify) you need to register a IScreenFactory for your ContainerType using ScreenManager.registerFactory.

I am in 1.14.2, thanks for telling me this.

 

Yes, I know the code is stupid, but what gets passed to me is a PlayerEntity, and I need a ServerPlayerEntity for NetworkHooks#openGui(). 

 

I just realized I forgot !world.isRemote when I did that, my problem is fixed now by casting player to ServerPlayerEntity server-side.

Edited by Big_Bad_E
Link to comment
Share on other sites

after doing an instanceof check I hope

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

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.