diff options
Diffstat (limited to 'src/main/java/org')
30 files changed, 1035 insertions, 68 deletions
diff --git a/src/main/java/org/bukkit/Bukkit.java b/src/main/java/org/bukkit/Bukkit.java index 43fa781b..b52166a0 100644 --- a/src/main/java/org/bukkit/Bukkit.java +++ b/src/main/java/org/bukkit/Bukkit.java @@ -13,6 +13,9 @@ import org.bukkit.command.ConsoleCommandSender; import org.bukkit.command.PluginCommand; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; +import org.bukkit.event.inventory.InventoryType; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.InventoryHolder; import org.bukkit.inventory.Recipe; import org.bukkit.map.MapView; import org.bukkit.plugin.PluginManager; @@ -321,4 +324,16 @@ public final class Bukkit { public static OfflinePlayer[] getOfflinePlayers() { return server.getOfflinePlayers(); } + + public static Inventory createInventory(InventoryHolder owner, InventoryType type) { + return server.createInventory(owner, type); + } + + public static Inventory createInventory(InventoryHolder owner, int size) { + return server.createInventory(owner, size); + } + + public static Inventory createInventory(InventoryHolder owner, int size, String title) { + return server.createInventory(owner, size, title); + } } diff --git a/src/main/java/org/bukkit/Server.java b/src/main/java/org/bukkit/Server.java index 958c41e5..dc6e4aa5 100644 --- a/src/main/java/org/bukkit/Server.java +++ b/src/main/java/org/bukkit/Server.java @@ -13,6 +13,9 @@ import org.bukkit.command.CommandSender; import org.bukkit.command.ConsoleCommandSender; import org.bukkit.command.PluginCommand; import org.bukkit.entity.Player; +import org.bukkit.event.inventory.InventoryType; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.InventoryHolder; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.Recipe; import org.bukkit.map.MapView; @@ -560,4 +563,33 @@ public interface Server extends PluginMessageRecipient { * @return Messenger responsible for this server. */ public Messenger getMessenger(); + + /** + * Creates an empty inventory of the specified type. If the type is {@link InventoryType#CHEST}, + * the new inventory has a size of 27; otherwise the new inventory has the normal size for + * its type. + * @param owner The holder of the inventory; can be null if there's no holder. + * @param type The type of inventory to create. + * @return The new inventory. + */ + Inventory createInventory(InventoryHolder owner, InventoryType type); + + /** + * Creates an empty inventory of type {@link InventoryType#CHEST} with the specified size. + * @param owner The holder of the inventory; can be null if there's no holder. + * @param size The size of inventory to create; must be a multiple of 9. + * @return The new inventory. + * @throws IllegalArgumentException If the size is not a multiple of 9. + */ + Inventory createInventory(InventoryHolder owner, int size); + + /** + * Creates an empty inventory of type {@link InventoryType#CHEST} with the specified size and title. + * @param owner The holder of the inventory; can be null if there's no holder. + * @param size The size of inventory to create; must be a multiple of 9. + * @param title The title of the inventory, to be displayed when it is viewed. + * @return The new inventory. + * @throws IllegalArgumentException If the size is not a multiple of 9. + */ + Inventory createInventory(InventoryHolder owner, int size, String title); } diff --git a/src/main/java/org/bukkit/block/BrewingStand.java b/src/main/java/org/bukkit/block/BrewingStand.java index 61b65004..c66a51c1 100644 --- a/src/main/java/org/bukkit/block/BrewingStand.java +++ b/src/main/java/org/bukkit/block/BrewingStand.java @@ -1,5 +1,7 @@ package org.bukkit.block; +import org.bukkit.inventory.BrewerInventory; + /** * Represents a brewing stand. */ @@ -18,4 +20,6 @@ public interface BrewingStand extends BlockState, ContainerBlock { * @param brewTime Brewing time */ void setBrewingTime(int brewTime); + + public BrewerInventory getInventory(); } diff --git a/src/main/java/org/bukkit/block/ContainerBlock.java b/src/main/java/org/bukkit/block/ContainerBlock.java index ab342921..f51d0fa8 100644 --- a/src/main/java/org/bukkit/block/ContainerBlock.java +++ b/src/main/java/org/bukkit/block/ContainerBlock.java @@ -1,16 +1,10 @@ package org.bukkit.block; -import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.InventoryHolder; /** * Indicates a block type that has inventory. + * @deprecated in favour of {@link InventoryHolder} */ -public interface ContainerBlock { - - /** - * Get the block's inventory. - * - * @return The inventory. - */ - public Inventory getInventory(); -} +@Deprecated +public interface ContainerBlock extends InventoryHolder {} diff --git a/src/main/java/org/bukkit/block/Furnace.java b/src/main/java/org/bukkit/block/Furnace.java index 0865a032..94af85e1 100644 --- a/src/main/java/org/bukkit/block/Furnace.java +++ b/src/main/java/org/bukkit/block/Furnace.java @@ -1,5 +1,7 @@ package org.bukkit.block; +import org.bukkit.inventory.FurnaceInventory; + /** * Represents a furnace. */ @@ -32,4 +34,6 @@ public interface Furnace extends BlockState, ContainerBlock { * @param cookTime Cook time */ public void setCookTime(short cookTime); + + public FurnaceInventory getInventory(); } diff --git a/src/main/java/org/bukkit/entity/HumanEntity.java b/src/main/java/org/bukkit/entity/HumanEntity.java index 654a7475..92201d4a 100644 --- a/src/main/java/org/bukkit/entity/HumanEntity.java +++ b/src/main/java/org/bukkit/entity/HumanEntity.java @@ -1,6 +1,10 @@ package org.bukkit.entity; import org.bukkit.GameMode; +import org.bukkit.Location; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.InventoryHolder; +import org.bukkit.inventory.InventoryView; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.PlayerInventory; import org.bukkit.permissions.Permissible; @@ -8,7 +12,7 @@ import org.bukkit.permissions.Permissible; /** * Represents a human entity, such as an NPC or a player */ -public interface HumanEntity extends LivingEntity, AnimalTamer, Permissible { +public interface HumanEntity extends LivingEntity, AnimalTamer, Permissible, InventoryHolder { /** * Returns the name of this player @@ -25,6 +29,59 @@ public interface HumanEntity extends LivingEntity, AnimalTamer, Permissible { public PlayerInventory getInventory(); /** + * If the player currently has an inventory window open, this method will set a + * property of that window, such as the state of a progress bar. + * @param prop The property. + * @param value The value to set the property to. + * @return True if the property was successfully set. + */ + public boolean setWindowProperty(InventoryView.Property prop, int value); + + /** + * Gets the inventory view the player is currently viewing. If they do not have + * an inventory window open, it returns their internal crafting view. + * @return The inventory view. + */ + public InventoryView getOpenInventory(); + + /** + * Opens an inventory window with the specified inventory on the top and the player's inventory + * on the bottom. + * @param inventory The inventory to open + * @return The newly opened inventory view + */ + public InventoryView openInventory(Inventory inventory); + + /** + * Opens an empty workbench inventory window with the player's inventory on the bottom. + * @param location The location to attach it to. If null, the player's location is used. + * @param force If false, and there is no workbench block at the location, no inventory will be + * opened and null will be returned. + * @return The newly opened inventory view, or null if it could not be opened. + */ + public InventoryView openWorkbench(Location location, boolean force); + + /** + * Opens an empty enchanting inventory window with the player's inventory on the bottom. + * @param location The location to attach it to. If null, the player's location is used. + * @param force If false, and there is no enchanting table at the location, no inventory will be + * opened and null will be returned. + * @return The newly opened inventory view, or null if it could not be opened. + */ + public InventoryView openEnchanting(Location location, boolean force); + + /** + * Opens an inventory window to the specified inventory view + * @param inventory The view to open + */ + public void openInventory(InventoryView inventory); + + /** + * Force-closes the currently open inventory view for this player, if any. + */ + public void closeInventory(); + + /** * Returns the ItemStack currently in your hand, can be empty. * * @return The ItemStack of the item you are currently holding. @@ -40,6 +97,22 @@ public interface HumanEntity extends LivingEntity, AnimalTamer, Permissible { public void setItemInHand(ItemStack item); /** + * Returns the ItemStack currently on your cursor, can be empty. + * Will always be empty if the player currently has no open window. + * + * @return The ItemStack of the item you are currently moving around. + */ + public ItemStack getItemOnCursor(); + + /** + * Sets the item to the given ItemStack, this will replace whatever the + * user was moving. Will always be empty if the player currently has no open window. + * + * @param item The ItemStack which will end up in the hand + */ + public void setItemOnCursor(ItemStack item); + + /** * Returns whether this player is slumbering. * * @return slumber state diff --git a/src/main/java/org/bukkit/entity/StorageMinecart.java b/src/main/java/org/bukkit/entity/StorageMinecart.java index feeef6b5..c3fc263e 100644 --- a/src/main/java/org/bukkit/entity/StorageMinecart.java +++ b/src/main/java/org/bukkit/entity/StorageMinecart.java @@ -1,16 +1,8 @@ package org.bukkit.entity; -import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.InventoryHolder; /** * Represents a storage minecart. */ -public interface StorageMinecart extends Minecart { - - /** - * Return the inventory object for this StorageMinecart. - * - * @return The inventory for this Minecart - */ - public Inventory getInventory(); -} +public interface StorageMinecart extends Minecart, InventoryHolder {} diff --git a/src/main/java/org/bukkit/event/enchantment/EnchantItemEvent.java b/src/main/java/org/bukkit/event/enchantment/EnchantItemEvent.java index 172e9e1b..4b40d366 100644 --- a/src/main/java/org/bukkit/event/enchantment/EnchantItemEvent.java +++ b/src/main/java/org/bukkit/event/enchantment/EnchantItemEvent.java @@ -7,14 +7,15 @@ import org.bukkit.block.Block; import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.Player; import org.bukkit.event.Cancellable; -import org.bukkit.event.Event; import org.bukkit.event.HandlerList; +import org.bukkit.event.inventory.InventoryEvent; +import org.bukkit.inventory.InventoryView; import org.bukkit.inventory.ItemStack; /** * Called when an ItemStack is successfully enchanted (currently at enchantment table) */ -public class EnchantItemEvent extends Event implements Cancellable { +public class EnchantItemEvent extends InventoryEvent implements Cancellable { private static final HandlerList handlers = new HandlerList(); private final Block table; private final ItemStack item; @@ -22,14 +23,17 @@ public class EnchantItemEvent extends Event implements Cancellable { private boolean cancelled; private final Map<Enchantment,Integer> enchants; private final Player enchanter; + private int button; - public EnchantItemEvent(final Player enchanter, final Block table, final ItemStack item, final int level, final Map<Enchantment, Integer> enchants) { + public EnchantItemEvent(final Player enchanter, final InventoryView view, final Block table, final ItemStack item, final int level, final Map<Enchantment, Integer> enchants, final int i) { + super(view); this.enchanter = enchanter; this.table = table; this.item = item; this.level = level; this.enchants = new HashMap<Enchantment, Integer>(enchants); this.cancelled = false; + this.button = i; } /** @@ -85,6 +89,14 @@ public class EnchantItemEvent extends Event implements Cancellable { return enchants; } + /** + * Which button was pressed to initiate the enchanting. + * @return The button index (0, 1, or 2). + */ + public int whichButton() { + return button; + } + public boolean isCancelled() { return cancelled; } diff --git a/src/main/java/org/bukkit/event/enchantment/PrepareItemEnchantEvent.java b/src/main/java/org/bukkit/event/enchantment/PrepareItemEnchantEvent.java index dbc24a40..d97adf3b 100644 --- a/src/main/java/org/bukkit/event/enchantment/PrepareItemEnchantEvent.java +++ b/src/main/java/org/bukkit/event/enchantment/PrepareItemEnchantEvent.java @@ -3,14 +3,15 @@ package org.bukkit.event.enchantment; import org.bukkit.block.Block; import org.bukkit.entity.Player; import org.bukkit.event.Cancellable; -import org.bukkit.event.Event; import org.bukkit.event.HandlerList; +import org.bukkit.event.inventory.InventoryEvent; +import org.bukkit.inventory.InventoryView; import org.bukkit.inventory.ItemStack; /** * Called when an ItemStack is inserted in an enchantment table - can be called multiple times */ -public class PrepareItemEnchantEvent extends Event implements Cancellable { +public class PrepareItemEnchantEvent extends InventoryEvent implements Cancellable { private static final HandlerList handlers = new HandlerList(); private final Block table; private final ItemStack item; @@ -19,7 +20,8 @@ public class PrepareItemEnchantEvent extends Event implements Cancellable { private boolean cancelled; private final Player enchanter; - public PrepareItemEnchantEvent(final Player enchanter, final Block table, final ItemStack item, final int[] levelsOffered, final int bonus) { + public PrepareItemEnchantEvent(final Player enchanter, InventoryView view, final Block table, final ItemStack item, final int[] levelsOffered, final int bonus) { + super(view); this.enchanter = enchanter; this.table = table; this.item = item; diff --git a/src/main/java/org/bukkit/event/inventory/BrewEvent.java b/src/main/java/org/bukkit/event/inventory/BrewEvent.java new file mode 100644 index 00000000..ed9c53b1 --- /dev/null +++ b/src/main/java/org/bukkit/event/inventory/BrewEvent.java @@ -0,0 +1,39 @@ +package org.bukkit.event.inventory; + +import org.bukkit.block.Block; +import org.bukkit.event.Cancellable; +import org.bukkit.event.HandlerList; +import org.bukkit.event.block.BlockEvent; +import org.bukkit.inventory.BrewerInventory; + +public class BrewEvent extends BlockEvent implements Cancellable { + private static final HandlerList handlers = new HandlerList(); + private BrewerInventory contents; + private boolean cancelled; + + public BrewEvent(Block brewer, BrewerInventory contents) { + super(brewer); + this.contents = contents; + } + + public BrewerInventory getContents() { + return contents; + } + + public boolean isCancelled() { + return cancelled; + } + + public void setCancelled(boolean cancel) { + cancelled = cancel; + } + + @Override + public HandlerList getHandlers() { + return handlers; + } + + public static HandlerList getHandlerList() { + return handlers; + } +} diff --git a/src/main/java/org/bukkit/event/inventory/CraftItemEvent.java b/src/main/java/org/bukkit/event/inventory/CraftItemEvent.java new file mode 100644 index 00000000..aceea83c --- /dev/null +++ b/src/main/java/org/bukkit/event/inventory/CraftItemEvent.java @@ -0,0 +1,21 @@ +package org.bukkit.event.inventory; + +import org.bukkit.event.inventory.InventoryType.SlotType; +import org.bukkit.inventory.InventoryView; +import org.bukkit.inventory.Recipe; + +public class CraftItemEvent extends InventoryClickEvent { + private Recipe recipe; + + public CraftItemEvent(Recipe recipe, InventoryView what, SlotType type, int slot, boolean right, boolean shift) { + super(what, type, slot, right, shift); + this.recipe = recipe; + } + + /** + * @return A copy of the current recipe on the crafting matrix. + */ + public Recipe getRecipe() { + return recipe; + } +} diff --git a/src/main/java/org/bukkit/event/inventory/FurnaceBurnEvent.java b/src/main/java/org/bukkit/event/inventory/FurnaceBurnEvent.java index 73caf150..8ca1ff7b 100644 --- a/src/main/java/org/bukkit/event/inventory/FurnaceBurnEvent.java +++ b/src/main/java/org/bukkit/event/inventory/FurnaceBurnEvent.java @@ -2,23 +2,22 @@ package org.bukkit.event.inventory; import org.bukkit.block.Block; import org.bukkit.event.Cancellable; -import org.bukkit.event.Event; import org.bukkit.event.HandlerList; +import org.bukkit.event.block.BlockEvent; import org.bukkit.inventory.ItemStack; /** * Called when an ItemStack is successfully burned as fuel in a furnace. */ -public class FurnaceBurnEvent extends Event implements Cancellable { +public class FurnaceBurnEvent extends BlockEvent implements Cancellable { private static final HandlerList handlers = new HandlerList(); - private final Block furnace; private final ItemStack fuel; private int burnTime; private boolean cancelled; private boolean burning; public FurnaceBurnEvent(final Block furnace, final ItemStack fuel, final int burnTime) { - this.furnace = furnace; + super(furnace); this.fuel = fuel; this.burnTime = burnTime; this.cancelled = false; @@ -29,9 +28,11 @@ public class FurnaceBurnEvent extends Event implements Cancellable { * Gets the block for the furnace involved in this event * * @return the block of the furnace + * @deprecated In favour of {@link #getBlock()}. */ + @Deprecated public Block getFurnace() { - return furnace; + return getBlock(); } /** diff --git a/src/main/java/org/bukkit/event/inventory/FurnaceSmeltEvent.java b/src/main/java/org/bukkit/event/inventory/FurnaceSmeltEvent.java index 6139de71..e9d1a540 100644 --- a/src/main/java/org/bukkit/event/inventory/FurnaceSmeltEvent.java +++ b/src/main/java/org/bukkit/event/inventory/FurnaceSmeltEvent.java @@ -2,22 +2,21 @@ package org.bukkit.event.inventory; import org.bukkit.block.Block; import org.bukkit.event.Cancellable; -import org.bukkit.event.Event; import org.bukkit.event.HandlerList; +import org.bukkit.event.block.BlockEvent; import org.bukkit.inventory.ItemStack; /** * Called when an ItemStack is successfully smelted in a furnace. */ -public class FurnaceSmeltEvent extends Event implements Cancellable { +public class FurnaceSmeltEvent extends BlockEvent implements Cancellable { private static final HandlerList handlers = new HandlerList(); - private final Block furnace; private final ItemStack source; private ItemStack result; private boolean cancelled; public FurnaceSmeltEvent(final Block furnace, final ItemStack source, final ItemStack result) { - this.furnace = furnace; + super(furnace); this.source = source; this.result = result; this.cancelled = false; @@ -27,9 +26,11 @@ public class FurnaceSmeltEvent extends Event implements Cancellable { * Gets the block for the furnace involved in this event * * @return the block of the furnace + * @deprecated In favour of {@link #getBlock()}. */ + @Deprecated public Block getFurnace() { - return furnace; + return getBlock(); } /** diff --git a/src/main/java/org/bukkit/event/inventory/InventoryClickEvent.java b/src/main/java/org/bukkit/event/inventory/InventoryClickEvent.java new file mode 100644 index 00000000..26e1d382 --- /dev/null +++ b/src/main/java/org/bukkit/event/inventory/InventoryClickEvent.java @@ -0,0 +1,143 @@ +package org.bukkit.event.inventory; + +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.InventoryView; +import org.bukkit.entity.HumanEntity; +import org.bukkit.event.Cancellable; +import org.bukkit.event.HandlerList; +import org.bukkit.event.inventory.InventoryType.SlotType; +import org.bukkit.inventory.ItemStack; + +public class InventoryClickEvent extends InventoryEvent implements Cancellable { + private static final HandlerList handlers = new HandlerList(); + private SlotType slot_type; + private boolean rightClick, shiftClick; + private Result result; + private int whichSlot; + private int rawSlot; + private ItemStack current = null; + + public InventoryClickEvent(InventoryView what, SlotType type, int slot, boolean right, boolean shift) { + super(what); + this.slot_type = type; + this.rightClick = right; + this.shiftClick = shift; + this.result = Result.DEFAULT; + this.rawSlot = slot; + this.whichSlot = what.convertSlot(slot); + } + + /** + * Get the type of slot that was clicked. + * @return The slot type. + */ + public SlotType getSlotType() { + return slot_type; + } + + /** + * Get the current item on the cursor. + * @return The cursor item + */ + public ItemStack getCursor() { + return getView().getCursor(); + } + + /** + * Get the current item in the clicked slot. + * @return The slot item. + */ + public ItemStack getCurrentItem() { + if(slot_type == SlotType.OUTSIDE) return current; + return getView().getItem(rawSlot); + } + + /** + * @return True if the click is a right-click. + */ + public boolean isRightClick() { + return rightClick; + } + + /** + * @return True if the click is a left-click. + */ + public boolean isLeftClick() { + return !rightClick; + } + + /** + * Shift can be combined with right-click or left-click as a modifier. + * @return True if the click is a shift-click. + */ + public boolean isShiftClick() { + return shiftClick; + } + + public void setResult(Result newResult) { + result = newResult; + } + + public Result getResult() { + return result; + } + + /** + * Get the player who performed the click. + * @return The clicking player. + */ + public HumanEntity getWhoClicked() { + return getView().getPlayer(); + } + + /** + * Set the item on the cursor. + * @param what The new cursor item. + */ + public void setCursor(ItemStack what) { + getView().setCursor(what); + } + + /** + * Set the current item in the slot. + * @param what The new slot item. + */ + public void setCurrentItem(ItemStack what) { + if(slot_type == SlotType.OUTSIDE) current = what; + else getView().setItem(rawSlot, what); + } + + public boolean isCancelled() { + return result == Result.DENY; + } + + public void setCancelled(boolean toCancel) { + result = toCancel ? Result.DENY : Result.ALLOW; + } + + /** + * The slot number that was clicked, ready for passing to {@link Inventory#getItem(int)}. Note + * that there may be two slots with the same slot number, since a view links two different inventories. + * @return The slot number. + */ + public int getSlot() { + return whichSlot; + } + + /** + * The raw slot number, which is unique for the view. + * @return The slot number. + */ + public int getRawSlot() { + return rawSlot; + } + + @Override + public HandlerList getHandlers() { + return handlers; + } + + public static HandlerList getHandlerList() { + return handlers; + } +} diff --git a/src/main/java/org/bukkit/event/inventory/InventoryCloseEvent.java b/src/main/java/org/bukkit/event/inventory/InventoryCloseEvent.java new file mode 100644 index 00000000..d4962500 --- /dev/null +++ b/src/main/java/org/bukkit/event/inventory/InventoryCloseEvent.java @@ -0,0 +1,34 @@ + +package org.bukkit.event.inventory; + +import org.bukkit.entity.HumanEntity; +import org.bukkit.event.HandlerList; +import org.bukkit.inventory.InventoryView; + +/** + * Represents a player related inventory event + */ +public class InventoryCloseEvent extends InventoryEvent { + private static final HandlerList handlers = new HandlerList(); + + public InventoryCloseEvent(InventoryView transaction) { + super(transaction); + } + + /** + * Returns the player involved in this event + * @return Player who is involved in this event + */ + public final HumanEntity getPlayer() { + return transaction.getPlayer(); + } + + @Override + public HandlerList getHandlers() { + return handlers; + } + + public static HandlerList getHandlerList() { + return handlers; + } +} diff --git a/src/main/java/org/bukkit/event/inventory/InventoryEvent.java b/src/main/java/org/bukkit/event/inventory/InventoryEvent.java new file mode 100644 index 00000000..abca85c7 --- /dev/null +++ b/src/main/java/org/bukkit/event/inventory/InventoryEvent.java @@ -0,0 +1,58 @@ + +package org.bukkit.event.inventory; + +import java.util.List; + +import org.bukkit.event.HandlerList; +import org.bukkit.entity.HumanEntity; +import org.bukkit.event.Event; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.InventoryView; + +/** + * Represents a player related inventory event + */ +public class InventoryEvent extends Event { + private static final HandlerList handlers = new HandlerList(); + protected InventoryView transaction; + + public InventoryEvent(InventoryView transaction) { + this.transaction = transaction; + } + + /** + * Gets the primary Inventory involved in this transaction + * + * @return The upper inventory. + */ + public Inventory getInventory() { + return transaction.getTopInventory(); + } + + /** + * Gets the list of players viewing the primary (upper) inventory involved in this event + * + * @return A list of people viewing. + */ + public List<HumanEntity> getViewers() { + return transaction.getTopInventory().getViewers(); + } + + /** + * Gets the view object itself + * + * @return InventoryView + */ + public InventoryView getView() { + return transaction; + } + + @Override + public HandlerList getHandlers() { + return handlers; + } + + public static HandlerList getHandlerList() { + return handlers; + } +} diff --git a/src/main/java/org/bukkit/event/inventory/InventoryOpenEvent.java b/src/main/java/org/bukkit/event/inventory/InventoryOpenEvent.java new file mode 100644 index 00000000..2da8b5fe --- /dev/null +++ b/src/main/java/org/bukkit/event/inventory/InventoryOpenEvent.java @@ -0,0 +1,61 @@ + +package org.bukkit.event.inventory; + +import org.bukkit.inventory.InventoryView; +import org.bukkit.entity.HumanEntity; +import org.bukkit.event.Cancellable; +import org.bukkit.event.HandlerList; + +/** + * Represents a player related inventory event + */ +public class InventoryOpenEvent extends InventoryEvent implements Cancellable { + private static final HandlerList handlers = new HandlerList(); + private boolean cancelled; + + public InventoryOpenEvent(InventoryView transaction) { + super(transaction); + this.cancelled = false; + } + + /** + * Returns the player involved in this event + * @return Player who is involved in this event + */ + public final HumanEntity getPlayer() { + return transaction.getPlayer(); + } + + /** + * Gets the cancellation state of this event. A cancelled event will not + * be executed in the server, but will still pass to other plugins + * + * If an inventory open event is cancelled, the inventory screen will not show. + * + * @return true if this event is cancelled + */ + public boolean isCancelled() { + return cancelled; + } + + /** + * Sets the cancellation state of this event. A cancelled event will not + * be executed in the server, but will still pass to other plugins + * + * If an inventory open event is cancelled, the inventory screen will not show. + * + * @param cancel true if you wish to cancel this event + */ + public void setCancelled(boolean cancel) { + cancelled = cancel; + } + + @Override + public HandlerList getHandlers() { + return handlers; + } + + public static HandlerList getHandlerList() { + return handlers; + } +} diff --git a/src/main/java/org/bukkit/event/inventory/InventoryType.java b/src/main/java/org/bukkit/event/inventory/InventoryType.java new file mode 100644 index 00000000..4a343c90 --- /dev/null +++ b/src/main/java/org/bukkit/event/inventory/InventoryType.java @@ -0,0 +1,91 @@ +package org.bukkit.event.inventory; + +public enum InventoryType { + /** + * A chest inventory, with 0, 9, 18, 27, 36, 45, or 54 slots of type CONTAINER. + */ + CHEST(27,"Chest"), + /** + * A dispenser inventory, with 9 slots of type CONTAINER. + */ + DISPENSER(9,"Dispenser"), + /** + * A furnace inventory, with a RESULT slot, a CRAFTING slot, and a FUEL slot. + */ + FURNACE(3,"Furnace"), + /** + * A workbench inventory, with 9 CRAFTING slots and a RESULT slot. + */ + WORKBENCH(10,"Crafting"), + /** + * A player's crafting inventory, with 4 CRAFTING slots and a RESULT slot. Also implies that the + * 4 ARMOR slots are accessible. + */ + CRAFTING(5,"Crafting"), + /** + * An enchantment table inventory, with one CRAFTING slot and three enchanting buttons. + */ + ENCHANTING(1,"Enchanting"), + /** + * A brewing stand inventory, with one FUEL slot and three CRAFTING slots. + */ + BREWING(4,"Brewing"), + /** + * A player's inventory, with 9 QUICKBAR slots, 27 CONTAINER slots, and 4 ARMOR slots. The + * ARMOUR slots may not be visible to the player, though. + */ + PLAYER(36,"Player"), + /** + * The creative mode inventory, with only 9 QUICKBAR slots and nothing else. (The actual + * creative interface with the items is client-side and cannot be altered by the server.) + */ + CREATIVE(9,"Creative"); + private final int size; + private final String title; + + private InventoryType(int defaultSize, String defaultTitle) { + size = defaultSize; + title = defaultTitle; + } + + public int getDefaultSize() { + return size; + } + + public String getDefaultTitle() { + return title; + } + + public enum SlotType { + /** + * A result slot in a furnace or crafting inventory. + */ + RESULT, + /** + * A slot in the crafting matrix, or the input slot in a furnace inventory, + * the potion slot in the brewing stand, or the enchanting slot. + */ + CRAFTING, + /** + * An armour slot in the player's inventory. + */ + ARMOR, + /** + * A regular slot in the container or the player's inventory; anything not covered + * by the other enum values. + */ + CONTAINER, + /** + * A slot in the bottom row or quickbar. + */ + QUICKBAR, + /** + * A pseudo-slot representing the area outside the inventory window. + */ + OUTSIDE, + /** + * The fuel slot in a furnace inventory, or the ingredient slot in a brewing stand inventory. + */ + FUEL; + } +} diff --git a/src/main/java/org/bukkit/event/inventory/PrepareItemCraftEvent.java b/src/main/java/org/bukkit/event/inventory/PrepareItemCraftEvent.java new file mode 100644 index 00000000..d809ed1c --- /dev/null +++ b/src/main/java/org/bukkit/event/inventory/PrepareItemCraftEvent.java @@ -0,0 +1,52 @@ +package org.bukkit.event.inventory; + +import org.bukkit.event.HandlerList; +import org.bukkit.inventory.CraftingInventory; +import org.bukkit.inventory.InventoryView; +import org.bukkit.inventory.Recipe; + +public class PrepareItemCraftEvent extends InventoryEvent { + private static final HandlerList handlers = new HandlerList(); + private boolean repair; + private CraftingInventory matrix; + + public PrepareItemCraftEvent(CraftingInventory what, InventoryView view, boolean isRepair) { + super(view); + this.matrix = what; + this.repair = isRepair; + } + + /** + * Get the recipe that has been formed. If this event was triggered by a tool repair, this + * will be a temporary shapeless recipe representing the repair. + * @return The recipe being crafted. + */ + public Recipe getRecipe() { + return matrix.getRecipe(); + } + + /** + * @return The crafting inventory on which the recipe was formed. + */ + @Override + public CraftingInventory getInventory() { + return matrix; + } + + /** + * Check if this event was triggered by a tool repair operation rather than a crafting recipe. + * @return True if this is a repair. + */ + public boolean isRepair() { + return repair; + } + + @Override + public HandlerList getHandlers() { + return handlers; + } + + public static HandlerList getHandlerList() { + return handlers; + } +} diff --git a/src/main/java/org/bukkit/event/player/PlayerInventoryEvent.java b/src/main/java/org/bukkit/event/player/PlayerInventoryEvent.java index d71dc0ae..8c67ee4b 100644 --- a/src/main/java/org/bukkit/event/player/PlayerInventoryEvent.java +++ b/src/main/java/org/bukkit/event/player/PlayerInventoryEvent.java @@ -2,11 +2,16 @@ package org.bukkit.event.player; import org.bukkit.entity.Player; import org.bukkit.event.HandlerList; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.event.inventory.InventoryOpenEvent; import org.bukkit.inventory.Inventory; /** - * Represents a player related inventory event + * Represents a player related inventory event; note that this event never actually did anything + * @deprecated Use {@link InventoryClickEvent} or {@link InventoryOpenEvent} instead, or one of + * the other inventory events in {@link org.bukkit.event.inventory}. */ +@Deprecated public class PlayerInventoryEvent extends PlayerEvent { private static final HandlerList handlers = new HandlerList(); protected Inventory inventory; diff --git a/src/main/java/org/bukkit/inventory/BrewerInventory.java b/src/main/java/org/bukkit/inventory/BrewerInventory.java new file mode 100644 index 00000000..a3ebf069 --- /dev/null +++ b/src/main/java/org/bukkit/inventory/BrewerInventory.java @@ -0,0 +1,18 @@ +package org.bukkit.inventory; + +import org.bukkit.block.BrewingStand; + +public interface BrewerInventory extends Inventory { + /** + * Get the current ingredient for brewing. + * @return The ingredient. + */ + ItemStack getIngredient(); + /** + * Set the current ingredient for brewing. + * @param ingredient The ingredient + */ + void setIngredient(ItemStack ingredient); + + BrewingStand getHolder(); +} diff --git a/src/main/java/org/bukkit/inventory/CraftingInventory.java b/src/main/java/org/bukkit/inventory/CraftingInventory.java new file mode 100644 index 00000000..9c1a0c1e --- /dev/null +++ b/src/main/java/org/bukkit/inventory/CraftingInventory.java @@ -0,0 +1,33 @@ +package org.bukkit.inventory; + +/** + * Interface to the crafting inventories + */ +public interface CraftingInventory extends Inventory { + /** + * Check what item is in the result slot of this crafting inventory. + * @return The result item. + */ + ItemStack getResult(); + /** + * Get the contents of the crafting matrix. + * @return The contents. + */ + ItemStack[] getMatrix(); + /** + * Set the item in the result slot of the crafting inventory. + * @param newResult The new result item. + */ + void setResult(ItemStack newResult); + /** + * Replace the contents of the crafting matrix + * @param contents The new contents. + * @throws IllegalArgumentException if the length of contents is greater than the size of the crafting matrix. + */ + void setMatrix(ItemStack[] contents); + /** + * Get the current recipe formed on the crafting inventory, if any. + * @return The recipe, or null if the current contents don't match any recipe. + */ + Recipe getRecipe(); +}
\ No newline at end of file diff --git a/src/main/java/org/bukkit/inventory/DoubleChestInventory.java b/src/main/java/org/bukkit/inventory/DoubleChestInventory.java new file mode 100644 index 00000000..81d60304 --- /dev/null +++ b/src/main/java/org/bukkit/inventory/DoubleChestInventory.java @@ -0,0 +1,15 @@ +package org.bukkit.inventory; + +public interface DoubleChestInventory extends Inventory { + /** + * Get the left half of this double chest. + * @return The left side inventory + */ + Inventory getLeftSide(); + + /** + * Get the right side of this double chest. + * @return The right side inventory + */ + Inventory getRightSide(); +} diff --git a/src/main/java/org/bukkit/inventory/EnchantingInventory.java b/src/main/java/org/bukkit/inventory/EnchantingInventory.java new file mode 100644 index 00000000..40e85946 --- /dev/null +++ b/src/main/java/org/bukkit/inventory/EnchantingInventory.java @@ -0,0 +1,14 @@ +package org.bukkit.inventory; + +public interface EnchantingInventory extends Inventory { + /** + * Set the item being enchanted. + * @param item The new item + */ + void setItem(ItemStack item); + /** + * Get the item being enchanted. + * @return The current item. + */ + ItemStack getItem(); +} diff --git a/src/main/java/org/bukkit/inventory/FurnaceInventory.java b/src/main/java/org/bukkit/inventory/FurnaceInventory.java new file mode 100644 index 00000000..fb3246f4 --- /dev/null +++ b/src/main/java/org/bukkit/inventory/FurnaceInventory.java @@ -0,0 +1,43 @@ +package org.bukkit.inventory; + +import org.bukkit.block.Furnace; + +public interface FurnaceInventory extends Inventory { + /** + * Get the current item in the result slot. + * @return The item + */ + ItemStack getResult(); + + /** + * Get the current fuel. + * @return The item + */ + ItemStack getFuel(); + + /** + * Get the item currently smelting. + * @return The item + */ + ItemStack getSmelting(); + + /** + * Set the current fuel. + * @param stack The item + */ + void setFuel(ItemStack stack); + + /** + * Set the current item in the result slot. + * @param stack The item + */ + void setResult(ItemStack stack); + + /** + * Set the item currently smelting. + * @param stack The item + */ + void setSmelting(ItemStack stack); + + Furnace getHolder(); +} diff --git a/src/main/java/org/bukkit/inventory/Inventory.java b/src/main/java/org/bukkit/inventory/Inventory.java index 2a276246..b42ffe21 100644 --- a/src/main/java/org/bukkit/inventory/Inventory.java +++ b/src/main/java/org/bukkit/inventory/Inventory.java @@ -1,12 +1,17 @@ package org.bukkit.inventory; import java.util.HashMap; +import java.util.List; +import java.util.ListIterator; + import org.bukkit.Material; +import org.bukkit.entity.HumanEntity; +import org.bukkit.event.inventory.InventoryType; /** * Interface to the various inventories */ -public interface Inventory { +public interface Inventory extends Iterable<ItemStack> { /** * Returns the size of the inventory @@ -69,7 +74,8 @@ public interface Inventory { /** * Set the inventory's contents * - * @param items A complete replacement for the contents; the length must be equal to {@link #getSize()}. + * @param items A complete replacement for the contents; the length must be less than or equal to {@link #getSize()}. + * @throws IllegalArgumentException If the array has more items than the inventory. */ public void setContents(ItemStack[] items); @@ -216,4 +222,34 @@ public interface Inventory { * Clear out the whole index */ public void clear(); + + /** + * Get a list of players viewing. Note that a player is considered to be viewing their own + * inventory and internal crafting screen even when said inventory is not open. They will normally + * be considered to be viewing their inventory even when they have a different inventory screen open, + * but it's possible for customized inventory screens to exclude the viewer's inventory, so this should + * never be assumed to be non-empty. + * @return A list of players. + */ + public List<HumanEntity> getViewers(); + + /** + * Get the title of this inventory. + * @return The title. + */ + public String getTitle(); + + /** + * Check what type of inventory this is. + * @return The type of inventory. + */ + public InventoryType getType(); + + /** + * Gets the block or entity belonging to the open inventory + * @return The holder of the inventory; null if it has no holder. + */ + public InventoryHolder getHolder(); + + public ListIterator<ItemStack> iterator(); } diff --git a/src/main/java/org/bukkit/inventory/InventoryHolder.java b/src/main/java/org/bukkit/inventory/InventoryHolder.java new file mode 100644 index 00000000..9d788438 --- /dev/null +++ b/src/main/java/org/bukkit/inventory/InventoryHolder.java @@ -0,0 +1,10 @@ +package org.bukkit.inventory; + +public interface InventoryHolder { + /** + * Get the object's inventory. + * + * @return The inventory. + */ + public Inventory getInventory(); +} diff --git a/src/main/java/org/bukkit/inventory/InventoryView.java b/src/main/java/org/bukkit/inventory/InventoryView.java new file mode 100644 index 00000000..fd2c4080 --- /dev/null +++ b/src/main/java/org/bukkit/inventory/InventoryView.java @@ -0,0 +1,188 @@ +package org.bukkit.inventory; + +import org.bukkit.entity.HumanEntity; +import org.bukkit.event.inventory.InventoryType; + +/** + * Represents a view linking two inventories and a single player + * (whose inventory may or may not be one of the two) + * + * Note: If you implement this interface but fail to satisfy the expected + * contracts of certain methods, there's no guarantee that the game + * will work as it should. + */ +public abstract class InventoryView { + public final static int OUTSIDE = -999; + /** + * Represents various extra properties of certai inventory windows. + */ + public enum Property { + /** + * The progress of the down-pointing arrow in a brewing inventory. + */ + BREW_TIME(0, InventoryType.BREWING), + /** + * The progress of the right-pointing arrow in a furnace inventory. + */ + COOK_TIME(0, InventoryType.FURNACE), + /** + * The progress of the flame in a furnace inventory. + */ + BURN_TIME(1, InventoryType.FURNACE), + /** + * How many total ticks the current fuel should last. + */ + TICKS_FOR_CURRENT_FUEL(2, InventoryType.FURNACE), + /** + * In an enchanting inventory, the top button's experience level value. + */ + ENCHANT_BUTTON1(0, InventoryType.ENCHANTING), + /** + * In an enchanting inventory, the middle button's experience level value. + */ + ENCHANT_BUTTON2(1, InventoryType.ENCHANTING), + /** + * In an enchanting inventory, the bottom button's experience level value. + */ + ENCHANT_BUTTON3(2, InventoryType.ENCHANTING); + int id; + InventoryType style; + private Property(int id, InventoryType appliesTo) { + this.id = id; + style = appliesTo; + } + + public InventoryType getType() { + return style; + } + + public int getId() { + return id; + } + } + /** + * Get the upper inventory involved in this transaction. + * @return the inventory + */ + public abstract Inventory getTopInventory(); + + /** + * Get the lower inventory involved in this transaction. + * @return the inventory + */ + public abstract Inventory getBottomInventory(); + + /** + * Get the player viewing. + * @return the player + */ + public abstract HumanEntity getPlayer(); + + /** + * Determine the type of inventory involved in the transaction. This indicates + * the window style being shown. It will never return PLAYER, since that is + * common to all windows. + * @return the inventory type + */ + public abstract InventoryType getType(); + + /** + * Sets one item in this inventory view by its raw slot ID. + * @param slot The ID as returned by InventoryClickEvent.getRawSlot() + * @param item The new item to put in the slot, or null to clear it. + */ + public void setItem(int slot, ItemStack item) { + if (slot != OUTSIDE) { + if (slot < getTopInventory().getSize()) { + getTopInventory().setItem(convertSlot(slot),item); + } else { + getBottomInventory().setItem(convertSlot(slot),item); + } + } + } + + /** + * Sets one item in this inventory view by its raw slot ID. + * @param slot The ID as returned by InventoryClickEvent.getRawSlot() + * @return The item currently in the slot. + */ + public ItemStack getItem(int slot) { + if (slot == OUTSIDE) { + return null; + } + if (slot < getTopInventory().getSize()) { + return getTopInventory().getItem(convertSlot(slot)); + } else { + return getBottomInventory().getItem(convertSlot(slot)); + } + } + + /** + * Sets the item on the cursor of one of the viewing players. + * @param item The item to put on the cursor, or null to remove the item on their cursor. + */ + public final void setCursor(ItemStack item) { + getPlayer().setItemOnCursor(item); + } + + /** + * Get the item on the cursor of one of the viewing players. + * @return The item on the player's cursor, or null if they aren't holding one. + */ + public final ItemStack getCursor() { + return getPlayer().getItemOnCursor(); + } + + /** + * Converts a raw slot ID into its local slot ID into whichever of the two inventories + * the slot points to. If the raw slot refers to the upper inventory, it will be returned + * unchanged and thus be suitable for getTopInventory().getItem(); if it refers to the + * lower inventory, the output will differ from the input and be suitable for + * getBottomInventory().getItem(). + * @param rawSlot The raw slot ID. + * @return The converted slot ID. + */ + public final int convertSlot(int rawSlot) { + int numInTop = getTopInventory().getSize(); + if (rawSlot < numInTop) { + return rawSlot; + } + int slot = rawSlot - numInTop; + if (getType() == InventoryType.CRAFTING) { + if(slot < 4) return 39 - slot; + else slot -= 4; + } + if (slot >= 27) slot -= 27; + else slot += 9; + return slot; + } + + /** + * Closes the inventory view. + */ + public final void close() { + getPlayer().closeInventory(); + } + + /** + * Check the total number of slots in this view, combining the upper and lower inventories. + * Note though that it's possible for this to be greater than the sum of the two inventories + * if for example some slots are not being used. + * @return The total size + */ + public final int countSlots() { + return getTopInventory().getSize() + getBottomInventory().getSize(); + } + + public final boolean setProperty(Property prop, int value) { + return getPlayer().setWindowProperty(prop, value); + } + + /** + * Get the title of this inventory window. + * @return The title. + */ + public final String getTitle() { + return getTopInventory().getTitle(); + } +} diff --git a/src/main/java/org/bukkit/inventory/PlayerInventory.java b/src/main/java/org/bukkit/inventory/PlayerInventory.java index e5f797a0..57221091 100644 --- a/src/main/java/org/bukkit/inventory/PlayerInventory.java +++ b/src/main/java/org/bukkit/inventory/PlayerInventory.java @@ -1,5 +1,7 @@ package org.bukkit.inventory; +import org.bukkit.entity.HumanEntity; + /** * Includes interface to the 4 armor slots */ @@ -99,4 +101,6 @@ public interface PlayerInventory extends Inventory { * @return Held item slot number */ public int getHeldItemSlot(); + + public HumanEntity getHolder(); } diff --git a/src/main/java/org/bukkit/inventory/Slot.java b/src/main/java/org/bukkit/inventory/Slot.java deleted file mode 100644 index 0e546ca6..00000000 --- a/src/main/java/org/bukkit/inventory/Slot.java +++ /dev/null @@ -1,28 +0,0 @@ -package org.bukkit.inventory; - -/** - * Represents a slot in an inventory - */ -public interface Slot { - - /** - * Gets the inventory this slot belongs to - * - * @return The inventory - */ - public Inventory getInventory(); - - /** - * Get the index this slot belongs to - * - * @return Index of the slot - */ - public int getIndex(); - - /** - * Get the item from the slot. - * - * @return ItemStack in the slot. - */ - public ItemStack getItem(); -} |