Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

Using Java 8, Forge 1.12.2
I've been trying to use reflection to change a private variable in another class, I'm able to get the class, and the variable,


*I've tried to use the public set method to change the variable, that doesn't work, and ends in this error:

java.lang.IllegalArgumentException: object is not an instance of declaring class


*I've tried changing the variable to accesible = true and then changing it, resulting in this error:

java.lang.IllegalArgumentException: Can not set int field playerProperties.VanillaSpellsCapability.mirrorspellSpread to java.lang.Class

 

The Code:

Class clad = Class.forName(message.cladName);
                
                Field MSS = clad.getDeclaredField(message.mirrorspellSpread);
                
                MSS.setAccessible(true);

                MSS.set(clad, 1);
                //Method SetMS = clad.getMethod("setIsMSSpreadOut", Integer.TYPE);
                //SetMS.invoke(clad, 1);

Edited by CorruptedVulture

8 minutes ago, CorruptedVulture said:

*I've tried to use the public set method to change the variable, that doesn't work, and ends in this error:

java.lang.IllegalArgumentException: object is not an instance of declaring class

If the method is public then you don't need reflection. Anyway, as the error clearly states you are passing an object as an instance of the class while the object in question isn't an instance of said class. This is one of reflection's basics.

 

9 minutes ago, CorruptedVulture said:

java.lang.IllegalArgumentException: Can not set int field playerProperties.VanillaSpellsCapability.mirrorspellSpread to java.lang.Class

Same here - you are passing a Class as the instance, which will never be the case. 

 

https://www.oracle.com/technetwork/articles/java/javareflection-1536171.html

 

  • Author

Ok, I tried with the new instance by declaring a new object, and setting it to be the class's new instance, and my intentions with this was to update a capability variable on the client-side, however it's just as I thought that making a new instance wouldn't affect the player's capability.
So is there any other solution? Like to use the class instance I already have instead of declaring a new one?

The new Code:
 

Class clad = Class.forName(message.cladName);
                Object cc = clad.newInstance();
                //Field MSS = clad.getDeclaredField(message.mirrorspellSpread);
                /*
                MSS.setAccessible(true);

                MSS.set(clad, 1);*/

                Method SetMS = clad.getMethod("setIsMSSpreadOut", Integer.TYPE);
                SetMS.invoke(cc, 1);

4 minutes ago, CorruptedVulture said:

So is there any other solution? Like to use the class instance I already have instead of declaring a new one?

Well, yes, you need to use the instance you want to update the value of. 

  • Author

Except I can't do that because using the instance I want to change results in this error
java.lang.IllegalArgumentException: object is not an instance of declaring class
Using this code
//Method SetMS = clad.getMethod("setIsMSSpreadOut", Integer.TYPE);

//SetMS.invoke(clad, 1);

Keep in mind, not declaring a new object, like here

 Object cc = clad.newInstance();

So then is there anything else I can do, to use the instance I got?

Edited by CorruptedVulture

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

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.