From d17826bf67eec6de4561041832670d18074655e8 Mon Sep 17 00:00:00 2001 From: OnyxDarkKnight Date: Mon, 8 Jun 2015 23:18:07 +0100 Subject: Improved Hearts and Added Spawn Eggs for them. --- .../darkknight/jewelrycraft/item/ItemSpawnEgg.java | 177 +++++++++++++++++++++ 1 file changed, 177 insertions(+) create mode 100644 src/main/java/darkknight/jewelrycraft/item/ItemSpawnEgg.java (limited to 'src/main/java/darkknight/jewelrycraft/item/ItemSpawnEgg.java') diff --git a/src/main/java/darkknight/jewelrycraft/item/ItemSpawnEgg.java b/src/main/java/darkknight/jewelrycraft/item/ItemSpawnEgg.java new file mode 100644 index 0000000..708c69c --- /dev/null +++ b/src/main/java/darkknight/jewelrycraft/item/ItemSpawnEgg.java @@ -0,0 +1,177 @@ +package darkknight.jewelrycraft.item; + +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +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; +import cpw.mods.fml.relauncher.Side; +import cpw.mods.fml.relauncher.SideOnly; +import darkknight.jewelrycraft.entities.EntityHeart; +import darkknight.jewelrycraft.util.Variables; + +/** + * @author Betweenlands + */ +public class ItemSpawnEgg extends ItemMonsterPlacer { + private static final Map eggTypes = new LinkedHashMap(); + + public static void registerSpawnEgg(Class 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; + } + + @SuppressWarnings({ "unchecked", "rawtypes" }) + @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 entityClass; + int primaryColor; + int secondaryColor; + + EggData(int id, String entityName, Class 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 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; + } + } +} -- cgit v1.2.3