summaryrefslogtreecommitdiff
path: root/src/main/java/com/sosnitzka/taiga/traits/TraitTantrum.java
blob: d0b6208007214378ae5417f992e84d12c0e5f7e6 (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.Keybindings;
import com.sosnitzka.taiga.util.Utils;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.player.ItemTooltipEvent;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import net.minecraftforge.event.world.BlockEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import slimeknights.tconstruct.common.Sounds;
import slimeknights.tconstruct.library.traits.AbstractTrait;
import slimeknights.tconstruct.library.utils.TagUtil;
import slimeknights.tconstruct.library.utils.TinkerUtil;

import static com.sosnitzka.taiga.Blocks.tiberiumOre;

/*
 *  Collects tiberium, to release it for an explosion
 */
public class TraitTantrum extends AbstractTrait {

    public static float max_charges = 12f;
    public static float max_power = 5;

    public TraitTantrum() {
        super("tantrum", TextFormatting.RED);
        MinecraftForge.EVENT_BUS.register(this);
    }

    @Override
    public void blockHarvestDrops(ItemStack tool, BlockEvent.HarvestDropsEvent event) {
        World w = event.getWorld();
        if (!w.isRemote) {
            if (event.getState().getBlock().equals(tiberiumOre)) {
                event.getDrops().clear();
                NBTTagCompound tag = TagUtil.getExtraTag(tool);
                Data data = Data.read(tag);
                if (data.amount >= max_charges) {
                    return;
                }
                data.amount += (0.25f + Utils.round2(random.nextDouble() / 4));
                if (data.amount >= max_charges) {
                    TagUtil.setEnchantEffect(tool, true);
                    if (event.getHarvester() instanceof EntityPlayerMP) {
                        Sounds.PlaySoundForPlayer(event.getHarvester(), Sounds.shocking_discharge, 1f, 0.8f + .2f *
                                random.nextFloat());
                    }
                }
                data.write(tag);
                TagUtil.setExtraTag(tool, tag);
            }
        }
    }

    @SubscribeEvent
    public void rightClickItem(PlayerInteractEvent.RightClickItem event) {
        World w = event.getWorld();
        BlockPos pos = event.getPos();
        ItemStack tool = event.getEntityPlayer().getHeldItemMainhand();
        if (!w.isRemote && TinkerUtil.hasTrait(TagUtil.getTagSafe(tool), identifier) && Keybindings.altKey.isKeyDown()) {
            NBTTagCompound tag = TagUtil.getExtraTag(tool);
            Data data = Data.read(tag);
            if (data.amount > 1f) {
                double d = Math.min(Utils.round2(random.nextDouble() * data.amount), max_power);
                w.newExplosion(event.getEntityPlayer(), pos.getX(), pos.getY(), pos.getZ(), (float) Math.pow((double)
                        1.2f, d), false, true);
                data.amount -= d;
                data.write(tag);
                TagUtil.setExtraTag(tool, tag);
                TagUtil.setEnchantEffect(tool, false);
            }
        }
    }

    @SideOnly(Side.CLIENT)
    @SubscribeEvent
    public void onItemTooltip(ItemTooltipEvent e) {
        ItemStack tool = e.getItemStack();
        if (TinkerUtil.hasTrait(TagUtil.getTagSafe(tool), identifier)) {
            NBTTagCompound tag = TagUtil.getExtraTag(tool);
            Data data = Data.read(tag);
            e.getToolTip().add(TextFormatting.RED + "Charge: " + data.amount);
        }
    }

    public static class Data {
        float amount;

        public static Data read(NBTTagCompound tag) {
            Data data = new Data();
            data.amount = tag.getFloat("amount");
            return data;
        }

        public void write(NBTTagCompound tag) {
            tag.setFloat("amount", amount);
        }
    }
}