Declaring Global Variables

Started by sawntoe on

Topic category: Help with modding (Java Edition)

Last seen on 14:58, 31. Dec 2022
Joined Dec 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Declaring Global Variables

Hey! I'm writing a Fabric 1.19.2 mod that disables durability. My current code iterates through all player's inventories and removes damage once a tick (this is the only way I found to do this, as the Fabric generator lacks support for many triggers). However, this is *really really* laggy, and I want to do it less often, like once every 5 seconds or so.

To do this, I tried creating a global variable in the variables tab, but that didn't work because I'm coding in Java.

I did my research and I know that I have to create a class with globals with a public static type, but how would I do this? Thanks!

(Footnote: I have read https://mcreator.net/forum/90685/tutorial-how-use-global-variables-overlayjava-files, but I have absolutely no idea what the post writer was babbling about. However, it does seem like it is the solution to my problem.)

 

(I have very little experience with Java and only know the basics)

Last seen on 04:04, 3. Feb 2023
Joined Dec 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
When you create a procedure…
Tue, 12/27/2022 - 13:47

When you create a procedure that uses a global variable, you are able to go into the custom code for that procedure to see the way that it accesses it.

Make sure you grab the import for your global variables from the top of the code view for the procedure, and copy-paste the code for get/set as needed into your custom code.

In my project, it looks like this - but since you're in Fabric and I'm in Forge, the specifics may be different for you.

//the import for globals
import net.mcreator.loftyphantomail.network.LoftyPhantomailModVariables;

//grabbing a variable - in this case the tag you are looking for is actually at the bottom
String address = new Object()
{
public String getValue(LevelAccessor world, BlockPos pos, String tag)
{
BlockEntity blockEntity = world.getBlockEntity(pos);
if (blockEntity != null)
return blockEntity.getPersistentData().getString(tag);
return "";
}
}.getValue(world, new BlockPos(x, y, z), "phantomail_address");