Solved configs

Discussion in 'Spigot Plugin Development' started by M7MDMCGamer, May 2, 2017.

  1. how to access to my config from 2 classes
    i used this for the config
    Code (Text):

    File configfile = new File(getDataFolder(), "config.yml");
    YamlConfiguration config = YamlConfiguration.loadConfiguration(configfile);
     
     
  2. Why not make it a static variable?
     
    • Useful Useful x 1
  3. im making a simple economy plugin with some more commands
    here is my main class
    Code (Text):

    package gd.rf.m7md.m7mdcraft;

    import java.io.File;

    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;

    import gd.rf.m7md.m7mdcraft.fly.Fly;

    public class M7MDCraft extends JavaPlugin implements Listener {
        File configfile = new File(getDataFolder(), "config.yml");
        YamlConfiguration config = YamlConfiguration.loadConfiguration(configfile);
        public void onEnable(){
            createConfig();
            getCommand("fly").setExecutor(new Fly());
            getServer().getPluginManager().registerEvents(this, this);
       
        }
        private void createConfig() {
           try {
               if (!getDataFolder().exists()) {
                   getDataFolder().mkdirs();
               }
           
               if (!configfile.exists()) {
                   configfile.createNewFile();
                   saveDefaultConfig();
               } else {
                   getLogger().info("Config.yml found, loading!");
               }
           } catch (Exception e) {
               e.printStackTrace();

           }

        }

    }
     
    and here the class that i want use the config in it
    Code (Text):

    package gd.rf.m7md.m7mdcraft.points;

    import java.util.HashMap;

    public class Methods{
     
        HashMap<String, Double> money = new HashMap<String, Double>();
        public double GetBalance(String player){
         
         
         
            return money.get(player);
        }
        public void SetBalance(String player,double bal){
         
            money.put(player, bal);
        }
        public double RemoveBalance(String player,double bal){
         
            money.put(player, money.get(player) - bal);
            return money.get(player);
        }
        public double AddBalance(String player,double bal){
         
            money.put(player, money.get(player) + bal);
            return money.get(player);
        }
    }
     
     
  4. As stated by Michel, I suggest learning more about OOP,

    But lemme help you with this either way.

    Each class can have a constructor such as
    public CLASS_NAME(Required arguements for construction) {

    }

    You can use this to get instance of ur config onto other class by asking for the config in the arguement.
    Use this in your other class
    Code (Text):
    private YamlConfiguration config;
    public CLASS_NAME (YamlConfiguration c){
    config = c;
    }
    This was you can use config to refer to your config. When u call ur other class provide it with ur config

    Code (Text):
    new CLASS_NAME (config_here);
     
  5. Like I said before, if you don't want to create a new file instance, I suggest that you make the variable static.
    Code (Java):
    class A{
        static int staticField = 0;
    }

    class B{
        void method(){
            System.out.println(A.staticField) //Accessing the variable
        }
    }
    Even better
    Code (Java):

    class A{
        private int privateField = 0;
        int getInteger(){
            return privateField;
        }
    }

    class B{
        void method(){
            A a;
            System.out.println(a.getInteger()) //using the getter method
        }
    }
     
     
    #6 spammy23, May 2, 2017
    Last edited: May 2, 2017
  6. new CLASS_NAME (config_here);
    what you mean with config_here
     
  7. Why does it need to be static is the real question? Using static for everything is not using oop and is lazy, and can even be inefficient. The only thing that should be static in my opinion is your singleton to your main class. And even that can be replaced by simply using DI to pass your main plugin instance... To addon, using static variables improperly can end you up with memory leaks.
     
  8. There is a thing called static abuse.
    According to:
    https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html
    I'm convinced, this would be static abuse, because it is not a constant and it is not necessarily the same for every instance (altough Bukkit cares about that).

    You should pass it by the constructor like @Yugikid1 suggested.
     
    • Like Like x 1
  9. By config_here i mean put your YamlConfiguration instance there.

    Also,
    #StopStaticAbuseee xD
     
    • Agree Agree x 2
  10. Sorry, I wrongly assumed that this person had already tried and not wanted to use a getter method. That was the reason why I put in a "better" way of doing things when I wrote the code.
     
  11. Make it private static and create a getter for it.

    Then
    YourMainClass.getConfig()
    From any class


    Sent from my iPhone using Tapatalk
     
  12. if you have a getter method, what's the point in it being static?
     
  13. Because its a PRIVATE static.