Jump to content

[1.14.4] (What happened to itemstack NBT tags) - Problems with Capabilites + Durability Bar


Cerandior

Recommended Posts

It's been a while since I have worked with NBT Data, but it seems like none of the methods that existed before related to nbt exist now.

 

For starters, there doesn't seem to be a "onUpdate" method for items right now. That's where I used to create the tag.So basically, I can't even figure out how to give items tags let alone do anything with it. Also, it seems like there are two types of tags right now?! Looking through some classes, I found mentions of shareTags and normal Tags. What is their difference?

Could someone point me in the right direction. How do you setup tag compounds for items in 1.14?
Thank you in advance.

Edited by Cerandior
Link to comment
Share on other sites

Maybe look at this? [LINK]

  • Like 1

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

You should be using Capabilities now.

  • Like 1

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

On 11/29/2019 at 10:15 PM, Draco18s said:

You should be using Capabilities now.

Those docs are a little outdated. Things were a little different for me for 1.14.

I managed to get it working, but I don't understand some of the things I did to get it working.

What exactly is LazyOptional? That is so confusing. Why do I have to return an instance of LazyOptional instead of an instance of my Capability when I try to get the capability?

I used the .orElse(null) (which seems to return an instance of my Capability automatically?) method to get the capability after looking at the class of LazyOptional, however I am not sure if that's how you are supposed to do it. I must clarify. It did work without errors, but I don't know if it is the correct way of doing things.

 

Apart from having a little trouble understanding capabilities, I can't seem to get the durability bar to display correctly. I am trying to give a certain item a cooldown after usage. I implemented this through capabilities and the cooldown bit seems to work, however I want to use the durability bar to display the "time left" before next use, and the durability bar is not being rendered at all. It probably has to do with showDurabilityBar method, but I am not sure.

Everything related to my capabilities you can find here: https://github.com/Cerandior/VanillaExtended/tree/master/src/main/java/teabx/vanillaextended/capabilities

 

And here is the item in question: https://github.com/Cerandior/VanillaExtended/blob/master/src/main/java/teabx/vanillaextended/items/LordStaff.java

 

Thank you for your help.

Edited by Cerandior
Link to comment
Share on other sites

3 hours ago, Cerandior said:

What exactly is LazyOptional? That is so confusing. Why do I have to return an instance of LazyOptional instead of an instance of my Capability when I try to get the capability?

A LazyOptional allows you to create the actual Capability when it is needed (but the LazyOptional can exist before then!) For most use-cases, you'll create them both at the same time.

3 hours ago, Cerandior said:

I used the .orElse(null) (which seems to return an instance of my Capability automatically?)

It will, if your capability was already created. You can also use ifPresent() which takes a lambda expression in the form of x=>{ /* your code here*/ } where x is your capability.

 

  • Thanks 1

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

52 minutes ago, diesieben07 said:

LazyOptional is exactly what it's name suggests: It is an optional value (the requested capability or nothing if the capability is not provided by the queried capability provider) which is lazily computed (only once you ask for it's value is that value actually computed).

Don't want to be a nuisance, but I am having trouble with that durability bar. It is not being updated correctly. Is it because there is a lack of communication between the server and the client at the moment? (I haven't setup packets yet).

Here is the class of the item: https://github.com/Cerandior/VanillaExtended/blob/master/src/main/java/teabx/vanillaextended/items/LordStaff.java

Link to comment
Share on other sites

1 hour ago, diesieben07 said:

Capability NBT is not sent to the client by default. You have to override getShareTag and readShareTag in your Item class.

I am still not getting any data in the client even when I use the itemstack.getShareTag. I am little confused, how am I supposed to implement this?

Link to comment
Share on other sites

7 minutes ago, diesieben07 said:

Show what you tried.

    @Nullable
    @Override
    public CompoundNBT getShareTag(ItemStack stack) {
        ICoolDownItem cap = stack.getCapability(CapabilityRegistry.COOLDOWN_ITEM, null).orElse(null);
        CompoundNBT nbt = new CompoundNBT();
        nbt.putInt("cooldown", cap.getCooldown());
        nbt.putInt("maxcooldown", cap.getMaxCooldown());
        return nbt;
    }

    @Override
    public void readShareTag(ItemStack stack, @Nullable CompoundNBT nbt) { }

    @Override
    public boolean showDurabilityBar(ItemStack stack) {
        return true;
    }

    @Override
    public double getDurabilityForDisplay(ItemStack stack) {
        return 1-(stack.getShareTag().getInt("cooldown")/stack.getShareTag().getInt("maxcooldown"));
    }


I feel like I am doing the same thing I was doing before with extra steps. I think the problem is at the getShareTag, but I am not sure.

Link to comment
Share on other sites

10 minutes ago, diesieben07 said:

Well... your readShareTag does... nothing. What did you expect?

 

Also, why are you using orElse(null), but then blindly calling methods on the result? If you expect the capability to always be there, use orElseThrow with a meaningful error message instead of failing with a meaningless, random NullPointerException.

I was doing something before, but I got rid of it before posting it here. I was using the nbt data to update the values in the capability but that didn't work. What am I supposed to do with the data I have stored in the nbt tag at the readShareTag?

About the second thing. I just stuck at the first thing that didn't give me any errors, did not think it through. Will change them, thank you.

Link to comment
Share on other sites

2 minutes ago, diesieben07 said:

This:

Show what you tried.

@Override
public void readShareTag(ItemStack stack, @Nullable CompoundNBT nbt) {
    if(nbt != null){
        ICoolDownItem cap = stack.getCapability(CapabilityRegistry.COOLDOWN_ITEM, null).orElseThrow(null);
        cap.setCooldown(nbt.getInt("cooldown"));
    }
}
Link to comment
Share on other sites

Just now, diesieben07 said:

That should be impossible, because it is called every time an ItemStack of your item is received on the client.

 

I put the breakpoint at it, and intelliJ didn't pause the debugging nor show any data below.

This is the class of my item: https://github.com/Cerandior/VanillaExtended/blob/master/src/main/java/teabx/vanillaextended/items/LordStaff.java

in case I have done anything blatantly stupid.

Link to comment
Share on other sites

2 minutes ago, diesieben07 said:

Apparently this changed in 1.14.4 and it's now completely impossible to sync the capability data of itemstacks, because the share tag is ignored except for when the client takes stacks from the creative menu.

 

So... nothing you can do.

When you say completely impossible, you mean like I can't do anything at all about it? Even with packets? That is kind of disappointing. When is the next version of forge planned for release? 

Edited by Cerandior
Link to comment
Share on other sites

11 minutes ago, diesieben07 said:

I am stupid and didn't even read the code right...

 

It's that getShareTag is (basically) always used when writing stacks and readShareTag is always used when reading them.

 

It should be impossible for you to see readShareTag not being called.

 

I recorded this quick gif. Am I setting the breakpoint right?

 

 

Edited by Cerandior
Link to comment
Share on other sites

8 minutes ago, diesieben07 said:

I mean... it looks fine?

Well I got everything working except for the representation of the cooldown through the durability bar.
The breakpoint is set correctly and readNBT is not called. I don't really know what else I can do from here. Is there any chance that I have screwed up the item in some other way that does not allow it to have a shared tag?

The github link I provided before contains all the files of the project I am currently working on. When you got the time maybe you can look at it . At the moment, I can't ask you to do anything more, I have spent enough of your time already.

I can't think of anything else to do myself. Anyway, thank you for your help.

Edited by Cerandior
Link to comment
Share on other sites

4 minutes ago, diesieben07 said:

There is literally one way for getShareTag not to be called: You have overridden shouldSyncTag to return false.

Considering you haven't done that: I'm guessing something about your setup is broken so that the breakpoint doesnt trigger even though the method is called.

I even put some System.out.println (which is a stupid way to debug, I know) in both methods, but nothing shows up in the console.

Link to comment
Share on other sites

6 hours ago, diesieben07 said:

That is ancient...

Please update.

 

I just moved over to Build: 1.14.4-28.1.96. Yet the same problem persists. Can anybody else recreate the same thing in this version of forge? Or has anything similar to this been reported before?

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

    • Update your AMD/ATI drivers - get the drivers from their website - do not update via system  
    • As the title says i keep on crashing on forge 1.20.1 even without any mods downloaded, i have the latest drivers (nvidia) and vanilla minecraft works perfectly fine for me logs: https://pastebin.com/5UR01yG9
    • 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
  • Topics

×
×
  • Create New...

Important Information

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