aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gtPlusPlus/core/item/food/BaseItemMetaFood.java
blob: 72e74b2d2bd2581a70399aa12fb34e26b47bbc90 (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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
package gtPlusPlus.core.item.food;

import static gregtech.api.enums.Mods.GTPlusPlus;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.EnumRarity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemFood;
import net.minecraft.item.ItemStack;
import net.minecraft.potion.Potion;
import net.minecraft.potion.PotionEffect;
import net.minecraft.util.IIcon;
import net.minecraft.world.World;

import cpw.mods.fml.common.registry.GameRegistry;
import gregtech.api.enums.Materials;
import gregtech.api.objects.ItemData;
import gregtech.api.objects.MaterialStack;
import gregtech.api.util.GTOreDictUnificator;
import gtPlusPlus.core.creative.AddToCreativeTab;
import gtPlusPlus.core.item.ModItems;
import gtPlusPlus.core.util.math.MathUtils;
import gtPlusPlus.core.util.minecraft.EntityUtils;
import gtPlusPlus.core.util.minecraft.ItemUtils;

public class BaseItemMetaFood extends ItemFood {

    private static final HashMap<Integer, IIcon> mIconMap = new HashMap<>();
    private static int mTotalMetaItems = 0;

    /*
     * 0 - Raw Human Meat 1 - Cooked Human Meat 2 - Raw Horse Meat 3 - Cooked Horse Meat 4 - Raw Wolf Meat 5 - Cooked
     * Wolf Meat 6 - Raw Ocelot Meat 7 - Cooked Ocelot Meat 8 - Blaze Flesh
     */

    // listAllmeatraw
    // listAllmeatcooked

    public static void registerMetaFoods() {
        registerNewMetaFood(
            0,
            "I wouldn't eat this unless I was starving",
            2,
            0,
            64,
            getPotionEffectPackage(new EffectWeaknessModerate(80), new EffectSlownessModerate(80)),
            getOreDictNamesAsArrayList("listAllmeatraw"));
        registerNewMetaFood(
            1,
            "Doesn't look any better cooked",
            4,
            1,
            64,
            getPotionEffectPackage(new EffectWeaknessBasic(50), new EffectSlownessBasic(50)),
            getOreDictNamesAsArrayList("listAllmeatcooked"));
        registerNewMetaFood(
            2,
            "",
            2,
            0,
            64,
            getPotionEffectPackage(new EffectWeaknessBasic(30), new EffectSlownessBasic(30)),
            getOreDictNamesAsArrayList("listAllmeatraw"));
        registerNewMetaFood(3, "", 4, 1, 64, getOreDictNamesAsArrayList("listAllmeatcooked"));
        registerNewMetaFood(
            4,
            "",
            2,
            0,
            64,
            getPotionEffectPackage(new EffectWeaknessBasic(25), new EffectSlownessBasic(30)),
            getOreDictNamesAsArrayList("listAllmeatraw"));
        registerNewMetaFood(5, "", 4, 1, 64, getOreDictNamesAsArrayList("listAllmeatcooked"));
        registerNewMetaFood(
            6,
            "",
            2,
            0,
            64,
            getPotionEffectPackage(new EffectWeaknessBasic(30), new EffectSlownessBasic(25)),
            getOreDictNamesAsArrayList("listAllmeatraw"));
        registerNewMetaFood(7, "", 4, 1, 64, getOreDictNamesAsArrayList("listAllmeatcooked"));
        registerNewMetaFood(
            8,
            "Warm to the touch",
            EnumRarity.uncommon,
            4,
            1,
            64,
            new ArrayList<>(),
            new setOnFire(),
            getOreDictNamesAsArrayList("listAllmeatcooked"));
    }

    private static final HashMap<Integer, Integer> mMaxStackSizeMap = new HashMap<>();
    private static final HashMap<Integer, String> mTooltipMap = new HashMap<>();
    private static final HashMap<Integer, EnumRarity> mRarityMap = new HashMap<>();
    private static final HashMap<Integer, Integer> mHealAmountMap = new HashMap<>();
    private static final HashMap<Integer, Float> mSaturationAmountMap = new HashMap<>();
    private static final HashMap<Integer, ArrayList<PotionEffectPackage>> mPotionEffectsMap = new HashMap<>();
    private static final HashMap<Integer, Boolean> mHasSpecialBehaviourMap = new HashMap<>();
    private static final HashMap<Integer, SpecialFoodBehaviour> mSpecialBehaviourMap = new HashMap<>();
    private static final HashMap<Integer, ArrayList<String>> mOreDictNames = new HashMap<>();

    public static void registerNewMetaFood(final int aMetaID, String aTooltip, final int aHealAmount,
        final float aSaturationModifier, final int aMaxStacksize) {
        registerNewMetaFood(
            aMetaID,
            aTooltip,
            EnumRarity.common,
            aHealAmount,
            aSaturationModifier,
            aMaxStacksize,
            new ArrayList<>(),
            null,
            new ArrayList<>());
    }

    public static void registerNewMetaFood(final int aMetaID, String aTooltip, final int aHealAmount,
        final float aSaturationModifier, final int aMaxStacksize, final ArrayList<String> aOreDictNames) {
        registerNewMetaFood(
            aMetaID,
            aTooltip,
            EnumRarity.common,
            aHealAmount,
            aSaturationModifier,
            aMaxStacksize,
            new ArrayList<>(),
            null,
            aOreDictNames);
    }

    public static void registerNewMetaFood(final int aMetaID, String aTooltip, final int aHealAmount,
        final float aSaturationModifier, final int aMaxStacksize, final ArrayList<PotionEffectPackage> aPotionEffects,
        final ArrayList<String> aOreDictNames) {
        registerNewMetaFood(
            aMetaID,
            aTooltip,
            EnumRarity.common,
            aHealAmount,
            aSaturationModifier,
            aMaxStacksize,
            aPotionEffects,
            null,
            aOreDictNames);
    }

    public static void registerNewMetaFood(final int aMetaID, String aTooltip, EnumRarity aRarity,
        final int aHealAmount, final float aSaturationModifier, final int aMaxStacksize,
        final ArrayList<PotionEffectPackage> aPotionEffects, final SpecialFoodBehaviour aSpecialBehaviour) {
        registerNewMetaFood(
            aMetaID,
            aTooltip,
            EnumRarity.common,
            aHealAmount,
            aSaturationModifier,
            aMaxStacksize,
            aPotionEffects,
            null,
            new ArrayList<>());
    }

    public static void registerNewMetaFood(final int aMetaID, String aTooltip, EnumRarity aRarity,
        final int aHealAmount, final float aSaturationModifier, final int aMaxStacksize,
        final ArrayList<PotionEffectPackage> aPotionEffects, final SpecialFoodBehaviour aSpecialBehaviour,
        final ArrayList<String> aOreDictNames) {
        mTotalMetaItems++;
        mMaxStackSizeMap.put(aMetaID, aMaxStacksize);
        mTooltipMap.put(aMetaID, aTooltip);
        mRarityMap.put(aMetaID, aRarity);
        mHealAmountMap.put(aMetaID, aHealAmount);
        mSaturationAmountMap.put(aMetaID, aSaturationModifier);
        mPotionEffectsMap.put(aMetaID, aPotionEffects);
        mHasSpecialBehaviourMap.put(aMetaID, (aSpecialBehaviour != null));
        if (aSpecialBehaviour != null) {
            mSpecialBehaviourMap.put(aMetaID, aSpecialBehaviour);
        }
        mOreDictNames.put(aMetaID, aOreDictNames);
        if (aOreDictNames.contains("listAllmeatraw")) {
            GTOreDictUnificator.addItemData(
                new ItemStack(ModItems.itemMetaFood, 1, aMetaID),
                new ItemData(Materials.MeatRaw, 3628800L, new MaterialStack(Materials.Bone, 403200L)));
        } else if (aOreDictNames.contains("listAllmeatcooked")) {
            GTOreDictUnificator.addItemData(
                new ItemStack(ModItems.itemMetaFood, 1, aMetaID),
                new ItemData(Materials.MeatCooked, 3628800L, new MaterialStack(Materials.Bone, 403200L)));
        }
    }

    public static void registerFoodsToOreDict() {
        for (int aMetaID = 0; aMetaID < mTotalMetaItems; aMetaID++) {
            ArrayList<String> aOreDictNames = mOreDictNames.get(aMetaID);
            if (aOreDictNames != null && !aOreDictNames.isEmpty()) {
                ItemStack aFoodStack = ItemUtils.simpleMetaStack(ModItems.itemMetaFood, aMetaID, 1);
                for (String aOreName : aOreDictNames) {
                    ItemUtils.addItemToOreDictionary(aFoodStack, aOreName);
                }
            }
        }
    }

    public BaseItemMetaFood() {
        super(0, 0, false);
        this.setHasSubtypes(true);
        this.setNoRepair();
        this.setMaxStackSize(64);
        this.setMaxDamage(0);
        this.setUnlocalizedName("BasicMetaFood");
        this.setCreativeTab(AddToCreativeTab.tabMisc);
        GameRegistry.registerItem(this, this.getUnlocalizedName());
    }

    private static int getMetaKey(ItemStack aStack) {
        return aStack.getItemDamage();
    }

    // Heal Amount
    @Override
    public int func_150905_g(ItemStack aStack) {
        return mHealAmountMap.get(getMetaKey(aStack));
    }

    // Saturation Amount
    @Override
    public float func_150906_h(ItemStack aStack) {
        return mSaturationAmountMap.get(getMetaKey(aStack));
    }

    // Whether wolves like this food, sadly doesn't support meta items
    @Override
    public boolean isWolfsFavoriteMeat() {
        return false;
    }

    @Override
    protected void onFoodEaten(final ItemStack aStack, final World aWorld, final EntityPlayer aPlayer) {
        // super.onFoodEaten(stack, world, player);
        ArrayList<PotionEffectPackage> aPotionEffects = mPotionEffectsMap.get(getMetaKey(aStack));
        if (!aWorld.isRemote && aPotionEffects != null && !aPotionEffects.isEmpty()) {
            for (PotionEffectPackage aFoodEffect : aPotionEffects) {
                if (MathUtils.randInt(0, 100) <= aFoodEffect.getChance() || aFoodEffect.getChance() == 100) {
                    PotionEffect aEffect = aFoodEffect.getEffect();
                    if (aEffect != null && aEffect.getPotionID() > 0) {
                        aPlayer.addPotionEffect(
                            new PotionEffect(
                                aEffect.getPotionID(),
                                aEffect.getDuration() * 20,
                                aEffect.getAmplifier(),
                                aEffect.getIsAmbient()));
                    }
                }
            }
        }

        boolean aHasEpcialBehaviour = mHasSpecialBehaviourMap.get(getMetaKey(aStack));
        if (!aWorld.isRemote && aHasEpcialBehaviour) {
            SpecialFoodBehaviour aBehaviour = mSpecialBehaviourMap.get(getMetaKey(aStack));
            if (aBehaviour != null) {
                aBehaviour.doBehaviour(aPlayer);
            }
        }
    }

    @Override
    public ItemStack onEaten(ItemStack aStack, World aWorld, EntityPlayer aPlayer) {
        return super.onEaten(aStack, aWorld, aPlayer);
    }

    @Override
    public int getItemStackLimit(ItemStack aStack) {
        return mMaxStackSizeMap.get(getMetaKey(aStack));
    }

    @Override
    public boolean isDamageable() {
        return false;
    }

    @Override
    public boolean shouldRotateAroundWhenRendering() {
        return super.shouldRotateAroundWhenRendering();
    }

    @Override
    public void onUpdate(ItemStack p_77663_1_, World p_77663_2_, Entity p_77663_3_, int p_77663_4_,
        boolean p_77663_5_) {
        super.onUpdate(p_77663_1_, p_77663_2_, p_77663_3_, p_77663_4_, p_77663_5_);
    }

    @Override
    public String getItemStackDisplayName(ItemStack aStack) {
        return super.getItemStackDisplayName(aStack);
    }

    @Override
    public void addInformation(ItemStack aStack, EntityPlayer p_77624_2_, List aList, boolean p_77624_4_) {
        try {
            String aTooltip = mTooltipMap.get(getMetaKey(aStack));
            if (aTooltip != null && !aTooltip.isEmpty()) {
                aList.add(aTooltip);
            }
        } catch (Throwable t) {
            t.printStackTrace();
        }
        super.addInformation(aStack, p_77624_2_, aList, p_77624_4_);
    }

    @Override
    public EnumRarity getRarity(ItemStack aStack) {
        return mRarityMap.get(getMetaKey(aStack));
    }

    @Override
    public boolean requiresMultipleRenderPasses() {
        return false;
    }

    @Override
    public void getSubItems(Item aItem, CreativeTabs p_150895_2_, List aList) {
        for (int i = 0; i < mIconMap.size(); i++) {
            aList.add(ItemUtils.simpleMetaStack(aItem, i, 1));
        }
    }

    @Override
    public boolean getIsRepairable(ItemStack p_82789_1_, ItemStack p_82789_2_) {
        return false;
    }

    @Override
    public boolean isRepairable() {
        return false;
    }

    @Override
    public boolean isBookEnchantable(ItemStack stack, ItemStack book) {
        return false;
    }

    @Override
    public boolean showDurabilityBar(ItemStack stack) {
        return false;
    }

    @Override
    public int getItemEnchantability() {
        return 0;
    }

    @Override
    public int getItemEnchantability(ItemStack stack) {
        return 0;
    }

    @Override
    public void registerIcons(final IIconRegister u) {
        for (int i = 0; i < mTotalMetaItems; i++) {
            String aPath = GTPlusPlus.ID + ":" + "food/MetaItem1/" + i;
            mIconMap.put(i, u.registerIcon(aPath));
        }
    }

    @Override
    public IIcon getIconFromDamageForRenderPass(final int damage, final int pass) {
        return mIconMap.get(damage);
    }

    @Override
    public IIcon getIconFromDamage(int damage) {
        return mIconMap.get(damage);
    }

    @Override
    public IIcon getIcon(ItemStack aStack, int renderPass, EntityPlayer player, ItemStack usingItem, int useRemaining) {
        return mIconMap.get(getMetaKey(aStack));
    }

    @Override
    public IIcon getIcon(ItemStack aStack, int pass) {
        return mIconMap.get(getMetaKey(aStack));
    }

    @Override
    public String getUnlocalizedName(ItemStack stack) {
        return super.getUnlocalizedName() + "." + stack.getItemDamage();
    }

    private static class PotionEffectPackage {

        private final PotionEffect mEffect;
        private final int mChance;

        private PotionEffectPackage(PotionEffect aEffect, int aChance) {
            mEffect = aEffect;
            mChance = aChance;
        }

        public PotionEffect getEffect() {
            return this.mEffect;
        }

        public int getChance() {
            return this.mChance;
        }
    }

    private static ArrayList<PotionEffectPackage> getPotionEffectPackage(PotionEffectPackage... aEffects) {
        ArrayList<PotionEffectPackage> aPackage = new ArrayList<>();
        if (aEffects != null && aEffects.length > 0) {
            Collections.addAll(aPackage, aEffects);
        }
        return aPackage;
    }

    private static ArrayList<String> getOreDictNamesAsArrayList(String... aOreDictNames) {
        ArrayList<String> aPackage = new ArrayList<>();
        if (aOreDictNames != null) {
            aPackage.addAll(Arrays.asList(aOreDictNames));
        }
        return aPackage;
    }

    private static class EffectWeaknessBasic extends PotionEffectPackage {

        protected EffectWeaknessBasic(int aChance) {
            super(new PotionEffect(Potion.weakness.getId(), 1, 20), aChance);
        }
    }

    private static class EffectWeaknessModerate extends PotionEffectPackage {

        protected EffectWeaknessModerate(int aChance) {
            super(new PotionEffect(Potion.weakness.getId(), 2, 40), aChance);
        }
    }

    private static class EffectWeaknessSevere extends PotionEffectPackage {

        protected EffectWeaknessSevere(int aChance) {
            super(new PotionEffect(Potion.weakness.getId(), 3, 60), aChance);
        }
    }

    private static class EffectSlownessBasic extends PotionEffectPackage {

        protected EffectSlownessBasic(int aChance) {
            super(new PotionEffect(Potion.moveSlowdown.getId(), 1, 20), aChance);
        }
    }

    private static class EffectSlownessModerate extends PotionEffectPackage {

        protected EffectSlownessModerate(int aChance) {
            super(new PotionEffect(Potion.moveSlowdown.getId(), 2, 40), aChance);
        }
    }

    private static class EffectSlownessSevere extends PotionEffectPackage {

        protected EffectSlownessSevere(int aChance) {
            super(new PotionEffect(Potion.moveSlowdown.getId(), 3, 60), aChance);
        }
    }

    private abstract static class SpecialFoodBehaviour {

        protected final int mChance;

        public SpecialFoodBehaviour(int aChance) {
            mChance = aChance;
        }

        public final void doBehaviour(EntityPlayer aPlayer) {
            if (aPlayer != null && !aPlayer.worldObj.isRemote) {
                if (MathUtils.randInt(0, 100) < mChance || mChance == 100) {
                    behaviour(aPlayer);
                }
            }
        }

        protected abstract void behaviour(EntityPlayer aPlayer);
    }

    private static class setOnFire extends SpecialFoodBehaviour {

        public setOnFire() {
            super(100);
        }

        @Override
        public void behaviour(EntityPlayer aPlayer) {
            EntityUtils.setEntityOnFire(aPlayer, 5);
        }
    }
}