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);
Use a getter method. Learn Java. Learn OOP. https://www.google.com/?q=oop+getter If you want further help, post both classes you want to interact with.
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); } }
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);
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 } }
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.
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.
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.
Make it private static and create a getter for it. Then YourMainClass.getConfig() From any class Sent from my iPhone using Tapatalk