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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
package gtPlusPlus.core.handler.events;
import java.util.ArrayList;
import java.util.HashMap;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraftforge.event.entity.living.LivingDropsEvent;
import org.jetbrains.annotations.NotNull;
import com.kuba6000.mobsinfo.api.IMobExtraInfoProvider;
import com.kuba6000.mobsinfo.api.MobDrop;
import com.kuba6000.mobsinfo.api.MobRecipe;
import cpw.mods.fml.common.Optional;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import gtPlusPlus.api.objects.Logger;
import gtPlusPlus.api.objects.data.Triplet;
import gtPlusPlus.core.item.ModItems;
import gtPlusPlus.core.util.math.MathUtils;
import gtPlusPlus.core.util.minecraft.ItemUtils;
import gtPlusPlus.core.util.minecraft.PlayerUtils;
@Optional.Interface(iface = "com.kuba6000.mobsinfo.api.IMobExtraInfoProvider", modid = "mobsinfo")
public class EntityDeathHandler implements IMobExtraInfoProvider {
private static final HashMap<Class<?>, ArrayList<Triplet<ItemStack, Integer, Integer>>> mMobDropMap = new HashMap<>();
private static final ArrayList<Class<?>> mInternalClassKeyCache = new ArrayList<>();
/**
* Provides the ability to provide custom drops upon the death of EntityLivingBase objects.
*
* @param aMobClass - The Base Class you want to drop this item.
* @param aStack - The ItemStack, stack size is not respected.
* @param aMaxAmount - The maximum size of the ItemStack which drops.
* @param aChance - Chance out of 10000, where 100 is 1%. (1 = 0.01% - this is ok)
*/
public static void registerDropsForMob(Class aMobClass, ItemStack aStack, int aMaxAmount, int aChance) {
Triplet<ItemStack, Integer, Integer> aData = new Triplet<>(aStack, aMaxAmount, aChance);
ArrayList<Triplet<ItemStack, Integer, Integer>> aDataMap = mMobDropMap.get(aMobClass);
if (aDataMap == null) {
aDataMap = new ArrayList<>();
}
aDataMap.add(aData);
mMobDropMap.put(aMobClass, aDataMap);
Logger.INFO(
"[Loot] Registered " + aStack
.getDisplayName() + " (1-" + aMaxAmount + ") as a valid drop for " + aMobClass.getCanonicalName());
mInternalClassKeyCache.add(aMobClass);
}
private static ItemStack processItemDropTriplet(Triplet<ItemStack, Integer, Integer> aData) {
ItemStack aLoot = aData.getValue_1();
int aMaxDrop = aData.getValue_2();
int aChanceOutOf10000 = aData.getValue_3();
if (MathUtils.randInt(0, 10000) <= aChanceOutOf10000) {
aLoot = ItemUtils.getSimpleStack(aLoot, MathUtils.randInt(1, aMaxDrop));
if (ItemUtils.checkForInvalidItems(aLoot)) {
return aLoot;
}
}
return null;
}
private static boolean processDropsForMob(EntityLivingBase entityLiving) {
ArrayList<Triplet<ItemStack, Integer, Integer>> aMobData = mMobDropMap.get(entityLiving.getClass());
boolean aDidDrop = false;
if (aMobData != null) {
if (!aMobData.isEmpty()) {
ItemStack aPossibleDrop;
for (Triplet<ItemStack, Integer, Integer> g : aMobData) {
aPossibleDrop = processItemDropTriplet(g);
if (aPossibleDrop != null) {
if (entityLiving.entityDropItem(aPossibleDrop, MathUtils.randFloat(0, 1)) != null) {
aDidDrop = true;
}
}
}
}
}
return aDidDrop;
}
private static void dropMeatFromPlayer(EntityPlayer aPlayer) {
// always drop some meat.
int aBigMeatStackSize1 = MathUtils.randInt(4, 8);
aPlayer.entityDropItem(
ItemUtils.simpleMetaStack(ModItems.itemMetaFood, 0, aBigMeatStackSize1),
MathUtils.randInt(0, 1));
// additional chances for more meat.
if (MathUtils.randInt(0, 10) < 7) {
int aBigMeatStackSize2 = MathUtils.randInt(4, 8);
aPlayer.entityDropItem(
ItemUtils.simpleMetaStack(ModItems.itemMetaFood, 0, aBigMeatStackSize2),
MathUtils.randInt(0, 1));
}
if (MathUtils.randInt(0, 10) < 4) {
int aBigMeatStackSize3 = MathUtils.randInt(4, 8);
aPlayer.entityDropItem(
ItemUtils.simpleMetaStack(ModItems.itemMetaFood, 0, aBigMeatStackSize3),
MathUtils.randInt(0, 1));
}
if (MathUtils.randInt(0, 10) < 2) {
int aBigMeatStackSize4 = MathUtils.randInt(4, 8);
aPlayer.entityDropItem(
ItemUtils.simpleMetaStack(ModItems.itemMetaFood, 0, aBigMeatStackSize4),
MathUtils.randInt(0, 1));
}
}
@SubscribeEvent
public void onEntityDrop(LivingDropsEvent event) {
if (event == null || event.entityLiving == null) {
return;
}
if (PlayerUtils.isRealPlayer(event.entityLiving)) {
EntityPlayer aPlayer = (EntityPlayer) event.entityLiving;
dropMeatFromPlayer(aPlayer);
} else {
for (Class<?> c : mInternalClassKeyCache) {
if (c.isInstance(event.entityLiving)) {
processDropsForMob(event.entityLiving);
}
}
}
}
@Optional.Method(modid = "mobsinfo")
@Override
public void provideExtraDropsInformation(@NotNull String entityString, @NotNull ArrayList<MobDrop> drops,
@NotNull MobRecipe recipe) {
ArrayList<Triplet<ItemStack, Integer, Integer>> dropEntry = mMobDropMap.get(recipe.entity.getClass());
if (dropEntry != null && !dropEntry.isEmpty()) {
for (Triplet<ItemStack, Integer, Integer> data : dropEntry) {
ItemStack loot = data.getValue_1();
int maxDrop = data.getValue_2();
int chance = data.getValue_3();
if (loot == null) continue;
loot = loot.copy();
loot.stackSize = 1;
MobDrop drop = new MobDrop(
loot,
MobDrop.DropType.Normal,
(int) (MobDrop.getChanceBasedOnFromTo(1, maxDrop) * 10000d * ((double) chance / 10000d)),
null,
null,
false,
false);
drop.clampChance();
drops.add(drop);
}
}
}
}
|