So I'm trying to get a message sent from chat into a YML. It umm sorta works but config looks all wacky. Code (Text): package me.TheBlackFish.Suggestions; import java.util.HashMap; import org.bukkit.Bukkit; 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.event.player.PlayerJoinEvent; import org.bukkit.plugin.java.JavaPlugin; import net.md_5.bungee.api.ChatColor; public class Main extends JavaPlugin implements Listener { HashMap<Player, Player> talk = new HashMap<Player,Player>(); public void onEnable(){ Bukkit.getServer().getPluginManager().registerEvents(this, this); getConfig().options().copyDefaults(true); saveDefaultConfig(); } public void onDisable(){ getConfig(); saveConfig(); } public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) { Player p = (Player) sender; if(cmd.getName().equalsIgnoreCase("suggest")){ p.sendMessage(ChatColor.GREEN + "What would you like to suggest? Type your suggestion in the chat box then hit " + ChatColor.RED + "ENTER"); talk.put(p, p); } return false; } @EventHandler public void noChat(AsyncPlayerChatEvent e){ Player p = e.getPlayer(); if(talk.containsKey(p)){ getConfig().set(e.getMessage().toString(), e.getPlayer().getUniqueId()); e.setCancelled(true); p.sendMessage(ChatColor.GREEN + "Suggested! " + e.getMessage()); talk.remove(p); }else return; } @EventHandler public void onJoin(PlayerJoinEvent e){ Player p = e.getPlayer(); if(!(getConfig().contains(p.getUniqueId().toString() + ".suggestions"))){ getConfig().addDefault(p.getUniqueId().toString() + ".suggestions", p); saveConfig(); }else return; } }
I'm on school wifi rn, not gonna run a local host server but all of the sudden my config is blank (because I deleted it) and forgot to run it before I got to school :/
I would recommend storing the Player's UUIDs instead of the whole Player object. You're not cleaning up the HashMap when they leave so your Player object will cause memory leaks.
Make sure you save it after changing anything. Otherwise, if I remember correctly next time you get the config it'll not have the last change if it wasn't saved. And as said above don't store the player object, use the UUID.
I would recommend storing the Player's UUIDs instead of the whole Player object. You're not cleaning up the HashMap when they leave so your Player object will cause memory leaks. And as for your error, you're setting the key to be the message and the value to be the UUID. Instead, set the key to the UUID and the suggestion as the value.
It should fix your problem. Your config will look like this now: Code (Text): UUID1: 'you should add this' UUID2: 'yeah pls add'
Also saving the config every time a player joins will probably cause lag. I've seen some very bad timings on one of my plugins and after removing all of the saveConfig(), the plugin didn't even show up on the timings result. Can you post your updated code.
Code (Text): package me.TheBlackFish.Suggestions; import java.util.HashMap; import java.util.UUID; import org.bukkit.Bukkit; import org.bukkit.Sound; 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.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.plugin.java.JavaPlugin; import net.md_5.bungee.api.ChatColor; public class Main extends JavaPlugin implements Listener { HashMap<UUID, String> talk = new HashMap<UUID, String>(); public void onEnable(){ Bukkit.getServer().getPluginManager().registerEvents(this, this); getConfig().options().copyDefaults(true); saveDefaultConfig(); } public void onDisable(){ getConfig(); saveConfig(); } public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) { Player p = (Player) sender; if(cmd.getName().equalsIgnoreCase("suggest")){ p.sendMessage(ChatColor.GREEN + "What would you like to suggest? Type your suggestion in the chat box then hit " + ChatColor.RED + "ENTER"); talk.put(p.getUniqueId(), p.getUniqueId().toString()); } return false; } @EventHandler public void noChat(AsyncPlayerChatEvent e){ Player p = e.getPlayer(); if(talk.containsKey(p)){ getConfig().set(p.getUniqueId().toString(), e.getMessage().toString()); e.setCancelled(true); p.playSound(p.getLocation(), Sound.LEVEL_UP, 3, 2); talk.remove(p); }else return; } @EventHandler public void onLeave(PlayerQuitEvent e){ talk.remove(e.getPlayer()); } @EventHandler public void onJoin(PlayerJoinEvent e){ Player p = e.getPlayer(); if(!(getConfig().contains(p.getUniqueId().toString() + ".suggestions"))){ getConfig().addDefault(p.getUniqueId().toString() + ".suggestions", p); saveConfig(); }else return; } }
Code (Text): package me.TheBlackFish.Suggestions; import java.util.HashMap; import java.util.UUID; import org.bukkit.Bukkit; import org.bukkit.Sound; 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.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.plugin.java.JavaPlugin; import net.md_5.bungee.api.ChatColor; public class Main extends JavaPlugin implements Listener { //Hashmap to put them in for when they suggest it allows for chat without showing everyone else HashMap<UUID, String> talk = new HashMap<UUID, String>(); public void onEnable(){ Bukkit.getServer().getPluginManager().registerEvents(this, this); getConfig().options().copyDefaults(true); saveDefaultConfig(); } public void onDisable(){ getConfig(); saveConfig(); } //Obvious command lines below public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) { Player p = (Player) sender; if(cmd.getName().equalsIgnoreCase("suggest")){ //Lines 41-43 sets them up for the suggestment p.sendMessage(ChatColor.GREEN + "What would you like to suggest? Type your suggestion in the chat box then hit " + ChatColor.RED + "ENTER"); talk.put(p.getUniqueId(), p.getUniqueId().toString()); } return false; } @EventHandler public void noChat(AsyncPlayerChatEvent e){ Player p = e.getPlayer(); //check if they're in suggest mode if(talk.containsKey(p)){ getConfig().set(p.getUniqueId().toString(), e.getMessage().toString()); e.setCancelled(true); p.playSound(p.getLocation(), Sound.LEVEL_UP, 3, 2); talk.remove(p); //if not don't do anything }else return; } @EventHandler public void onLeave(PlayerQuitEvent e){ //Remove from hashmap talk.remove(e.getPlayer()); } @EventHandler public void onJoin(PlayerJoinEvent e){ Player p = e.getPlayer(); //Simple config check for new players if(!(getConfig().contains(p.getUniqueId().toString() + ".suggestions"))){ getConfig().addDefault(p.getUniqueId().toString() + ".suggestions", p.getUniqueId()); //<- added p.getUniqueId() instead of just p saveConfig(); }else return; } }