Jump to content

[1.18.1] Creating an Entity with its own inventory


Skyriis

Recommended Posts

Hey Guys,

i'm trying to create an entity which has it's own inventory and a openable container.

The entity breaks blocks and put the drops into the inventory.

Upgrades and food impact the breaking speed and the drops from the entity.

The container should look like this:

Spoiler

Screenshot-2022-01-29-131207.png

red -> Player Inventory

Blue -> Entity Inventory (Players can only take items from it but can't put items into it)

Brown -> Entity Food Slot (only items with a `minion_food` tag)

Yellow -> Special Upgrade Slot (only items with a `minon_special_upgrade` tag)

Green -> Upgrade Slot (only items with a `minon_upgrade` tag)

I already created an Entity which can break and place blocks but i got no idea how to add such an inventory / container to my entity.

 

How can i create such an container / inventory?

Link to comment
Share on other sites

If you don't have a MenuConstructor you can make a new one and give it a ChestMenu with your Items in the createMenu function, but this only work if you have an Inventory with the same slots as a MenuType (3*3 Slots, 9*1 Slots, 9*2 Slots, 9*3 Slots ... 9*6 Slots)

Sorry if my Posts are weird sometimes, I just try to help and learn as much as I can :D

Also: PLEASE use SPOILERS for logs!

Link to comment
Share on other sites

I got the container class:

public class MinerMinionMenu extends AbstractContainerMenu {
    private final InvWrapper playerInvWrapper;
    private final ItemStackHandler minionInventory;
    private final MinerEntity minerEntity;

    public MinerMinionMenu(int pContainerId, Inventory inventory, MinerEntity minerEntity) {
        super(SkyblockRegistries.container.minerMinionMenu.get(), pContainerId);
        this.playerInvWrapper = new InvWrapper(inventory);
        this.minionInventory = minerEntity.getInventory();
        this.minerEntity = minerEntity;
    }

    @Override
    public boolean stillValid(Player pPlayer) {
        return false;
    }
}

and register it using

    public static final RegistryObject<MenuType<MinerMinionMenu>> minerMinionMenu = registry.register("miner_minion_menu", () -> IForgeMenuType.create(
        (windowId, inv, data) -> {
            Entity entity = Skyblock.getProxy().getLevel().getEntity(data.readInt());

            if (entity instanceof MinerEntity minerEntity) {
                return new MinerMinionMenu(windowId, inv, minerEntity);
            } else {
                throw new IllegalStateException("Tried to open a miner minion inventory with a non miner minion entity!");
            }
        }));

but how do i provide the extra data (the entity Id) to open the container?

Link to comment
Share on other sites

3 minutes ago, diesieben07 said:

You already do exactly that in your IForgeMenuType

but when i try to open the menu i'll get a NPE because `data` is null. I'm trying to open the menu using:

        pPlayer.openMenu(new SimpleMenuProvider((pContainerId, pInventory, pPlayer1) -> {

            return SkyblockRegistries.container.minerMinionMenu.get().create(pContainerId, pPlayer1.getInventory());
        }, getCustomName()));

 

 

Link to comment
Share on other sites

4 minutes ago, diesieben07 said:

You must use NetworkHooks.openGui

Okay i'm using 

        if (pPlayer instanceof ServerPlayer serverPlayer) {
            NetworkHooks.openGui(serverPlayer, new SimpleMenuProvider((pContainerId, pInventory, pPlayer1) -> {
                return SkyblockRegistries.container.minerMinionMenu.get().create(pContainerId, pPlayer1.getInventory());
            }, getCustomName()), buf -> buf.writeInt(getId()));
        }

in my entity class now but i'm still running into the NPE

Link to comment
Share on other sites

1 minute ago, diesieben07 said:

Those 4 slots are used for special items which are present in all types of my entity.

I thought it's easier to always add them to the amount of slots, the entity has as inventory space instead of always remembering to add them.

Edited by Skyriis
Link to comment
Share on other sites

  • 2 weeks later...

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.



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.