summaryrefslogtreecommitdiff
path: root/src/main/java/com/sosnitzka/taiga/traits/TraitDecay.java
blob: a1d1a57f099fd25dcb7625bffc390ebaf732fbf2 (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
package com.sosnitzka.taiga.traits;

import com.google.common.collect.ImmutableList;
import net.minecraft.entity.Entity;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.world.World;
import net.minecraftforge.common.util.FakePlayer;
import slimeknights.tconstruct.library.materials.HeadMaterialStats;
import slimeknights.tconstruct.library.tools.ToolNBT;
import slimeknights.tconstruct.library.utils.TagUtil;

import java.util.List;

/**
 * Gives the tool bonus stats on crafting.
 * The bonus stats are distributed over time and are more or less random.
 * The stats that will be rewarded are already designated on the first time the tool is crafted
 */
public class TraitDecay extends TraitProgressiveStats {

    protected static int TICK_PER_STAT = 24;
    protected static int DURABILITY_STEP = 1;
    protected static float SPEED_STEP = 0.05f;
    protected static float ATTACK_STEP = 0.05f;

    public TraitDecay() {
        super("decay", TextFormatting.GREEN);
    }

    @Override
    public void applyEffect(NBTTagCompound rootCompound, NBTTagCompound modifierTag) {
        // check if we have stats already distributed, and if not add them
        if (!hasPool(rootCompound)) {
            // ok, we need new stats. Let the fun begin!
            StatNBT data = new StatNBT();

            int statPoints = 800; // we distribute a whopping X points worth of stats!
            for (; statPoints > 0; statPoints--) {
                switch (random.nextInt(3)) {
                    // durability
                    case 0:
                        data.durability += DURABILITY_STEP;
                        break;
                    // speed
                    case 1:
                        data.speed += SPEED_STEP;
                        break;
                    // attack
                    case 2:
                        data.attack += ATTACK_STEP;
                        break;
                }
            }

            setPool(rootCompound, data);
        }

        super.applyEffect(rootCompound, modifierTag);
    }

    @Override
    public void onUpdate(ItemStack tool, World world, Entity entity, int itemSlot, boolean isSelected) {
        if (entity instanceof FakePlayer || entity.world.isRemote || entity.ticksExisted % TICK_PER_STAT != 0 || playerIsBreakingBlock(entity)) {
            return;
        }

        NBTTagCompound root = TagUtil.getTagSafe(tool);
        StatNBT distributed = getBonus(root);
        ToolNBT data = TagUtil.getToolStats(tool);

        // attack
        if (entity.ticksExisted % (TICK_PER_STAT * 3) == 0) {
            float A = ATTACK_STEP * random.nextFloat();
            data.attack -= A;
            distributed.attack -= A;
        }
        // speed
        else if (entity.ticksExisted % (TICK_PER_STAT * 2) == 0) {
            float S = SPEED_STEP * random.nextFloat();
            data.speed -= S;
            distributed.speed -= S;
        }
        // durability
        else {
            int D = random.nextInt(DURABILITY_STEP) + 1;
            data.durability -= D;
            distributed.durability -= D;
        }

        // update tool stats
        TagUtil.setToolTag(root, data.get());
        // update statistics on distributed stats
        setBonus(root, distributed);
    }

    @Override
    public List<String> getExtraInfo(ItemStack tool, NBTTagCompound modifierTag) {
        StatNBT pool = getBonus(TagUtil.getTagSafe(tool));

        return ImmutableList.of(HeadMaterialStats.formatDurability(pool.durability),
                HeadMaterialStats.formatMiningSpeed(pool.speed),
                HeadMaterialStats.formatAttack(pool.attack));
    }
}