I have a plugin that has a list off banned words, it works for the most part. add the word manually into the config.yml and then reload the config in game But now i am trying to make it so you can add the word ingame and make it work Code: if (CommandLabel.equalsIgnoreCase("curseadd") && p.isOp()){ if(args.length == 1){ getConfig().getStringList("cursewords").add(args[0]); saveDefaultConfig(); p.sendMessage(ChatColor.RED + (args[0]) + ChatColor.DARK_AQUA + " added to banned words!"); } else p.sendMessage(ChatColor.RED + "Usage /curse bannedword"); return true; } I thought that this may work when i /curseadd test Could I get a little help with this please?
well, everything's great, but when you get the string list and add something to it, you have to add it to config then, I'll show you an example: Code:java if (CommandLabel.equalsIgnoreCase("curseadd") && p.isOp()){ if(args.length == 1){ getConfig().set("cursewords", getConfig().getStringList("cursewords").add(args[0])); // ^ I've changed this line ^ saveDefaultConfig(); p.sendMessage(ChatColor.RED + (args[0]) + ChatColor.DARK_AQUA + " added to banned words!"); } else p.sendMessage(ChatColor.RED + "Usage /curse bannedword"); return true; } So for next programming, use getConfig().set(path,data)
I changed the code too: Code: if (CommandLabel.equalsIgnoreCase("curseadd") && p.isOp()){ if(args.length == 1){ getConfig().set("cursewords", getConfig().getStringList("cursewords").add(args[0])); saveConfig(); p.sendMessage(ChatColor.RED + (args[0]) + ChatColor.DARK_AQUA + " added to banned words!"); } else p.sendMessage(ChatColor.RED + "Usage /curse bannedword"); return true; } } Now when i look in my config file the list of banned words is gone and it says: cursewords: true I am at a complete loss now And when i type '/curseword test' it isn't banning that word I have changed it back to saveDefaultConfig() temporarily so that i can at least modify the config then '/cursereload' to load the new list
Code: package me.rebz.anticurse; 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.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.AsyncPlayerChatEvent; import org.bukkit.plugin.java.JavaPlugin; public class Main extends JavaPlugin implements Listener{ public void onEnable() { saveDefaultConfig(); Bukkit.getServer().getPluginManager().registerEvents(this, this); getLogger().info("CC-AnitiCurse Enabled!"); } public void onDisable() { saveDefaultConfig(); getLogger().info("CC-AntiCurse Disabled!"); } @EventHandler public void onPlayerChat(AsyncPlayerChatEvent e){ Player p = e.getPlayer(); for (String text : e.getMessage().split(" ")){ if (getConfig().getStringList("cursewords").contains(text)){ e.setCancelled(true); Bukkit.broadcastMessage(ChatColor.DARK_AQUA + p.getName() + ChatColor.RED + " Please watch your language!"); } } } public boolean onCommand(CommandSender sender, Command cmd, String CommandLabel, String[] args){ Player p = (Player) sender;{ if (CommandLabel.equalsIgnoreCase("cursereload") && p.isOp()){ reloadConfig(); p.sendMessage(ChatColor.GREEN + "Configuration Reloaded!"); return true; } if (CommandLabel.equalsIgnoreCase("curseadd") && p.isOp()){ if(args.length == 1){ getConfig().set("cursewords", getConfig().getStringList("cursewords").add(args[0])); saveDefaultConfig(); p.sendMessage(ChatColor.RED + (args[0]) + ChatColor.DARK_AQUA + " added to banned words!"); } else p.sendMessage(ChatColor.RED + "Usage /curse bannedword"); return true; } } return false; } }
@RebzO1 Remove the saveDefaultConfig on disable and you don't need messages for enable and disable. Also use cmd.getName() not commandLabel and always check before casting (Look at @mine-care's signature if needed) Use saveConfig when saving in a command.
Code: package me.rebz.anticurse; 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.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.AsyncPlayerChatEvent; import org.bukkit.plugin.java.JavaPlugin; public class Main extends JavaPlugin implements Listener{ public void onEnable() { saveDefaultConfig(); Bukkit.getServer().getPluginManager().registerEvents(this, this); } public void onDisable() { } @EventHandler public void onPlayerChat(AsyncPlayerChatEvent e){ Player p = e.getPlayer(); for (String text : e.getMessage().split(" ")){ if (getConfig().getStringList("cursewords").contains(text)){ e.setCancelled(true); Bukkit.broadcastMessage(ChatColor.DARK_AQUA + p.getName() + ChatColor.RED + " Please watch your language!"); } } } public boolean onCommand(CommandSender sender, Command cmd, String CommandLabel, String[] args){ Player p = (Player) sender;{ if (cmd.getName().equalsIgnoreCase("cursereload") && p.isOp()){ reloadConfig(); p.sendMessage(ChatColor.GREEN + "Configuration Reloaded!"); return true; } if (cmd.getName().equalsIgnoreCase("curseadd") && p.isOp()){ if(args.length == 1){ getConfig().set("cursewords", getConfig().getStringList("cursewords").add(args[0])); saveConfig(); p.sendMessage(ChatColor.RED + (args[0]) + ChatColor.DARK_AQUA + " added to banned words!"); } else p.sendMessage(ChatColor.RED + "Usage /curse bannedword"); return true; } } return false; } } made the changes u wanted could you check through it and lemme know
Ran the changes on the test server and again it changed my config.yml when I type /curseadd test. The config.yml changed from the list of words to cursewords:true. I really appreciate the replies btw sorry to be a pain I just have no clue what is going on. My bad, i didn't even notice that Removed it. Issue is still the same as my post above
@RebzO1 Try doing List<String> words = getTheListOfStrings Add the new swear and then reset the list of strings that that list
Remove the onDisabled, since it does nothing. Your a broadcasting to everyone on the server by using getServer.broadcastMessage(), but from the text it seems you just want to send it to that player. The ArrayList#.add method returns a boolean, not the array instance. Create an instance of the array, add the word, then set it in the config.