From 045121d095bacebc92db8901d96c6dc48b4b4d8a Mon Sep 17 00:00:00 2001 From: Des Herriott Date: Fri, 18 Jan 2013 08:56:12 +0000 Subject: [PATCH] Implement PlayerBookEditEvent. Adds BUKKIT-1995 --- .../minecraft/server/PlayerConnection.java | 11 +++----- .../craftbukkit/event/CraftEventFactory.java | 26 +++++++++++++++++++ .../craftbukkit/inventory/CraftItemStack.java | 4 +-- 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/src/main/java/net/minecraft/server/PlayerConnection.java b/src/main/java/net/minecraft/server/PlayerConnection.java index cc99ff5f29..f1c2d48292 100644 --- a/src/main/java/net/minecraft/server/PlayerConnection.java +++ b/src/main/java/net/minecraft/server/PlayerConnection.java @@ -1661,10 +1661,10 @@ public class PlayerConnection extends Connection { itemstack1 = this.player.inventory.getItemInHand(); if (itemstack != null && itemstack.id == Item.BOOK_AND_QUILL.id && itemstack.id == itemstack1.id) { - itemstack1.a("pages", (NBTBase) itemstack.getTag().getList("pages")); + CraftEventFactory.handleEditBookEvent(player, itemstack); // CraftBukkit } - } catch (Exception exception) { // CraftBukkit start + } catch (Throwable exception) { this.minecraftServer.getLogger().warning(this.player.name + " sent invalid MC|BEdit data", exception); this.disconnect("Invalid book data!"); // CraftBukkit end @@ -1679,13 +1679,10 @@ public class PlayerConnection extends Connection { itemstack1 = this.player.inventory.getItemInHand(); if (itemstack != null && itemstack.id == Item.WRITTEN_BOOK.id && itemstack1.id == Item.BOOK_AND_QUILL.id) { - itemstack1.a("author", (NBTBase) (new NBTTagString("author", this.player.name))); - itemstack1.a("title", (NBTBase) (new NBTTagString("title", itemstack.getTag().getString("title")))); - itemstack1.a("pages", (NBTBase) itemstack.getTag().getList("pages")); - itemstack1.id = Item.WRITTEN_BOOK.id; + CraftEventFactory.handleEditBookEvent(player, itemstack); // CraftBukkit } - } catch (Exception exception1) { // CraftBukkit start + } catch (Throwable exception1) { this.minecraftServer.getLogger().warning(this.player.name + " sent invalid MC|BSign data", exception1); this.disconnect("Invalid book data!"); // CraftBukkit end diff --git a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java index 56582bc5ee..2b56c75fae 100644 --- a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java +++ b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java @@ -18,10 +18,13 @@ import net.minecraft.server.EntityLiving; import net.minecraft.server.EntityPlayer; import net.minecraft.server.EntityPotion; import net.minecraft.server.Explosion; +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.Packet103SetSlot; +import net.minecraft.server.Slot; import net.minecraft.server.World; import net.minecraft.server.WorldServer; @@ -64,6 +67,7 @@ import org.bukkit.event.inventory.PrepareItemCraftEvent; import org.bukkit.event.player.*; import org.bukkit.event.server.ServerListPingEvent; import org.bukkit.inventory.InventoryView; +import org.bukkit.inventory.meta.BookMeta; public class CraftEventFactory { public static final DamageSource MELTING = CraftDamageSource.copyOf(DamageSource.BURN); @@ -672,4 +676,26 @@ public class CraftEventFactory { human.world.getServer().getPluginManager().callEvent(event); 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)); + } + } } diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java index 96bf95ff93..c70d41bd63 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java @@ -298,7 +298,7 @@ public final class CraftItemStack extends ItemStack { return getItemMeta(handle); } - static ItemMeta getItemMeta(net.minecraft.server.ItemStack item) { + public static ItemMeta getItemMeta(net.minecraft.server.ItemStack item) { if (!hasItemMeta(item)) { return CraftItemFactory.instance().getItemMeta(getType(item)); } @@ -338,7 +338,7 @@ public final class CraftItemStack extends ItemStack { 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) { return false; }