Jump to content

[1.12.2] Unable to Store my Items in the Item Handler Capability [SOLVED]


HalestormXV

Recommended Posts

If anyone can maybe take a look at this and see what I am royally fucking up I'd appreciate it, because I have been at it for hours. I just want to open my GUI lol. Here is the relevant pieces of code: 
https://pastebin.com/dLrL2JDu (The Item) 

 


Something in there has to be screwed up somewhere. I just can't see it maybe because I have been working too long on it. According to the crash it looks like it is crashing when it attempts to open up the container as opposed to the GUI. I see that it is throwing a null but I don't quite understand why? The item exists, you need to actually right-click it, the slots are there in the container? Maybe I am injecting the capability into the item wrong? Maybe I forgot to register it somewhere? (although it is a forge pre-made cap which I think automatically registers with the annotation?) The report seems to point to adding slots? Which also doesn't seem to make much sense.

 

So any help f would be greatly appreciated. I know there may be "inefficient" code in this but I am just trying to get this work at the moment and I can work with efficency later since this mod isn't released yet.

 

EDIT: And yes I know this could be the completly wrong way to do this and thats fine. The http://mcforge.readthedocs.io/en/latest/datastorage/capabilities/ is where I was bascially copying from. If it is completly wrong to do it this way I understand but I still would like to know the correct way to do this. I created a few capabilities already and I know I could do the same for this, but if I can use a built in capability (which I have not tried yet hence why I am trying this) then why not.

 

EDIT 2: Yeah apparently I did the whole capability thing wrong so I reworked the whole thing (I left the original pastebin link, but put a strikethrough) and here is the new version of the Item. I am stuck on how to just make sure it saves the entire inventory. Help is appreciated:

https://pastebin.com/uaSan6C0

 

EDIT 3: I fixed the crashing (I made the stupidest mistake in my Container code), now I just need to figure out how to get the items to save correctly to the ItemStack. Please help with that if possible. I spoilered out the other links to the other pieces of code since they are no longer relevant for this issue.

Edited by HalestormXV
Link to comment
Share on other sites

35 minutes ago, jhdoran said:

Creating a new ItemStackHandler in getCapability does not seem like a good idea.  Anytime someone wants to use the inventory, they get a brand new one.

Okay, so what would you suggest then. Like I said, this is a first time for me working with the pre-made Capabilities so I am shooting in the dark and just hoping for the best until I get and understand it.

Link to comment
Share on other sites

15 hours ago, diesieben07 said:
  • You cannot store the ItemStackHandler in your item class like that. Your Item instance is singleton-like, there is only one instance of it. This needs to go in your ICapabilityProvider that's returned from initCapabilities.
  • Why do you have both an ItemStackHandler and a List<ItemStack>? ItemStackHandler takes care of the entire inventory management, it's all you need.
  • You need to return the existing ItemStackHandler from getCapability.

- That was dumb, should have recognized it and have since changed it.

- I don't quite follow? Do you mean that ItemStackHelper isn't to be used at all? Everything is done with the ItemStackHandler ? So, I'd have to cycle through the 4 slots etc. and add the items like that? I originally put the List<ItemStack> because I thought that is what is required by ItemStackHelper. 

- Alright

 

https://hastebin.com/bikilixoca.java

 

So that is the updated code. Better? Note I left the List<ItemStack> in there because I know that is still the outstanding issue now and it is simply a place holder at this point.

Link to comment
Share on other sites

ItemStackHandler (not Helper, that is a different class) implements IItemHandler -- this is the Capability.  If you walk up to a cow and ask for an inventory capability, it will hand you an ItemStackHandler.  So this single ItemStackHandler is given to the cow early on (when it is constructed the capability will attach) in the form of a CapabilityProvider.

Link to comment
Share on other sites

Alright, so like above I created the ItemStackHandler in the constructor. So ItemStackHelper (which is what I have seen used in TileEntities) doesn't need to be used at all here to save the items. So then how do you save everything here: 

 

@Override
public void deserializeNBT(NBTTagCompound nbt)
{
    //Do your saving here
    if (nbt.hasKey("RunesStored"))
    {
        CapabilityItemHandler.ITEM_HANDLER_CAPABILITY.getStorage().readNBT(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, handler, null, nbt.getTag("RunesStored"));
    }
}

 

Do you need to put everything into a List/Field? Or into something like that. Or am I a complete idiot and just having that is all I need? I'm beginning to think I am just a total idiot becasue I am testing with this code now:https://hastebin.com/ajakojucic.java and it seems to be working just fine. And is that what you mean @diesieben07 when you were saying I literally only needed the handler?

Edited by HalestormXV
Link to comment
Share on other sites

4 hours ago, HalestormXV said:

And is that what you mean @diesieben07 when you were saying I literally only needed the handler?

Remove the List<ItemStack>

Make it gone.

You do not need it.

Take it out.

The ItemStackHandler contains its own List

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

4 hours ago, diesieben07 said:

I am failing to see any capability code there.

I placed the wrong link. https://pastebin.com/sE4jZ6TH

 

7 hours ago, Draco18s said:

Remove the List<ItemStack>

Make it gone.

You do not need it.

Take it out.

The ItemStackHandler contains its own List

- Did

- Done

- Got It

- Done

- Alright now I understand.

 

The code linked above in this post works just fine it seems. And to get this particular capability off this item I'd do:

stack.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);

right?

Edited by HalestormXV
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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Hello everyone, I'm making this post to seek help for my modded block, It's a special block called FrozenBlock supposed to take the place of an old block, then after a set amount of ticks, it's supposed to revert its Block State, Entity, data... to the old block like this :  The problem I have is that the system breaks when handling multi blocks (I tried some fix but none of them worked) :  The bug I have identified is that the function "setOldBlockFields" in the item's "setFrozenBlock" function gets called once for the 1st block of multiblock getting frozen (as it should), but gets called a second time BEFORE creating the first FrozenBlock with the data of the 1st block, hence giving the same data to the two FrozenBlock :   Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=head] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@73681674 BlockEntityData : id:"minecraft:bed",x:3,y:-60,z:-6} Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=3, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=2, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} here is the code inside my custom "freeze" item :    @Override     public @NotNull InteractionResult useOn(@NotNull UseOnContext pContext) {         if (!pContext.getLevel().isClientSide() && pContext.getHand() == InteractionHand.MAIN_HAND) {             BlockPos blockPos = pContext.getClickedPos();             BlockPos secondBlockPos = getMultiblockPos(blockPos, pContext.getLevel().getBlockState(blockPos));             if (secondBlockPos != null) {                 createFrozenBlock(pContext, secondBlockPos);             }             createFrozenBlock(pContext, blockPos);             return InteractionResult.SUCCESS;         }         return super.useOn(pContext);     }     public static void createFrozenBlock(UseOnContext pContext, BlockPos blockPos) {         BlockState oldState = pContext.getLevel().getBlockState(blockPos);         BlockEntity oldBlockEntity = oldState.hasBlockEntity() ? pContext.getLevel().getBlockEntity(blockPos) : null;         CompoundTag oldBlockEntityData = oldState.hasBlockEntity() ? oldBlockEntity.serializeNBT() : null;         if (oldBlockEntity != null) {             pContext.getLevel().removeBlockEntity(blockPos);         }         BlockState FrozenBlock = setFrozenBlock(oldState, oldBlockEntity, oldBlockEntityData);         pContext.getLevel().setBlockAndUpdate(blockPos, FrozenBlock);     }     public static BlockState setFrozenBlock(BlockState blockState, @Nullable BlockEntity blockEntity, @Nullable CompoundTag blockEntityData) {         BlockState FrozenBlock = BlockRegister.FROZEN_BLOCK.get().defaultBlockState();         ((FrozenBlock) FrozenBlock.getBlock()).setOldBlockFields(blockState, blockEntity, blockEntityData);         return FrozenBlock;     }  
    • It is an issue with quark - update it to this build: https://www.curseforge.com/minecraft/mc-mods/quark/files/3642325
    • Remove Instant Massive Structures Mod from your server     Add new crash-reports with sites like https://paste.ee/  
    • Update your drivers: https://www.amd.com/en/support/graphics/amd-radeon-r9-series/amd-radeon-r9-200-series/amd-radeon-r9-280x
  • Topics

×
×
  • Create New...

Important Information

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