1.8.8 BukkitRunnables

Discussion in 'Spigot Plugin Development' started by DarkKnights22, Jan 25, 2020.

  1. The code I have keeps breaking
    Code (Text):
    BukkitTask schedulerID = new BukkitRunnable() {
                    @Override
                    public void run() {
                        if (p.getWorld().equals(generatorLoc.getWorld())) {
                            if (p.getLocation().distance(generatorLoc) <= 32) {
                                //| Generate Blocks
                                for (int j = 0; j < blockAmount; j++) {
                                    Location above = generatorLoc.clone().add(0, j, 0);
                                    if (above.getBlock().getType() != Material.AIR) continue;
                                    String randomMaterial = PlatinumGenerator.getInstance().blockHandler.getRandomSpawnBlockMaterial(p, generator);
                                    for (String block : PlatinumGenerator.getInstance().customBlocksFile.getFile().getConfigurationSection("Custom-Blocks").getKeys(false)) {
                                        if (PlatinumGenerator.getInstance().customBlocksFile.getFile().getConfigurationSection("Custom-Blocks").contains(randomMaterial)) {
                                            if (!randomMaterial.equalsIgnoreCase(block)) continue;
                                            above.getBlock().setType(Material.valueOf(PlatinumGenerator.getInstance().customBlocksFile.getFile().getString("Custom-Blocks." + block + ".Block")));
                                            List<String> generatedCustom = PlatinumGenerator.getInstance().dataFile.getFile().getStringList(p.getUniqueId().toString() + ".generators." + generator + ".generated-custom-blocks") != null ? PlatinumGenerator.getInstance().dataFile.getFile().getStringList(p.getUniqueId().toString() + ".generators." + generator + ".generated-custom-blocks") : Lists.newArrayList();

                                            generatedCustom.add(above.getBlockX() + ":" + above.getBlockY() + ":" + above.getBlockZ() + ":" + above.getWorld().getName() + ":" + block);
                                            PlatinumGenerator.getInstance().dataFile.getFile().set(p.getUniqueId().toString() + ".generators." + generator + ".generated-custom-blocks", generatedCustom);
                                            PlatinumGenerator.getInstance().dataFile.saveFile();

                                        } else {
                                            above.getBlock().setType(Material.valueOf(randomMaterial));
                                        }
                                    }
                                    ArrayList<String> commands = PlatinumGenerator.getInstance().blockHandler.getCommands(p, generator).stream().map(s -> Chat.colorRaw(s).replaceAll("%player%", p.getName())).collect(Collectors.toCollection(ArrayList::new));
                                    commands.forEach(cmd -> PlatinumGenerator.getInstance().getServer().dispatchCommand(PlatinumGenerator.getInstance().getServer().getConsoleSender(), cmd));
                                    Location loc = new Location(generatorLoc.getWorld(), generatorLoc.getX(), generatorLoc.getY(), generatorLoc.getZ());
                                    generatorLoc.getWorld().playEffect(loc.add(0, 1, 0), Effect.valueOf(PlatinumGenerator.getInstance().dataFile.getFile().getString(p.getUniqueId().toString() + ".generators." + generator + ".effect")), Integer.MAX_VALUE);
                                    generatorLoc.getWorld().playEffect(loc.add(0, 1, 0), Effect.valueOf(PlatinumGenerator.getInstance().dataFile.getFile().getString(p.getUniqueId().toString() + ".generators." + generator + ".effect")), Integer.MAX_VALUE);
                                    break;
                                }
                            }
                        }
                    }
                }.runTaskTimer(PlatinumGenerator.getInstance(), 20, 20 * generateTime);
                if (PlatinumGenerator.getInstance().playerSchedulers.get(p.getUniqueId()) == null) {
                    ArrayList<Integer> schedulersList = new ArrayList<>();
                    schedulersList.add(schedulerID.getTaskId());
                    PlatinumGenerator.getInstance().playerSchedulers.put(p.getUniqueId(), schedulersList);
                } else {
                    if (!PlatinumGenerator.getInstance().playerSchedulers.get(p.getUniqueId()).contains(schedulerID.getTaskId())) {
                        PlatinumGenerator.getInstance().playerSchedulers.get(p.getUniqueId()).add(schedulerID.getTaskId());
                    }
                }
            }
    The runnables stop randomly and it breaks a lot of stuff and is very buggy. I use this method to reload the runnables:

    Code (Text):
    public void cancelSchedulers(Player p) {
            for (int i = 0; i < PlatinumGenerator.getInstance().playerSchedulers.get(p.getUniqueId()).size(); i++) {
                Bukkit.getServer().getScheduler().cancelTask(PlatinumGenerator.getInstance().playerSchedulers.get(p.getUniqueId()).get(i));
            }

            PlatinumGenerator.getInstance().playerSchedulers.get(p.getUniqueId()).clear();
        }

        public void reloadSchedulers(Player p) {
            cancelSchedulers(p);

            new BukkitRunnable() {
                @Override
                public void run() {
                    startBlockGenerateSchedulers(p);

                    new BukkitRunnable() {
                        @Override
                        public void run() {
                            startBlockMiningSchedulers(p);
                        }
                    }.runTaskLaterAsynchronously(PlatinumGenerator.getInstance(), 5);
                }
            }.runTaskLaterAsynchronously(PlatinumGenerator.getInstance(), 3);
        }
    The playerSchedulars map is:
    Code (Text):
    public HashMap<UUID, ArrayList<Integer>> playerSchedulers = Maps.newHashMap();
    Does anyone see why this is happening?
     
  2. You are doing a lot in your scheduler, maybe that‘s the issue?
    Also, that startBlockGenerate and Mining stuff sounds like you are using Bukkit-API in an asynchronous task, which is an absolute no-go.
     
  3. Should I do it non-asynchronous then? Like just runTaskLater?
     
  4. Yes. That one is synchronous
     
  5. Is FileConfiguration#save(File) async or sync?
     
  6. That one is synchronous (But if I‘m correct, that one doesn‘t even use any Bukkit scheduler so it just follows the Programm flow).
    This special method is an I/O task and can be done asynchronously. What cannot be done asynchronously are Bukkit-API Tasks (like setting a Block at a certain location) with some exceptions.