summaryrefslogtreecommitdiff
path: root/TF2 Crates/src/main/java/tf2crates/handler/DeathHandler.java
blob: b680e4032aa964a6d464c36b3256d5bc2e30197e (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
package tf2crates.handler;

import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.monster.EntityMob;
import net.minecraft.entity.projectile.EntityArrow;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.event.entity.living.LivingDropsEvent;
import tf2crates.ReferenceTC;
import tf2crates.ServerProxyTC;
import tf2crates.crate.Crates;
import tlhpoeCore.util.MathUtil;

public class DeathHandler {
	public static int[]	crateChance;
	public static int[]	keyChance;

	@SubscribeEvent
	public void mobDeath(LivingDropsEvent event) {
		EntityLivingBase entity = event.entityLiving;

		if (entity instanceof EntityMob) {
			if (MathUtil.getChance(crateChance[0], crateChance[1])) {
				String special = ReferenceTC.CURRENT_SPECIAL_CRATE;

				if (special != null && MathUtil.getChance(1, 5)) {
					event.drops.add(new EntityItem(entity.worldObj,
							entity.posX, entity.posY + 1, entity.posZ,
							new ItemStack(ServerProxyTC.crate, 1,
									Crates.getSeriesFromName(special))));
				} else {
					event.drops.add(new EntityItem(entity.worldObj,
							entity.posX, entity.posY + 1, entity.posZ,
							new ItemStack(ServerProxyTC.crate, 1, MathUtil
									.nextInt(Crates.getNumberOfSeries()
											- Crates.getNumberOfSpecialCrates()))));
				}
			}

			if (MathUtil.getChance(keyChance[0], keyChance[1])) {
				event.drops.add(new EntityItem(entity.worldObj,
						entity.posX, entity.posY + 1, entity.posZ,
						new ItemStack(ServerProxyTC.key, 1)));
			}

			Entity attacker = event.source.getSourceOfDamage();

			if (attacker != null) {
				EntityLivingBase attackerL = null;

				if (attacker instanceof EntityLivingBase) {
					attackerL = (EntityLivingBase) attacker;
				} else if (attacker instanceof EntityArrow) {
					EntityArrow arrow = (EntityArrow) attacker;

					if (arrow.shootingEntity != null
							&& arrow.shootingEntity instanceof EntityLivingBase) {
						attackerL =
								(EntityLivingBase) arrow.shootingEntity;
					}
				}

				if (attackerL != null) {
					ItemStack held = attackerL.getHeldItem();

					if (held != null) {
						NBTTagCompound nbt = held.getTagCompound();

						if (nbt != null && (nbt.hasKey("Strange")
								|| nbt.hasKey("Golden"))) {
							nbt.setInteger("Strange",
									nbt.getInteger("Strange") + 1);
						}

						if (held.getItem() != null && held
								.getItem() == ServerProxyTC.powerJack) {
							attackerL.heal(
									attackerL.getMaxHealth() * 0.25F);
						}
					}
				}
			}
		}
	}
}