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

import com.sosnitzka.taiga.util.Utils;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
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.MinecraftForge;
import net.minecraftforge.common.util.FakePlayer;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import slimeknights.tconstruct.library.tools.ToolNBT;
import slimeknights.tconstruct.library.utils.TagUtil;
import slimeknights.tconstruct.library.utils.TinkerUtil;
import slimeknights.tconstruct.library.utils.ToolHelper;

import static com.sosnitzka.taiga.Keybindings.altKey;


public class TraitBerserk extends TraitProgressiveStats {

    protected static int TICK_PER_STAT = 8;

    public TraitBerserk() {
        super(TraitBerserk.class.getSimpleName().toLowerCase().substring(5), TextFormatting.RED);
        MinecraftForge.EVENT_BUS.register(this);
    }

    @Override
    public void miningSpeed(ItemStack tool, PlayerInteractEvent.BreakSpeed event) {
        NBTTagCompound tag = TagUtil.getExtraTag(tool);
        Utils.GeneralNBTData data = Utils.GeneralNBTData.read(tag);
        if (!data.active) return;
        event.setNewSpeed(event.getNewSpeed() * 4);
    }

    @Override
    public float damage(ItemStack tool, EntityLivingBase player, EntityLivingBase target, float damage, float
            newDamage, boolean isCritical) {
        NBTTagCompound tag = TagUtil.getExtraTag(tool);
        Utils.GeneralNBTData data = Utils.GeneralNBTData.read(tag);
        if (!data.active) return newDamage;
        return newDamage * 4;
    }

    @Override
    public void onUpdate(ItemStack tool, World world, Entity entity, int itemSlot, boolean isSelected) {
        if (!world.isRemote) {
            NBTTagCompound tag = TagUtil.getExtraTag(tool);
            EntityLivingBase player = (EntityLivingBase) entity;
            Utils.GeneralNBTData data = Utils.GeneralNBTData.read(tag);
            NBTTagCompound root = TagUtil.getTagSafe(tool);
            StatNBT distributed = getBonus(root);
            if (data.active) {
                if (!TagUtil.hasEnchantEffect(root))
                    TagUtil.setEnchantEffect(root, true);

                if (entity instanceof FakePlayer || entity.ticksExisted % TICK_PER_STAT != 0) {
                    return;
                }

                ToolNBT stat = TagUtil.getToolStats(tool);
                if (random.nextFloat() > .80f) {
                    stat.durability -= 1;
                    distributed.durability -= 1;
                } else
                    ToolHelper.damageTool(tool, 1, player);
                TagUtil.setToolTag(root, stat.get());
                setBonus(root, distributed);
            } else {
                if (TagUtil.hasEnchantEffect(root))
                    TagUtil.setEnchantEffect(root, false);
            }
        }
    }

    @SubscribeEvent
    public void onRightClickItem(PlayerInteractEvent.RightClickItem event) {
        World w = event.getWorld();
        ItemStack tool = event.getEntityPlayer().getHeldItemMainhand();
        if (!w.isRemote && TinkerUtil.hasTrait(TagUtil.getTagSafe(tool), identifier) && altKey.isKeyDown()) {
            NBTTagCompound tag = TagUtil.getExtraTag(tool);
            Utils.GeneralNBTData data = Utils.GeneralNBTData.read(tag);
            NBTTagCompound root = TagUtil.getTagSafe(tool);
            StatNBT distributed = getBonus(root);
            ToolNBT stat = TagUtil.getToolStats(tool);
            if (data.active) {
                data.active = false;
                TagUtil.setEnchantEffect(root, false);
                TagUtil.setExtraTag(root, tag);
                data.write(tag);
            } else {
                stat.durability -= 10;
                distributed.durability -= 10;
                TagUtil.setToolTag(root, stat.get());
                setBonus(root, distributed);
                data.active = true;
                data.write(tag);

                TagUtil.setExtraTag(root, tag);
                data.write(tag);
            }
        }
    }
}