summaryrefslogtreecommitdiff
path: root/src/main/java/darkknight/jewelrycraft/effects/EffectEnderPearl.java
blob: 562d83e28e60cab46a03a278ce384b95c34d503b (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package darkknight.jewelrycraft.effects;

import java.util.Iterator;
import java.util.List;

import darkknight.jewelrycraft.api.ModifierEffects;
import darkknight.jewelrycraft.item.ItemBracelet;
import darkknight.jewelrycraft.item.ItemEarrings;
import darkknight.jewelrycraft.item.ItemNecklace;
import darkknight.jewelrycraft.item.ItemRing;
import net.minecraft.entity.Entity;
import net.minecraft.entity.item.EntityTNTPrimed;
import net.minecraft.entity.monster.EntityEnderman;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.projectile.EntityArrow;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.potion.Potion;
import net.minecraft.potion.PotionEffect;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.DamageSource;

public class EffectEnderPearl extends ModifierEffects {
	public EffectEnderPearl() {
		super(new ItemStack(Items.ender_pearl));
	}

	@Override
	public void action(ItemStack item, EntityPlayer player,
			Item jewelry) {
		if (jewelry instanceof ItemEarrings) {
			AxisAlignedBB axisalignedbb = player.boundingBox
					.expand(2.0D, 2.0D, 2.0D);
			List<?> list = player.worldObj
					.getEntitiesWithinAABB(
							EntityArrow.class,
							axisalignedbb);
			if (!player.worldObj.isRemote && list != null
					&& !list.isEmpty()) {
				Iterator<?> iterator = list.iterator();
				while (iterator.hasNext()) {
					EntityArrow arrow = (EntityArrow) iterator
							.next();
					if (arrow.shootingEntity == null
							|| !(arrow.shootingEntity
									.equals(player)))
						// Negative earrings
						if (rand.nextInt(
								30) == 0) {
							arrow.worldObj.createExplosion(
									new EntityTNTPrimed(
											arrow.worldObj),
									arrow.posX,
									arrow.posY,
									arrow.posZ,
									2F,
									true);
							arrow.setDead();
						}
						// Positive earrings
						else
							arrow.setPosition(
									arrow.posX + rand
											.nextInt(16)
											- rand.nextInt(16),
									arrow.posY + rand
											.nextInt(16),
									arrow.posZ + rand
											.nextInt(16)
											- rand.nextInt(16));
				}
			}
		}
		// Negative Necklace
		if (jewelry instanceof ItemNecklace)
			player.addPotionEffect(new PotionEffect(
					Potion.resistance.id, 60, -10,
					true));
		// Negative bracelet
		if (jewelry instanceof ItemBracelet && player.isInWater())
			player.setPositionAndUpdate(
					player.posX + rand.nextInt(16)
							- rand.nextInt(16),
					player.posY + rand.nextInt(4),
					player.posZ + rand.nextInt(16)
							- rand.nextInt(16));
	}

	@Override
	public void onPlayerAttacked(ItemStack item, EntityPlayer player,
			DamageSource source, Item jewelry, float amount) {
		// Positive Necklace
		if (jewelry instanceof ItemNecklace
				&& source.getEntity() != null)
			source.getEntity().attackEntityFrom(source,
					amount);
		// Positive bracelet
		if (jewelry instanceof ItemBracelet
				&& !player.worldObj.isRemote) {
			int id = player.worldObj.provider.dimensionId;
			if (player.getHealth() <= 6F)
				if (player.getBedLocation(id) != null)
					player.setPositionAndUpdate(player
							.getBedLocation(id).posX,
							player.getBedLocation(
									id).posY,
							player.getBedLocation(
									id).posZ);
				else
					player.setPositionAndUpdate(
							player.worldObj.getSpawnPoint().posX,
							player.worldObj.getSpawnPoint().posY,
							player.worldObj.getSpawnPoint().posZ);
		}
	}

	@Override
	public void onEntityAttacked(ItemStack item, EntityPlayer player,
			Entity target, Item jewelry, float amount) {
		if (jewelry instanceof ItemRing) {
			// Negative ring
			if (target instanceof EntityEnderman)
				player.addPotionEffect(new PotionEffect(
						Potion.weakness.id, 400, 2,
						true));
			// Positive ring
			else
				target.setPosition(target.posX
						+ rand.nextInt(16)
						- rand.nextInt(16),
						target.posY + rand.nextInt(
								4),
						target.posZ + rand.nextInt(
								16)
								- rand.nextInt(16));
		}
	}

	@Override
	public boolean onEntityAttackedCancellable(ItemStack item,
			EntityPlayer player, Entity target, Item jewelry,
			float amount) {
		return false;
	}
}