summaryrefslogtreecommitdiff
path: root/TF2 Crates/src/main/java/tf2crates/item/ItemKnife.java
blob: 46412ee1b61f97908d1d278175c78c25fd6de8d2 (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
package tf2crates.item;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemSword;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.IIcon;
import tf2crates.ReferenceTC;
import tf2crates.ServerProxyTC;
import tf2crates.crate.RandomLoot;
import tf2crates.entity.EntityDamageSourceBackstab;
import tlhpoeCore.network.MessagePlaySound;
import tlhpoeCore.util.MiscUtil;

public class ItemKnife extends ItemSword {
	public static double	backstabDifficulty	= 0.75D;

	public IIcon			goldTexture;

	public ItemKnife(String name) {
		super(ServerProxyTC.MOD_WEAPON);

		this.setUnlocalizedName(name);
		this.setTextureName(ReferenceTC.ID + ":weapon/" + name);

		RandomLoot.WEAPONS.add(this);
	}

	public ItemKnife(String name, float dmg) {
		this(name);

		MiscUtil.replaceField("field_150934_a", ItemSword.class, dmg,
				this);
	}

	@Override
	@SideOnly(Side.CLIENT)
	public void registerIcons(IIconRegister register) {
		super.registerIcons(register);

		this.goldTexture =
				register.registerIcon(ReferenceTC.ID + ":weapon/gold/"
						+ this.getUnlocalizedName().substring(5));
	}

	@Override
	@SideOnly(Side.CLIENT)
	public IIcon getIconIndex(ItemStack itemStack) {
		NBTTagCompound nbt = itemStack.getTagCompound();

		if (nbt != null && nbt.getBoolean("Golden")) {
			return this.goldTexture;
		}

		return super.getIconIndex(itemStack);
	}

	@Override
	public IIcon getIcon(ItemStack itemStack, int pass) {
		NBTTagCompound nbt = itemStack.getTagCompound();

		if (nbt != null && nbt.getBoolean("Golden")) {
			return this.goldTexture;
		}

		return super.getIcon(itemStack, pass);
	}

	@Override
	public boolean hitEntity(ItemStack itemStack, EntityLivingBase prey,
			EntityLivingBase attacker) {
		itemStack.damageItem(1, attacker);

		float pYaw = prey.rotationYawHead % 360F;

		pYaw = pYaw < 0 ? 360 + pYaw : prey.rotationYawHead;

		float aYaw = attacker.rotationYawHead < 0
				? 360 + attacker.rotationYawHead
				: attacker.rotationYawHead;

		float newRot = pYaw - aYaw;

		if (newRot < (90 * backstabDifficulty)
				&& newRot > (-90 * backstabDifficulty)) {
			if (!attacker.worldObj.isRemote) {
				new MessagePlaySound(ReferenceTC.ID + ":tf2crates.crit")
						.sendTo((EntityPlayerMP) attacker);
			}

			if (itemStack.getItem() == ServerProxyTC.conniversKunai) {
				attacker.heal(prey.getMaxHealth());
			}

			prey.attackEntityFrom(EntityDamageSourceBackstab
					.causeBackstabDamage(attacker),
					prey.getMaxHealth() * 600);
		}

		return false;
	}
}