Иконка ресурса

Руководство Лёгкие кулдауны - BUKKIT API

Поддерживаемые версии
  1. 1.8
  2. 1.9
  3. 1.10
  4. 1.11
  5. 1.12
  6. 1.13
  7. 1.14
  8. 1.15
  9. 1.16
  10. 1.17
  11. 1.18
  12. 1.19
Код:
Java:
public abstract class Cooldowns {

    private final HashMap<UUID, Long> cooldown = new HashMap<>();

    public void setCooldown(UUID uuid) {
        cooldown.put(uuid, System.currentTimeMillis());
    }

    private Long getCooldown(UUID uuid) {
        return (System.currentTimeMillis() - cooldown.get(uuid))/1000;
    }

    public String getCooldownValue(UUID uuid, Integer timecd) {
        if(!cooldown.containsKey(uuid)) {
            return "Нет информации";
        }
        long cd = timecd-getCooldown(uuid);
        String time = cd+"сек.";
        if(cd >= 60) {
            time = (cd/60)+"мин.";
        }
        if(cd >= 60*60) {
            time = ((cd/60)/60)+"ч.";
        }
        if(cd >= 60*60*24) {
            time = ((cd/60)/60)/24+"д.";
        }
        return time;
    }

    // time - в секундах
    public boolean isCooldown(UUID uuid, Integer time) {
        if(cooldown.containsKey(uuid)) {
            return getCooldown(uuid) <= time;
        }
        return false;
    }
}

Пример использования:
Java:
public class FlyCommand extends Cooldowns implements CommandExecutor {
    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if(!(sender instanceof Player)) {
            return true;
        }

        Player player = (Player) sender;

        if(isCooldown(player.getUniqueId(), 60)) { // проверка на то, есть ли у игрока кулдаун (60 сек).
            // если есть
            String time = getCooldownValue(player.getUniqueId(), 60);
            player.sendMessage("У Вас задержка на использование команды! Подождите: "+time);
            return true;
        }

        setCooldown(player.getUniqueId()); // установка кулдауна

        if(!player.getAllowFlight()) { // если false
            player.setAllowFlight(true);
            player.sendMessage("Флай включен.");
        } else {
            player.setAllowFlight(false);
            player.sendMessage("Флай выключен.");
        }

        return true;
    }
}
Автор
Destroy
Просмотры
1,180
Первый выпуск
Обновление
Оценка
0.00 звёзд 0 оценок

Другие ресурсы пользователя Destroy

Поделиться ресурсом

Назад
Сверху Снизу