I don't know why i get this error. I am just calling a method in a other class file. Because when i used the method in the other class file in the WesAdvert.java class file it worked just fine. Help please? ERROR Code: 2012-05-11 19:43:12 [WARNING] Unexpected exception while parsing console command org.bukkit.command.CommandException: Unhandled exception executing command 'advert' in plugin WesAdvert v1.03 at org.bukkit.command.PluginCommand.execute(PluginCommand.java:42) at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:166) at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:473) at org.bukkit.craftbukkit.CraftServer.dispatchServerCommand(CraftServer.java:469) at net.minecraft.server.MinecraftServer.b(MinecraftServer.java:596) at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:565) at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:449) at net.minecraft.server.ThreadServerApplication.run(SourceFile:492) Caused by: java.lang.IllegalArgumentException: File cannot be null at org.bukkit.configuration.file.YamlConfiguration.loadConfiguration(YamlConfiguration.java:171) at org.bukkit.plugin.java.JavaPlugin.reloadConfig(JavaPlugin.java:117) at org.bukkit.plugin.java.JavaPlugin.getConfig(JavaPlugin.java:111) at me.wesleydeman.wesadvert.Functions.Advert(Functions.java:19) at me.wesleydeman.wesadvert.WesAdvert.onCommand(WesAdvert.java:66) at org.bukkit.command.PluginCommand.execute(PluginCommand.java:40) ... 7 more WesAdvert.java code Code: package me.wesleydeman.wesadvert; import java.util.logging.Logger; import me.wesleydeman.wesadvert.Functions; import net.milkbowl.vault.economy.Economy; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.java.JavaPlugin; public class WesAdvert extends JavaPlugin { private static final Logger log = Logger.getLogger("Minecraft"); public void loadConfiguration(){ String path = "cost.amount"; String path1 = "cost.enabled"; String path2 = "tag"; String path3 = "username"; getConfig().addDefault(path, 5.00); getConfig().addDefault(path1, true); getConfig().addDefault(path2, "[Advert]"); getConfig().addDefault(path3, true); getConfig().options().copyDefaults(true); saveConfig(); } public void onEnable(){ loadConfiguration(); if(getConfig().getBoolean("cost.enabled") == true) { if (!setupEconomy() ) { log.info(String.format("[%s] Disabled due to no Vault dependency found!", getDescription().getName())); getServer().getPluginManager().disablePlugin(this); return; } else { log.info(String.format("[%s] Enabled Version %s", getDescription().getName(), getDescription().getVersion())); } } else { loadConfiguration(); log.info(String.format("[%s] Enabled Version %s", getDescription().getName(), getDescription().getVersion())); } } public void onDisable(){ log.info(String.format("[%s] Disabled Version %s", getDescription().getName(), getDescription().getVersion())); } public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){ Functions function = new Functions(); Player player = null; if (sender instanceof Player) { player = (Player) sender; } if (cmd.getName().equalsIgnoreCase("advert")){ function.Advert(player, sender, cmd, commandLabel, args); } return false; } public static Economy econ = null; private boolean setupEconomy() { if (getServer().getPluginManager().getPlugin("Vault") == null) { return false; } RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class); if (rsp == null) { return false; } econ = rsp.getProvider(); return econ != null; } } Functions.java Code Code: package me.wesleydeman.wesadvert; import net.milkbowl.vault.economy.Economy; import net.milkbowl.vault.economy.EconomyResponse; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.plugin.java.JavaPlugin; public class Functions extends JavaPlugin { public static Economy econ = null; public boolean Advert(Player player,CommandSender sender,Command cmd,String commandLabel,String[] args) { if (player == null) { if (args.length < 1) { sender.sendMessage("[WesAdvert] " + ChatColor.RED + "No text specified!"); return false; } String message = ""; for (String part : args) { if (message != "") message += " "; message += part; } String tag = getConfig().getString("tag"); if(tag == "") { tag = "[Advert]"; } if(getConfig().getBoolean("username") == true) { Bukkit.getServer().broadcastMessage(ChatColor.YELLOW + tag + ChatColor.RED + "Console: " + ChatColor.WHITE + message); return true; } else { Bukkit.getServer().broadcastMessage(ChatColor.YELLOW + tag + " " + ChatColor.WHITE + message); return true; } } else { if (args.length < 1) { sender.sendMessage("[WesAdvert] " + ChatColor.RED + "No text specified!"); return false; } if(player.hasPermission("wesadvert.advert")) { String message = ""; for (String part : args) { if (message != "") message += " "; message += part; } if(getConfig().getBoolean("cost.enabled") == true) { EconomyResponse r = econ.withdrawPlayer(player.getName(), getConfig().getInt("cost.amount")); if(r.transactionSuccess()) { sender.sendMessage(String.format("[WesAdvert] You were withdrawn %s and now have %s", econ.format(r.amount), econ.format(r.balance))); String tag = getConfig().getString("tag"); if(tag == "") { tag = "[Advert]"; } if(getConfig().getBoolean("username") == true) { Bukkit.getServer().broadcastMessage(ChatColor.YELLOW + tag + ChatColor.WHITE + player.getName() + ": " + message); return true; } else { Bukkit.getServer().broadcastMessage(ChatColor.YELLOW + tag + " " + ChatColor.WHITE + message); return true; } } else { sender.sendMessage(String.format("[WesAdvert] " + ChatColor.RED + "%s", r.errorMessage)); } return true; } else { String tag = getConfig().getString("tag"); if(tag == "") { tag = "[Advert]"; } if(getConfig().getBoolean("username") == true) { Bukkit.getServer().broadcastMessage(ChatColor.YELLOW + tag + ChatColor.WHITE + player.getName() + ": " + message); return true; } else { Bukkit.getServer().broadcastMessage(ChatColor.YELLOW + tag + " " + ChatColor.WHITE + message); return true; } } } return false; } } }
Because Functions.java extends JavaPlugin, it can be counted as a different plugin. The plugin.yml in the jar file says what the main class is, and because Functions isn't being loaded, there isn't actually a config. The config needs to be provided by the actual plugin main class, ie WesAdvert.java.
So if i remove the extends JavaPlugin from Functions.java and and add Bukkit before functions that will need it then its fixed?
No. What I would do is pass a reference of the main plugin class to the Functions class, then call plugin.getConfig() instead of getConfig(). Do the same for all methods what will error after you remove the extends JavaPlugin.
Ok i did what you said, but i get i think the same error again? ERROR Code: 2012-05-11 23:22:49 [WARNING] Unexpected exception while parsing console command org.bukkit.command.CommandException: Unhandled exception executing command 'advert' in plugin WesAdvert v1.03 at org.bukkit.command.PluginCommand.execute(PluginCommand.java:42) at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:166) at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:473) at org.bukkit.craftbukkit.CraftServer.dispatchServerCommand(CraftServer.java:469) at net.minecraft.server.MinecraftServer.b(MinecraftServer.java:596) at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:565) at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:449) at net.minecraft.server.ThreadServerApplication.run(SourceFile:492) Caused by: java.lang.IllegalArgumentException: File cannot be null at org.bukkit.configuration.file.YamlConfiguration.loadConfiguration(YamlConfiguration.java:171) at org.bukkit.plugin.java.JavaPlugin.reloadConfig(JavaPlugin.java:117) at org.bukkit.plugin.java.JavaPlugin.getConfig(JavaPlugin.java:111) at me.wesleydeman.wesadvert.Functions.Advert(Functions.java:31) at me.wesleydeman.wesadvert.WesAdvert.onCommand(WesAdvert.java:64) at org.bukkit.command.PluginCommand.execute(PluginCommand.java:40) ... 7 more Here my 2 java files. WesAdvert.java Code: package me.wesleydeman.wesadvert; import java.util.logging.Logger; import net.milkbowl.vault.economy.Economy; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.java.JavaPlugin; public class WesAdvert extends JavaPlugin { private static final Logger log = Logger.getLogger("Minecraft"); public void loadConfiguration(){ String path = "cost.amount"; String path1 = "cost.enabled"; String path2 = "tag"; String path3 = "username"; getConfig().addDefault(path, 5.00); getConfig().addDefault(path1, true); getConfig().addDefault(path2, "[Advert]"); getConfig().addDefault(path3, true); getConfig().options().copyDefaults(true); saveConfig(); } public void onEnable(){ loadConfiguration(); if(getConfig().getBoolean("cost.enabled") == true) { if (!setupEconomy() ) { log.info(String.format("[%s] Disabled due to no Vault dependency found!", getDescription().getName())); getServer().getPluginManager().disablePlugin(this); return; } else { log.info(String.format("[%s] Enabled Version %s", getDescription().getName(), getDescription().getVersion())); } } else { loadConfiguration(); log.info(String.format("[%s] Enabled Version %s", getDescription().getName(), getDescription().getVersion())); } } public void onDisable(){ log.info(String.format("[%s] Disabled Version %s", getDescription().getName(), getDescription().getVersion())); } public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){ Functions function = new Functions(); Player player = null; if (sender instanceof Player) { player = (Player) sender; } if (cmd.getName().equalsIgnoreCase("advert")){ function.Advert(player, sender, cmd, commandLabel, args); } return false; } public static Economy econ = null; private boolean setupEconomy() { if (getServer().getPluginManager().getPlugin("Vault") == null) { return false; } RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class); if (rsp == null) { return false; } econ = rsp.getProvider(); return econ != null; } } Functions.java Code: package me.wesleydeman.wesadvert; import net.milkbowl.vault.economy.Economy; import net.milkbowl.vault.economy.EconomyResponse; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; public class Functions { public static Economy econ = null; public boolean Advert(Player player,CommandSender sender,Command cmd,String commandLabel,String[] args) { WesAdvert plugin = new WesAdvert(); if (player == null) { if (args.length < 1) { sender.sendMessage("[WesAdvert] " + ChatColor.RED + "No text specified!"); return false; } String message = ""; for (String part : args) { if (message != "") message += " "; message += part; } String tag = plugin.getConfig().getString("tag"); if(tag == "") { tag = "[Advert]"; } if(plugin.getConfig().getBoolean("username") == true) { Bukkit.getServer().broadcastMessage(ChatColor.YELLOW + tag + ChatColor.RED + "Console: " + ChatColor.WHITE + message); return true; } else { Bukkit.getServer().broadcastMessage(ChatColor.YELLOW + tag + " " + ChatColor.WHITE + message); return true; } } else { if (args.length < 1) { sender.sendMessage("[WesAdvert] " + ChatColor.RED + "No text specified!"); return false; } if(player.hasPermission("wesadvert.advert")) { String message = ""; for (String part : args) { if (message != "") message += " "; message += part; } if(plugin.getConfig().getBoolean("cost.enabled") == true) { EconomyResponse r = econ.withdrawPlayer(player.getName(), plugin.getConfig().getInt("cost.amount")); if(r.transactionSuccess()) { sender.sendMessage(String.format("[WesAdvert] You were withdrawn %s and now have %s", econ.format(r.amount), econ.format(r.balance))); String tag = plugin.getConfig().getString("tag"); if(tag == "") { tag = "[Advert]"; } if(plugin.getConfig().getBoolean("username") == true) { Bukkit.getServer().broadcastMessage(ChatColor.YELLOW + tag + ChatColor.WHITE + player.getName() + ": " + message); return true; } else { Bukkit.getServer().broadcastMessage(ChatColor.YELLOW + tag + " " + ChatColor.WHITE + message); return true; } } else { sender.sendMessage(String.format("[WesAdvert] " + ChatColor.RED + "%s", r.errorMessage)); } return true; } else { String tag = plugin.getConfig().getString("tag"); if(tag == "") { tag = "[Advert]"; } if(plugin.getConfig().getBoolean("username") == true) { Bukkit.getServer().broadcastMessage(ChatColor.YELLOW + tag + ChatColor.WHITE + player.getName() + ": " + message); return true; } else { Bukkit.getServer().broadcastMessage(ChatColor.YELLOW + tag + " " + ChatColor.WHITE + message); return true; } } } return false; } } }
The config for the plugin isn't set. I would use a custom method. Java provides a great one with their Properties class.
The config is set because when i transported the Advert function from Functions.java to WesAdvert.java it works.
So why does the function Advert from Functions.java work when i put the function Advert from Functions.java in WesAdvert.java the main class?
So why does the function Advert from Functions.java work when i put the function Advert from Functions.java in WesAdvert.java the main class? Then it reads the config.yml when the function is in Functions.java it does not
Have you got a file called config.yml? is it located in the same place as your src folder and your plugin.yml?
Thats not needed. Because on plugin enable it creates the config.yml in WesAdvert/config.yml With Code: public void loadConfiguration(){ String path = "cost.amount"; String path1 = "cost.enabled"; String path2 = "tag"; String path3 = "username"; getConfig().addDefault(path, 5.00); getConfig().addDefault(path1, true); getConfig().addDefault(path2, "[Advert]"); getConfig().addDefault(path3, true); getConfig().options().copyDefaults(true); saveConfig(); } And also when i didden't port the Advert function to functions.java it just worked fine. Since i ported it to Functions.java it does not.
I think saveConfig() saves the config.yml, and if you don't have one it'll throw exceptions (wild guess btw!)
It gives the error on line 31 from functions.java at me.wesleydeman.wesadvert.Functions.Advert(Functions.java:31) That is this line: Code: String tag = plugin.getConfig().getString("tag"); So on the first read from the config it generates that error.
Ok i got it fixed i was forgotten to make a constructor in the Functions.java where i put the a instance of WesAdvert in. Here are the 2 fixed java files. Notice that i do: In WesAdvert.java under onCommand Functions function = new Functions(this); And in Functions.java i do this right under the start of the class. WesAdvert plugin; public Functions(WesAdvert instance) { plugin = instance; } WesAdvert.java: Code: package me.wesleydeman.wesadvert; import java.util.logging.Logger; import net.milkbowl.vault.economy.Economy; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.java.JavaPlugin; public class WesAdvert extends JavaPlugin { private static final Logger log = Logger.getLogger("Minecraft"); public void loadConfiguration(){ String path = "cost.amount"; String path1 = "cost.enabled"; String path2 = "tag"; String path3 = "username"; getConfig().addDefault(path, 5.00); getConfig().addDefault(path1, true); getConfig().addDefault(path2, "[Advert]"); getConfig().addDefault(path3, true); getConfig().options().copyDefaults(true); saveConfig(); } @Override public void onEnable(){ loadConfiguration(); if(getConfig().getBoolean("cost.enabled") == true) { if (!setupEconomy() ) { log.info(String.format("[%s] Disabled due to no Vault dependency found!", getDescription().getName())); getServer().getPluginManager().disablePlugin(this); return; } else { log.info(String.format("[%s] Enabled Version %s", getDescription().getName(), getDescription().getVersion())); } } else { loadConfiguration(); log.info(String.format("[%s] Enabled Version %s", getDescription().getName(), getDescription().getVersion())); } } @Override public void onDisable(){ log.info(String.format("[%s] Disabled Version %s", getDescription().getName(), getDescription().getVersion())); } public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){ Functions function = new Functions(this); Player player = null; if (sender instanceof Player) { player = (Player) sender; } if (cmd.getName().equalsIgnoreCase("advert")){ return function.Advert(player, sender, cmd, commandLabel, args,econ); } return false; } public static Economy econ = null; private boolean setupEconomy() { if (getServer().getPluginManager().getPlugin("Vault") == null) { return false; } RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class); if (rsp == null) { return false; } econ = rsp.getProvider(); return econ != null; } } Functions.java: Code: package me.wesleydeman.wesadvert; import net.milkbowl.vault.economy.Economy; import net.milkbowl.vault.economy.EconomyResponse; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; public class Functions { WesAdvert plugin; public Functions(WesAdvert instance) { plugin = instance; } public static Economy econ = null; public boolean Advert(Player player,CommandSender sender,Command cmd,String commandLabel,String[] args,Economy econ) { if (player == null) { if (args.length < 1) { sender.sendMessage("[WesAdvert] " + ChatColor.RED + "No text specified!"); return false; } String message = ""; for (String part : args) { if (message != "") message += " "; message += part; } String tag = plugin.getConfig().getString("tag"); if(tag == "") { tag = "[Advert]"; } if(plugin.getConfig().getBoolean("username") == true) { Bukkit.getServer().broadcastMessage(ChatColor.YELLOW + tag + ChatColor.RED + "Console: " + ChatColor.WHITE + message); return true; } else { Bukkit.getServer().broadcastMessage(ChatColor.YELLOW + tag + " " + ChatColor.WHITE + message); return true; } } else { if (args.length < 1) { sender.sendMessage("[WesAdvert] " + ChatColor.RED + "No text specified!"); return false; } if(player.hasPermission("wesadvert.advert")) { String message = ""; for (String part : args) { if (message != "") message += " "; message += part; } if(plugin.getConfig().getBoolean("cost.enabled") == true) { EconomyResponse r = econ.withdrawPlayer(player.getName(), plugin.getConfig().getInt("cost.amount")); if(r.transactionSuccess()) { sender.sendMessage(String.format("[WesAdvert] You were withdrawn %s and now have %s", econ.format(r.amount), econ.format(r.balance))); String tag = plugin.getConfig().getString("tag"); if(tag == "") { tag = "[Advert]"; } if(plugin.getConfig().getBoolean("username") == true) { Bukkit.getServer().broadcastMessage(ChatColor.YELLOW + tag + ChatColor.WHITE + player.getName() + ": " + message); return true; } else { Bukkit.getServer().broadcastMessage(ChatColor.YELLOW + tag + " " + ChatColor.WHITE + message); return true; } } else { sender.sendMessage(String.format("[WesAdvert] " + ChatColor.RED + "%s", r.errorMessage)); } return true; } else { String tag = plugin.getConfig().getString("tag"); if(tag == "") { tag = "[Advert]"; } if(plugin.getConfig().getBoolean("username") == true) { Bukkit.getServer().broadcastMessage(ChatColor.YELLOW + tag + ChatColor.WHITE + player.getName() + ": " + message); return true; } else { Bukkit.getServer().broadcastMessage(ChatColor.YELLOW + tag + " " + ChatColor.WHITE + message); return true; } } } return false; } } }
If you're using class to hold methods and not data, that's bad scripting. Its better to put them in the main class from an OO perspective. Classes should have a CAN relationship. For example, class Cat CAN meow(). It makes less sense if you say class Function CAN advert(). It would make slightly more sense if you say class WesAdvert CAN advert(). If you really want to look at your scripting practices, Advert() should be advert(). It is convention in Java, and most other languages to have classes Capitalized (ThisIsAClass), methods/functions without the first letter lowercase (thisIsAMethod), and constants in uppercase (THIS_IS_A_CONSTANT). In Java, all keywords are lowercase.