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

import com.sosnitzka.taiga.Keybindings;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.init.Blocks;
import net.minecraft.init.SoundEvents;
import net.minecraft.item.ItemStack;
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.PlayerInteractEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import slimeknights.tconstruct.library.traits.AbstractTrait;
import slimeknights.tconstruct.library.utils.TagUtil;
import slimeknights.tconstruct.library.utils.TinkerUtil;
import slimeknights.tconstruct.library.utils.ToolHelper;


public class TraitPorted extends AbstractTrait {

    public static int distance = 10;

    public TraitPorted() {
        super("ported", TextFormatting.DARK_PURPLE);
        MinecraftForge.EVENT_BUS.register(this);
    }

    @SubscribeEvent
    public void onItemRightClick(PlayerInteractEvent.RightClickItem e) {
        ItemStack tool = e.getEntityPlayer().getHeldItemMainhand();
        if (TinkerUtil.hasTrait(TagUtil.getTagSafe(tool), identifier) && Keybindings.altKey.isKeyDown())
            teleport(e.getEntityPlayer(), e.getWorld());
    }

    @Override
    public void afterBlockBreak(ItemStack tool, World world, IBlockState state, BlockPos pos, EntityLivingBase
            player, boolean wasEffective) {
        if (random.nextFloat() <= 0.005) {
            player.playSound(SoundEvents.ENTITY_ENDERMEN_TELEPORT, 1.0F, 1.0F);
            teleport(player, world);
        }
    }

    @Override
    public void afterHit(ItemStack tool, EntityLivingBase player, EntityLivingBase target, float damage, boolean
            wasCritical, boolean wasHit) {
        if (random.nextFloat() <= 0.005) {
            target.playSound(SoundEvents.ENTITY_ENDERMEN_TELEPORT, 1.0F, 1.0F);
            teleport(player, player.getEntityWorld());
        }
    }

    private void teleport(EntityLivingBase e, World w) {
        BlockPos tPos = new BlockPos(e.getPosition().up(distance));
        if (e.getPosition().getY() >= 128) {
            return;
        }

        while (!w.getBlockState(tPos).equals(Blocks.AIR.getDefaultState()) && tPos.getY() <= 128) {
            tPos = tPos.up();
        }
        if (!w.getBlockState(tPos).equals(Blocks.AIR.getDefaultState())) {
            return;
        }
        e.setPosition(tPos.getX(), tPos.getY(), tPos.getZ());
        ToolHelper.damageTool(e.getHeldItemMainhand(), ToolHelper.getCurrentDurability(e.getHeldItemMainhand()) / 2 +
                1, e);
    }
}