public static void startScheduler() { ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); Bukkit.getLogger().info("Started Event"); //TASK #1 (Start Event) DayOfWeek weekStart = DayOfWeek.MONDAY; LocalDate date = LocalDate.now().with(TemporalAdjusters.nextOrSame(weekStart)); Long midnight = LocalDateTime.now().until(date.atStartOfDay(), ChronoUnit.MINUTES); scheduler.scheduleAtFixedRate(TimeUtils::spawn, midnight, ChronoUnit.WEEKS.getDuration().toMinutes(), TimeUnit.MINUTES); //TASK #2 (Stop event) DayOfWeek eventEnd = DayOfWeek.TUESDAY; LocalDate dateEnd = LocalDate.now().with(TemporalAdjusters.nextOrSame(eventEnd)); Long midnightEnd = LocalDateTime.now().until(dateEnd.atStartOfDay(), ChronoUnit.MINUTES); scheduler.scheduleAtFixedRate(TimeUtils::despawn, midnightEnd, ChronoUnit.WEEKS.getDuration().toMinutes(), TimeUnit.MINUTES); } public static void despawn() { if(CitizensAPI.getNPCRegistry().getById(67).isSpawned()) { CitizensAPI.getNPCRegistry().getById(67).spawn(new Location(Bukkit.getWorld("world"), -125.700, 86, 23.527, -90, 3)); CitizensAPI.getNPCRegistry().getById(67).despawn(DespawnReason.PENDING_RESPAWN); Bukkit.broadcast(ChatColor.translateAlternateColorCodes('&', "&d&lTESTWEEK &7» &eGone."), "dailyshop.notify"); } } public static void spawn() { if(!CitizensAPI.getNPCRegistry().getById(67).isSpawned()) { Set itemsKeys = plugin.getConfig().getConfigurationSection("items").getKeys(false); List itemsList = itemsKeys .stream() .filter(item -> plugin.getConfig().getString("items." + item + ".rarity").equals("Common")) .mapToInt(Integer::parseInt) .boxed() .collect(Collectors.toList()); List legendary = itemsKeys .stream() .filter(item -> plugin.getConfig().getString("items." + item + ".rarity").equals("Legendary")) .mapToInt(item -> Integer.parseInt(item)) .boxed() .collect(Collectors.toList()); List itemsRandom = new ArrayList<>(); plugin.getConfig().set("Common", null); plugin.getConfig().set("Legendary", null); shuffle(itemsList); shuffle(legendary); for (int i = 0; i < 7; i++) { itemsRandom.add(itemsList.get(i)); } plugin.getConfig().set("Common", itemsRandom); plugin.getConfig().set("Legendary", legendary.get(0)); plugin.saveConfig(); CitizensAPI.getNPCRegistry().getById(67).spawn(new Location(Bukkit.getWorld("world"), -125.700, 86, 23.527, -90, 3)); Bukkit.broadcast(ChatColor.translateAlternateColorCodes('&', "&d&lTESTWEEK &7» &eBack!"), "dailyshop.notify"); CitizensAPI.getNPCRegistry().saveToStore(); } }