aboutsummaryrefslogtreecommitdiff
path: root/src/Java/gtPlusPlus/xmod/witchery/WitchUtils.java
blob: 9e5efb452cc1fe9648c64d9f113c8ea69d773dc5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package gtPlusPlus.xmod.witchery;

import java.lang.reflect.Field;
import java.util.ArrayList;

import com.emoniph.witchery.Witchery;
import com.emoniph.witchery.entity.ai.EntityAIDigBlocks;
import com.mojang.authlib.GameProfile;

import gtPlusPlus.core.lib.LoadedMods;
import gtPlusPlus.core.util.reflect.ReflectionUtils;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.FurnaceRecipes;
import net.minecraftforge.event.world.BlockEvent;
import net.minecraftforge.oredict.OreDictionary;

public class WitchUtils {

	private static final Field KOBOLDITE_MINER_PROFILE;
	
	static {
		if (LoadedMods.Witchery) {
			KOBOLDITE_MINER_PROFILE = getField("com.emoniph.witchery.entity.ai.EntityAIDigBlocks", "KOBOLDITE_MINER_PROFILE");			
		}
		else {
			KOBOLDITE_MINER_PROFILE = null;
		}
		
	}
	
	//com.emoniph.witchery.entity.ai.EntityAIDigBlocks.onHarvestDrops(EntityPlayer, HarvestDropsEvent)
    public static void onHarvestDrops(final EntityPlayer harvester, final BlockEvent.HarvestDropsEvent event) {
        if (harvester != null && !harvester.worldObj.isRemote && !event.isCanceled() && (isEqual(harvester.getGameProfile(), EntityAIDigBlocks.KOBOLDITE_MINER_PROFILE) || isEqual(harvester.getGameProfile(), EntityAIDigBlocks.NORMAL_MINER_PROFILE))) {
            final boolean hasKobolditePick = isEqual(harvester.getGameProfile(), EntityAIDigBlocks.KOBOLDITE_MINER_PROFILE);
            final ArrayList<ItemStack> newDrops = new ArrayList<ItemStack>();
            double kobolditeChance = hasKobolditePick ? 0.02 : 0.01;
            for (final ItemStack drop : event.drops) {
                final int[] oreIDs = OreDictionary.getOreIDs(drop);
                boolean addOriginal = true;
                if (oreIDs.length > 0) {
                    final String oreName = OreDictionary.getOreName(oreIDs[0]);
                    if (oreName != null && oreName.startsWith("ore")) {
                        final ItemStack smeltedDrop = FurnaceRecipes.smelting().getSmeltingResult(drop);
                        if (smeltedDrop != null && hasKobolditePick && harvester.worldObj.rand.nextDouble() < 0.5) {
                            addOriginal = false;
                            newDrops.add(smeltedDrop.copy());
                            newDrops.add(smeltedDrop.copy());
                            if (harvester.worldObj.rand.nextDouble() < 0.25) {
                                newDrops.add(smeltedDrop.copy());
                            }
                        }
                        kobolditeChance = (hasKobolditePick ? 0.08 : 0.05);
                    }
                }
                if (addOriginal) {
                    newDrops.add(drop);
                }
            }
            event.drops.clear();
            for (final ItemStack newDrop : newDrops) {
                event.drops.add(newDrop);
            }
            if (kobolditeChance > 0.0 && harvester.worldObj.rand.nextDouble() < kobolditeChance) {
                event.drops.add(Witchery.Items.GENERIC.itemKobolditeDust.createStack());
            }
        }
    }
    
	public static Field getField(String aClassName, String aFieldName) {
		Class c;
		c = ReflectionUtils.getClass(aClassName);
		if (c != null) {
			Field f = ReflectionUtils.getField(c, aFieldName);
			if (f != null) {
				return f;
			}
		}
		return null;
	}
    
    public static boolean isEqual(final GameProfile a, final GameProfile b) {
        return a != null && b != null && a.getId() != null && b.getId() != null && a.getId().equals(b.getId());
    }
	
}