Jump to content

[SOLVED] Help with NBTs


TesterTesting135

Recommended Posts

Hello, so I'm trying to make a sword that freezes entities on right click, but i cannot get the toggle working. There is probably a very easy solution to this, but I cannot for the life of me figure it out. Here is the right click method:

@Override
public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand handIn) {
    playerIn.setActiveHand(handIn);
    ItemStack stack = playerIn.getActiveItemStack();
    NBTTagCompound nbt;
    if (stack.hasTagCompound()) {
        nbt = stack.getTagCompound();
    } else {
        nbt = new NBTTagCompound();
    }
    if (nbt.hasKey("freeze"))
    {
        nbt.setBoolean("freeze", !nbt.getBoolean("freeze"));
    }
    FreezeTime(playerIn, worldIn, nbt.getBoolean("freeze"), 20);
    stack.setTagCompound(nbt);
    return super.onItemRightClick(worldIn, playerIn, handIn);
}

For some reason, instead of toggling "freeze" between true and false, it just stays at false. This code looks like it should work to me, but it doesn't. Please forgive me if this is a stupid mistake.

Thanks in advance.

Edited by TesterTesting135
Solved
Link to comment
Share on other sites

Ok, thanks for the reply. I added this:

@Override
public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand handIn) {
    playerIn.setActiveHand(handIn);
    ItemStack stack = playerIn.getActiveItemStack();
    NBTTagCompound nbt;
    if (stack.hasTagCompound())
    {
        nbt = stack.getTagCompound();
    }
    else
    {
        nbt = new NBTTagCompound();
    }

    if (nbt.hasKey("tmfreeze"))
    {
        nbt.setBoolean("tmfreeze", !nbt.getBoolean("tmfreeze"));
    }
    else
    {
        nbt.setBoolean("tmfreeze", false);
    }
    FreezeTime(playerIn, worldIn, nbt.getBoolean("freeze"), 20);
    stack.setTagCompound(nbt);
    return super.onItemRightClick(worldIn, playerIn, handIn);
}

(The else statement), but it would always set it to false. Any idea why?

Thanks.

Link to comment
Share on other sites

Ok, so i changed the string but for some reason, the hasKey method isn't working. Whenever i right click, it always says it's false. I added a debug line to it, saying "No Key" in chat if there wasn't a key. Here is my code:

public static final String FREEZE_KEY = "tmfreeze";
@Override
public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand handIn) {

    playerIn.setActiveHand(handIn);
    ItemStack stack = playerIn.getActiveItemStack();
    NBTTagCompound nbt;
    if (stack.hasTagCompound())
    {
        nbt = stack.getTagCompound();
    }
    else
    {
        nbt = new NBTTagCompound();
    }

    if (nbt.hasKey(FREEZE_KEY))
    {
        nbt.setBoolean(FREEZE_KEY, !nbt.getBoolean(FREEZE_KEY));
    }
    else
    {
        nbt.setBoolean(FREEZE_KEY, false);
        playerIn.sendMessage(new TextComponentString("No Key"));
    }
    FreezeTime(playerIn, nbt.getBoolean(FREEZE_KEY), ConfigHandler.SWORD_OF_INEVITIBILITY_FREEZE_RANGE);
    return super.onItemRightClick(worldIn, playerIn, handIn);
}

Why isn't this working? Thanks.

Edited by TesterTesting135
Constant
Link to comment
Share on other sites

36 minutes ago, TesterTesting135 said:

Oh wow! I did not notice that! Thank you so much!

As for the mod id, i actually do have a constant, but in the recipe json files and blockstate files you can't really use a constant.

Notepad++
Find Replace In Files

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

1 hour ago, diesieben07 said:

Or every IDE does this... At least I cannot imagine Eclipse not having it and IntelliJ definitely has it.

 

Also, don't return the result of super. Also, only do this stuff on the server.

Eclipse can too, I've just gotten used to using Notepadd++'s file search for all kinds of things (not just Minecraft/Java).

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

2 minutes ago, TesterTesting135 said:

No, what should i return?

Look at what super returns, copy it, change it to return a slightly different value.

I'd say "you want it to return SUCCESS" but the actual object is more complicated than that. The super method returns PASS.

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

Ok, so here is my code now:

public static final String FREEZE_KEY = "tmfreeze";

@Override
public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand handIn) {

    playerIn.setActiveHand(handIn);
    ItemStack stack = playerIn.getActiveItemStack();
    NBTTagCompound nbt;
    if (!worldIn.isRemote) {
        if (stack.hasTagCompound()) {
            nbt = stack.getTagCompound();
        } else {
            nbt = new NBTTagCompound();
        }
        if (nbt.hasKey(FREEZE_KEY)) {
            nbt.setBoolean(FREEZE_KEY, !nbt.getBoolean(FREEZE_KEY));
        } else {
            nbt.setBoolean(FREEZE_KEY, true);
            playerIn.sendMessage(new TextComponentString("No Key"));
        }
        stack.setTagCompound(nbt);
        FreezeTime(playerIn, nbt.getBoolean(FREEZE_KEY), ConfigHandler.SWORD_OF_INEVITIBILITY_FREEZE_RANGE);
        return new ActionResult<>(EnumActionResult.SUCCESS, playerIn.getHeldItem(handIn));
    }
    return super.onItemRightClick(worldIn, playerIn, handIn);
}

but it's still always true.

 

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.