Solved task onEnable

Discussion in 'Spigot Plugin Development' started by Marshmalah, Jan 8, 2020.

  1. Hello, the tasks does not run, and it impossible to launch them when the plugin is started? if yes would you have a solution please
    Code (Java):

        @Override
        public void onEnable()
        {
            saveDefaultConfig();
            int i = 1;
            while (i <= 50){
             if(getConfig().getString("Amethyst."+i) == null){
                int x = getConfig().getInt("Amethyst."+i+".block.x");
                int y = getConfig().getInt("Amethyst."+i+".block.y");
                int z = getConfig().getInt("Amethyst."+i+".block.z");
                String i2 = String.valueOf(i);
                 Bukkit.getScheduler().runTaskLater(this, () -> Bukkit.getServer().getWorld("world").getBlockAt(x, y, z).setType(Material.YELLOW_GLAZED_TERRACOTTA), 20 * 10);
                 Bukkit.getScheduler().runTaskLater(this, () -> getConfig().getConfigurationSection("Amethyst").set(i2, null), 20 * 10);
                 Bukkit.getScheduler().runTaskLater(this, () -> saveConfig(), 20 * 10);
                break;
             }
             i=i+1;
            }
     
    thanks for watching ;)
     
  2. Change this to != null
    Also why do you need the runnables?

    PS here are some suggestions on cleaner code:

    Code (Java):
      @Override
      public void onEnable() {

        saveDefaultConfig();
        FileConfiguration config = getConfig();

        for (int i = 0; i < 50; i++) {
          if (config.isSet("Amethyst." + i)) {
            int x = getConfig().getInt("Amethyst." + i + ".block.x");
            int y = getConfig().getInt("Amethyst." + i + ".block.y");
            int z = getConfig().getInt("Amethyst." + i + ".block.z");
            String i2 = String.valueOf(i);

            Bukkit.getScheduler().runTaskLater(this, () -> {
              Bukkit.getServer().getWorld("world").getBlockAt(x, y, z).setType(Material.YELLOW_GLAZED_TERRACOTTA);
              config.getConfigurationSection("Amethyst").set(i2, null);
              saveConfig();
            }, 20 * 10);

          }
        }
      }
     
    #2 7smile7, Jan 8, 2020
    Last edited: Jan 8, 2020
    • Like Like x 1
    • Useful Useful x 1
  3. This sould actually fix it... lul :ROFLMAO:

    @Marshmalah I would actually also reduce the 3 Runnables down to 1 like @7smile7 also suggested.
     
  4. Thank you, it works great, and thank you for correcting my code. I am starting and I explore java step by step in minecraft.