From fd20876189f020df0cdf0bd4a45e6712f82fc29d Mon Sep 17 00:00:00 2001
From: sk89q <the.sk89q@gmail.com>
Date: Sat, 8 Jan 2011 12:48:45 -0800
Subject: [PATCH] Implemented item drops.

---
 .../org/bukkit/craftbukkit/CraftItemDrop.java | 23 ++++++++++++++++
 .../org/bukkit/craftbukkit/CraftWorld.java    | 27 +++++++++++++++++++
 2 files changed, 50 insertions(+)
 create mode 100644 src/main/java/org/bukkit/craftbukkit/CraftItemDrop.java

diff --git a/src/main/java/org/bukkit/craftbukkit/CraftItemDrop.java b/src/main/java/org/bukkit/craftbukkit/CraftItemDrop.java
new file mode 100644
index 0000000000..0def182530
--- /dev/null
+++ b/src/main/java/org/bukkit/craftbukkit/CraftItemDrop.java
@@ -0,0 +1,23 @@
+package org.bukkit.craftbukkit;
+
+import net.minecraft.server.EntityItem;
+import org.bukkit.ItemDrop;
+import org.bukkit.ItemStack;
+
+/**
+ * Represents an item drop.
+ * 
+ * @author sk89q
+ */
+public class CraftItemDrop extends CraftEntity implements ItemDrop {
+    private EntityItem item;
+    
+    public CraftItemDrop(CraftServer server, EntityItem ent) {
+        super(server, ent);
+        this.item = ent;
+    }
+
+    public ItemStack getItemStack() {
+        return new CraftItemStack(item.a);
+    }
+}
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
index 0edeead30b..35f2a3a833 100644
--- a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
+++ b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
@@ -9,6 +9,7 @@ import java.util.Random;
 
 import net.minecraft.server.EntityBoat;
 import net.minecraft.server.EntityEgg;
+import net.minecraft.server.EntityItem;
 import net.minecraft.server.EntityLiving;
 import net.minecraft.server.EntityPlayerMP;
 import net.minecraft.server.EntitySnowball;
@@ -22,6 +23,8 @@ import org.bukkit.Arrow;
 import org.bukkit.Block;
 import org.bukkit.Boat;
 import org.bukkit.Chunk;
+import org.bukkit.ItemDrop;
+import org.bukkit.ItemStack;
 import org.bukkit.Location;
 import org.bukkit.Minecart;
 import org.bukkit.PoweredMinecart;
@@ -110,6 +113,28 @@ public class CraftWorld implements World {
     public WorldServer getHandle() {
         return world;
     }
+    
+    public ItemDrop dropItem(Location loc, ItemStack item) {
+        net.minecraft.server.ItemStack stack =
+                new net.minecraft.server.ItemStack(
+                        item.getTypeID(), item.getAmount(), item.getDamage());
+        EntityItem entity = new EntityItem(world, loc.getX(), loc.getY(),
+                loc.getZ(), stack);
+        entity.c = 10;
+        world.a(entity);
+        return new CraftItemDrop(world.getServer(), entity);
+    }
+    
+    public ItemDrop dropItemNaturally(Location loc, ItemStack item) {
+        double xs = world.l.nextFloat() * 0.7F + (1.0F - 0.7F) * 0.5D;
+        double ys = world.l.nextFloat() * 0.7F + (1.0F - 0.7F) * 0.5D;
+        double zs = world.l.nextFloat() * 0.7F + (1.0F - 0.7F) * 0.5D;
+        loc = loc.clone();
+        loc.setX(loc.getX() + xs);
+        loc.setX(loc.getY() + ys);
+        loc.setX(loc.getZ() + zs);
+        return dropItem(loc, item);
+    }
 
     public Arrow spawnArrow(Location loc, Vector velocity, float speed,
             float spread) {
@@ -169,6 +194,8 @@ public class CraftWorld implements World {
     public CraftEntity toCraftEntity(net.minecraft.server.Entity entity) {
         if (entity instanceof CraftMappable) {
             return ((CraftMappable)entity).getCraftEntity();
+        } else if (entity instanceof EntityItem) {
+            return new CraftItemDrop(world.getServer(), (EntityItem)entity);
         } else if (entity instanceof EntityArrow) {
             return new CraftArrow(world.getServer(), (EntityArrow)entity);
         } else if (entity instanceof EntityEgg) {