diff options
author | Des Herriott <des.herriott@gmail.com> | 2013-01-18 08:56:12 +0000 |
---|---|---|
committer | Nate Mortensen <nate.richard.mortensen@gmail.com> | 2013-06-10 12:06:37 -0600 |
commit | 045121d095bacebc92db8901d96c6dc48b4b4d8a (patch) | |
tree | 03c5238d4b9efe23ec93f0c60e51b3b685ed95ba /src/main | |
parent | c55c0db86d4bd2e54c22f1877ddef184e8709a11 (diff) | |
download | craftbukkit-045121d095bacebc92db8901d96c6dc48b4b4d8a.tar craftbukkit-045121d095bacebc92db8901d96c6dc48b4b4d8a.tar.gz craftbukkit-045121d095bacebc92db8901d96c6dc48b4b4d8a.tar.lz craftbukkit-045121d095bacebc92db8901d96c6dc48b4b4d8a.tar.xz craftbukkit-045121d095bacebc92db8901d96c6dc48b4b4d8a.zip |
Implement PlayerBookEditEvent. Adds BUKKIT-1995
Diffstat (limited to 'src/main')
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 cc99ff5f..f1c2d482 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 56582bc5..2b56c75f 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 96bf95ff..c70d41bd 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; } |