Player can do my command multiple times

Discussion in 'Spigot Plugin Development' started by Jaffaaaa, Aug 4, 2018.

Thread Status:
Not open for further replies.
  1. I have a command that a player should be only able to do once. To check if the player has done it my plugin writes to a file, however when I check the file after he does the command, it stays at "False"

    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 playerUUIDRefering = player.getUniqueId();
                UUID playerUUIDRefered = Bukkit.getOfflinePlayer(username).getUniqueId();
                File userdata = new File(Bukkit.getServer().getPluginManager().getPlugin("JReferrals").getDataFolder(), File.separator + "PlayerDatabase");
                File refering = new File(userdata, File.separator + playerUUIDRefering + ".yml");
                File refered = new File(userdata, File.separator + playerUUIDRefered + ".yml");
                FileConfiguration playerDataRefering = YamlConfiguration.loadConfiguration(refering);
                FileConfiguration playerDataRefered = YamlConfiguration.loadConfiguration(refered);
                   
                    //--------------------------------------------PlayerYAML On Open---------------------------------------------//  
               
                if (refering.exists()) {
                    if (playerDataRefering.getString("hasAlreadyRefered").equals("True")) {
                            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 (!refering.exists()) {
                    try {
                        playerDataRefering.set("hasAlreadyRefered", "True");
                        playerDataRefering.set("totalReferrals", 0);
                       
                        playerDataRefering.save(refering);
                    } catch (IOException exception) {
                        exception.printStackTrace();
                    }
                }
               
                if (!refered.exists()) {
                    try {
                        playerDataRefered.set("hasAlreadyRefered", "False");
                        playerDataRefered.set("totalReferrals", playerDataRefered.getInt("totalReferrals") + 0);
                       
                        playerDataRefered.save(refered);
                    } catch (IOException exception) {
                        exception.printStackTrace();
                    }
                }
               
                if (refered.exists()) {
                    try {
                        playerDataRefered.set("totalReferrals", playerDataRefered.getInt("totalReferrals") + 1);
                       
                        playerDataRefered.save(refered);
                    } catch (IOException exception) {
                        exception.printStackTrace();
                    }
                }
               
                if (playerDataRefering.getString("hasAlreadyRefered").equals("False")) {
                    playerDataRefering.set("hasAlreadyRefered", "True");
                    }
               
                //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."));
                    }
               
                if (Bukkit.getOfflinePlayer(username).isOnline()) {
                    Bukkit.getPlayer(username).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)) + ChatColor.BLUE + "because you refered " + ChatColor.AQUA + sender.getName()));
                }
                    return true;
                }
            return false;
        }
    }
     
  2. Does the folder "plugins/JReferrals/PlayerDatabase" exist?
    You should run
    Code (Text):
    userdata.mkdirs();
    before calling YamlConfiguration#save, to make sure the folder exists, or else the yaml files cannot be saved. Although in that case, there would be an IOException on your console... find out if that was the issue, then let's go from there. ;)
     
  3. It does create the folder and file when it does it :)
     
  4. Any console errors?
     
  5. Please don't bump a thread only after 25 minutes of no replys. By the way I think that you don't save the player's file after setting it to true.

     
    • Like Like x 1
  6. Perfect, ty :)
     
Thread Status:
Not open for further replies.