Possible to track one specific item?

Discussion in 'Plugin Development' started by Lanuk, Sep 7, 2012.

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

    Lanuk

    Sorry, this may be a noob question, but I was wondering if there was any way I could track a single specific item. Basically, I want to make it so when a player hold's an item in their hand, such as a sword for example, they will get a certain perk. The tricky part is that I want to make it so they will only get the perk if they are holding that exact item, and not another of it's kind. This will make it so when the item breaks, the player will not be able to use the perk again. Since I am assuming tracking a specific item is probably not possible, does anyone have a solution for this? Help would be appreciated, thanks!
     
  2. Offline

    QuantumDev

    You'd probably have to just track it through a myriad of events.
     
  3. Offline

    Malikk

    (woo, fallout :) )

    In my plugin MobCatcher, I assign each spawn egg an id number and save it to that specific egg via an unsafe enchantment. That's probably the best way to do this as well.
     
    Zidkon likes this.
  4. Offline

    Lanuk

    That seems to be exactly what I need! I am not sure I know what you mean though with "assigning an id number using an unsafe enchantment". I see that your plugin isn't open source so I can't check it out, but if you could help me out a bit I would really appreciate it :)
     
  5. Offline

    Malikk

    Too many plugins trying to do the exact same thing as me -.-

    Anyways, all I do for the ID numbers is assign the item an "unsafe" enchantment. It's like a normal enchantment, but it won't check whether the type of enchantment is applicable to that item, i.e. sword specific enchantments, bow enchants, etc. I specifically picked one that wouldn't work for eggs.

    You can assign enchantments like this.
    Code:
    ItemStack item = //whatever item you're dealing with
    item.addUnsafeEnchantment(Enchantment.ARROW_DAMAGE, ++Id);
    
    The enchantment type is just an enum, but make sure you don't pick one that will give an actual effect. If you stick with arrow damage or something that wouldn't normally be on a sword, i think it won't glow, but I'm not positive about that.

    The second parameter is the level of the enchantment, I serialize that variable to make sure I don't assign a number that's already been used after a restart/reload and I increment it each time before I use it. Easy enough.

    Now whether your a fan of data object classes or maps, etc. just make sure you've saved and have a way to check what ID goes with what data.

    Getting the id from your item,
    Code:
    int id = item.getEnchantmentLevel(Enchantment.ARROW_DAMAGE);
    
     
  6. Offline

    Lanuk

    Ahh, I understand now. Thanks a ton! Really smart idea :)
     
Thread Status:
Not open for further replies.

Share This Page