Jump to content

Recommended Posts

Posted

I am trying to change a mob's drops as if they were killed with Looting, but without using an enchanted item. The best I could come up with is to hook into LivingDropsEvent, and do this:

 

if (!mob.isChild() && mob.worldObj.getGameRules().getBoolean("doMobLoot")) {
mob.capturedDrops.clear();
mob.captureDrops = true;

try {
	Method dropFewItems = mob.getClass().getMethod("dropFewItems", boolean.class, int.class);
	Method dropEquipment = mob.getClass().getMethod("dropEquipment", boolean.class, int.class);
	Method addRandomDrop = mob.getClass().getMethod("addRandomDrop");

	dropFewItems.invoke(mob, true, customLootingLevel);
	dropEquipment.invoke(mob, true, customLootingLevel);

        if (mob.worldObj.rand.nextFloat() < 0.025F + (float)customLootingLevel* 0.01F) {
            addRandomDrop.invoke(mob);
        }
        
        mob.captureDrops = false;
        event.drops.clear();
	event.drops.addAll(mob.capturedDrops);
}

 

I now have 2 probems:

1. I am getting this error:

 java.lang.NoSuchMethodException: net.minecraft.entity.passive.EntityChicken.dropFewItems(boolean, int)

 

2. I realized this might break when its re-obfuscated because the method names change.

 

What is the best way to do this?

Posted

Is it also possible to get the Method with getDeclaredMethods() and then using the right number ? Or is the position changing (on server/client side, deobfuscated/reobfuscated)

catch(Exception e)

{

 

}

Yay, Pokémon exception handling, gotta catch 'em all (and then do nothing with 'em).

Posted

Using the index is a very unreliable method.

 

Is it better to iterate over the array and check params and return type ?

catch(Exception e)

{

 

}

Yay, Pokémon exception handling, gotta catch 'em all (and then do nothing with 'em).

Posted

First of all, look up the methods once and store them in a static field. You will also need to use

getDeclaredMethod

, since

getMethod

only works with public methods (read the JavaDocs on the methods you use -.-).

You will also have to call

setAccessible

on the Method objects, since they are protected.

 

Then you have to choose the correct method name based on

(Boolean) Launch.blackboard.get("fml.deobfuscatedEnvironment")

. If that is true, use the normal MCP name you can see in your source code. If it is false, you need to use the SRG name, which you can look up using MCPBot.

 

Moreover:

Do not mess with

mob.capturedDrops

or

mob.captureDrops

at all. Call

event.drops.clear()

before you invoke the methods, then invoke them. That is all you need to do.

 

Great advice, its working great now. I should add that I had to call getDeclaredMethod on EntityLivingBase.class, not the mob's class (if anybody else runs into this same problem).

 

The only thing left is: addRandomDrop. I checked methods.csv on the most recent MCP, and I can't find it. I found another mod that uses is, and decompiled to find func_82164_bB... but after some googling, I feel like that maps to something regarding armor. Am I missing something here?

Posted

Thanks! Here are some snippets of code I used to get this working:

 

private static Method dropFewItems;
private static Method dropEquipment;
private static Method addRandomDrop;

 

Boolean deobf = (Boolean) Launch.blackboard.get("fml.deobfuscatedEnvironment");
dropFewItems = EntityLivingBase.class.getDeclaredMethod(deobf ? "dropFewItems" : "func_70628_a",
	boolean.class, int.class);
dropEquipment = EntityLivingBase.class.getDeclaredMethod(deobf ? "dropEquipment" : "func_82160_b",
	boolean.class, int.class);
addRandomDrop = EntityLivingBase.class.getDeclaredMethod(deobf ? "addRandomDrop" : "func_82164_bB");
dropFewItems.setAccessible(true);
dropEquipment.setAccessible(true);
addRandomDrop.setAccessible(true);

 

 

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.