Solved How to? Time interval in config.yml file.

Discussion in 'Plugin Development' started by ToldiIII, Apr 14, 2017.

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

    ToldiIII

    My question is how you can do interval (code) config.yml the file while you continue to enter the specified command will list him how many másopdercig not use the command.
    Here's the code and config.yml file:
    Code:
    public class Main extends JavaPlugin {
      
        Logger log = Logger.getLogger("Minecraft");
        FileConfiguration config = getConfig();
        private HashSet<String> cooldowns = new HashSet<String>();
      
        @Override
        public void onEnable() {
            this.saveDefaultConfig();
            config.options().copyDefaults(true);
            loadConfiguration();
            config.addDefault("Plugin-enable_message", true);
            if (config.getBoolean("Plugin-enable_message")) {
                Bukkit.getConsoleSender().sendMessage(ChatColor.GOLD + "[" + ChatColor.DARK_GREEN + "Teleport" + ChatColor.YELLOW + "Signs" + ChatColor.GOLD + "] " + ChatColor.translateAlternateColorCodes('&', getConfig().getString("Plugin-enable")));
            }
            saveConfig();
            this.getConfig();
        }
      
        public void loadConfiguration() {
            saveConfig();
        }
      
        @Override
        public void onDisable() {
            config.addDefault("Plugin-disable_message", true);
            if (config.getBoolean("Plugin-disable_message")) {
            Bukkit.getConsoleSender().sendMessage(ChatColor.GOLD + "[" + ChatColor.DARK_GREEN + "Teleport" + ChatColor.YELLOW + "Signs" + ChatColor.GOLD + "] " + ChatColor.translateAlternateColorCodes('&', getConfig().getString("Plugin-disable")));
            saveDefaultConfig();
            }
        }
      
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
          if (cmd.getName().equalsIgnoreCase("tshelp")) {
                    if (!cooldowns.contains(sender.getName())) {
                      } else {
                        sender.sendMessage(ChatColor.GOLD + "[" + ChatColor.DARK_GREEN + "Teleport" + ChatColor.YELLOW + "Signs" + ChatColor.GOLD + "] " + ChatColor.translateAlternateColorCodes('&', getConfig().getString("Command-wait")).replaceAll("%seconds%", getConfig().getInt("Command-wait-time", 5) + ""));
                        return true;
                      }
                    final String name = sender.getName();
                    cooldowns.add(name);
                    new BukkitRunnable() {
                      public void run() {
                        cooldowns.remove(name);
                      }
                    }.runTaskLater(this, 100L);
                    if (!sender.hasPermission("teleportsigns.help")) {
                        if (args.length > 0) {
                            sender.sendMessage(
                                    ChatColor.GOLD + "[" + ChatColor.DARK_GREEN + "Teleport" + ChatColor.YELLOW + "Signs" + ChatColor.GOLD + "] " + ChatColor.translateAlternateColorCodes('&', getConfig().getString("Unknown-command")));
                            return true;
                        }
                        sender.sendMessage(ChatColor.GOLD + "[" + ChatColor.DARK_GREEN + "Teleport" + ChatColor.YELLOW + "Signs" + ChatColor.GOLD + "] " + ChatColor.translateAlternateColorCodes('&', getConfig().getString("No-permission")));
                        return true;
                    }
                    if (args.length > 0) {
                        sender.sendMessage(
                                ChatColor.GOLD + "[" + ChatColor.DARK_GREEN + "Teleport" + ChatColor.YELLOW + "Signs" + ChatColor.GOLD + "] " + ChatColor.translateAlternateColorCodes('&', getConfig().getString("Unknown-command")));
                        return true;
                    }
                    sender.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("Help-header")));
                    sender.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("Help-command")));
                    sender.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("Help-reloadcommand")));
                    sender.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("Help-footer")));
                    return true;
                }
            if (cmd.getName().equalsIgnoreCase("tsreload")) {
                if (!cooldowns.contains(sender.getName())) {
                  } else {
                    sender.sendMessage(ChatColor.GOLD + "[" + ChatColor.DARK_GREEN + "Teleport" + ChatColor.YELLOW + "Signs" + ChatColor.GOLD + "] " + ChatColor.translateAlternateColorCodes('&', getConfig().getString("Command-wait")).replaceAll("%seconds%", getConfig().getInt("Command-wait-time", 5) + ""));
                    return true;
                  }
                final String name = sender.getName();
                cooldowns.add(name);
                new BukkitRunnable() {
                  public void run() {
                    cooldowns.remove(name);
                  }
                }.runTaskLater(this, 100L);
                if (!sender.hasPermission("teleportsigns.reload")) {
                    if (args.length > 0) {
                        sender.sendMessage(
                                ChatColor.GOLD + "[" + ChatColor.DARK_GREEN + "Teleport" + ChatColor.YELLOW + "Signs" + ChatColor.GOLD + "] " + ChatColor.translateAlternateColorCodes('&', getConfig().getString("Unknown-command")));
                        return true;
                    }
                    sender.sendMessage(ChatColor.GOLD + "[" + ChatColor.DARK_GREEN + "Teleport" + ChatColor.YELLOW + "Signs" + ChatColor.GOLD + "] " + ChatColor.translateAlternateColorCodes('&', getConfig().getString("No-permission")));
                    return true;
                }
                if (args.length > 0) {
                    sender.sendMessage(
                            ChatColor.GOLD + "[" + ChatColor.DARK_GREEN + "Teleport" + ChatColor.YELLOW + "Signs" + ChatColor.GOLD + "] " + ChatColor.translateAlternateColorCodes('&', getConfig().getString("Unknown-command")));
                    return true;
                }
            }
            reloadConfig();
            saveConfig();
            sender.sendMessage(ChatColor.GOLD + "[" + ChatColor.DARK_GREEN + "Teleport" + ChatColor.YELLOW + "Signs" + ChatColor.GOLD + "] " + ChatColor.translateAlternateColorCodes('&', getConfig().getString("Plugin-reload")));
            return true;
        }
    }
    
    Config.yml:
    PHP:
    # Command waiting period. If you set it to 0 you will not have to wait.
    # Measured in seconds. 1 seconds - 20 ticks.
    # (This feature is known that there is no excessive lagg.)
    Command-wait-time<-- Here it must be something to do.
    Command-wait'&cPlease wait &7%seconds%&c seconds to use again!'
    HTML:
                    if (!cooldowns.contains(sender.getName())) {
                      } else {
                        sender.sendMessage(ChatColor.GOLD + "[" + ChatColor.DARK_GREEN + "Teleport" + ChatColor.YELLOW + "Signs" + ChatColor.GOLD + "] " + ChatColor.translateAlternateColorCodes('&', getConfig().getString("Command-wait")).replaceAll("%seconds%", getConfig().getInt("Command-wait-time", 5) + ""));
                        return true;
                      }
                    final String name = sender.getName();
                    cooldowns.add(name);
                    new BukkitRunnable() {
                      public void run() {
                        cooldowns.remove(name);
                      }
                    }.runTaskLater(this, 100L);
    
    I tried this method (it was good), just so I'd have to rewrite the config.yml if the file '5' 0 disables it (if 0), and if you enter the command twice if it is set to 5 off writes him a second time (when you type the command): "&cPlease wait &74&c seconds to use again!" ... and so on.
     
    Last edited: Apr 14, 2017
  2. Offline

    Side8StarLite

    @ToldiIII
    To set field values in config, follow this tutorial.
    But if you are setting the cooldown in the config, won't that apply to everyone? For example
    Person 1 enters command: You have to wait 5 seconds!
    4 seconds later...
    Person 2 enters command: You have to wait 5 seconds!
    1 second later
    Person 1 enters command: You have to wait 4 seconds! <-- Even though they already waited 5 seconds

    So maybe it might be better to store a hashmap with each person's uuid. For a tutorial on cooldowns, click here.
     
  3. Offline

    ToldiIII

    @Side8StarLite But I ought to, if you type the command again, you can print it, you can not use the command for how long. I looked at the two sides, but it did not work.
     
  4. Offline

    Side8StarLite

    @ToldiIII
    Ah sorry I misread your post :p
    Well like I posted before, this link is a pretty easy way to do cooldown. Basically, you have a hashmap with a uuid and int, and every second, a task subtracts 1 from 'int'. And anytime you want to check how much cooldown is left, just do Map#get(uuid). If it's less than 0, then it means their cooldown is over. Likewise, if you want to set the cooldown, just Map#set(uuid, int cooldown).
    Btw, I'm using uuid because player names can easily be changed using 'nick'. If you want to use console as well, you might have to find a workaround.
     
  5. Offline

    ToldiIII

    @Side8StarLite Yes. Now I'm thinking about looking how to command cooldown is done.

    I can not figure it out. Help!

    We continue to wait for the answers!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Apr 15, 2017
Thread Status:
Not open for further replies.

Share This Page