Jump to content

Recommended Posts

Posted (edited)

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
Posted

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.

Posted (edited)

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
Posted
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.

Posted
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.

Posted
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.

Posted

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.

 

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

    • I hosted forge modded server using feather client I was able to join without any issues yesterday, but today after I tested my shader on my single world then tried to join the world but it made error meassage. (I also changed server.properties's render settings, but I reverted it as same as yesterday) So I removed my shader and removed optifine on server and on my mod file then it made this error: Internal Exception: io.netty.handler.codec.DecoderException: net.minecraft.ResourceLocationException: Non [a-z0-9/-1 character in path of location: inecraft:ask_server\u0012\u0001\uFFFD\n\u0007targets\u001D\u0001\u0014minecraft:ask_server\u0012\u0002\uFFFD\n\uFFFD\n\u0002id!\u0014minecraft:ask_server\u0002 \u0001\uFFFD\n\u0006target\u0006\u0001\u0002\u0001\uFFFD\n\ttarget My server/client is 1.20.1 forge. And I got 34 mods total, it was working pretty fine yesterday (I did not add/remove any mods before it started happening) I hope it's not about my worlds, it's been quite long since using this world I'm not native english speaker so there may be grammar issue! Thank you for reading!
    • I run a forge server with a bunch of mods that randomly started having extreme tick problems. I have no clue on how to or where to start reading this crash report, so some help would be greatly appreciated. I've tried changing max tick time to -1 in server.properties, and this did stop the server from crashing, but there's no point in playing as every action in-game takes several seconds to execute.   log: https://pastebin.com/UjQ6G5A4 crash report:  https://pastebin.com/RDZmpYMD
    • bruh this is silly, it wount let me delete.
    • So am trying to make a custom 1.19.2 modpack and everything works until I add Oculus. I have tried Oculus+Embedium and Oculus+Rubdium and by themselves they work but as soon as I add anything it crashes no matter what it is. The modpack works fine with just Embedium and Rubdium. Can you help me to see if this is something i can fix or do i just have to deal with not having shaders. Here is the crash log. Thank you for your time. https://paste.ee/p/WXfNZ24K
    • What do I do now when it says "1 error"?
  • Topics

×
×
  • Create New...

Important Information

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