summaryrefslogtreecommitdiff
path: root/src/main/java/darkknight/jewelrycraft/item/ItemSpawnEgg.java
blob: cceab129e6970c08b9dc4fc7e7a14d076d83ef39 (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package darkknight.jewelrycraft.item;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import darkknight.jewelrycraft.entities.EntityHeart;
import darkknight.jewelrycraft.util.Variables;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.IEntityLivingData;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemMonsterPlacer;
import net.minecraft.item.ItemStack;
import net.minecraft.util.Facing;
import net.minecraft.util.MathHelper;
import net.minecraft.util.MovingObjectPosition;
import net.minecraft.util.MovingObjectPosition.MovingObjectType;
import net.minecraft.util.StatCollector;
import net.minecraft.world.World;

/**
 * @author Betweenlands
 */
public class ItemSpawnEgg extends ItemMonsterPlacer {
	private static final Map<Short, EggData> eggTypes =
			new LinkedHashMap<Short, EggData>();

	public static void registerSpawnEgg(
			Class<? extends EntityLiving> entity, String entityName,
			int id, int eggBackgroundColor, int eggForegroundColor) {
		eggTypes.put((short) id, new EggData(id, entityName, entity,
				eggBackgroundColor, eggForegroundColor));
	}

	private static EggData getEggData(ItemStack is) {
		return eggTypes.get((short) is.getItemDamage());
	}

	public ItemSpawnEgg() {
		setHasSubtypes(true);
		setCreativeTab(CreativeTabs.tabMisc);
	}

	@Override
	public String getItemStackDisplayName(ItemStack is) {
		String s = StatCollector
				.translateToLocal(getUnlocalizedName() + ".name").trim();
		String mob = "";

		EggData egg = getEggData(is);
		if (egg != null)
			mob = StatCollector.translateToLocal("entity."
					+ Variables.MODID + "." + egg.entityName + ".name");

		return String.format(s, mob);
	}

	@Override
	public boolean onItemUse(ItemStack is, EntityPlayer player,
			World world, int x, int y, int z, int side, float hitX,
			float hitY, float hitZ) {
		if (world.isRemote)
			return true;

		Block block = world.getBlock(x, y, z);
		x += Facing.offsetsXForSide[side];
		y += Facing.offsetsYForSide[side];
		z += Facing.offsetsZForSide[side];

		EggData egg = getEggData(is);
		if (egg != null) {
			egg.spawnMob(world, x + 0.5D,
					y + (side == 1 && block != null
							&& block.getRenderType() == 11 ? 0.5D : 0D),
					z + 0.5D, is);

			if (!player.capabilities.isCreativeMode)
				--is.stackSize;
		}

		return true;
	}

	@Override
	public ItemStack onItemRightClick(ItemStack is, World world,
			EntityPlayer player) {
		if (world.isRemote)
			return is;

		MovingObjectPosition mop =
				getMovingObjectPositionFromPlayer(world, player, true);

		if (mop != null && mop.typeOfHit == MovingObjectType.BLOCK) {
			int x = mop.blockX, y = mop.blockY, z = mop.blockZ;

			if (!world.canMineBlock(player, x, y, z)
					|| !player.canPlayerEdit(x, y, z, mop.sideHit, is))
				return is;

			if (world.getBlock(x, y, z).getMaterial() == Material.water) {
				EggData egg = getEggData(is);
				if (egg != null) {
					egg.spawnMob(world, x, y, z, is);

					if (!player.capabilities.isCreativeMode)
						--is.stackSize;
				}
			}
		}

		return is;
	}

	public static EntityLiving getEntity(World world, double x, double y,
			double z, ItemStack is) {
		EggData egg = getEggData(is);
		return egg.spawnMob(world, x, y, z, is);
	}

	@Override
	@SideOnly(Side.CLIENT)
	public int getColorFromItemStack(ItemStack is, int pass) {
		EggData egg = getEggData(is);
		return egg != null
				? pass == 0 ? egg.primaryColor : egg.secondaryColor
				: 16777215;
	}

	@Override
	@SideOnly(Side.CLIENT)
	public void getSubItems(Item id, CreativeTabs tab, List list) {
		for (Short s : eggTypes.keySet())
			list.add(new ItemStack(id, 1, s));
	}

	static class EggData {
		private final short							id;
		String										entityName;
		private final Class<? extends EntityLiving>	entityClass;
		int											primaryColor;
		int											secondaryColor;

		EggData(int id, String entityName,
				Class<? extends EntityLiving> entityClass,
				int[] rgbPrimaryColor, int[] rgbSecondaryColor) {
			this(id, entityName, entityClass,
					rgbPrimaryColor[0] << 16 | rgbPrimaryColor[1] << 8
							| rgbPrimaryColor[2],
					rgbSecondaryColor[0] << 16 | rgbSecondaryColor[1] << 8
							| rgbSecondaryColor[2]);
		}

		EggData(int id, String entityName,
				Class<? extends EntityLiving> entityClass,
				int primaryColor, int secondaryColor) {
			this.id = (short) id;
			this.entityName = entityName;
			this.entityClass = entityClass;
			this.primaryColor = primaryColor;
			this.secondaryColor = secondaryColor;
		}

		public EntityLiving spawnMob(World world, double x, double y,
				double z, ItemStack is) {
			EntityLiving e = null;

			try {
				e = entityClass.getConstructor(World.class)
						.newInstance(world);
			} catch (Exception ex) {
				ex.printStackTrace();
				return null;
			}

			if (e == null)
				return null;

			if (e instanceof EntityHeart) {
				if (id == 2)
					((EntityHeart) e).setType("White");
				else if (id == 3)
					((EntityHeart) e).setType("Blue");
				else if (id == 4)
					((EntityHeart) e).setType("Black");
				else if (id == 6)
					((EntityHeart) e).setType("White");
				else if (id == 7)
					((EntityHeart) e).setType("Blue");
				else if (id == 8)
					((EntityHeart) e).setType("Black");
				else
					((EntityHeart) e).setType("Red");
			}
			e.setLocationAndAngles(x, y, z, MathHelper
					.wrapAngleTo180_float(world.rand.nextFloat() * 360F),
					0F);
			e.rotationYawHead = e.rotationYaw;
			e.renderYawOffset = e.rotationYaw;
			e.onSpawnWithEgg((IEntityLivingData) null);
			world.spawnEntityInWorld(e);
			e.playLivingSound();

			if (is.hasDisplayName())
				e.setCustomNameTag(is.getDisplayName());

			return e;
		}

		@Override
		public int hashCode() {
			return id;
		}
	}
}