001 package org.bukkit.event.player;
002
003 import org.apache.commons.lang.Validate;
004 import org.bukkit.Bukkit;
005 import org.bukkit.entity.Player;
006 import org.bukkit.event.Cancellable;
007 import org.bukkit.event.HandlerList;
008 import org.bukkit.inventory.meta.BookMeta;
009
010 /**
011 * Called when a player edits or signs a book and quill item. If the event is
012 * cancelled, no changes are made to the BookMeta
013 */
014 public class PlayerEditBookEvent extends PlayerEvent implements Cancellable {
015 private static final HandlerList handlers = new HandlerList();
016
017 private final BookMeta previousBookMeta;
018 private final int slot;
019 private BookMeta newBookMeta;
020 private boolean isSigning;
021 private boolean cancel;
022
023 public PlayerEditBookEvent(Player who, int slot, BookMeta previousBookMeta, BookMeta newBookMeta, boolean isSigning) {
024 super(who);
025
026 Validate.isTrue(slot >= 0 && slot <=8, "Slot must be in range 0-8 inclusive");
027 Validate.notNull(previousBookMeta, "Previous book meta must not be null");
028 Validate.notNull(newBookMeta, "New book meta must not be null");
029
030 Bukkit.getItemFactory().equals(previousBookMeta, newBookMeta);
031
032 this.previousBookMeta = previousBookMeta;
033 this.newBookMeta = newBookMeta;
034 this.slot = slot;
035 this.isSigning = isSigning;
036 this.cancel = false;
037 }
038
039 /**
040 * Gets the book meta currently on the book.
041 * <p>
042 * Note: this is a copy of the book meta. You cannot use this object to
043 * change the existing book meta.
044 *
045 * @return the book meta currently on the book
046 */
047 public BookMeta getPreviousBookMeta() {
048 return previousBookMeta.clone();
049 }
050
051 /**
052 * Gets the book meta that the player is attempting to add to the book.
053 * <p>
054 * Note: this is a copy of the proposed new book meta. Use {@link
055 * #setNewBookMeta(BookMeta)} to change what will actually be added to the
056 * book.
057 *
058 * @return the book meta that the player is attempting to add
059 */
060 public BookMeta getNewBookMeta() {
061 return newBookMeta.clone();
062 }
063
064 /**
065 * Gets the inventory slot number for the book item that triggered this
066 * event.
067 * <p>
068 * This is a slot number on the player's hotbar in the range 0-8.
069 *
070 * @return the inventory slot number that the book item occupies
071 */
072 public int getSlot() {
073 return slot;
074 }
075
076 /**
077 * Sets the book meta that will actually be added to the book.
078 *
079 * @param newBookMeta new book meta
080 * @throws IllegalArgumentException if the new book meta is null
081 */
082 public void setNewBookMeta(BookMeta newBookMeta) throws IllegalArgumentException {
083 Validate.notNull(newBookMeta, "New book meta must not be null");
084 Bukkit.getItemFactory().equals(newBookMeta, null);
085 this.newBookMeta = newBookMeta.clone();
086 }
087
088 /**
089 * Gets whether or not the book is being signed. If a book is signed the
090 * Material changes from BOOK_AND_QUILL to WRITTEN_BOOK.
091 *
092 * @return true if the book is being signed
093 */
094 public boolean isSigning() {
095 return isSigning;
096 }
097
098 /**
099 * Sets whether or not the book is being signed. If a book is signed the
100 * Material changes from BOOK_AND_QUILL to WRITTEN_BOOK.
101 *
102 * @param signing whether or not the book is being signed.
103 */
104 public void setSigning(boolean signing) {
105 isSigning = signing;
106 }
107
108 @Override
109 public HandlerList getHandlers() {
110 return handlers;
111 }
112
113 public static HandlerList getHandlerList() {
114 return handlers;
115 }
116
117 public boolean isCancelled() {
118 return cancel;
119 }
120
121 public void setCancelled(boolean cancel) {
122 this.cancel = cancel;
123 }
124 }