Solved Access config from another class

Discussion in 'Plugin Development' started by ZyphiorMC, Nov 23, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    ZyphiorMC

    Hello guys, I am making a custom plugin for my upcoming server, but I seem to run in to a little problem. I am trying to access a configuration file from another class. I used my main.java to create a config.yml, but I am not sure how to access that config from my PlayerJoin.java

    I am using the Config and ConfigManager from Appljuze (bukkitlessons.com).

    Main.java
    Code:java
    1. public class main extends JavaPlugin {
    2. MyConfigManager manager;
    3. MyConfig config;
    4. MyConfig data;
    5.  
    6. public void onEnable() {
    7. manager = new MyConfigManager(this);
    8. config = manager.getNewConfig("config.yml");
    9. data = manager.getNewConfig("data.yml");
    10.  
    11. }
    12. }



    Trying to get some config values from the config.yml in my PlayerJoin.java:
    Code:java
    1. World lobbyWorld = main.config.get("Settings.Locations.Lobby.World");
    2. double lobbyX = main.config.get("Settings.Locations.Lobby.X");
    3. double lobbyY = main.config.get("Settings.Locations.Lobby.Y");
    4. double lobbyZ = main.config.get("Settings.Locations.Lobby.Z");
    5. int lobbyPitch = main.config.get("Settings.Locations.Lobby.Pitch");
    6. int lobbyYaw = main.config.get("Settings.Locations.Lobby.Yaw");



    So, I am wondering, how can I access it? If anyone can help me, then that would be greatly appreciated! Thank you! :D
     
  2. Offline

    Skionz

    ZyphiorMC Use a constructor to pass your config to a different class.
     
  3. Offline

    TGRHavoc

    ZyphiorMC
    I assume that with "MyConfigManager(this)" you're storing the main class in the config manager class.
    If so then all you need to do is "mainClassReference.getConfig()" where "mainClassReference" is the variables passed into the constructor.
     
  4. Offline

    ZyphiorMC

    TGRHavoc Skionz
    Could you show me how I would do that?

    I know you probably will say "I'm not going to spoon feed you".
    I learn fastest from seeing things done by other people.
     
  5. Offline

    Skionz

  6. Offline

    TGRHavoc

    ZyphiorMC
    The following is some pseudo-code (It doesn't work however, it should show you what you should be doing)
    Code:java
    1. public class ClassName { // Class called "ClassName"
    2. Object myObject; // Variable stored in this class
    3.  
    4. public ClassName( Object value ) { // Constructor of this class (Object can be anything e.g. int, String, Another class)
    5. myObject = value; // Set this classes variable to the variable passed
    6. }
    7.  
    8. }


    Edit: Or you could just follow the tutorial above :p
     
  7. Offline

    ZyphiorMC

    Skionz
    Would this be correct usage?


    In main class
    Code:java
    1. public ConfigListener(ConfigExample instance) {
    2. plugin = instance;
    3. }



    In PlayerJoin class
    Code:java
    1. World lobbyWorld = plugin.getConfig().get("Settings.Locations.lobby.world"));
     
  8. Offline

    TGRHavoc

    ZyphiorMC
    Assuming that "plugin" is also a instance of "ConfigExample" and "ConfigExample" is you main class (extends JavaPlugin) then yes, yes it will.
    P.S. You wouldn't just be able to do "World lobbyWorld = someString" you would have to do something like "World lobbyWorld = Bukkit.getServer().getWorld ( someString );"
     
  9. Offline

    ZyphiorMC

    TGRHavoc
    Not really related to what my original post was about, but I am having a little error now with my code.
    [​IMG]
     
  10. Offline

    Asecta

    ZyphiorMC You're importing location from the wrong place :p
     
  11. Offline

    TGRHavoc

    ZyphiorMC
    Make sure that you've imported the location from org.bukkit and not from a com.java package.
     
  12. Offline

    ZyphiorMC

    Thank you :D
     
  13. Offline

    TGRHavoc

    ZyphiorMC
    No problem :D If your problem has been solved, don't forget to mark this thread as "Solved".
     
  14. Offline

    Asecta

  15. Offline

    ZyphiorMC

    TGRHavoc
    I just noticed something that will be a problem if I am wrong

    Should this part in the red box be the name of my config (data), or should it be my "MyConfig" class?
    [​IMG]
     
    Skionz likes this.
  16. Offline

    TGRHavoc

    The way that you're using it in your previous post, it should be your config. This means that you would need to change "MyConfig" to "YamlConfiguration" and then you would pass the config through the constructor e.g. "new PlayerJoin(getConfig())".
    However, if you wanted "MyConfig" to be a class then it should be you main class (The class that extends JavaPlugin), if you're taking this route then you would need to call "plugin.getConfig().otherMethod" to get your values from the config file.
     
  17. Offline

    Dragonphase

    ZyphiorMC

    The principles of Object Oriented Programming should be an understanding and a requirement for things like this. There are two ways of achieving what you want. The first is to pass the instance of your plugin to the class you wish to access it from via the constructor:

    Code:java
    1. public class MyPlugin extends JavaPlugin {
    2. private MyConfigManager configManager;
    3. private MyConfig config;
    4.  
    5. public void onEnable() {
    6. configManager = new MyConfigManager(this);
    7. config = configManager.getNewConfig("something.yml");
    8. }
    9.  
    10. public MyConfig getMyConfig() {
    11. return config;
    12. }
    13. }
    14.  
    15. public class SomeOtherClass {
    16. private MyPlugin plugin;
    17.  
    18. public SomeOtherClass(MyPlugin instance) {
    19. plugin = instance;
    20.  
    21. plugin.getMyConfig().doSomething();
    22. }
    23. }


    The second way is to create a static method to retrieve your plugin instance, which is plausible as Bukkit plugins are naturally singletons:

    Code:java
    1. public class MyPlugin extends JavaPlugin {
    2. private MyConfigManager configManager;
    3. private MyConfig config;
    4.  
    5. public void onEnable() {
    6. configManager = new MyConfigManager(this);
    7. config = configManager.getNewConfig("something.yml");
    8. }
    9.  
    10. public MyConfig getMyConfig() {
    11. return config;
    12. }
    13.  
    14. public static MyPlugin getInstance() {
    15. return (MyPlugin) Bukkit.getPluginManager().getPlugin("MyPlugin");
    16. }
    17. }
    18.  
    19. public class SomeOtherClass {
    20.  
    21. public SomeOtherClass() {
    22. MyPlugin.getInstance().getMyConfig().doSomething();
    23. }
    24. }


    I personally prefer the first method of doing this as it's better Object-Oriented practice. Both methods have their own use-cases however, such as the second method being especially useful for API-related programming. It is worth learning more about. desht goes into detail with this in this post.
     
Thread Status:
Not open for further replies.

Share This Page