diff --git a/src/main/java/org/maxgamer/quickshop/QuickShop.java b/src/main/java/org/maxgamer/quickshop/QuickShop.java index df628ae08..11bd7b72b 100644 --- a/src/main/java/org/maxgamer/quickshop/QuickShop.java +++ b/src/main/java/org/maxgamer/quickshop/QuickShop.java @@ -24,6 +24,7 @@ import me.minebuilders.clearlag.Clearlag; import me.minebuilders.clearlag.listeners.ItemMergeListener; import org.bukkit.Bukkit; import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Player; import org.bukkit.event.HandlerList; import org.bukkit.event.entity.ItemSpawnEvent; @@ -60,11 +61,13 @@ import org.maxgamer.quickshop.Watcher.*; import java.io.File; import java.io.IOException; +import java.io.InputStreamReader; import java.lang.reflect.Field; import java.nio.file.Files; import java.sql.SQLException; import java.util.*; import java.util.Map.Entry; +import java.util.stream.Collectors; @Getter public class QuickShop extends JavaPlugin { @@ -775,50 +778,49 @@ public class QuickShop extends JavaPlugin { return true; } -// public void configVaildate() { -// YamlConfiguration attached = YamlConfiguration.loadConfiguration(new InputStreamReader(Objects.requireNonNull(getResource("config.yml")))); -// Set keysA = new HashSet<>(attached.getKeys(true)); -// Set keysB = new HashSet<>(getConfig().getKeys(true)); -// Set ignoreCheckKeys = new HashSet<>(); -// ignoreCheckKeys.add("server-uuid"); -// final String msgForConfiguration = "Missing options in config.yml, the key [%key%] not exist in config.yml, it may cause plugin errors, please fix it! There is guide for you to fix: \n" + -// ".....\n" + -// "%data%\n" + -// ".....\n" + -// "Tips: Add \"auto-fix-configuration: true\" in config.yml to allow QuickShop automatic fix your configuration!"; -// keysA.stream().filter((key) -> !keysB.contains(key)).filter((key) -> !ignoreCheckKeys.contains(key)).collect(Collectors.toList()).forEach((miss) -> { -// if(!getConfig().getBoolean("auto-fix-configuration",false)){ -// String theMsg = msgForConfiguration; -// theMsg = theMsg.replace("%key%", miss); -// List tiers = new ArrayList<>(Arrays.asList(miss.split("\\."))); -// StringBuilder miss2Yaml = new StringBuilder(); -// int spaces = 2; -// Iterator iterator = tiers.iterator(); -// while (true) { -// String tier = iterator.next(); -// miss2Yaml.append(tier); -// if (iterator.hasNext()) { -// miss2Yaml.append(": "); -// miss2Yaml.append("\n"); -// for (int i = 0; i < spaces; i++) { -// miss2Yaml.append(" "); -// } -// spaces += 2; -// } else { -// miss2Yaml.append(": "); -// miss2Yaml.append(attached.get(miss)); -// break; -// } -// } -// theMsg = theMsg.replace("%data%", miss2Yaml.toString()); -// getLogger().warning(theMsg); -// }else{ -// getConfig().set(miss,attached.get(miss)); -// } -// -// -// }); -// } + public void configVaildate() { + YamlConfiguration attached = YamlConfiguration.loadConfiguration(new InputStreamReader(Objects.requireNonNull(getResource("config.yml")))); + Set keysA = new HashSet<>(attached.getKeys(true)); + Set keysB = new HashSet<>(getConfig().getKeys(true)); + Set ignoreCheckKeys = new HashSet<>(); + ignoreCheckKeys.add("server-uuid"); + final String msgForConfiguration = "Missing options in config.yml, the key [%key%] not exist in config.yml, it may cause plugin errors, please fix it! There is guide for you to fix: \n" + + ".....\n" + + "%data%\n" + + ".....\n" + + "Tips: Add \"auto-fix-configuration: true\" in config.yml to allow QuickShop automatic fix your configuration!"; + keysA.stream().filter((key) -> !keysB.contains(key)).filter((key) -> !ignoreCheckKeys.contains(key)).collect(Collectors.toList()).forEach((miss) -> { + if(!getConfig().getBoolean("auto-fix-configuration",false)){ + String theMsg = msgForConfiguration; + theMsg = theMsg.replace("%key%", miss); + List tiers = new ArrayList<>(Arrays.asList(miss.split("\\."))); + StringBuilder miss2Yaml = new StringBuilder(); + int spaces = 2; + Iterator iterator = tiers.iterator(); + while (true) { + String tier = iterator.next(); + miss2Yaml.append(tier); + if (iterator.hasNext()) { + miss2Yaml.append(": "); + miss2Yaml.append("\n"); + for (int i = 0; i < spaces; i++) { + miss2Yaml.append(" "); + } + spaces += 2; + } else { + miss2Yaml.append(": "); + miss2Yaml.append(attached.get(miss)); break; + } + } + theMsg = theMsg.replace("%data%", miss2Yaml.toString()); + getLogger().warning(theMsg); + }else{ + getConfig().set(miss,attached.get(miss)); + } + + + }); + } private void submitMeritcs() { if (!getConfig().getBoolean("disabled-metrics")) { @@ -1377,7 +1379,7 @@ public class QuickShop extends JavaPlugin { } catch (IOException ioe) { getLogger().warning("Error on spawning the example config file: " + ioe.getMessage()); } - //configVaildate(); + configVaildate(); } /** diff --git a/src/main/java/org/maxgamer/quickshop/Shop/ArmorStandDisplayItem.java b/src/main/java/org/maxgamer/quickshop/Shop/ArmorStandDisplayItem.java index 9b5f976b3..3495d85ea 100644 --- a/src/main/java/org/maxgamer/quickshop/Shop/ArmorStandDisplayItem.java +++ b/src/main/java/org/maxgamer/quickshop/Shop/ArmorStandDisplayItem.java @@ -304,4 +304,16 @@ public class ArmorStandDisplayItem implements DisplayItem { } return this.armorStand.isValid(); } + + boolean pendingRemoval; + + @Override + public boolean pendingRemoval() { + return pendingRemoval = true; + } + + @Override + public boolean isPendingRemoval() { + return pendingRemoval; + } } diff --git a/src/main/java/org/maxgamer/quickshop/Shop/DisplayItem.java b/src/main/java/org/maxgamer/quickshop/Shop/DisplayItem.java index 22dff7e50..be13d2b94 100644 --- a/src/main/java/org/maxgamer/quickshop/Shop/DisplayItem.java +++ b/src/main/java/org/maxgamer/quickshop/Shop/DisplayItem.java @@ -277,4 +277,7 @@ public interface DisplayItem { */ boolean isSpawned(); + boolean pendingRemoval(); + + boolean isPendingRemoval(); } diff --git a/src/main/java/org/maxgamer/quickshop/Shop/RealDisplayItem.java b/src/main/java/org/maxgamer/quickshop/Shop/RealDisplayItem.java index d92e92d85..27b0b620e 100644 --- a/src/main/java/org/maxgamer/quickshop/Shop/RealDisplayItem.java +++ b/src/main/java/org/maxgamer/quickshop/Shop/RealDisplayItem.java @@ -242,4 +242,16 @@ public class RealDisplayItem implements DisplayItem { } return this.item.isValid(); } + + boolean pendingRemoval; + + @Override + public boolean pendingRemoval() { + return pendingRemoval = true; + } + + @Override + public boolean isPendingRemoval() { + return pendingRemoval; + } } diff --git a/src/main/java/org/maxgamer/quickshop/Watcher/DisplayAutoDespawnWatcher.java b/src/main/java/org/maxgamer/quickshop/Watcher/DisplayAutoDespawnWatcher.java index e5e081853..90e57356b 100644 --- a/src/main/java/org/maxgamer/quickshop/Watcher/DisplayAutoDespawnWatcher.java +++ b/src/main/java/org/maxgamer/quickshop/Watcher/DisplayAutoDespawnWatcher.java @@ -31,26 +31,31 @@ public class DisplayAutoDespawnWatcher extends BukkitRunnable { @Override public void run() { - plugin.getShopManager().getLoadedShops().stream().forEach(shop -> { + int range = plugin.getConfig().getInt("shop.display-despawn-range"); + + plugin.getShopManager().getLoadedShops().parallelStream() + .filter(shop -> shop.getDisplay() != null) + .forEach(shop -> { //Check the range has player? - int range = plugin.getConfig().getInt("shop.display-despawn-range"); boolean anyPlayerInRegion = Bukkit.getOnlinePlayers() .stream() - .filter(player -> player.getWorld().equals(shop.getLocation().getWorld())) + .filter(player -> player.getWorld() == shop.getLocation().getWorld()) .anyMatch(player -> player.getLocation().distance(shop.getLocation()) < range); - if (shop.getDisplay() == null) { - return; - } if (anyPlayerInRegion) { if (!shop.getDisplay().isSpawned()) { Util.debugLog("Respawning the shop " + shop + " the display, cause it was despawned and a player close it"); shop.checkDisplay(); } - } else { - if (shop.getDisplay().isSpawned()) { + } else if (shop.getDisplay().isSpawned()) { + if (shop.getDisplay().isPendingRemoval()) { + // Actually remove the pending display Util.debugLog("Removing the shop " + shop + " the display, cause nobody can see it"); - shop.getDisplay().remove(); + shop.getDisplay().remove(); + } else { + // Delayed to next cycle + Util.debugLog("Pending to remove the shop " + shop + " the display, cause nobody can see it"); + shop.getDisplay().pendingRemoval(); } } });