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

import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityCreature;
import net.minecraft.entity.EntityList;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.SoundEvents;
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.living.LivingDeathEvent;
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;


public class TraitReviving extends AbstractTrait {

    public final float chance = 0.15f;

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

    @SubscribeEvent
    public void onEntityKill(LivingDeathEvent e) {
        BlockPos pos = e.getEntity().getPosition();
        World w = e.getEntity().getEntityWorld();
        if (!w.isRemote && e.getSource().getTrueSource() != null) {
            if (e.getSource().getTrueSource() instanceof EntityPlayer && e.getEntity() instanceof EntityCreature) {
                if (random.nextFloat() <= chance && TinkerUtil.hasTrait(TagUtil.getTagSafe(((EntityPlayer) e
                        .getSource().getTrueSource()).getHeldItemMainhand()), identifier)) {
                    int id = e.getEntity().getEntityId();
                    Entity ent = EntityList.createEntityByID(id, w);
                    if (ent != null) {
                        ent.setPosition(pos.getX(), pos.getY(), pos.getZ());
                        w.spawnEntity(ent);
                        e.getSource().getTrueSource().playSound(SoundEvents.AMBIENT_CAVE, 1.0F, 1.0F);
                    }
                }
            }
        }
    }
}