Jump to content

How to use Reflection to change vanilla values


JimiIT92

Recommended Posts

As by title, i don't know how to use reflection to change a vanilla value. In my particular case i need to change this values

swingProgressInt

from the EntityLivingBase class to make a faster animation when using a specific item (in my case a Katana).

I tried this code

Field field;
	try {
		field = Class.forName("net.minecraft.entity.EntityLivingBase").getDeclaredField("swingProgressInt");
		field.setAccessible(true);

		field.setInt(arg0, 4);
	} catch (NoSuchFieldException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (SecurityException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (ClassNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}

but i don't understand what to type in as arg0. The function takes an Object and an int. the in value is the new value of the variable i guess but the object i don't understand what it is

 

Don't blame me if i always ask for your help. I just want to learn to be better :)

Link to comment
Share on other sites

Someone in another post told me to do that :) Anyway i tried changing that value in the function by doing

attacker.swingProgressInt = 2;

but it doesn't affect the animation (is neither slower or faster)

Don't blame me if i always ask for your help. I just want to learn to be better :)

Link to comment
Share on other sites

Ok, so i've overrited this method

@Override
public boolean onLeftClickEntity(ItemStack stack, EntityPlayer player, Entity entity)
{
	player.swingProgressInt = 1;
	return this.hitEntity(stack, (EntityLivingBase) entity, player);
}

and my katana is faster (wich is what i want), BUT if i hit an entity it doesn't deal damage :/

Don't blame me if i always ask for your help. I just want to learn to be better :)

Link to comment
Share on other sites

Ok, so i've looked into the super method wich is only a return false statement. So i changed my function to this and it worked :)

@Override
public boolean onLeftClickEntity(ItemStack stack, EntityPlayer player, Entity entity)
{
	player.swingProgressInt = 3;
	return false;
}

 

Also fun fact: it seems that i can make my item only faster but not slower. xD

Don't blame me if i always ask for your help. I just want to learn to be better :)

Link to comment
Share on other sites

Negative numbers makes my katana to not swing at all. I think that the exact value to set could be find in ths function

private int getArmSwingAnimationEnd()
    {
        return this.isPotionActive(Potion.digSpeed) ? 6 - (1 + this.getActivePotionEffect(Potion.digSpeed).getAmplifier()) * 1 : (this.isPotionActive(Potion.digSlowdown) ? 6 + (1 + this.getActivePotionEffect(Potion.digSlowdown).getAmplifier()) * 2 : 6);
    }

but since is a private function i can't access that to see what value it returns. I tried using the exact value that is supposed to return if the haste or slowdown effect is applied (level 1) (5 for digSpeed and 8 for digSlowdown) but nothing happens (instead with a value of 5 the katana do a weird animation).

Don't blame me if i always ask for your help. I just want to learn to be better :)

Link to comment
Share on other sites

Although you're no longer using reflection, I'd like to clarify something from your OP. You had this:

 

                try {
		field = Class.forName("net.minecraft.entity.EntityLivingBase").getDeclaredField("swingProgressInt");
		field.setAccessible(true);

		field.setInt(arg0, 4);
	} catch...

 

If I understand reflection correctly, then it is only necessary to getDeclaredField once and setAccessible once (perhaps in a constructor). Later, one may use the field to operate upon instances of its class to make value assignments many times. Do I have that right?

 

The debugger is a powerful and necessary tool in any IDE, so learn how to use it. You'll be able to tell us more and get better help here if you investigate your runtime problems in the debugger before posting.

Link to comment
Share on other sites

Although you're no longer using reflection, I'd like to clarify something from your OP. You had this:

 

                try {
		field = Class.forName("net.minecraft.entity.EntityLivingBase").getDeclaredField("swingProgressInt");
		field.setAccessible(true);

		field.setInt(arg0, 4);
	} catch...

 

If I understand reflection correctly, then it is only necessary to getDeclaredField once and setAccessible once (perhaps in a constructor). Later, one may use the field to operate upon instances of its class to make value assignments many times. Do I have that right?

 

yes.

Link to comment
Share on other sites

  • 5 years later...

Replying for future modders. ObfuscationReflectionHelper is perfectly fine if you use it efficiently. You can use it to get a Field object, which you can store in a static member.

Example:

public class Example {
	private static Field someField = null;

	static {
		try {
			someField = ObfuscationReflectionHelper.findField(SomeMinecraftClass.class, "field_12345_a"); // You need to pass the SRG name
			someField.setAccessible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void doStuff(SomeMinecraftClass minecraftThing) {
		try {
        		Object o = someField.get(minecraftThing);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

I would also advise against passing the fully qualified class name as a String if you can help it. It's better to use the Class object directly if you can, and in most cases you can.

 

You can find SRG names at http://mcpbot.bspk.rs/

Edited by coalbricks
Added link to MCPBot site
Link to comment
Share on other sites

12 hours ago, diesieben07 said:
  1. Your exception handling is inexcusable. This is not how you handle exceptions.
  2. The Field should be stored in a static final field, to allow further JVM optimizations.

1. It's a basic example. And that exception handling is fine for the purposes of being a basic example. It's also fine in production code in certain cases which I don't want to debate here, although I will admit the doStuff() function should have a null guard.

2. Yes while it would be better to store it in a static final field, the JVM optimizations obtained by making the member final are minimal at best, unless the field is being used to perform reflective gets/sets frequently. This pattern works perfectly fine in MOST cases but if you really care you can separate the static initializer logic into a separate function and make the field final; in most cases it's just not worth the trouble.

Edited by coalbricks
Added a note about final fields
Link to comment
Share on other sites

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'm using Modrinth as a launcher for a forge modpack on 1.20.1, and can't diagnose the issue on the crash log myself. Have tried repairing the Minecraft instillation as well as removing a few mods that have been problematic for me in the past to no avail. Crash log is below, if any further information is necessary let me know. Thank you! https://paste.ee/p/k6xnS
    • Hey folks. I am working on a custom "Mecha" entity (extended from LivingEntity) that the player builds up from blocks that should get modular stats depending on the used blocks. e.g. depending on what will be used for the legs, the entity will have a different jump strength. However, something unexpected is happening when trying to override a few of LivingEntity's functions and using my new own "Mecha" specific fields: instead of their actual instance-specific value, the default value is used (0f for a float, null for an object...) This is especially strange as when executing with the same entity from a point in the code specific to the mecha entity, the correct value is used. Here are some code snippets to better illustrate what I mean: /* The main Mecha class, cut down for brevity */ public class Mecha extends LivingEntity { protected float jumpMultiplier; //somewhere later during the code when spawning the entity, jumpMultiplier is set to something like 1.5f //changing the access to public didn't help @Override //Overridden from LivingEntity, this function is only used in the jumpFromGround() function, used in the aiStep() function, used in the LivingEntity tick() function protected float getJumpPower() { //something is wrong with this function //for some reason I can't correctly access the fields and methods from the instanciated entity when I am in one of those overridden protected functions. this is very annoying LogUtils.getLogger().info(String.valueOf(this.jumpMultiplier))) //will print 0f return this.jumpMultiplier * super.getJumpPower(); } //The code above does not operate properly. Written as is, the entity will not jump, and adding debug logs shows that when executing the code, the value of this.jumpMultiplier is 0f //in contrast, it will be the correct value when done here: @Override public void tick() { super.tick(); //inherited LivingEntity logic //Custom logic LogUtils.getLogger().info(String.valueOf(this.jumpMultiplier))) //will print 1.5f } } My actual code is slightly different, as the jumpMuliplier is stored in another object (so I am calling "this.legModule.getJumpPower()" instead of the float), but even using a simple float exactly like in the code above didn't help. When running my usual code, the object I try to use is found to be null instead, leading to a crash from a nullPointerException. Here is the stacktrace of said crash: The full code can be viewed here. I have found a workaround in the case of jump strength, but have already found the same problem for another parameter I want to do, and I do not understand why the code is behaving as such, and I would very much like to be able to override those methods as intended - they seemed to work just fine like that for vanilla mobs... Any clues as to what may be happening here?
    • Please delete post. Had not noticed the newest edition for 1.20.6 which resolves the issue.
    • https://paste.ee/p/GTgAV Here's my debug log, I'm on 1.18.2 with forge 40.2.4 and I just want to get it to work!! I cant find any mod names in the error part and I would like some help from the pros!! I have 203 mods at the moment.
  • Topics

×
×
  • Create New...

Important Information

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