Dynamic external class access?

Discussion in 'Spigot Plugin Development' started by TheMcBaws, Jul 30, 2018.

  1. Hello fellow Minecrafters, I come to ask for some of your knowledge to be parted on me. So I have a command
    Code (Java):
    cmp.register(config.getString("ajoin.settings.commandprefix"), new Spawn("spawn", this));
    , it gets an argument this, which is the main class. Is there any way I could reaccess the main class? I need to access the latest config to use for my commands. Also, I cannot make an object of the main class, because it can only be loaded once. Thx if anyone knows how to help, or if this is possible
     
  2. If I understand correctly, you want to be able to access you main class from the spawn command class?

    If so, you can just save the instance of the main class passed through the parameter to a field in the spawn command class.
     
  3. I need it dynamically, because the config gotten from that instance don't reflect changes in the config
     
  4. also, the config file from that instance doesn't reflect the non default config(the one in the plugin folder)
     
  5. After you reload the config via pluginMainClassInstance.reloadConfig(), the config returned by pluginMainClassInstance.getConfig() will contain the latest values from the file.
     
    • Friendly Friendly x 1
  6. didn't work, here is my code incase you have some insight
    Code (Java):
    package net.atlanthea.themcbaws.ajoin.commands;

    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;

    import net.atlanthea.themcbaws.ajoin.main.Main;


    public class Spawn extends Command {

        Main plugin;
        FileConfiguration config;
        public Spawn(String name, Main instance) {
            super(name);
            this.plugin = instance;
            this.config = instance.getConfig();
            this.setAliases(config.getStringList("ajoin.settings.aliases"));
            this.setDescription(config.getString("ajoin.settings.plugindesc"));
            this.setUsage(config.getString("ajoin.settings.pluginusage"));
           
        }

        @Override
        public boolean execute(CommandSender sender, String commandLabel, String[] args) {
            Player executor = (Player) sender;

            if(args.length == 0) {
                Location spawn = getSpawn();
                executor.teleport(spawn);
                executor.sendMessage(ChatColor.translateAlternateColorCodes('&', config.getString("ajoin.spawn.message")));
            }
            return false;

        }
        public Location getSpawn() {
            plugin.reloadConfig();
            double x, y, z;
            float pitch, yaw;
            String worldString;
            x =  config.getDouble("ajoin.spawn.x");
            y =  config.getDouble("ajoin.spawn.y");
            z =  config.getDouble("ajoin.spawn.z");
            pitch =  (float) config.getDouble("ajoin.spawn.pitch");
            yaw =  (float) config.getDouble("ajoin.spawn.yaw");
            worldString = config.getString("ajoin.spawn.world");
            World world = plugin.getServer().getWorld(worldString);
            Location spawn = new Location(world, x, y, z, pitch, yaw);
            return spawn;
        }

    }
     
     
  7. You have to freshly get the newly loaded config by calling getConfig() again, because reloadConfig() will actually exchange the internal config instance.
     
    • Friendly Friendly x 1
  8. what im saying is that its getting from the default config, not the config that's in the plugin folder
     
    #8 TheMcBaws, Jul 31, 2018
    Last edited: Jul 31, 2018
  9. It will only use the value from the default config, if your loaded config file does not contain a corresponding value. Make sure that your config paths are correct.
    Another way to make sure to not use the default values is to specify default values yourself everytime you get a value from the config.
     
    • Friendly Friendly x 1
  10. wdym by specify default values?
     
  11. Example: config.getDouble("ajoin.spawn.x", 0.0D); will now return 0.0 instead of the value from the default config, in case your config file does not contain a value at path "ajoin.spawn.x". However, this should only make a difference if your config file is missing entries.. That's why I said: Make sure that your config paths in your config file are correct.

    Edit: Anyways, have you already tried what I suggested above (replacing your config instance via config = plugin.getConfig() after you have called plugin.reloadConfig())?
     
    • Friendly Friendly x 1
  12. that did not work here is my code rn:
    Code (Java):
    package net.atlanthea.themcbaws.ajoin.commands;

    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;

    import net.atlanthea.themcbaws.ajoin.main.Main;


    public class Spawn extends Command {

        Main plugin;
        FileConfiguration config;
        public Spawn(String name, Main instance) {
            super(name);
            this.plugin = instance;
            this.config = instance.getConfig();
            this.setAliases(config.getStringList("ajoin.settings.aliases"));
            this.setDescription(config.getString("ajoin.settings.plugindesc"));
            this.setUsage(config.getString("ajoin.settings.pluginusage"));
           
        }

        @Override
        public boolean execute(CommandSender sender, String commandLabel, String[] args) {
            Player executor = (Player) sender;

            if(args.length == 0) {
                Location spawn = getSpawn();
                executor.teleport(spawn);
                executor.sendMessage(ChatColor.translateAlternateColorCodes('&', config.getString("ajoin.spawn.message")));
            }
            return false;

        }
        public Location getSpawn() {
            plugin.reloadConfig();
            double x, y, z;
            float pitch, yaw;
            String worldString;
            x =  plugin.getConfig().getDouble("ajoin.spawn.x");
            y =  plugin.getConfig().getDouble("ajoin.spawn.y");
            z =  plugin.getConfig().getDouble("ajoin.spawn.z");
            pitch =  (float) plugin.getConfig().getDouble("ajoin.spawn.pitch");
            yaw =  (float) plugin.getConfig().getDouble("ajoin.spawn.yaw");
            worldString = plugin.getConfig().getString("ajoin.spawn.world");
            World world = plugin.getServer().getWorld(worldString);
            Location spawn = new Location(world, x, y, z, pitch, yaw);
            return spawn;
        }

    }
     
     
  13. Okay, what exactly is not working? I would expect that at least getSpawn() should now pick the latest values from the config whenever you call it. If it doesn't, can you also attach your config file?
     
    • Friendly Friendly x 1
  14. Code (YAML):
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    #                                                                                             #
    #                                                                                             #
    #                AAA                      JJJJJJJJJJJ                 iiii                    #
    #               A:::A                     J:::::::::J                i::::i                   #
    #              A:::::A                    J:::::::::J                 iiii                    #
    #             A:::::::A                   JJ:::::::JJ                                         #
    #            A:::::::::A                    J:::::J   ooooooooooo   iiiiiiinnnn  nnnnnnnn     #
    #           A:::::A:::::A                   J:::::J oo:::::::::::oo i:::::in:::nn::::::::nn   #
    #          A:::::A A:::::A                  J:::::Jo:::::::::::::::o i::::in::::::::::::::nn  #
    #         A:::::A   A:::::A                 J:::::jo:::::ooooo:::::o i::::inn:::::::::::::::n #
    #        A:::::A     A:::::A                J:::::Jo::::o     o::::o i::::i  n:::::nnnn:::::n #
    #       A:::::AAAAAAAAA:::::A   JJJJJJJ     J:::::Jo::::o     o::::o i::::i  n::::n    n::::n #
    #      A:::::::::::::::::::::A  J:::::J     J:::::Jo::::o     o::::o i::::i  n::::n    n::::n #
    #     A:::::AAAAAAAAAAAAA:::::A J::::::J   J::::::Jo::::o     o::::o i::::i  n::::n    n::::n #
    #    A:::::A             A:::::AJ:::::::JJJ:::::::Jo:::::ooooo:::::oi::::::i n::::n    n::::n #
    #   A:::::A               A:::::AJJ:::::::::::::JJ o:::::::::::::::oi::::::i n::::n    n::::n #
    #  A:::::A                 A:::::A JJ:::::::::JJ    oo:::::::::::oo i::::::i n::::n    n::::n #
    # AAAAAAA                   AAAAAAA  JJJJJJJJJ        ooooooooooo   iiiiiiii nnnnnn    nnnnnn #
    #                                    by Themcbaws#3884                                        #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    ajoin
    :
        join
    :
            enabled
    : true
            message
    : "&2&lWelcome, %player_name%!"
            silentjoinpermission
    : "a.silentjoin"
        firstjoin
    :
            message
    :
                enabled
    : true
                message
    : "&d&l%player_name% has joined for the first time!"
                silentfirstjoinpermission
    : "a.silentfirstjoin"
            teleport
    :
                enabled
    : true
                x
    : -53.97952054971176
                y
    : 68.0
                z
    : 249.2424689874761
                pitch
    : -4.915202
                yaw
    : -17.817612
                world
    : world
        leave
    :
            enabled
    : true
            message
    : "&c&lBye, %player_name%!"
            silentleavepermission
    : "a.silentleave"
        spawn
    :
            enabled
    : true
            message
    : "You were teleported to spawn"
            forcemessage
    : "You were teleported by %sender%"
            x
    : -53.97952054971176
            y
    : 68.0
            z
    : 249.2424689874761
            pitch
    : -4.915202
            yaw
    : -17.817612
            world
    : world
    # may or may not have papi support      
        messages
    :
            nopermission
    : "No perms"
            reload
    : "Config reloaded"
            papidetected
    : "Papi detected"
            papihooked
    : "Papi hooked"
            angrypapi
    : "Enable papi damnit"
            papiundetected
    : "Papi undetected"
            mustbeaplayer
    : "You must be a player"
            spawnset
    : "Spawn has been set"
    # setspawn is for both /<basecommand> setspawn (firstjoin spawn) and /setspawn (regular spawn)
        perms
    :
            reload
    : "a.reload"
            setspawn
    : "a.setspawn"
    # may need server restart or plugin unload/loading
        settings
    :
            useplaceholderapi
    : true
            opsaresilent
    : false
            basecommand
    : "ajoin"
            aliases
    :
           - "join"
            - "firstjoin"
            commandprefix
    : "a"
            plugindesc
    : "Base command for AJoin"
            pluginusage
    : "/ajoin <subcommand>"
    Code (YAML):
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    #                                                                                             #
    #                                                                                             #
    #                AAA                      JJJJJJJJJJJ                 iiii                    #
    #               A:::A                     J:::::::::J                i::::i                   #
    #              A:::::A                    J:::::::::J                 iiii                    #
    #             A:::::::A                   JJ:::::::JJ                                         #
    #            A:::::::::A                    J:::::J   ooooooooooo   iiiiiiinnnn  nnnnnnnn     #
    #           A:::::A:::::A                   J:::::J oo:::::::::::oo i:::::in:::nn::::::::nn   #
    #          A:::::A A:::::A                  J:::::Jo:::::::::::::::o i::::in::::::::::::::nn  #
    #         A:::::A   A:::::A                 J:::::jo:::::ooooo:::::o i::::inn:::::::::::::::n #
    #        A:::::A     A:::::A                J:::::Jo::::o     o::::o i::::i  n:::::nnnn:::::n #
    #       A:::::AAAAAAAAA:::::A   JJJJJJJ     J:::::Jo::::o     o::::o i::::i  n::::n    n::::n #
    #      A:::::::::::::::::::::A  J:::::J     J:::::Jo::::o     o::::o i::::i  n::::n    n::::n #
    #     A:::::AAAAAAAAAAAAA:::::A J::::::J   J::::::Jo::::o     o::::o i::::i  n::::n    n::::n #
    #    A:::::A             A:::::AJ:::::::JJJ:::::::Jo:::::ooooo:::::oi::::::i n::::n    n::::n #
    #   A:::::A               A:::::AJJ:::::::::::::JJ o:::::::::::::::oi::::::i n::::n    n::::n #
    #  A:::::A                 A:::::A JJ:::::::::JJ    oo:::::::::::oo i::::::i n::::n    n::::n #
    # AAAAAAA                   AAAAAAA  JJJJJJJJJ        ooooooooooo   iiiiiiii nnnnnn    nnnnnn #
    #                                    by Themcbaws#3884                                        #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    ajoin
    :
      join
    :
        enabled
    : true
        message
    : '&2&lWelcome, %player_name%!'
        silentjoinpermission
    : a.silentjoin
      firstjoin
    :
        message
    :
          enabled
    : true
          message
    : '&d&l%player_name% has joined for the first time!'
          silentfirstjoinpermission
    : a.silentfirstjoin
        teleport
    :
          enabled
    : true
          x
    : -9
          y
    : 71
          z
    : 13
          pitch
    : 69.12961
          yaw
    : -127.79519
          world
    : world_nether
      leave
    :
        enabled
    : true
        message
    : '&c&lBye, %player_name%!'
        silentleavepermission
    : a.silentleave
      spawn
    :
        enabled
    : true
        message
    : were teleported to spawn
        forcemessage
    : You were teleported by %sender%
        x
    : -9
        y
    : 71
        z
    : 13
        pitch
    : 69.12961
        yaw
    : -127.79519
        world
    : world_nether
      messages
    :
        nopermission
    : No perms
        reload
    : '&cConfig reloaded'
        papidetected
    : Papi detected
        papihooked
    : Papi hooked
        angrypapi
    : Enable papi damnit
        papiundetected
    : Papi undetected
        mustbeaplayer
    : You must be a player
        spawnset
    : Spawn has been set
      perms
    :
        reload
    : a.reload
        setspawn
    : a.setspawn
      settings
    :
        useplaceholderapi
    : true
        opsaresilent
    : false
        basecommand
    : ajoin
        aliases
    :
       - join
        - firstjoin
        commandprefix
    : a
        plugindesc
    : Base command for AJoin
        pluginusage
    : /ajoin <subcommand>
     
  15. There is an issue with your config file: ajoin.settings.aliases, the 'join' alias is missing a space. Maybe that causes the config loading to fail.

    Edit: I just tested, and this seems to be the issue. You should get a giant error inside your server log though.. no idea why you didn't notice that.
     
  16. oof, it was a mistake on transferring it over
     
  17. Maybe its my server, if you could test the file, it would be amazing https://www.spigotmc.org/resources/ajoin.59289/
     
  18. Your server should not be a problem, as long as you have the latest version there will be no difference between anyone else's server and yours. Check your default config and make sure your spacing is correct. All your methods seem right, so it should work. I cannot test it rn sadly.
     
  19. I justed tested the a:join command and that works fine (it used the coordinates from the config). Maybe remove your config file, let the plugin regenerate it, and then only make minimal changes (like only changing the coordinates of the spawn).
     
    • Friendly Friendly x 1
  20. Did you try /setspawn and /spawn?