001 package org.bukkit.event.enchantment; 002 003 import java.util.HashMap; 004 import java.util.Map; 005 006 import org.bukkit.block.Block; 007 import org.bukkit.enchantments.Enchantment; 008 import org.bukkit.entity.Player; 009 import org.bukkit.event.Cancellable; 010 import org.bukkit.event.HandlerList; 011 import org.bukkit.event.inventory.InventoryEvent; 012 import org.bukkit.inventory.InventoryView; 013 import org.bukkit.inventory.ItemStack; 014 015 /** 016 * Called when an ItemStack is successfully enchanted (currently at 017 * enchantment table) 018 */ 019 public class EnchantItemEvent extends InventoryEvent implements Cancellable { 020 private static final HandlerList handlers = new HandlerList(); 021 private final Block table; 022 private final ItemStack item; 023 private int level; 024 private boolean cancelled; 025 private final Map<Enchantment,Integer> enchants; 026 private final Player enchanter; 027 private int button; 028 029 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) { 030 super(view); 031 this.enchanter = enchanter; 032 this.table = table; 033 this.item = item; 034 this.level = level; 035 this.enchants = new HashMap<Enchantment, Integer>(enchants); 036 this.cancelled = false; 037 this.button = i; 038 } 039 040 /** 041 * Gets the player enchanting the item 042 * 043 * @return enchanting player 044 */ 045 public Player getEnchanter() { 046 return enchanter; 047 } 048 049 /** 050 * Gets the block being used to enchant the item 051 * 052 * @return the block used for enchanting 053 */ 054 public Block getEnchantBlock() { 055 return table; 056 } 057 058 /** 059 * Gets the item to be enchanted (can be modified) 060 * 061 * @return ItemStack of item 062 */ 063 public ItemStack getItem() { 064 return item; 065 } 066 067 /** 068 * Get cost in exp levels of the enchantment 069 * 070 * @return experience level cost 071 */ 072 public int getExpLevelCost() { 073 return level; 074 } 075 076 /** 077 * Set cost in exp levels of the enchantment 078 * 079 * @param level - cost in levels 080 */ 081 public void setExpLevelCost(int level) { 082 this.level = level; 083 } 084 085 /** 086 * Get map of enchantment (levels, keyed by type) to be added to item 087 * (modify map returned to change values). Note: Any enchantments not 088 * allowed for the item will be ignored 089 * 090 * @return map of enchantment levels, keyed by enchantment 091 */ 092 public Map<Enchantment, Integer> getEnchantsToAdd() { 093 return enchants; 094 } 095 096 /** 097 * Which button was pressed to initiate the enchanting. 098 * 099 * @return The button index (0, 1, or 2). 100 */ 101 public int whichButton() { 102 return button; 103 } 104 105 public boolean isCancelled() { 106 return cancelled; 107 } 108 109 public void setCancelled(boolean cancel) { 110 this.cancelled = cancel; 111 } 112 113 @Override 114 public HandlerList getHandlers() { 115 return handlers; 116 } 117 118 public static HandlerList getHandlerList() { 119 return handlers; 120 } 121 }