Creating external Libraries

Oct 9, 2021
Creating external Libraries
  • Creating External Libraries

    Creating an external library to use in all of your plugins




    What is an external library?(top)

    It's a local library, that only exists on your computer and can only be used by you.
    It will save you a lot of time, instead of repeating code a million times, you only type it once with external libraries!

    How can I get started?(top)

    You start by making a project like you do for any normal plugin.
    Then, You add Spigot dependency to your project.
    So it's very similar to creating a normal plugin!
    Then we create a new class in the library, that doesn't extend anything.
    this class will be the main class for our library, that our plugin will only set and get value with it.
    For example, JavaPlugin. When you are dealing with things like scheduler you need a JavaPlugin to run a task.
    So, our class will be like this:

    Code (Java):
    import org.bukkit.plugin.java.JavaPlugin;

    public class MyLibrary {

        private static JavaPlugin plugin;

        // For other classes in our library
        public static JavaPlugin getPlugin() {
            return plugin;
        }

        // This method must not be used any where in the library!
        public static void setPlugin(final JavaPlugin plugin) {
            MyLibrary.plugin = plugin;
        }

    }
    Now, after we made Setters and Getter for every data we want,
    Let's create our actual library!

    You can put what ever you want in this library, it's better to use several classes. not only one class.
    In this example i will make a Utils class that has 2 methods, all of them interact with players:
    Code (Java):
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.CommandSender;

    public class Utils {

        public static void tell(final CommandSender sender, final String message) {
            sender.sendMessage(colorize(message));
        }

        public static void log(final String message) {
            tell(Bukkit.getConsoleSender(), "[" + MyLibrary.getPlugin().getName() + "] " + message);
        }

        public static String colorize(final String message) {
            return ChatColor.translateAlternateColorCodes('&', message);
        }

    }
    Note: All methods in the library must be public static or you can't use them.

    How to use it in your plugins(top)

    To use it in your plugins, you need to add it to your classpath first. How?
    Like how you add Spigot to your plugin!

    Maven(top)

    In your pom.xml, add repository:
    Code (XML):
    <dependency>
      <groupId>[Your Library groupId]</groupId>
      <artifactId>[Your Library artifactId]</artifactId>
      <version>1.0.0</version>
      <scope>compile</scope>
    </dependency>

    Without Maven(top)

    Build your library to a jar file, then add it just like you add Spigot.

    Gaining Access to the Library(top)

    Now after we can access the library from our plugin's project, let's set the library plugin:

    Code (Java):
    @Override
    public void onEnable() {
        MyLibrary.setPlugin(this);
    }

    Now you are good to go!
    You can use every static method from the library.

    Thanks for reading this thread!

    Note: if you found any grammar mistake(s) feel free to edit the page!
  • Loading...
  • Loading...