Hey, I am pretty new to coding plugins in Minecraft. I recently created a plugin where you can have a custom join message in chat, but I want to create a option to enable / Dislable the plugin in the config. I am wondering what code I need to put in the code to make that work. Thanks for the help
I mean an option in the config where you can enable / disable the message in chat when someone joins. Like enable : true
multiple classes and then I link everything up in main class with new customjoinmessage (this); I can send you the source code later
Code (Java): File config = new File ("plugins/yourpluginname/config.yml"); FileConfiguration configConfig = new YamlConfiguration(); File folder = new File ("plugins/yourpluginname"); //this is the value that you want too set boolean isCustomJoinEnabled = false; if (!folder.exists()) { folder.mkdir(); } if (!config.exists()) { try { //create new file if not exist config.createNewFile(); configConfig.load(config); //set default value configConfig.set("customjoinmessage", true); configConfig.save(config); }catch(Exception ex) { ex.printStackTrace(); } } try { configConfig.load(config); //if some how the value doesn't exist it will just set the value always too true isCustomJoinEnabled = (configConfig.contains("customjoinmessage") ? configConfig.getBoolean("customjoinmessage") : true ); configConfig.save(config); }catch(Exception ex) { ex.printStackTrace(); }
If you want to learn how to use config files read this: Using the Configuration API It's pretty straight forward: Code (Java): // Use this to set a value to true (or use false instead if you want) <Your Class that extends JavaPlugin>.getConfig().set("use chat", true); // Check if the value is true or false from your config if (<Your Class that extends JavaPlugin>.getConfig().getBoolean("use chat")) { // run your chat code here }
The reason why you would do this is to enable online enabling/disabling of the plugin. Using Spigot‘s API, you can write this even simpler. First you need to get the config: Code (Java): final FileConfiguration config = getConfig(); getConfig() is defined in JavaPlugin. It cannot return null; when the file does not exist it will automatically be created new. Now you can get or set values; in your case the boolean value for „enable“ Code (Text): final boolean enabled = config.getBoolean(„enabled“); This can also not return null and will not fail if it doesn‘t exist; instead it will return false if that‘s the case. To give it a default value, you would use Code (Text): final boolean enabled = config.getBoolean(„enabled“, true); with true being the default value. You can now store the value for enabled/disabled in your Main-class and use it for further purposes. Once your plugin gets disabled, you want to store the value inside the config. You can do that using the various provided setters: Code (Java): config.set(„enabled“,this.isEnabled); I assumed that you have stored your state in the variable enabled. Note that the latter method should be called in your onDisable()-Method of your main class, while the other methods are being called in your onEnable()-Method
Don‘t do that. Spigot does all of that for you and you don‘t have to worry about file-names and where that file actually is e.t.c. You would put most of the configuration stuff inside your main-class and should then use auxiliary classes, when the overhead gets too high (in other words there are a lot of configuration options that have to be considered)
Use my code if you want to have a config that allows you to enable/disable the plugin where you can decide which parts of the plugin exactly to enable (you might for example want to allow some commands, even thou your plugin is disabled). Use permissions if that solves your problem more in a better way. Do nothing if you want to enable/disable your plugin globally and simply let spigot do that for you.
here is my source code! If someone can help me that would be epic package me.SPLATTER.essentials11.commands; import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerJoinEvent; import me.SPLATTER.essentials11.main; import me.SPLATTER.essentials11.utils.utils; public class customJoin implements Listener { private main plugin; public customJoin(main plugin) { this.plugin = plugin; Bukkit.getPluginManager().registerEvents(this, plugin) ; } @EventHandler public void onJoin(PlayerJoinEvent e) { Player p = e.getPlayer(); if(!p.hasPlayedBefore()) { Bukkit.broadcastMessage(utils.chat(plugin.getConfig().getString("firstJoin_message").replace("<player>", p.getName()))); } else { Bukkit.broadcastMessage(utils.chat(plugin.getConfig().getString("join_message").replace("<player>", p.getName()))); } } }
if you are using code in a message please put the code in a "code block" [ code=java ] CODE [ /code] and remove the space inside of the [ ] ex: Code (Java): public static void SomeWeirdCode(){ }
Code (Java): package me.SPLATTER.essentials11.commands; import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerJoinEvent; import me.SPLATTER.essentials11.main; import me.SPLATTER.essentials11.utils.utils; public class customJoin implements Listener { private main plugin; public customJoin(main plugin) { this.plugin = plugin; Bukkit.getPluginManager().registerEvents(this, plugin) ; } @EventHandler public void onJoin(PlayerJoinEvent e) { Player p = e.getPlayer(); if(!p.hasPlayedBefore()) { Bukkit.broadcastMessage(utils.chat(plugin.getConfig().getString("firstJoin_message").replace("<player>", p.getName()))); } else { Bukkit.broadcastMessage(utils.chat(plugin.getConfig().getString("join_message").replace("<player>", p.getName()))); } } } [ /code]
It always perplexes me when people choose to do things the hard way, when Spigot does the legwork for us automatically heh.