Cancel Anvil events?

Discussion in 'Plugin Development' started by Frazz86, Jan 9, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    Frazz86

    How could i cancel anvil repair events? I want it to cancel everything but putting a book on a weapon :D.

    Also could some one tell me how i could create a chest for each players and have it save exactly the content for whenever they next trigger the event? Similarly to ender chests

    fireblast709 psst :p.
     
  2. Offline

    fireblast709

    1. InventoryClickEvent. Check the slots (as there are no AnvilInventory related events atm)
    2. Bukkit.createInventory(params), you should save the inventory somewhere onInvClose() and load again onInvOpen()
     
  3. Offline

    evilmidget38

    Frazz86 For your first question: There currently are no anvil events, however, there's still a way to cancel them. You'll have to listen for InventoryClickEvent, check if they're clicking inside of an anvil, and see if they're clicking in the result slot(slot #2 for anvils). Then, you can get the item from that slot, check the enchantments or whatever, and then cancel the event if you need to. I haven't tested this myself, so it may or may not work exactly as wanted.

    Your second question: The easiest way to do this is to simply save and load the ItemStack's from YAML. Example code:
    Code:
        private ItemStack[] loadPlayerChest(String player) {
            player = player.toLowerCase();
            File playerFile = new File(root, player+".yml");
            if (!playerFile.exists()) {
                return new ItemStack[54];
            }
            FileConfiguration config = YamlConfiguration.loadConfiguration(playerFile);
            List<ItemStack> list = (List<ItemStack>) config.getList("items");
            // Ensure that the returned array is of the correct length.
            ItemStack[] items = new ItemStack[54];
            return list.toArray(items);
           
        }
        private void save(String player, ItemStack[] items) {
            player = player.toLowerCase();
            FileConfiguration config = new YamlConfiguration();
            config.set("items", Arrays.asList(items));
            try {
                config.save(new File(root, player+".yml"));
            } catch (IOException e) {
                e.printStackTrace();
            }
           
        }
    This code is used in a plugin I've made privately for someone which has a separate file for the inventory of each player. Something I haven't done, but would be a great idea to do, is to use a list that doesn't contain all the null elements in the array. If you don't do this, you end up with larger config files, such as:
    Show Spoiler

    Code:
    items:
    - ==: org.bukkit.inventory.ItemStack
      type: ITEM_FRAME
      amount: 64
    - ==: org.bukkit.inventory.ItemStack
      type: PAINTING
      amount: 32
    - ==: org.bukkit.inventory.ItemStack
      type: CHEST
      amount: 64
    - ==: org.bukkit.inventory.ItemStack
      type: PUMPKIN
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    
     
  4. Offline

    Frazz86

    Its fine, mine are only gonna hold 3-4 slots :p, at least i hope thats possible
     
  5. Offline

    rfsantos1996

    Yeah, i'm reviving this post ONE YEAR AND A HALF later and still no Anvil events :l
    Do anyone know any plugin that does that? (not a problem for me to make this "manually", but APIs are easier...)

    @EDIT: this may help people who are searching!

    And about reviving a post: I find it valid when it is unresolved, people may find this and are still looking for answers, so that link may help some of them.. So please be nice with reviving unresolved threads
     
Thread Status:
Not open for further replies.

Share This Page