1.16.5 Plugin in Constructor for Spigot + Bungee?

Discussion in 'Spigot Plugin Development' started by TaskID, Oct 23, 2020.

  1. Hello guys,
    I'm currently working on an API for my server. I have a class "DBConnection", where I want to create a file in the plugin folder with the defined file name, so they have to pass the plugin and the filename. I want that this is working for both spigot and bungeecord. Will there be errors if I have constructors or imports that don't exist in Spigot/Bungee? So that I just create one cunstructor with the org.bukkit.Plugin and one with the net.md_5.bungee.api.plugin.Plugin. Does someknow know if this works?
     
  2. SteelPhoenix

    Moderator

    I'm assuming you need to use #getDataFolder()?
    You can either pass the folder instead of the plugin, use reflection (wouldn't recommend) or specify some common interface both plugins use like this:

    Code (Java):
    public interface IPlugin {
        @NotNull
        public File getDataFolder();
    }

    // Your bungee plugin
    public class BungeePlugin extends Plugin implements IPlugin {}

    // Your bukkit plugin
    public class BukkitPlugin extends JavaPlugin implements IPlugin {}

    public MyClass {
        private final IPlugin plugin;
        public MyClass(IPlugin plugin) {
            this.plugin = Validate.notNull(plugin);
        }
        // Now  you can use plugin.getDataFolder() here
    }
     
    • Agree Agree x 1
    • Useful Useful x 1