What is the difference?

Discussion in 'Spigot Plugin Development' started by Deger, Jun 6, 2016.

  1. Hello! //EDIT read comment to understand my problem
    I have 2 classes, one implemetns commandexecutor and the main/core extends JavaPlugin and i can only getConfig() in the JavaPlugin and not in the commandexecutor!!!!
    I have seen some code on how to get so i can getconfig in commandexecutor class, but i really do not understand them!!!

    Please only helpful comments!

    Code (Text):
    package me.deger.spawn;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;

    public class Spawn1 implements CommandExecutor {

        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player p = (Player) sender;

            if (!(sender instanceof Player)) {
                sender.sendMessage("Only players are allowed to use this command!");
            if (label.equalsIgnoreCase("setspawn")) {
                Location location = p.getLocation();
                World world = location.getWorld();
                double LocX = location.getX();
                double LocY = location.getY();
                double LocZ = location.getZ();
                float LocYaw = location.getYaw();
                float LocPitch = p.getLocation().getPitch();
                plugin.getConfig().set("Spawn.World", world.getName());
                plugin.getConfig().set("Spawn.X", Double.valueOf(LocX));
                plugin.getConfig().set("Spawn.Y", Double.valueOf(LocY));
                plugin.getConfig().set("Spawn.Z", Double.valueOf(LocZ));
                plugin.getConfig().set("Spawn.Yaw", Float.valueOf(LocYaw));
                plugin.getConfig().set("Spawn.Pitch", Float.valueOf(LocPitch));
                p.sendMessage(ChatColor.GREEN + "The spawn location has been set.");

            // Location loc = p.getLocation();
            // p.getWorld().setSpawnLocation(loc.getBlockX(), loc.getBlockY(),
            // loc.getBlockZ());
            else if (label.equalsIgnoreCase("spawn")) {

                World w = Bukkit.getServer().getWorld("world");
                double x = plugin.getConfig().getDouble("spawn.x");
                double y = plugin.getConfig().getDouble("spawn.y");
                double z = plugin.getConfig().getDouble("spawn.z");
                p.teleport(new Location(w, x, y, z));

                // Location spawn = p.getWorld().getSpawnLocation();
                // p.teleport(spawn);
                // p.sendMessage(ChatColor.RED + "Teleported to spawn!");
            return true;

    I know i have to use ;

    Code (Text):

        private Main plugin;
        public Spawn1(Main pl){
            this.plugin = pl;

    But this is working just fine also, whats the difference?

    Code (Text):

        private Main plugin;
    In the Main/Core plugin that extends JavaPlugin i need to add (this) to the paramter when i set the executor!!!

    And with just:
    Code (Text):

        private Main plugin;
    I am not allowed to have any arguments in the setExecutor.........
    Explain? Both works and it is sort of an error!! (When i do not really understand the difference)
    Plz give examples when to use the one with :
    Code (Text):

        private Main plugin;
        public Spawn1(Main pl){
            this.plugin = pl;
    and the one with
    Code (Text):

        private Main plugin;
    #1 Deger, Jun 6, 2016
    Last edited: Jun 6, 2016
  2. I'm a little confused on what your main issue is. It is how to use getConfig() in your command class or something else. You talk about setExecutor(), but you don't show any code relating to it.
  3. Srry i will make it more clear, i have 2 classes, one implemets CommandExecutor and one extends JavaPlugin.
    And the extend javaplugin as you know can getConfig.options.xxxxxxxx but a CommandExecutor cant.... And then i need to "import" my main class into the CommandExecutor class to be able to getConfig.opstins.xxxxxx and my question is, should i use
    private Main plugin;


    private Main plugin;
    public Spawn1(Main pl){
    this.plugin = pl;

    I do not know what the difference makes, i wanna know when to use the code above and when to use the one on the bottom!!!!
    That is my question!!!
  4. Doing either will create a new instance of your main rather than using the one that is already created when the server starts. You may as well use the instance of your main that is already created by making a static method in your main class that passes the class instance to wherever you need it.

    You can use this in your main class:
    Code (Text):
    public static Main getMain() {
            return Main.getPlugin(Main.class);
    Then whenever you need access to your main from other classes, just use:
    Code (Text):
    Main main = Main.getMain();

    public void someMethod(){
  5. Thank you, i was looking for a example of this thanks!!
  6. That is a really bad way to do it.. Simpler to just use a single static variable.

    Much better to pass to the new class by constructor, for something this simple.
  7. What is wrong with it? Rather than creating a static instance of main and use more resources, you could just use the server's instance of main that is already created. Maybe it's slightly more complicated, but I think it is the more correct way to do it and by no means do I think the way I showed is bad.
  8. Just pass the instance of main to your command class via its constructor.. You're already creating an instance of the command class to register it...

    Keep it simple.
  9. Instead of using static methods you could just add this into your commandexecutor class
    Code (Text):
    <Main Class> plugin;
    public <Command Class>(<Main Class> instance) {
      this.plugin = instance;
    and just use plugin.getConfig().blablablac

    A lot simpler in my opinion..
  10. I try to use MainClass.getPlugin(MainClass.class);
    I don't know if it's better than creating static variables or dependency injection though.
  11. Let me explain all 3:

    Main.getPlugin(Main.class) is an internal static method from JavaPlugin. This method will search a Collection of loaded plugins that have a matching type of Main.class (aka, your main class). This is a great way to get your main class's instance when in a tight spot without needing to program your own static variables/getters.

    Static variables/getters in your main class are a little bit faster (no look up) at the cost of another pointer in memory. This is how a lot of people get their main class's instance. I prefer the internal JavaPlugin#getPlugin() method, unless you're doing a lot of lookups in a short period of time.

    Now, the best way is dependency injection. If you have the choice of this in your structure, use it over the other two methods. There's no statics, there's no extra pointer in memory, there's no lookups. The class that needs the instance will have it when it is instantiated. This is the most common choice. Only use one of the other methods if for some reason dependency injection is not a viable choice (like inside an enum or some static utility class).

    In your specific use case, dependency injection is the best choice and I highly advise you to use it over anything else.
    #11 BillyGalbreath, Jun 6, 2016
    Last edited: Jun 7, 2016
    • Informative Informative x 1
  12. Incorrect.
    public static <T extends JavaPlugin> T getPlugin(Class<T> clazz) {
    Validate.notNull(clazz, "Null class cannot have a plugin");
    if (!JavaPlugin.class.isAssignableFrom(clazz)) {
    throw new IllegalArgumentException(clazz + " does not extend " + JavaPlugin.class);
    ClassLoader classLoader = clazz.getClassLoader();
    if (!(classLoader instanceof PluginClassLoader)) {
    throw new IllegalArgumentException(clazz + " is not initialized by " + PluginClassLoader.class);
    JavaPlugin plugin = ((PluginClassLoader) classLoader).plugin;
    if (plugin == null) {
    throw new IllegalStateException("Cannot get plugin for " + clazz + " from a static initializer");
    return clazz.cast(plugin);
    • Like Like x 1