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

import net.minecraft.entity.Entity;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.text.TextFormatting;
import slimeknights.tconstruct.library.modifiers.ModifierNBT;
import slimeknights.tconstruct.library.tools.ToolNBT;
import slimeknights.tconstruct.library.traits.AbstractTrait;
import slimeknights.tconstruct.library.utils.TagUtil;

/**
 * Base class for tools that progressively gain/award stats.
 * The modifier persists 2 different stat-data on the tool:
 * - A 'pool' of stats to award
 * - A 'bonus' of already awarded stats
 * <p>
 * The modifier reapplies the 'bonus' stats on application.
 * The pool is not touched inheritly but only provided for the logic of the deriving trait.
 */
public abstract class TraitProgressiveStats extends AbstractTrait {

    protected final String pool_key;    // Key to the tag that contains the free unassigned
    protected final String applied_key; // Key to the tag that contains the already applied bonus stats

    public TraitProgressiveStats(String identifier, TextFormatting color) {
        super(identifier, color);

        pool_key = identifier + "StatPool";
        applied_key = identifier + "StatBonus";
    }

    /* Modifier management */

    protected static StatNBT getStats(NBTTagCompound root, String key) {
        return ModifierNBT.readTag(TagUtil.getTagSafe(TagUtil.getExtraTag(root), key), StatNBT.class);
    }

    protected static void setStats(NBTTagCompound root, StatNBT data, String key) {
        NBTTagCompound extra = TagUtil.getExtraTag(root);
        NBTTagCompound tag = new NBTTagCompound();
        data.write(tag);
        extra.setTag(key, tag);
        TagUtil.setExtraTag(root, extra);
    }

    @Override
    public void applyEffect(NBTTagCompound rootCompound, NBTTagCompound modifierTag) {
        super.applyEffect(rootCompound, modifierTag);
        // called on tool loading only
        // we just apply the saved bonus stats
        ToolNBT data = TagUtil.getToolStats(rootCompound);
        StatNBT bonus = getBonus(rootCompound);

        data.durability += bonus.durability;
        data.speed += bonus.speed;
        data.attack += bonus.attack;

        TagUtil.setToolTag(rootCompound, data.get());
    }

    protected boolean hasPool(NBTTagCompound root) {
        return TagUtil.getExtraTag(root).hasKey(pool_key);
    }

    protected StatNBT getPool(NBTTagCompound root) {
        return getStats(root, pool_key);
    }

    protected void setPool(NBTTagCompound root, StatNBT data) {
        setStats(root, data, pool_key);
    }

    protected StatNBT getBonus(NBTTagCompound root) {
        return getStats(root, applied_key);
    }

    protected void setBonus(NBTTagCompound root, StatNBT data) {
        setStats(root, data, applied_key);
    }

    protected boolean playerIsBreakingBlock(Entity entity) {
        return false;
    }

    public static class StatNBT extends ModifierNBT {

        // statpool
        public int durability;
        public float attack;
        public float speed;

        @Override
        public void read(NBTTagCompound tag) {
            super.read(tag);
            durability = tag.getInteger("durability");
            attack = tag.getFloat("attack");
            speed = tag.getFloat("speed");
        }

        @Override
        public void write(NBTTagCompound tag) {
            super.write(tag);
            tag.setInteger("durability", durability);
            tag.setFloat("attack", attack);
            tag.setFloat("speed", speed);
        }
    }
}