saving player names to config.yml

Discussion in 'Plugin Development' started by mrminer969, Jul 7, 2016.

Thread Status:
Not open for further replies.
  1. Offline

    mrminer969

    Im making a plugin where I want to save player's names so when they login, they don't need to type a command over and over. I've looked online, but no tutorial or anything tells me how to add to a config list, only to take from it.
     
  2. Offline

    mine-care

  3. Offline

    Forge_User_45064837 Retired Staff

    @mrminer969 To add on to @mine-care you should not save player names, you should save their UUID. Also, this should be in Plugin Development
     
    mine-care likes this.
  4. Offline

    mrminer969

    What line of code should I use. I am thinking using getConfig().set([path to key], player.GetUniqueID()

    Edit: This is my code
    package me.jakob.races;

    import java.util.logging.Logger;

    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.PlayerJoinEvent;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;

    public class Races_Main extends JavaPlugin implements Listener{

    public final Logger logger = Logger.getLogger("Minecraft");

    @Override
    public void onEnable(){
    PluginDescriptionFile pdfFile = this.getDescription();
    this.logger.info(pdfFile.getName() + " is enabled through the powers of Notch");
    registerConfig();
    }

    @Override
    public void onDisable(){
    PluginDescriptionFile pdfFile = this.getDescription();
    this.logger.info(pdfFile.getName() + " is disabled through the powers of Herobrine");
    }

    @EventHandler
    public void onPlayerJoinEvent(PlayerJoinEvent event, Player player) {

    event.setJoinMessage("Welcome to this new thing called a server");

    }

    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {

    if(label.equalsIgnoreCase("race")){
    if(args.length == 1){
    if(args[0].equalsIgnoreCase("human")){
    human(sender);
    }else if(args[0].equalsIgnoreCase("dwarf")){
    dwarf(sender);
    }else if(args[0].equalsIgnoreCase("murmaid")){
    murmaid(sender);
    }else if(args[0].equalsIgnoreCase("wendigo")){
    wendigo(sender);
    }else{
    Player player = (Player) sender;
    player.sendMessage(ChatColor.RED + "Usage: /race [race]");
    player.sendMessage(ChatColor.GREEN + "human, dwarf, murmaid, wendigo");
    }
    }else{
    Player player = (Player) sender;
    player.sendMessage(ChatColor.RED + "Usage: /race [race]");
    player.sendMessage(ChatColor.GREEN + "human, dwarf, murmaid, wendigo");
    }
    }

    return true;
    }

    private void wendigo(CommandSender sender) {
    Player player = (Player) sender;
    player.removePotionEffect(PotionEffectType.FAST_DIGGING);
    player.removePotionEffect(PotionEffectType.NIGHT_VISION);
    player.removePotionEffect(PotionEffectType.WATER_BREATHING);
    player.sendMessage(ChatColor.RED + "You are now a Wendigo");
    player.setPlayerListName(ChatColor.RED + "[Wendigo]" + ChatColor.WHITE + player.getName());
    player.setDisplayName(ChatColor.RED + "[Wendigo]" + ChatColor.WHITE + player.getName());
    player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 99999, 1));
    getConfig().set("races.wendigo", player.getUniqueId());
    }

    private void murmaid(CommandSender sender) {
    Player player = (Player) sender;
    player.removePotionEffect(PotionEffectType.FAST_DIGGING);
    player.removePotionEffect(PotionEffectType.NIGHT_VISION);
    player.removePotionEffect(PotionEffectType.SPEED);
    player.sendMessage(ChatColor.BLUE + "You are now a Murmaid");
    player.setPlayerListName(ChatColor.BLUE + "[Murmaid]" + ChatColor.WHITE + player.getName());
    player.setDisplayName(ChatColor.BLUE + "[Murmaid]" + ChatColor.WHITE + player.getName());
    player.addPotionEffect(new PotionEffect(PotionEffectType.WATER_BREATHING, 99999, 1));
    getConfig().set("races.murmaid", player.getUniqueId());
    }

    private void dwarf(CommandSender sender) {
    Player player = (Player) sender;
    player.removePotionEffect(PotionEffectType.WATER_BREATHING);
    player.removePotionEffect(PotionEffectType.SPEED);
    player.sendMessage(ChatColor.DARK_BLUE + "You are now a Dwarf");
    player.setPlayerListName(ChatColor.DARK_BLUE + "[Dwarf]" + ChatColor.WHITE + player.getName());
    player.setDisplayName(ChatColor.DARK_BLUE + "[Dwarf]" + ChatColor.WHITE + player.getName());
    player.addPotionEffect(new PotionEffect(PotionEffectType.FAST_DIGGING, 99999, 1));
    player.addPotionEffect(new PotionEffect(PotionEffectType.NIGHT_VISION, 99999, 1));
    getConfig().set("races.dwarf", player.getUniqueId());
    }

    private void human(CommandSender sender) {
    Player player = (Player) sender;
    player.removePotionEffect(PotionEffectType.FAST_DIGGING);
    player.removePotionEffect(PotionEffectType.NIGHT_VISION);
    player.removePotionEffect(PotionEffectType.WATER_BREATHING);
    player.removePotionEffect(PotionEffectType.SPEED);
    player.sendMessage(ChatColor.GREEN + "You are now Human");
    player.setPlayerListName(ChatColor.GREEN + "[Human]" + ChatColor.WHITE + player.getName());
    player.setDisplayName(ChatColor.GREEN + "[Human]" + ChatColor.WHITE + player.getName());
    getConfig().set("races.human", player.getUniqueId());
    }

    private void registerConfig() {
    getConfig().options().copyDefaults(true);
    saveConfig();

    }

    }

    Config.yml
    #Races Plugin by Mrminer969
    #Version 0.3.2
    races:
    human:
    dwarf:
    murmaid:
    wendigo:

    It is solved. I read up on the API Reference and I figured out I needed to save the config file.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jul 8, 2016
  5. Offline

    mine-care

    Dont watch TheBcBroz please!!! That is typical deprecated BcBroz code...

    Anyways if it is solved please mark the thread as 'Solved' (Edit Title>Solved)
     
    I Al Istannen likes this.
  6. Offline

    I Al Istannen

    @mrminer969
    And please use [code=java]<your code>[/code] tags the next time. Or use pastebin.
    The normal formatting ignores whitespaces at the beginning of a line and has no syntac highlighting, which makes the whole thing extremly hard to read.

    Consider this:
    private void human(CommandSender sender) {
    Player player = (Player) sender;
    player.removePotionEffect(PotionEffectType.FAST_DIGGING);
    player.removePotionEffect(PotionEffectType.NIGHT_VISION);
    player.removePotionEffect(PotionEffectType.WATER_BREATHING);
    player.removePotionEffect(PotionEffectType.SPEED);
    player.sendMessage(ChatColor.GREEN + "You are now Human");
    player.setPlayerListName(ChatColor.GREEN + "[Human]" + ChatColor.WHITE + player.getName());
    player.setDisplayName(ChatColor.GREEN + "[Human]" + ChatColor.WHITE + player.getName());
    getConfig().set("races.human", player.getUniqueId());
    }

    and this:
    Code:
    private void human(CommandSender sender) {
        Player player = (Player) sender;
        player.removePotionEffect(PotionEffectType.FAST_DIGGING);
        player.removePotionEffect(PotionEffectType.NIGHT_VISION);
        player.removePotionEffect(PotionEffectType.WATER_BREATHING);
        player.removePotionEffect(PotionEffectType.SPEED);
        player.sendMessage(ChatColor.GREEN + "You are now Human");
        player.setPlayerListName(ChatColor.GREEN + "[Human]" + ChatColor.WHITE + player.getName());
        player.setDisplayName(ChatColor.GREEN + "[Human]" + ChatColor.WHITE + player.getName());
        getConfig().set("races.human", player.getUniqueId());
    }
     
    mine-care likes this.
  7. Offline

    MCKrypto14

    oh ps this is just a tip, when you invoke the .toString() on a chatcolor, it returns § + the color code, which is the same as \u00a7 + the color code, so you can avoid concatenation on stuff like
    Code:
    ChatColor.GREEN + "[Human]" + ChatColor.WHITE
    and just use
    Code:
    "\u00a7a[Human]\u00a7f"
     
  8. Offline

    I Al Istannen

    @MCKrypto14
    No. Don't use unicode chars directly in your code. Escaped ones are somewhat better, but ugly.
    And, the ChatColor is there for a reason! In the future the char might change, and your plugin might be broken.

    If you define the prefix once, the time saved is not worth the drawbacks. Also not if you use it often. It is worth the replacement when profiling shows you that this is a bottleneck.

    But apart from that, yes, you are right:
    Code:
         4  invokespecial java.lang.StringBuilder() [13]
    // get the ChatColor.GREEN variable and put it on the stack
         7  getstatic org.bukkit.ChatColor.GREEN : org.bukkit.ChatColor [16]
    // invoke the append method. Now we have "§a"
        10  invokevirtual java.lang.StringBuilder.append(java.lang.Object) : java.lang.StringBuilder [22]
    // load the constant
        13  ldc <String "[Human]"> [26]
    // append it. Now we have "§a[Human]"
        15  invokevirtual java.lang.StringBuilder.append(java.lang.String) : java.lang.StringBuilder [28]
    // the same again
        18  getstatic org.bukkit.ChatColor.GREEN : org.bukkit.ChatColor [16]
    // append it: Now we have "§a[Human]§a"
        21  invokevirtual java.lang.StringBuilder.append(java.lang.Object) : java.lang.StringBuilder [22]
    // make it a String
        24  invokevirtual java.lang.StringBuilder.toString() : java.lang.String [31]
    // save it in the variable "lol"
        27  putstatic me.ialistannen.toy_around.TestToString.lol : java.lang.String [35]
    
    // WHAT MC CRYPTOP MENTIONED
    
    // load the constant
        30  ldc <String "§a[Human]§f"> [37]
    // save it
        32  putstatic me.ialistannen.toy_around.TestToString.lol2 : java.lang.String [39]
     
    Last edited: Jul 9, 2016
    mine-care and bwfcwalshy like this.
Thread Status:
Not open for further replies.

Share This Page