I have my main class, and a class called CommandSpawn. I am trying to retrieve the config from my Main class and use it in CommandSpawn. How would I make it so that it loads the configuration from Main?
2 Things... 1. Some code generally helps 2. You need to make a Constructor Here is a good tutorial. Let me know if it does not help. https://forums.bukkit.org/threads/beginner-constructors-wip.100772/
Use a constructor in your other class: PHP: // Where 'Main' is your main class's nameMain plugin; // Where 'ClassName' is your other class's namepublic ClassName(Main plugin) { this.plugin = plugin;} /** Calling fields and/or methods:* plugin.getConfig()...*/
Code:java public class CommandSpawn extends JavaPlugin implements CommandExecutor { public CommandSpawn(tgMain plugin) { getConfig(); } @Override public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) { Player player = (Player) sender; if(cmd.getName().equalsIgnoreCase("setspawn")) { getConfig().set("spawn.world", player.getWorld().getName()); getConfig().set("spawn.x", player.getLocation().getBlockX()); getConfig().set("spawn.y", player.getLocation().getBlockY()); getConfig().set("spawn.z", player.getLocation().getBlockZ()); saveConfig(); player.sendMessage(ChatColor.YELLOW + "Lobby spawn-point has been set at this position."); } That's in my CommandSpawn class, which executes my spawn command. My main class is: Code:java public class tgMain extends JavaPlugin implements Listener { public final Logger logger = Logger.getLogger("Minecraft"); public PluginDescriptionFile pdfFile = getDescription(); tgMain plugin; @Override public void onEnable() { FileConfiguration config = getConfig(); config.addDefault("Test.String", "Testing your test"); config.options().copyDefaults(true); saveConfig(); It doesn't do anything... :/ EDIT by Moderator: merged posts, please use the edit button instead of double posting.
Have you tried using the tutorial I sent your way... It explains whats happening a bit more detailed then this. Maybeyour going to have to do a bit of work reading though in the end you will be able to use this .
Yeah, I'm just going to take a look at that. I can't really explain what my problem is here . Thanks for all the help guys anyways. He didn't finish the tutorial all the way... :/. EDIT by Moderator: merged posts, please use the edit button instead of double posting.
It doesn't work. In my CommandSpawn class I have: Code:java public tgMain plugin; public CommandSpawn(tgMain plugin) { this.plugin = plugin; } and Code:java if(cmd.getName().equalsIgnoreCase("setspawn")) { plugin.getConfig().set("spawn.world", player.getWorld().getName()); plugin.getConfig().set("spawn.x", player.getLocation().getBlockX()); plugin.getConfig().set("spawn.y", player.getLocation().getBlockY()); plugin.getConfig().set("spawn.z", player.getLocation().getBlockZ()); plugin.saveConfig(); player.sendMessage(ChatColor.YELLOW + "Lobby spawn-point has been set at this position."); } In my main class I have: Code:java tgMain plugin; @Override public void onEnable() { FileConfiguration config = getConfig(); it does nothing whenever I try to /setspawn
ehArcher PHP: // In your primary class, in the onEnable()getCommand("setspawn").setExecutor(new CommandSpawn(this) this);
Okay, what exactly is the problem? Does the plugin enable? Does the console log execution of the command?