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

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?

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

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

  • Author

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?

  • Author

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

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.