TesterTesting135 Posted July 23, 2019 Share Posted July 23, 2019 (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 July 25, 2019 by TesterTesting135 Solved Quote Link to comment Share on other sites More sharing options...
TesterTesting135 Posted July 23, 2019 Author Share Posted July 23, 2019 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. Quote Link to comment Share on other sites More sharing options...
TesterTesting135 Posted July 24, 2019 Author Share Posted July 24, 2019 Ok, i will change the modid. The only problem is, i'm not sure how to use Intellij to refactor it for all my files. Also, where did I forget an instance of my string? Thanks. Quote Link to comment Share on other sites More sharing options...
TesterTesting135 Posted July 25, 2019 Author Share Posted July 25, 2019 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. Quote Link to comment Share on other sites More sharing options...
TesterTesting135 Posted July 25, 2019 Author Share Posted July 25, 2019 (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 July 25, 2019 by TesterTesting135 Constant Quote Link to comment Share on other sites More sharing options...
Draco18s Posted July 25, 2019 Share Posted July 25, 2019 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 Quote 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 More sharing options...
Draco18s Posted July 25, 2019 Share Posted July 25, 2019 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). Quote 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 More sharing options...
TesterTesting135 Posted July 25, 2019 Author Share Posted July 25, 2019 Ok, thanks for the tip. Any ideas why it's still not working? Thanks. Quote Link to comment Share on other sites More sharing options...
TesterTesting135 Posted July 25, 2019 Author Share Posted July 25, 2019 No, what should i return? Quote Link to comment Share on other sites More sharing options...
Draco18s Posted July 25, 2019 Share Posted July 25, 2019 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. Quote 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 More sharing options...
TesterTesting135 Posted July 25, 2019 Author Share Posted July 25, 2019 Why shouldn't i just return the super? Quote Link to comment Share on other sites More sharing options...
TesterTesting135 Posted July 25, 2019 Author Share Posted July 25, 2019 Ok, thank you Quote Link to comment Share on other sites More sharing options...
TesterTesting135 Posted July 25, 2019 Author Share Posted July 25, 2019 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. Quote Link to comment Share on other sites More sharing options...
TesterTesting135 Posted July 25, 2019 Author Share Posted July 25, 2019 Ok, so i fixed that, but I'm still getting "No Key". Quote Link to comment Share on other sites More sharing options...
TesterTesting135 Posted July 25, 2019 Author Share Posted July 25, 2019 Ok, woops. I didn't remove the first 2 statements. I removed them, and fixed the itemstack now. It works! Thank you so much! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.