1.10.2 Letting devs create addon enchantments for a CE plugin?

Discussion in 'Spigot Plugin Development' started by Askingg, Jan 20, 2020.

  1. So, I'm looking at potentially creating my own CE plugin. The only thing I'm sort of stumped on is how would I allow other developers to create their own enchantments, that hook into my plugin. For example if they wanted to code their own 'Explosive' enchantment, that was using my plugin. How would I go about letting other devs do this?

    Note, I'm not looking to be spoonfed copy & pasteable code.
     
  2. just release your basic API to hook to your CE plugin.
     
  3. So I know how to create an API, the only thing I'm not sure on how to do is, how could I make it actually run the code from the other plugins within my block-break event?
     
  4. You'd likely want those addons to register themselves in some way and then notify them when this event occurs. Then if the addon is interested in such a notification, they register themselves and your API adds that addon to some kind of collection. Then when the event happens, you go through that collection and notify all those registered addons.
     
  5. there are many different ways to do this. for instance

    • let each addon to let your plugin know which event those addon wish to listen to and your plugin maintain that event listener target and relay the event process, or
    • let each addon to talk to bukkit plugin manager to register its own event listener, or
    • Your plugin define own interface and let addons to implement that interface and you invoke them from your plugin, etc..
     
  6. Should I be using an interface the achieve this?
     
  7. regardless of which approach you take, your plugin would need to be able to invoke something in addon's code. hence you would probably end up using interfaces.
     
  8. And how would I actually store / run the code which the API collects?
     
  9. You could go about a couple of ways.

    You could keep it very very simple and let the addon developer do most of the work. They would just import your plugin as a dependency, and do something like this:

    Code (Java):

    private CustomEnchantPlugin cePlugin;

    public void onEnable() {
        cePlugin = Bukkit.getPlugin(CustomEnchantPlugin.class);
        cePlugin.getEnchantmentManager().registerEnchantment(new CustomEnchantment("Explosion"));
    }

    @EventHandler
    public void onBlockBreak(BlockBreakEvent event) {
        ItemStack item = /* get item from event */;
        if (cePlugin.getEnchantmentManager().isItemEnchantedWith(item, "Explosion")) {
            // code to perform whatever the enchantment should do
        }
    }
     
    This would have the addon developer do most work, which means it's more prone to mistakes. You could also decide to handle the activation of an enchantment yourself, by for example having an enum for every action that can trigger an event. Then have the addon dev pass that to your plugin, as well as a lambda function, something like this:

    Code (Java):

    private CustomEnchantPlugin cePlugin;

    public void onEnable() {
        cePlugin = Bukkit.getPlugin(CustomEnchantPlugin.class);
        cePlugin.getEnchantmentManager().registerEnchantment(new CustomEnchantment("Explosion"), TriggerType.BLOCK_BREAK, (player, block) -> { /* Do whatever */ }); // This is a shorthand for the EnchantmentTriggerBlockBreak down below
    }

    public interface EnchantmentTriggerBlockBreak {
        void trigger(Player player, Block block);
    }
     
    You then have to register these things like you normally would, assuming your plugin has a structure like described above. A class that represents an enchantment, a class that manages enchants, etc.
     
    • Like Like x 1
  10. Thank you! I'll have a bit of a test later on today and see how far I can get.