countdown if enabled in the config HELP!

Discussion in 'Spigot Plugin Development' started by RuloGamer, May 16, 2015.

  1. hello, I want to do:

    in config.yml I have something like this:

    Code (Text):
    settings:
      PrefixMessages: '[TPvP]' // Prefix messages
      AutoRun: 'true' // If true a command is executed every "time" seconds, if false the command is not executed
      time: '1000' //while if AutoRun is true the command runs each "time"
     
    OnEnable in Main Class:
    Code (Text):
        @SuppressWarnings("unchecked")
        @Override
        public void onEnable() {
         
            instance = this;
             File configFile = new File(getDataFolder(), "config.yml");
            if (!configFile.exists()) {
            saveDefaultConfig();
            }
            getServer().getPluginManager().registerEvents(new TListener(), this);
            getCommand("tournament").setExecutor(new TCommand());
            if (getConfig().get("save.mysaveditems") != null) {
                for(String key : getConfig().getConfigurationSection("save.mysaveditems.").getKeys(false)) {
                    ItemStack[] value = ((List<ItemStack>) getConfig().get("save.mysaveditems." + key)).toArray(new ItemStack[0]);
                    mySavedItems.put(key, value);
                }
            }
            if (getConfig().get("save.effect") != null) {
                for (String l : getConfig().getConfigurationSection("save.effect.").getKeys(false)) {
                    Collection<PotionEffect> pe = (Collection<PotionEffect>) getConfig().get("save.effect." + l);
                    effect.put(l, pe);
                }
            }
            if (getConfig().get("save.mysavedarmor") != null) {
                for(String key : getConfig().getConfigurationSection("save.mysavedarmor.").getKeys(false)) {
                    ItemStack[] value = ((List<ItemStack>) getConfig().get("save.mysavedarmor." + key)).toArray(new ItemStack[0]);
                    mySavedArmor.put(key, value);
                }
            }
            if (getConfig().get("save.food") != null) {
                for(String key : getConfig().getConfigurationSection("save.food.").getKeys(false)) {
                    int value = (int) getConfig().get("save.food." + key);
                    food.put(key, value);
                }
            }
            if (getConfig().get("save.location") != null) {
                for(String key : getConfig().getConfigurationSection("save.location.").getKeys(false)) {
                    Location value = (TLocation) getConfig().get("save.location." + key);
                    Location.put(key, value);
                }
            }
            if (getConfig().get("save.level") != null) {
                for(String key : getConfig().getConfigurationSection("save.level.").getKeys(false)) {
                    int value = (int) getConfig().get("save.level." + key);
                    level.put(key, value);
                }
            }
            if (getConfig().get("save.exp") != null) {
                for(String key : getConfig().getConfigurationSection("save.exp.").getKeys(false)) {
                    float value = (float) getConfig().getDouble("save.exp." + key);
                    exp.put(key, value);
                }
            }
            if (getConfig().get("save.health") != null) {
                for(String key : getConfig().getConfigurationSection("save.health.").getKeys(false)) {
                    double value = (double) getConfig().get("save.health." + key);
                    health.put(key, value);
                }
            }
            getConfig().set("save", null);
            saveConfig();
        }
    Countdown:
    Code (Text):
            {
                BukkitScheduler scheduler = Bukkit.getServer().getScheduler();
                scheduler.scheduleSyncRepeatingTask(this, new Runnable() {
                    @Override
                    public void run() {
                          //CODE
                    }
                }, 0L, 100L);
            }
    my question is where would have to put the "countdown" to execute the command "/ tpvp open 10 Tank" as I declare the variable "AutoRun" and "timer" and placed in the "countdown" without error, I I tried but I make mistake :(

    Thanks!
     
  2. hi, thanks for answering.

    I have achieved the following:

    Main Class:
    Code (Text):
    public class TMain extends JavaPlugin {
       
        String PrefixM = getConfig().getString("settings.PrefixMessages").replaceAll("&", "§");
        boolean AutoRun = getConfig().getString("settings.AutoRun") != null;
        String time = getConfig().getString("settings.time");

        @SuppressWarnings("unchecked")
        @Override
        public void onEnable() {
           
            instance = this;
             File configFile = new File(getDataFolder(), "config.yml");
            if (!configFile.exists()) {
            saveDefaultConfig();
            }
            getServer().getPluginManager().registerEvents(new TListener(), this);
            getCommand("tournament").setExecutor(new TCommand());
            if (getConfig().get("save.mysaveditems") != null) {
                for(String key : getConfig().getConfigurationSection("save.mysaveditems.").getKeys(false)) {
                    ItemStack[] value = ((List<ItemStack>) getConfig().get("save.mysaveditems." + key)).toArray(new ItemStack[0]);
                    mySavedItems.put(key, value);
                }
            }
            if (getConfig().get("save.effect") != null) {
                for (String l : getConfig().getConfigurationSection("save.effect.").getKeys(false)) {
                    Collection<PotionEffect> pe = (Collection<PotionEffect>) getConfig().get("save.effect." + l);
                    effect.put(l, pe);
                }
            }
            if (getConfig().get("save.mysavedarmor") != null) {
                for(String key : getConfig().getConfigurationSection("save.mysavedarmor.").getKeys(false)) {
                    ItemStack[] value = ((List<ItemStack>) getConfig().get("save.mysavedarmor." + key)).toArray(new ItemStack[0]);
                    mySavedArmor.put(key, value);
                }
            }
            if (getConfig().get("save.food") != null) {
                for(String key : getConfig().getConfigurationSection("save.food.").getKeys(false)) {
                    int value = (int) getConfig().get("save.food." + key);
                    food.put(key, value);
                }
            }
            if (getConfig().get("save.location") != null) {
                for(String key : getConfig().getConfigurationSection("save.location.").getKeys(false)) {
                    Location value = (TLocation) getConfig().get("save.location." + key);
                    Location.put(key, value);
                }
            }
            if (getConfig().get("save.level") != null) {
                for(String key : getConfig().getConfigurationSection("save.level.").getKeys(false)) {
                    int value = (int) getConfig().get("save.level." + key);
                    level.put(key, value);
                }
            }
            if (getConfig().get("save.exp") != null) {
                for(String key : getConfig().getConfigurationSection("save.exp.").getKeys(false)) {
                    float value = (float) getConfig().getDouble("save.exp." + key);
                    exp.put(key, value);
                }
            }
            if (getConfig().get("save.health") != null) {
                for(String key : getConfig().getConfigurationSection("save.health.").getKeys(false)) {
                    double value = (double) getConfig().get("save.health." + key);
                    health.put(key, value);
                }
            }
            getConfig().set("save", null);
            saveConfig();
           

            {
                if (AutoRun == true) {
                    BukkitScheduler scheduler = Bukkit.getServer().getScheduler();
                    scheduler.scheduleSyncRepeatingTask(this, new Runnable() {
                        @Override
                        public void run() {
                            Bukkit.getServer().broadcastMessage(PrefixM + "&a&lTest xD".replaceAll("&", "§"));
                        }
                    }, 0L, 60L);
                } else if (AutoRun == false) {
                    getLogger().info(ChatColor.GREEN + "AutoRun is Disabled");
                } else {
                    getLogger().info(ChatColor.DARK_RED + "TournamentPvP Error, AutoRun must be true or false");
                }
            }
        }
    with this the smooth countdown is on, but does not work the way I want, if AutoRun is true in the config.yml sends a broadcast message saying "<Prefix> Test xD" but if I put in is still running even false message.

    I know that I'm wrong?

    PS: sorry if I do not understand very well, use GoogleTranslate :C
     
  3. Remove the '' around true. Also, why would the string ever be null if you have it set to true or false? So, this is where your problem originates.

    Instead, when you use this:
    Use the #getBoolean instead of #get (and of course, remove != null). This way, this won't run when you false in set in the config.