diff --git a/src/main/java/de/fanta/challenges/Challenges.java b/src/main/java/de/fanta/challenges/Challenges.java index d66d817..94980bf 100644 --- a/src/main/java/de/fanta/challenges/Challenges.java +++ b/src/main/java/de/fanta/challenges/Challenges.java @@ -176,7 +176,6 @@ reloadConfig(); saveConfig(); this.backpack.clearConfig(); - AllItemsChallenge.clearConfig(); this.rndDrops.shuffleItems(); this.rndDrops.saveItems(); this.rndDrops.loadItems(); @@ -185,7 +184,6 @@ this.rndDrops.loadItems(); this.backpack.loadInventoryFromConfig(); timer.setTime(getConfig().getInt("timertime")); - AllItemsChallenge.loadItems(); } if (plugin.getServerType() == ServerType.ADVENTURE) { @@ -222,9 +220,6 @@ getConfig().set("showtimer", true); } - if (getConfig().getBoolean("allitems")) { - AllItemsChallenge.start(); - } for (String string : getItemConfig().getStringList("items")) { try { Material material = Material.valueOf(string); @@ -241,6 +236,10 @@ } } + if (getConfig().getBoolean("allitems")) { + AllItemsChallenge.start(); + } + if (serverType != ServerType.ADVENTURE) { for (@NotNull Iterator it = Bukkit.advancementIterator(); it.hasNext(); ) { Advancement advancement = it.next(); @@ -282,7 +281,6 @@ getConfig().set("backpack_size", backpack.getSize() / 9); } else { this.backpack.clearConfig(); - AllItemsChallenge.clearConfig(); } try { this.RndDropsConfig.save(this.RndDropsConfigFile); @@ -348,11 +346,13 @@ private void createAllItemsConfig() { this.AllItemsConfigFile = new File(getDataFolder(), "allitems.yml"); - this.getAllItemsConfigFile().getParentFile().mkdirs(); - saveResource("allitems.yml", true); + if (!AllItemsConfigFile.exists()) { + this.getAllItemsConfigFile().getParentFile().mkdirs(); + saveResource("allitems.yml", true); + } this.AllItemsConfig = new YamlConfiguration(); try { - this.AllItemsConfig.load(this.getAllItemsConfigFile()); + this.AllItemsConfig.load(this.AllItemsConfigFile); } catch (IOException | org.bukkit.configuration.InvalidConfigurationException e) { e.printStackTrace(); } diff --git a/src/main/java/de/fanta/challenges/challenges/AllItemsChallenge.java b/src/main/java/de/fanta/challenges/challenges/AllItemsChallenge.java index 6a64b29..7533758 100644 --- a/src/main/java/de/fanta/challenges/challenges/AllItemsChallenge.java +++ b/src/main/java/de/fanta/challenges/challenges/AllItemsChallenge.java @@ -23,18 +23,27 @@ public static int ItemScheduler; public static BossBar bossBar; public static Material item; - public static List items = new ArrayList<>(); + public static List foundItems = new ArrayList<>(); + public static List itemsToSearch = new ArrayList<>(); private static final Challenges plugin = Challenges.getPlugin(); private static int itemcount = 0; - private static boolean itemcountboolean = false; public static void start() { - if (items.isEmpty()) { + if (foundItems.isEmpty()) { + loadItems(); + } + + if (itemsToSearch.isEmpty()) { for (Material mat : Material.values()) { - if (mat.isItem() && !mat.isAir() && !plugin.getNotAvailableMaterials().contains(mat)) - items.add(mat.toString()); + if (mat.isItem() && !mat.isAir() && !plugin.getNotAvailableMaterials().contains(mat)) { + itemcount++; + + if (!foundItems.contains(mat)) { + itemsToSearch.add(mat); + } + } } } @@ -45,10 +54,10 @@ if (item == null) { Random r = new Random(); - item = Material.valueOf(items.get(r.nextInt((items.size() - 1) + 1))); - + item = itemsToSearch.get(r.nextInt((itemsToSearch.size() - 1) + 1)); Config.setValue("allitemscurrentitem", new ItemStack(item)); } + bossBar = Bukkit.createBossBar("", BarColor.GREEN, BarStyle.SOLID); bossBar.setVisible(true); for (Player pl : Bukkit.getOnlinePlayers()) { @@ -60,7 +69,7 @@ public static void update() { ItemScheduler = Bukkit.getScheduler().scheduleSyncRepeatingTask(Challenges.getPlugin(), () -> { if (Config.getBoolean("allitems") && Challenges.getPlugin().getTimer().isRunning()) { - if (items.size() != 0) { + if (itemsToSearch.size() != 0) { for (Player pp : Bukkit.getOnlinePlayers()) { bossBar.addPlayer(pp); if (pp.getInventory().contains(item)) { @@ -68,16 +77,9 @@ } } - if (!itemcountboolean) { - for (Material mat : Material.values()) { - if (mat.isItem() && !mat.isAir() && !plugin.getNotAvailableMaterials().contains(mat)) - itemcount++; - } - itemcountboolean = true; - } - - - bossBar.setTitle(ChatUtil.GREEN + "Item » " + ChatUtil.BLUE + item.toString().replace("_", " ") + ChatUtil.BLUE + " (" + ChatUtil.GREEN + (itemcount - items.size()) + "/" + itemcount + ChatUtil.BLUE + ")"); + bossBar.setTitle(ChatUtil.GREEN + "Item » " + ChatUtil.BLUE + item.toString().replace("_", " ") + ChatUtil.BLUE + " (" + ChatUtil.GREEN + foundItems.size() + "/" + itemcount + ChatUtil.BLUE + ")"); + double progress = foundItems.size() * 100.0 / itemcount / 100.0; + bossBar.setProgress(progress); } else { bossBar.setTitle(ChatUtil.GREEN + "Alle Items gesammelt!"); ChatUtil.sendBrodcastMessage("Alle Items gesammelt!"); @@ -95,27 +97,30 @@ public static void next(Player p, Boolean skipped) { Material old = item; - items.remove(old.toString()); - if (items.size() != 0) { + itemsToSearch.remove(old); + foundItems.add(old); + saveItems(); + if (itemsToSearch.size() != 0) { Random r = new Random(); - item = Material.valueOf(items.get(r.nextInt((items.size() - 1) + 1))); + item = itemsToSearch.get(r.nextInt((itemsToSearch.size() - 1) + 1)); Config.setValue("allitemscurrentitem", new ItemStack(item)); if (skipped) { ChatUtil.sendBrodcastMessage("Item: " + ChatUtil.BLUE + old.toString().replace("_", " ") + ChatUtil.GREEN + " wurde von " + p.getName() + " übersprungen."); } else { ChatUtil.sendBrodcastMessage("Item: " + ChatUtil.BLUE + old.toString().replace("_", " ") + ChatUtil.GREEN + " wurde von " + p.getName() + " Registriert."); } - ChatUtil.sendBrodcastMessage("Neues Item: " + ChatUtil.BLUE + item.toString().replace("_", " ") + ChatUtil.GREEN + " (Es fehlen noch " + ChatUtil.BLUE + (items.size()) + ChatUtil.GREEN + " Items)"); + ChatUtil.sendBrodcastMessage("Neues Item: " + ChatUtil.BLUE + item.toString().replace("_", " ") + ChatUtil.GREEN + " (Es fehlen noch " + ChatUtil.BLUE + (foundItems.size()) + ChatUtil.GREEN + " Items)"); for (Player pp : Bukkit.getOnlinePlayers()) { pp.playSound(pp.getLocation(), Sound.BLOCK_NOTE_BLOCK_PLING, 1, 1); } } } - public static void saveItems() { - clearConfig(); + private static void saveItems() { FileConfiguration config = Challenges.getPlugin().getAllItemsConfig(); - Config.setValue("items", items); + ArrayList itemStings = new ArrayList<>(); + foundItems.forEach(material -> itemStings.add(material.name())); + config.set("items", itemStings); try { Challenges.getPlugin().AllItemsConfig.save(Challenges.getPlugin().getAllItemsConfigFile()); } catch (IOException e) { @@ -123,15 +128,10 @@ } } - public static void loadItems() { - items.clear(); + private static void loadItems() { FileConfiguration config = Challenges.getPlugin().getAllItemsConfig(); if (config.isList("items")) { - items.addAll(config.getStringList("items")); + config.getStringList("items").forEach(itemSting -> foundItems.add(Material.valueOf(itemSting))); } } - - public static void clearConfig() { - Challenges.getPlugin().getAllItemsConfigFile().delete(); - } } diff --git a/src/main/java/de/fanta/challenges/utils/SaveWorldUtils.java b/src/main/java/de/fanta/challenges/utils/SaveWorldUtils.java index 8ce315a..0f1a243 100644 --- a/src/main/java/de/fanta/challenges/utils/SaveWorldUtils.java +++ b/src/main/java/de/fanta/challenges/utils/SaveWorldUtils.java @@ -36,7 +36,6 @@ Challenges.getPlugin().getConfig().set("backpack_size", Challenges.getPlugin().getBackpack().getSize() / 9); Challenges.getPlugin().saveConfig(); Challenges.getPlugin().getBackpack().saveInventoryToConfig(); - AllItemsChallenge.saveItems(); World world_world = Bukkit.getWorlds().get(0); World world_nether = Bukkit.getWorlds().get(1); World world_end = Bukkit.getWorlds().get(2);