From 4e5359c080f2e0255c674c4512e0787066702c78 Mon Sep 17 00:00:00 2001 From: CraftBukkit/Spigot Date: Mon, 5 Mar 2012 14:21:43 -0500 Subject: [PATCH] [Bleeding] Added getting and setting drops to all appropriate events. Fixes BUKKIT-397 and fixes BUKKIT-1252 - Allows drops in creative mode by adding items to the getDrops() list - Contents of containers are not reported - Contents of storage minecarts are not reported By: Celtic Minstrel --- .../craftbukkit/event/CraftEventFactory.java | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java b/paper-server/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java index cebaa8f034..394e52ab76 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java @@ -1,6 +1,8 @@ package org.bukkit.craftbukkit.event; import java.net.InetAddress; +import java.util.ArrayList; +import java.util.Iterator; import java.util.List; import java.util.Map; @@ -16,10 +18,12 @@ import net.minecraft.server.EntityItem; import net.minecraft.server.EntityLiving; import net.minecraft.server.EntityPlayer; import net.minecraft.server.EntityPotion; +import net.minecraft.server.IInventory; import net.minecraft.server.InventoryCrafting; import net.minecraft.server.Item; import net.minecraft.server.ItemStack; import net.minecraft.server.Packet101CloseWindow; +import net.minecraft.server.Packet53BlockChange; import net.minecraft.server.World; import net.minecraft.server.WorldServer; @@ -485,6 +489,7 @@ public class CraftEventFactory { public static ItemStack callPreCraftEvent(InventoryCrafting matrix, ItemStack result, InventoryView lastCraftView, boolean isRepair) { CraftInventoryCrafting inventory = new CraftInventoryCrafting(matrix, matrix.resultInventory); inventory.setResult(new CraftItemStack(result)); + PrepareItemCraftEvent event = new PrepareItemCraftEvent(inventory, lastCraftView, isRepair); Bukkit.getPluginManager().callEvent(event); @@ -506,4 +511,42 @@ public class CraftEventFactory { Bukkit.getPluginManager().callEvent(event); return event; } + + public static boolean callBlockBreakEvent(World world, int x, int y, int z, int id, int data, boolean creative, EntityHuman player) { + net.minecraft.server.Block blockType = net.minecraft.server.Block.byId[id]; + Block block = world.getWorld().getBlockAt(x, y, z); + + // Tell client the block is gone immediately then process events + if (world.getTileEntity(x, y, z) == null) { + Packet53BlockChange packet = new Packet53BlockChange(x, y, z, world); + + packet.material = 0; + packet.data = 0; + ((EntityPlayer) player).netServerHandler.sendPacket(packet); + } + + List drops = new ArrayList(); + if (!creative && player.b(blockType)) { + for (ItemStack stack : blockType.calculateDrops(world, player, x, y, z, data)) { + drops.add(new CraftItemStack(stack)); + } + } + + BlockBreakEvent event = new BlockBreakEvent(block, (org.bukkit.entity.Player) player.getBukkitEntity(), drops); + world.getServer().getPluginManager().callEvent(event); + + if (event.isCancelled()) { + // Let the client know the block still exists + ((EntityPlayer) player).netServerHandler.sendPacket(new Packet53BlockChange(x, y, z, world)); + return true; + } + + ArrayList toDrop = new ArrayList(); + for (org.bukkit.inventory.ItemStack stack : drops) { + toDrop.add(CraftItemStack.createNMSItemStack(stack)); + } + blockType.setDrops(toDrop); + + return false; // Event not cancelled + } }