Creating external Libraries

Nov 1, 2019
Creating external Libraries
  • Creating external Libraries

    Creating an external library as java plugin




    What is an external library(top)

    It's a local library, that only exists in your computer and can only be used by you.
    It will save you a lot of time, instead of repeating code 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(JavaPlugin plugin) {
            this.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(CommandSender sender, 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.
    Now that we finished making the library:

    How to use it in your plugins(top)


    you need to add it your classpath first, How?
    Like how you add Spigot to your dependency!

    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.

    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 methods from the library!

    Thanks for reading this thread!

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