Posted January 29, 20223 yr 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 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?
January 29, 20223 yr Author 12 minutes ago, Luis_ST said: you can use a ItemStackHandler How would i use the ItemStackHandler?
January 29, 20223 yr you can simply create a new instance and store it in a final field, then override addAdditionalSaveData and readAdditionalSaveData to serialize and deserialize the Inventory
January 29, 20223 yr Author Okay this seems to work but how do i open a screen with the player & entity inventory?
January 29, 20223 yr Hi! You can use player.openMenu and give it a new SimpleMenuProvider with your MenuConstructor and the name of the entity Sorry if my Posts are weird sometimes, I just try to help and learn as much as I can Also: PLEASE use SPOILERS for logs!
January 29, 20223 yr 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 Also: PLEASE use SPOILERS for logs!
January 29, 20223 yr 1 hour ago, Skyriis said: Okay this seems to work but how do i open a screen with the player & entity inventory? take a look into the AbstractHore class
January 29, 20223 yr Author 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?
January 29, 20223 yr Author 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()));
January 29, 20223 yr Author 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
January 31, 20223 yr Author I got another problem. I'm running into a IndexOutOfBoundsException when opening the container of my entity. Entity ItemStackHandler Entity Container Stacktrace Any ideas what's going on there?
January 31, 20223 yr Author 1 minute ago, diesieben07 said: https://github.com/Blutmond-Development/Skyblock/blob/ea804e25ad953e273c17d2db72babae819e6cff0/src/main/java/de/blutmondgilde/skyblock/container/MinionInventory.java#L27 Is this really what you want? Always adding 4 to whatever size is passed in? 🤔 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 January 31, 20223 yr by Skyriis
January 31, 20223 yr Author oh what. So every time the entity get's loaded -> it'll add 4 slots to the inventory? Well i guess i need to change that
January 31, 20223 yr Author I've uploaded a video how to trigger the exception but i got no idea how to fix that
February 12, 20223 yr Author i ran into a new problem on the same thing... shift-clicking a item in the player inventory results into a game freeze without any error message. any ideas what could cause such a behavior?
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.