For Loop sending too many messages / running to much commands

Discussion in 'Plugin Development' started by Payless, Feb 11, 2019.

  1. Offline


    I'm trying to run a message and command once player hits the needed time but it sends the same number of messages to the amount of values in the configuration keys. How do i make it send it once, I tried using break; doesnt work.

    package org.autorank.rupticmc.general;
    import me.lucko.luckperms.LuckPerms;
    import org.autorank.rupticmc.AutoRank;
    import org.autorank.rupticmc.database.PlayerData;
    import org.autorank.rupticmc.database.threads.PlayerUpdateThread;
    import org.autorank.rupticmc.database.threads.StartTimeThread;
    import org.autorank.rupticmc.utils.StringUtils;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.ConsoleCommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.scheduler.BukkitRunnable;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Set;
    public class ThreadUpdater extends BukkitRunnable {
       public void run() {
           for(Player player : Bukkit.getServer().getOnlinePlayers()) {
               PlayerData data = AutoRankManager.i().getStorage().getPlayerData(player.getUniqueId());
                   Bukkit.getScheduler().runTaskAsynchronously(AutoRank.getInstance(), new StartTimeThread(player.getUniqueId()));
                   String rank = null;
                   String command = null;
                   for (String keys : RankFlatFile.getCFG().getConfigurationSection("Ranks").getKeys(false)) {
                       if (data.getRank().toUpperCase().equalsIgnoreCase(keys.toUpperCase())) {
                           if (data.getRank() != RankFlatFile.getCFG().getString("Ranks." + keys + ".nextRank")) {
                               if (data.getTime() == RankFlatFile.getCFG().getInt("Ranks." + keys + ".timeNeeded")) {
                                   rank = RankFlatFile.getCFG().getString("Ranks." + keys + ".nextRank").toUpperCase();
                                   player.sendMessage(StringUtils.getPrefix() + ChatColor.RED + "Rank Updated to " + rank);
                                   command = "luckperms user " + player.getName() + " parent set " + rank;
                                   Bukkit.dispatchCommand(Bukkit.getConsoleSender(), command);
  2. Offline


    Add a check to know when this has not been applied, if it has been applied then use continue; it will skip the following code and continues to iterate.
  3. Offline


  4. Offline


    @Payless Put the 'player.sendMessage()' and 'Bukkit.dispatchCommand()' outside of the loop.

Share This Page