YAML Int

Discussion in 'Spigot Plugin Development' started by Jaffaaaa, Jul 31, 2018.

Thread Status:
Not open for further replies.
  1. In my plugin I have a command that when executed is mean to add (in a custom yml) +1 to the name: "totalRefers". But when I do the command it comes out like this:

    [​IMG]
    Code (Java):
    //Give Money To Referer
                EconomyResponse r2 = main.getPlugin(main.class).econ.depositPlayer(username, main.getPlugin(main.class).getConfig().getInt("RewardToReferer"));{
                        player.sendMessage((ChatColor.translateAlternateColorCodes('&', main.getPlugin(main.class).getConfig().getString("prefix")) + ChatColor.GREEN + " Referral successful."));
                //2ndDataBase
                        if (!f2.exists()) {
                            try {
                                playerData.set("totalReferrals", ("totalReferrals") + 1);
                                playerData.save(f2);
                            } catch (IOException exception) {
                                exception.printStackTrace();
                            }
                        }
                    }
                    return true;

    I understand that it's because I'm trying to add a String with an integer but I just don't know how to fix this.
     
  2. It's literally as you said: you're trying to add an integer to a string... try this:

    Code (Java):
    config.set("myIntHere", config.getInt("myIntHere") + 1);
     
  3. Just stays at 1 now each time I do the command.
     
  4. Entire class in-case you want more context, doubt you need it though:
    Code (Java):
    package com.jaffaaaa.jaffareferrals.complexCommands;

    import java.io.File;
    import java.io.IOException;

    import org.bukkit.Bukkit;
    import org.bukkit.OfflinePlayer;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;

    import com.jaffaaaa.jaffareferrals.main;

    import net.md_5.bungee.api.ChatColor;
    import net.milkbowl.vault.economy.EconomyResponse;

    public class jrefer implements CommandExecutor {

        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
           
            if (label.equalsIgnoreCase("jrefer")) {
                if (!(sender instanceof Player)) {
                    sender.sendMessage("You must be a player to use this command.");
                    return false;
                }
               
                Player player = (Player) sender;
               
                if (!(args.length > 0)) {
                    player.sendMessage(ChatColor.translateAlternateColorCodes('&', main.getPlugin(main.class).getConfig().getString("prefix")) + ChatColor.BLUE
                            + " Command format: " + ChatColor.DARK_AQUA + "/jrefer {USERNAME}");
                    return false;
                }
               
                String username = args[0];
               
                //--------------------------------------------PlayerYAML On Open---------------------------------------------//  
               
                String playerName = player.getName();
                File userdata = new File(Bukkit.getServer().getPluginManager().getPlugin("JReferrals").getDataFolder(), File.separator + "PlayerDatabase");
                File f = new File(userdata, File.separator + playerName + "-REF" + ".yml");
                File f2 = new File(userdata, File.separator + username + "-VAL" + ".yml");
                FileConfiguration playerData = YamlConfiguration.loadConfiguration(f);
                   
                    //--------------------------------------------PlayerYAML On Open---------------------------------------------//  
               
                if (f.exists()) {
                    sender.sendMessage((ChatColor.translateAlternateColorCodes('&', main.getPlugin(main.class).getConfig().getString("prefix"))
                            + ChatColor.DARK_RED + " You have already used a referral."));
                    return false;
                }
               
                if ((player.getName()).equals(username)) {
                    player.sendMessage(ChatColor.translateAlternateColorCodes('&', main.getPlugin(main.class).getConfig().getString("prefix")) + ChatColor.BLUE
                            + " You cannot refer yourself.");
                    return false;
                }
           
                if (!(sender.hasPermission("jreferrals.jrefer"))) {
                    sender.sendMessage(ChatColor.DARK_RED + "You do not have permission to do this command");
                    return false;
                }
           
                OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(username);
               
                if (!(offlinePlayer.hasPlayedBefore())) {
                    sender.sendMessage((ChatColor.translateAlternateColorCodes('&', main.getPlugin(main.class).getConfig().getString("prefix"))
                            + ChatColor.BLUE + " Player " + ChatColor.GREEN + username + ChatColor.BLUE + " has never joined this server before. Enter a valid player."));
                    return false;
                }
               
                //Give Money To Sender
                EconomyResponse r = main.getPlugin(main.class).econ.depositPlayer(sender.getName(), main.getPlugin(main.class).getConfig().getInt("RewardToReferee"));
                if(r.transactionSuccess()) {
                    player.sendMessage(String.format((ChatColor.translateAlternateColorCodes('&', main.getPlugin(main.class).getConfig().getString("prefix"))) + ChatColor.BLUE + " You were given " + ChatColor.AQUA + "%s.", main.getPlugin(main.class).econ.format(r.amount), main.getPlugin(main.class).econ.format(r.balance)));
                } else {
                    player.sendMessage(String.format(String.format((ChatColor.translateAlternateColorCodes('&', main.getPlugin(main.class).getConfig().getString("prefix"))) + " An error occured: %s", r.errorMessage)));
                }
               
                //--------------------------------------------PlayerYAML---------------------------------------------//          
               
                //1ndDataBase
                if (!f.exists()) {
                    try {
                        playerData.set("hasAlreadyRefered", "True");
                     
                        playerData.save(f);
                    } catch (IOException exception) {
                        exception.printStackTrace();
                    }
                }
               
                //Give Money To Referer
                EconomyResponse r2 = main.getPlugin(main.class).econ.depositPlayer(username, main.getPlugin(main.class).getConfig().getInt("RewardToReferer"));{
                        player.sendMessage((ChatColor.translateAlternateColorCodes('&', main.getPlugin(main.class).getConfig().getString("prefix")) + ChatColor.GREEN + " Referral successful."));
                //2ndDataBase
                        if (!f2.exists()) {
                            try {
                                playerData.set("totalReferrals", playerData.getInt("totalReferrals") + 1);
                                playerData.save(f2);
                            } catch (IOException exception) {
                                exception.printStackTrace();
                            }
                        }
                    }
                    return true;
                }
            return false;
        }
    }
     
  5. It stays at one because of the logic you have programmed. The only time the number will get incremented is if the file doesn't exist.

    FileConfiguration also supports boolean type and many other types. Rather than saving boolean values as strings, store them directly.
     
  6. Ehh how do I do this xD
     
  7. learn to code java.
     
  8. FrostedSnowman

    Resource Staff

    boi tf u mean? read your code. if you really can't fix this you need to go back to java basics. it's simple if-statement logic here
     
  9. Nobody here will help after your last comment. We don't spoonfeed, we like to help people learn - not help somebody help their mate by throwing together a few lines of code (our code) because he doesn't want to pay for it, use an existing one, or learn how properly.
     
  10. I'm not asking to be spoonfed. If you want you could just link me a doc or steer me in the right direction.
     
  11. Ok, you are only updating your config
    Code (Java):
    if(!f2.exists())
    what if it does exist? It won't be updated. Case solved.
     
  12. Changed my class to this:
    Code (Java):
    package com.jaffaaaa.jaffareferrals.complexCommands;

    import java.io.File;
    import java.io.IOException;

    import org.bukkit.Bukkit;
    import org.bukkit.OfflinePlayer;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;

    import com.jaffaaaa.jaffareferrals.main;

    import net.md_5.bungee.api.ChatColor;
    import net.milkbowl.vault.economy.EconomyResponse;

    public class jrefer implements CommandExecutor {

        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
           
            if (label.equalsIgnoreCase("jrefer")) {
                if (!(sender instanceof Player)) {
                    sender.sendMessage("You must be a player to use this command.");
                    return false;
                }
               
                Player player = (Player) sender;
               
                if (!(args.length > 0)) {
                    player.sendMessage(ChatColor.translateAlternateColorCodes('&', main.getPlugin(main.class).getConfig().getString("prefix")) + ChatColor.BLUE
                            + " Command format: " + ChatColor.DARK_AQUA + "/jrefer {USERNAME}");
                    return false;
                }
               
                String username = args[0];
               
                //--------------------------------------------PlayerYAML On Open---------------------------------------------//  
               
                String playerName = player.getName();
                File userdata = new File(Bukkit.getServer().getPluginManager().getPlugin("JReferrals").getDataFolder(), File.separator + "PlayerDatabase");
                File f = new File(userdata, File.separator + playerName + "-REF" + ".yml");
                File f2 = new File(userdata, File.separator + username + "-VAL" + ".yml");
                FileConfiguration playerData = YamlConfiguration.loadConfiguration(f);
                   
                    //--------------------------------------------PlayerYAML On Open---------------------------------------------//  
               
                if (f.exists()) {
                    sender.sendMessage((ChatColor.translateAlternateColorCodes('&', main.getPlugin(main.class).getConfig().getString("prefix"))
                            + ChatColor.DARK_RED + " You have already used a referral."));
                    return false;
                }
               
                if ((player.getName()).equals(username)) {
                    player.sendMessage(ChatColor.translateAlternateColorCodes('&', main.getPlugin(main.class).getConfig().getString("prefix")) + ChatColor.BLUE
                            + " You cannot refer yourself.");
                    return false;
                }
           
                if (!(sender.hasPermission("jreferrals.jrefer"))) {
                    sender.sendMessage(ChatColor.DARK_RED + "You do not have permission to do this command");
                    return false;
                }
           
                OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(username);
               
                if (!(offlinePlayer.hasPlayedBefore())) {
                    sender.sendMessage((ChatColor.translateAlternateColorCodes('&', main.getPlugin(main.class).getConfig().getString("prefix"))
                            + ChatColor.BLUE + " Player " + ChatColor.GREEN + username + ChatColor.BLUE + " has never joined this server before. Enter a valid player."));
                    return false;
                }
               
                //Give Money To Sender
                EconomyResponse r = main.getPlugin(main.class).econ.depositPlayer(sender.getName(), main.getPlugin(main.class).getConfig().getInt("RewardToReferee"));
                if(r.transactionSuccess()) {
                    player.sendMessage(String.format((ChatColor.translateAlternateColorCodes('&', main.getPlugin(main.class).getConfig().getString("prefix"))) + ChatColor.BLUE + " You were given " + ChatColor.AQUA + "%s.", main.getPlugin(main.class).econ.format(r.amount), main.getPlugin(main.class).econ.format(r.balance)));
                } else {
                    player.sendMessage(String.format(String.format((ChatColor.translateAlternateColorCodes('&', main.getPlugin(main.class).getConfig().getString("prefix"))) + " An error occured: %s", r.errorMessage)));
                }
               
                //--------------------------------------------PlayerYAML---------------------------------------------//          
               
                //1ndDataBase
                if (!f.exists()) {
                    try {
                        playerData.set("hasAlreadyRefered", "True");
                     
                        playerData.save(f);
                    } catch (IOException exception) {
                        exception.printStackTrace();
                    }
                }
               
                //Give Money To Referer
                EconomyResponse r2 = main.getPlugin(main.class).econ.depositPlayer(username, main.getPlugin(main.class).getConfig().getInt("RewardToReferer"));{
                        player.sendMessage((ChatColor.translateAlternateColorCodes('&', main.getPlugin(main.class).getConfig().getString("prefix")) + ChatColor.GREEN + " Referral successful."));
                //2ndDataBase
                        if (!f2.exists()) {
                            try {
                                playerData.set("totalReferrals", 0);
                                playerData.save(f2);
                            } catch (IOException exception) {
                                exception.printStackTrace();
                            }
                        }
                       
                        if (f2.exists()) {
                            try {
                                playerData.set("totalReferrals", playerData.getInt("totalReferrals") + 1);
                                playerData.save(f2);
                            } catch (IOException exception) {
                                exception.printStackTrace();
                            }
                        }
                    }
                    return true;
                }
            return false;
        }
    }
    Still sticks at 1
     
  13. Strahan

    Benefactor

    That's an all around weird way to approach the problem. Wasteful with file resources, there is no need to make a bunch of files like that. You should just log it in one and use UUID instead of player name. Also you should do the perm check first. No sense in checking the other stuff if the person isn't even allowed to use the command. Also you are checking if f2 doesn't exist then if not, creating it... then the next section checks if f2 exists. Since you just created it, it will so that value will never be zero as it creates at zero then sees the file and increments.
     
  14. Thanks for the tips :). Now fixed: Files merged, UUID instead of Name, Perm check now first, doesn't recheck anymore.

    Not sure what you meant about the last sentence. Could you explain? Sorry :(

    Code (Java):
    package com.jaffaaaa.jaffareferrals.complexCommands;

    import java.io.File;
    import java.io.IOException;
    import java.util.UUID;

    import org.bukkit.Bukkit;
    import org.bukkit.OfflinePlayer;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;

    import com.jaffaaaa.jaffareferrals.main;

    import net.md_5.bungee.api.ChatColor;
    import net.milkbowl.vault.economy.EconomyResponse;

    public class jrefer implements CommandExecutor {

        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
           
            if (label.equalsIgnoreCase("jrefer")) {
               
                if (!(sender.hasPermission("jreferrals.jrefer"))) {
                    sender.sendMessage(ChatColor.DARK_RED + "You do not have permission to do this command");
                    return false;
                }
               
                if (!(sender instanceof Player)) {
                    sender.sendMessage("You must be a player to use this command.");
                    return false;
                }
               
                Player player = (Player) sender;
               
                if (!(args.length > 0)) {
                    player.sendMessage(ChatColor.translateAlternateColorCodes('&', main.getPlugin(main.class).getConfig().getString("prefix")) + ChatColor.BLUE
                            + " Command format: " + ChatColor.DARK_AQUA + "/jrefer {USERNAME}");
                    return false;
                }
               
                String username = args[0];
               
                //--------------------------------------------PlayerYAML On Open---------------------------------------------//  
               
                UUID playerUUID = player.getUniqueId();
                File userdata = new File(Bukkit.getServer().getPluginManager().getPlugin("JReferrals").getDataFolder(), File.separator + "PlayerDatabase");
                File f = new File(userdata, File.separator + playerUUID + "-REF" + ".yml");
                FileConfiguration playerData = YamlConfiguration.loadConfiguration(f);
                   
                    //--------------------------------------------PlayerYAML On Open---------------------------------------------//  
               
                if (f.exists()) {
                    sender.sendMessage((ChatColor.translateAlternateColorCodes('&', main.getPlugin(main.class).getConfig().getString("prefix"))
                            + ChatColor.DARK_RED + " You have already used a referral."));
                    return false;
                }
               
                if ((player.getName()).equals(username)) {
                    player.sendMessage(ChatColor.translateAlternateColorCodes('&', main.getPlugin(main.class).getConfig().getString("prefix")) + ChatColor.BLUE
                            + " You cannot refer yourself.");
                    return false;
                }
           
                OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(username);
               
                if (!(offlinePlayer.hasPlayedBefore())) {
                    sender.sendMessage((ChatColor.translateAlternateColorCodes('&', main.getPlugin(main.class).getConfig().getString("prefix"))
                            + ChatColor.BLUE + " Player " + ChatColor.GREEN + username + ChatColor.BLUE + " has never joined this server before. Enter a valid player."));
                    return false;
                }
               
                //Give Money To Sender
                EconomyResponse r = main.getPlugin(main.class).econ.depositPlayer(sender.getName(), main.getPlugin(main.class).getConfig().getInt("RewardToReferee"));
                if(r.transactionSuccess()) {
                    player.sendMessage(String.format((ChatColor.translateAlternateColorCodes('&', main.getPlugin(main.class).getConfig().getString("prefix"))) + ChatColor.BLUE + " You were given " + ChatColor.AQUA + "%s.", main.getPlugin(main.class).econ.format(r.amount), main.getPlugin(main.class).econ.format(r.balance)));
                } else {
                    player.sendMessage(String.format(String.format((ChatColor.translateAlternateColorCodes('&', main.getPlugin(main.class).getConfig().getString("prefix"))) + " An error occured: %s", r.errorMessage)));
                }
               
                //--------------------------------------------PlayerYAML---------------------------------------------//          
               
                //1ndDataBase
                if (!f.exists()) {
                    try {
                        playerData.set("hasAlreadyRefered", "True");
                        playerData.set("totalReferrals", 0);
                       
                        playerData.save(f);
                    } catch (IOException exception) {
                        exception.printStackTrace();
                    }
                } else {
                    try {
                        playerData.set("totalReferrals", playerData.getInt("totalReferrals") + 1);
                        playerData.save(f);
                    } catch (IOException exception) {
                        exception.printStackTrace();
                    }
                }
               
                //Give Money To Referer
                EconomyResponse r2 = main.getPlugin(main.class).econ.depositPlayer(username, main.getPlugin(main.class).getConfig().getInt("RewardToReferer"));{
                        player.sendMessage((ChatColor.translateAlternateColorCodes('&', main.getPlugin(main.class).getConfig().getString("prefix")) + ChatColor.GREEN + " Referral successful."));
                    }
                    return true;
                }
            return false;
        }
    }
     
  15. its literally been less than a day just search up "config coding spigot tutorial" omfg
     
  16. I don't want a tutorial. I wanted someone to ellaborate on something. And to my knowledge custom yaml files and spigot config files aren't the same. Might be wrong tho
     
  17. lmao just use FIleConfiguration and they are the same
     
Thread Status:
Not open for further replies.