Solved Selling custom items with essentials

Discussion in 'Plugin Development' started by NathanWolf, Dec 30, 2013.

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

    NathanWolf

    Is there any way to tie into Essential's API to allow selling custom items via the sign shop interface?

    I was thinking something like registering an item name that essentials would know to ask my plugin to create when purchased.

    I know I can tie into essentials to deduct money, but I was hoping to piggyback on its sign functionality as well.

    If this is not possible, then I'm wondering if I should tie into Essentials or Vault. They both seem kind of interchangeable, and I know essentials will pass through to vault, but I'm not sure at this point which plugin is used more, and I would prefer not to code for both since this will be a soft dependency.

    Thanks very much!

    Bumpity bump ... I'm thinking this isn't possible. I did do a good bit of searching before posting this.

    It seems like it will be a pain to implement my own sign handling that doesn't interfere with Essentials, so it'd be really nice if Essentials let you extend the functionality somehow. I'm hesitant to try and dig into the code and try to hack it up somehow :)

    I submitted a feature request to Essentials for this.. if it's something you'd be interested in having for your own plugins, please go vote on it :)

    https://essentials3.atlassian.net/browse/ESS-5078

    Otherwise it doesn't seem like this is possible, save perhaps with some hacky overriding of some of the internal Essentials classes, which doesn't seem like a good idea at all :D

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  2. Offline

    NathanWolf

    Mmmmmk, well I'm guessing the Essentials team will not really approve of this, but perhaps that will motivate them to add an API :D

    Here's what I ended up doing, which works, but is completely hacky and I don't recommend following this path.

    in my onEnable, I check for essentials. If it's there, I use reflection to override the ItemDB with my own.

    Code:java
    1.  
    2. final Spells me = this;
    3. Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
    4. public void run() {
    5. try {
    6. Object essentials = me.plugin.getServer().getPluginManager().getPlugin("Essentials");
    7. if (essentials != null) {
    8. Class<?> essentialsClass = essentials.getClass();
    9. Field itemDbField = essentialsClass.getDeclaredField("itemDb");
    10. itemDbField.setAccessible(true);
    11. itemDbField.set(essentials, new MagicItemDb(me, essentials));
    12. log.info("Essentials found, hooked up custom item handler");
    13. }
    14. } catch (Throwable ex) {
    15. ex.printStackTrace();
    16. }
    17. }
    18. }, 5);


    My MagicItemDb class, which checks an item id string to see if it's a wand, if not I pass up to the default functionality.

    Code:java
    1.  
    2. package com.elmakers.mine.bukkit.essentials;
    3.  
    4. import net.ess3.api.IEssentials;
    5.  
    6. import org.bukkit.inventory.ItemStack;
    7.  
    8. import com.earth2me.essentials.ItemDb;
    9. import com.elmakers.mine.bukkit.plugins.magic.Spells;
    10. import com.elmakers.mine.bukkit.plugins.magic.Wand;
    11.  
    12. public class MagicItemDb extends ItemDb {
    13.  
    14. private final Spells spells;
    15.  
    16. public MagicItemDb(final Spells spells, final Object ess) {
    17. super((IEssentials)ess);
    18. this.spells = spells;
    19. }
    20.  
    21. @Override
    22. public ItemStack get(final String id) throws Exception
    23. {
    24. if (id.startsWith("wand:")) {
    25. id.replace("wand:", "");
    26. Wand wand = Wand.createWand(spells, id.trim());
    27. if (wand != null) {
    28. return wand.getItem();
    29. }
    30. }
    31.  
    32. return super.get(id);
    33. }
    34. }
    35.  


    Marking this as solved, though I still hope there's a better way, or will be in the future.
     
    mickverm likes this.
Thread Status:
Not open for further replies.

Share This Page