Implement PlayerBookEditEvent. Adds BUKKIT-1995

By: Des Herriott <des.herriott@gmail.com>
This commit is contained in:
CraftBukkit/Spigot 2013-01-18 08:56:12 +00:00
parent c992bf7959
commit 7ddeb4b6df
2 changed files with 28 additions and 2 deletions

View File

@ -18,10 +18,13 @@ import net.minecraft.server.EntityLiving;
import net.minecraft.server.EntityPlayer; import net.minecraft.server.EntityPlayer;
import net.minecraft.server.EntityPotion; import net.minecraft.server.EntityPotion;
import net.minecraft.server.Explosion; import net.minecraft.server.Explosion;
import net.minecraft.server.IInventory;
import net.minecraft.server.InventoryCrafting; import net.minecraft.server.InventoryCrafting;
import net.minecraft.server.Item; import net.minecraft.server.Item;
import net.minecraft.server.ItemStack; import net.minecraft.server.ItemStack;
import net.minecraft.server.Packet101CloseWindow; import net.minecraft.server.Packet101CloseWindow;
import net.minecraft.server.Packet103SetSlot;
import net.minecraft.server.Slot;
import net.minecraft.server.World; import net.minecraft.server.World;
import net.minecraft.server.WorldServer; import net.minecraft.server.WorldServer;
@ -64,6 +67,7 @@ import org.bukkit.event.inventory.PrepareItemCraftEvent;
import org.bukkit.event.player.*; import org.bukkit.event.player.*;
import org.bukkit.event.server.ServerListPingEvent; import org.bukkit.event.server.ServerListPingEvent;
import org.bukkit.inventory.InventoryView; import org.bukkit.inventory.InventoryView;
import org.bukkit.inventory.meta.BookMeta;
public class CraftEventFactory { public class CraftEventFactory {
public static final DamageSource MELTING = CraftDamageSource.copyOf(DamageSource.BURN); public static final DamageSource MELTING = CraftDamageSource.copyOf(DamageSource.BURN);
@ -672,4 +676,26 @@ public class CraftEventFactory {
human.world.getServer().getPluginManager().callEvent(event); human.world.getServer().getPluginManager().callEvent(event);
human.activeContainer.transferTo(human.defaultContainer, human.getBukkitEntity()); human.activeContainer.transferTo(human.defaultContainer, human.getBukkitEntity());
} }
public static void handleEditBookEvent(EntityPlayer player, ItemStack newBookItem) {
int itemInHandIndex = player.inventory.itemInHandIndex;
PlayerEditBookEvent editBookEvent = new PlayerEditBookEvent(player.getBukkitEntity(), player.inventory.itemInHandIndex, (BookMeta) CraftItemStack.getItemMeta(player.inventory.getItemInHand()), (BookMeta) CraftItemStack.getItemMeta(newBookItem), newBookItem.id == Item.WRITTEN_BOOK.id);
player.world.getServer().getPluginManager().callEvent(editBookEvent);
ItemStack itemInHand = player.inventory.getItem(itemInHandIndex);
// If they've got the same item in their hand, it'll need to be updated.
if (itemInHand.id == Item.BOOK_AND_QUILL.id) {
if (!editBookEvent.isCancelled()) {
CraftItemStack.setItemMeta(itemInHand, editBookEvent.getNewBookMeta());
if (editBookEvent.isSigning()) {
itemInHand.id = Item.WRITTEN_BOOK.id;
}
}
// Client will have updated its idea of the book item; we need to overwrite that
Slot slot = player.activeContainer.a((IInventory) player.inventory, itemInHandIndex);
player.playerConnection.sendPacket(new Packet103SetSlot(player.activeContainer.windowId, slot.g, itemInHand));
}
}
} }

View File

@ -298,7 +298,7 @@ public final class CraftItemStack extends ItemStack {
return getItemMeta(handle); return getItemMeta(handle);
} }
static ItemMeta getItemMeta(net.minecraft.server.ItemStack item) { public static ItemMeta getItemMeta(net.minecraft.server.ItemStack item) {
if (!hasItemMeta(item)) { if (!hasItemMeta(item)) {
return CraftItemFactory.instance().getItemMeta(getType(item)); return CraftItemFactory.instance().getItemMeta(getType(item));
} }
@ -338,7 +338,7 @@ public final class CraftItemStack extends ItemStack {
return setItemMeta(handle, itemMeta); return setItemMeta(handle, itemMeta);
} }
static boolean setItemMeta(net.minecraft.server.ItemStack item, ItemMeta itemMeta) { public static boolean setItemMeta(net.minecraft.server.ItemStack item, ItemMeta itemMeta) {
if (item == null) { if (item == null) {
return false; return false;
} }