Jump to content

Recommended Posts

Posted

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 :)

Posted

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 :)

Posted

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 :)

Posted

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 :)

Posted

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 :)

Posted

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.

Posted

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.

  • 5 years later...
Posted (edited)

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
Posted (edited)
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

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 want to understand how complex mods with ASM transformation and coremods work, such as Xray or AntiXray. Why do they break when you simply rename packages? What features of their architecture make refactoring difficult? And what techniques are used to protect these mods? I am interested in technical aspects in order to better understand the bytecode and Forge loader system."
    • I can't figure out if you're looking for help trying to steal someone elses work, or cheat at the game....
    • Title: Why Is It So Hard to Rename and Restructure Mods Like Xray or AntiXray? 🤔 Post text: Hey everyone! I’ve been digging into Minecraft modding for a while and have one big question that I can’t figure out on my own. Maybe someone with more experience could help or give me some advice. Here’s the issue: When I take a “normal” Minecraft mod — for example, one that just adds some blocks or new items — I can easily change its structure, package names, or even rebrand it entirely. It’s straightforward. But as soon as I try this with cheat-type mods like XrayMod or AntiXray, everything falls apart. Even if I just rename the classes, refactor the packages, or hide its identity somehow, the mod either breaks or stops working properly. XrayMod in particular is proving to be a nightmare to modify without losing its core function. So my question is — why is this so much harder with cheat mods like Xray? Is there something fundamentally different about how they’re coded, loaded, or protected that prevents simple renaming or restructuring? And if so, how can I actually learn to understand someone else’s cheat mod enough to safely refactor it without breaking the core features? I’ve already been spending over two months trying to figure this out and haven’t gotten anywhere. It feels like there must be some trick or knowledge I’m missing. Would really appreciate any thoughts, tips, or references — maybe there are guides or techniques for understanding cheat-mod internals? Or if you’ve successfully “disguised” a cheat mod like Xray before, I’d love to hear how you did it. Thanks in advance for any help or discussion. ✌️
    • just started making cinamatic contect check it out on my channel or check out my facebook page    Humbug City Minecraft Youtube https://www.youtube.com/watch?v=v2N6OveKwno https://www.facebook.com/profile.php?id=61575866982337  
    • Where did you get the schematic? Source/Link? And do use an own modpack or a pre-configured from curseforge? If yes, which one On a later time, I can make some tests on my own - but I need the schematic and the modpack name
  • Topics

×
×
  • Create New...

Important Information

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