EnchantmentTable

Discussion in 'Bukkit Preview' started by Mike Primm, Jan 24, 2012.

  1. Offline

    Mike Primm

    This feature provides a set of events, with associated CraftBukkit hooks, to support tailoring of the behavior of Enchantment Tables (particularly, the use of those tables while enchanting items).

    The implementation provides two events - both coded to be purely matching the new event model (there are no corresponding callbacks in the existing Listener classes, nor an Event.Type.* entry).

    The first event, PrepareItemEnchantEvent, is fired whenever an item is placed in the enchantment table that is a candidate for enchanting. At this point, the server code has calculated the enchantment bonus due to the bookshelves, if any, and is generating 3 enchantment levels (experience level costs) that will be offered to the user (if they are non-zero). The event allows plugins to see the player, the enchantment table, the item being considered, enchantment bonus used, and the 3 experience level costs being offered - and allows the event to be cancelled, or for the offered levels to be modified (allowing overriding of the logic used by MC to generate these - which is a pseudorandom value, weighted by the enchantment bonus from the bookshelves, but independent of the player and item). This event will fire one time each time the table generates these values - which is typically 2 or 3 times each time an item is inserted in the table (the last one is all that is used), and the results of each run are generally unique. NOTE: the specific enchantments are not determined at this time - the numbers are all that is done, and the "galactic universal alphabet" messages on the GUI are client side nonsense. All that comes out of this process is the set of 3 experience level costs that the user can opt to pay to enchant the item.

    The second event, EnchantItemEvent, is fired when the player has selected one of the offered enchantment levels to enchant the item. Once again, the event offers information on the player, the table, and the item to be enchanted. It also includes the experience level cost selected (which can be modified), the set of enchantments to be applied (as a Map<Enchantment,Integer>, just like in the bukkit ItemStack - also modifiable). The to-be-enchanted ItemStack can also be modified - allowing enchantment to transform items as well (turn an iron sword into an enchanted gold one, for example). The enchantments are added safely to the item - any enchantments in the map that are not suitable for the type of item returned by the event will be ignored. If the event is cancelled, or the experience cost exceeds the player's experience level, or the added enchantment map is empty, the experience level cost is not applied and no enchantments are added.
     
    angelofdev, ZNickq, Technius and 2 others like this.

Share This Page