Jump to content

Recommended Posts

Posted (edited)

 

	  public static GameRules.RuleKey<GameRules.BooleanValue> doAttackCooldown;

	  @SubscribeEvent
	  public void init(FMLClientSetupEvent event)
	  {
		  doAttackCooldown = GameRules.register("doAttackCooldown", new GameRules(new GameRules.RuleType<GameRules.BooleanValue>(), true));
	  }

I'm trying to add a new gamerule, but I'm stuck at the first argument of new GameRules(). I'm not sure what to put here. I originally just followed the Vanilla format of:

register("doFireTick", GameRules.BooleanValue.create(true));

However, the create function is private for all GameRule value types.

 

Thank you in advance.

Edited by MattNL
  • Like 1
Posted

Okay, I feel like I'm getting closer, but I'm not quite there yet. I'm doing something wrong, and I'm not exactly sure what.

		  try{
			  Method createBool = GameRules.BooleanValue.class.getClass().getMethod("create", boolean.class);
			  createBool.setAccessible(true);
			  DeferredWorkQueue.runLater( () ->
			  {
				  GameRules.register("doAttackCooldown", createBool.invoke(???, true));
			  } );
		  } catch (InvocationTargetException |
			  ClassNotFoundException |
			  NoSuchMethodException |
			  IllegalAccessException exception) {
		  }

 

I don't think I'm reflecting properly...

Posted
		  try{
			  Method createBoolean = GameRules.BooleanValue.class.getClass().getMethod("create", boolean.class);
			  createBoolean.setAccessible(true);
			  DeferredWorkQueue.runLater( () ->
			  {
			  	try
				{
					Object boolTrue = createBoolean.invoke(GameRules.BooleanValue.class, true);
					GameRules.register("doAttackCooldown", (GameRules.RuleType<GameRules.BooleanValue>) boolTrue);
				}
				catch (IllegalAccessException e) {  }
				catch (InvocationTargetException e) { }
			  } );
		  }
		  catch (IllegalArgumentException e) { }
		  catch (NoSuchMethodException e) { }

So I've finally managed to clear all syntax errors in my code, and built the mod... however... it still hasn't registered the game rule...

2020-02-17_11.49.29.png

Posted (edited)

After doing some testing, it may seem that my "init" function is NEVER run for some bizarre reason, despite being subscribed. In fact, I can't seem to get FMLClientSetupEvent to fire at all. At first I thought my Logger was wrong, but, no, because it is printing to the log for other events.

	@SubscribeEvent
	public void setupClient(FMLClientSetupEvent event)
	{
		LOGGER.debug("Inital Client Setup!");
	}

So, I've moved everything to my main function:

	public Mod()
	{
	   MinecraftForge.EVENT_BUS.register(this);

	   LOGGER.debug("Initial Setup!");
	   try{
	   	Method createBoolean = GameRules.BooleanValue.class.getClass().getMethod("create", boolean.class);
	   	createBoolean.setAccessible(true);
	   	DeferredWorkQueue.runLater( () ->
		{
			try
			{
				Object boolTrue = createBoolean.invoke(GameRules.BooleanValue.class, true);
				doAttackCooldown = GameRules.register("doAttackCooldown", (GameRules.RuleType<GameRules.BooleanValue>) boolTrue);
			}
			catch (IllegalAccessException e) {
				LOGGER.error("Illegal Access Exception!");
				e.printStackTrace();
			}
			catch (InvocationTargetException e) {
				LOGGER.error("Invocation Target Exception!");
				e.printStackTrace();
			}
		});
	   }
	   catch (IllegalArgumentException e) {
	   	LOGGER.error("Illegal Argument Exception!");
	   	e.printStackTrace();
	   }
	   catch (NoSuchMethodException e) {
	   	LOGGER.error("No Such Method Exception!");
	   	e.printStackTrace();
	   }
	}

From there, I finally got the log to speak to me:

[16:12:59] [modloading-worker-1/DEBUG] [(NAMESPACE HERE)/]: Initial Setup!
[16:12:59] [modloading-worker-1/ERROR] [(NAMESPACE HERE)/]: No Such Method Exception!

I'm not sure what's going on with FMLClientSetupEvent, whether it's a bug, or I simply broke something.

 

In case it is a bug: I'm running Forge 31.1.0

Edited by MattNL
Posted

That did it. ObfuscationReflectionHelper was very helpful. (With terrible exception handling, or not) Thank you, diesieben07!

 

	   try{
	   	Method createBoolean = ObfuscationReflectionHelper.findMethod(GameRules.BooleanValue.class, "create",boolean.class);
	   	createBoolean.setAccessible(true);
	   	DeferredWorkQueue.runLater( () ->
		{
			try
			{
				Object boolTrue = createBoolean.invoke(GameRules.BooleanValue.class, true);
				doAttackCooldown = GameRules.register("doAttackCooldown", (GameRules.RuleType<GameRules.BooleanValue>) boolTrue);
			}
			catch (IllegalAccessException e) {
				LOGGER.error("Illegal Access Exception!");
				e.printStackTrace();
			}
			catch (InvocationTargetException e) {
				LOGGER.error("Invocation Target Exception!");
				e.printStackTrace();
			}
		});
	   }
	   catch (IllegalArgumentException e) {
	   	LOGGER.error("Illegal Argument Exception!");
	   	e.printStackTrace();
	   	throw e;
	   }

 

Now to work on that exception handling... there's no excuse for that...

 

 

2020-02-18_10.42.16.png

  • MattNL changed the title to [SOLVED] [1.15.2] Game Rule registration
Posted (edited)

Found it!

Here are the names for all of the game rule type creation methods:

 

func_223559_b -> GameRules$IntegerValue.create

func_223568_b -> GameRules$BooleanValue.create

 

	   try{
	   	Method createBoolean = ObfuscationReflectionHelper.findMethod(GameRules.BooleanValue.class, "func_223568_b",boolean.class);
	   	createBoolean.setAccessible(true);
	   	DeferredWorkQueue.runLater( () ->
		{
			try
			{
				Object boolTrue = createBoolean.invoke(GameRules.BooleanValue.class, true);
				doAttackCooldown = GameRules.register("doAttackCooldown", (GameRules.RuleType<GameRules.BooleanValue>) boolTrue);
			}
			catch (IllegalAccessException e) {
				LOGGER.error("Illegal Access Exception!");
				e.printStackTrace();
			}
			catch (InvocationTargetException e) {
				LOGGER.error("Invocation Target Exception!");
				e.printStackTrace();
			}
		});
	   }
	   catch (IllegalArgumentException e) {
	   	LOGGER.error("Illegal Argument Exception!");
	   	e.printStackTrace();
	   	throw e;
	   }

 

Edited by MattNL
Posted (edited)

Here's what I did:

 

At that point, I now knew that the particular function I needed had to be "RuleType" type function. So, the next thing I did was decompile a jar that had the java files with deobfuscated names and obfuscated classes... such a file can be found in your MDK folder...

 

MDK_ROOT/build/fg_cache/net/minecraftforge/forge/MCVERSION-FORGEVERSION_at_STRING

In my instance: C:\dev\mdk\build\fg_cache\net\minecraftforge\forge\1.15.2-31.1.0_at_b54d5c04b317e9e50ee2e93c270c8b976d4cdb59

 

Any of the jars in that folder should contain what you're looking for.

 

In my instance, I needed the "create" function in "BooleanValue" in "net.minecraft.world.gamerules"

 

Upon decompiling, you should see the first image... then find your class, just as seen in the second image... next, look in your class for the proper type of function (in my case RuleType), as seen in the third image. The only RuleType function is "func_223567_b". That should be my function, but we can double check by searching for that name in the mappings, either in Excel or even a text editor will do. And if you search for it, the obfuscated name should line up with the deobfuscated name, as seen in the final image.

 

Hopefully this is helpful. ?

 

image.png

image.png

image.png

image.png

Edited by MattNL
  • Confused 1
Posted

Or you could join the Discord and let the bot do the lookup for you

This is my Forum Signature, I am currently attempting to transform it into a small guide for fixing easier issues using spoiler blocks to keep things tidy.

 

As the most common issue I feel I should put this outside the main bulk:

The only official source for Forge is https://files.minecraftforge.net, and the only site I trust for getting mods is CurseForge.

If you use any site other than these, please take a look at the StopModReposts project and install their browser extension, I would also advise running a virus scan.

 

For players asking for assistance with Forge please expand the spoiler below and read the appropriate section(s) in its/their entirety.

  Reveal hidden contents

 

Posted
  On 2/26/2020 at 4:26 PM, DaemonUmbra said:

Or you could join the Discord and let the bot do the lookup for you

Expand  

Yes, that's what I'm going to continue to do. I misunderstood the following:

 

  On 2/18/2020 at 3:53 PM, diesieben07 said:

Well, I would hope that whatever text editor you are using has a search feature.

Expand  

to mean that the currently used mappings were available by default somewhere in my IDE.

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.