diff --git a/pom.xml b/pom.xml index 49f596e..8fdd707 100644 --- a/pom.xml +++ b/pom.xml @@ -49,6 +49,10 @@ codemc-repo https://repo.codemc.io/repository/maven-public/ + + jitpack.io + https://jitpack.io + diff --git a/spigot/plugin/pom.xml b/spigot/plugin/pom.xml index 56ad4ed..36d6b85 100644 --- a/spigot/plugin/pom.xml +++ b/spigot/plugin/pom.xml @@ -183,5 +183,11 @@ 2.11.6 provided + + com.github.HSGamer + VarBlocks + 7ef04127f5 + provided + \ No newline at end of file diff --git a/spigot/plugin/src/main/java/me/hsgamer/topper/spigot/plugin/TopperPlugin.java b/spigot/plugin/src/main/java/me/hsgamer/topper/spigot/plugin/TopperPlugin.java index c7daf63..263137b 100644 --- a/spigot/plugin/src/main/java/me/hsgamer/topper/spigot/plugin/TopperPlugin.java +++ b/spigot/plugin/src/main/java/me/hsgamer/topper/spigot/plugin/TopperPlugin.java @@ -5,6 +5,8 @@ import me.hsgamer.hscore.bukkit.config.BukkitConfig; import me.hsgamer.hscore.bukkit.utils.MessageUtils; import me.hsgamer.hscore.checker.spigotmc.SpigotVersionChecker; +import me.hsgamer.hscore.common.CollectionUtils; +import me.hsgamer.hscore.common.MapUtils; import me.hsgamer.hscore.config.Config; import me.hsgamer.hscore.config.PathString; import me.hsgamer.hscore.config.proxy.ConfigGenerator; @@ -19,7 +21,13 @@ import me.hsgamer.topper.spigot.plugin.listener.JoinListener; import me.hsgamer.topper.spigot.plugin.manager.TopManager; import me.hsgamer.topper.spigot.plugin.manager.TopQueryManager; +import me.hsgamer.varblocks.VarBlocks; +import me.hsgamer.varblocks.api.BlockEntry; +import me.hsgamer.varblocks.manager.BlockManager; +import me.hsgamer.varblocks.manager.TemplateManager; import org.bstats.bukkit.Metrics; +import org.bukkit.Bukkit; +import org.bukkit.plugin.java.JavaPlugin; import java.io.File; import java.util.*; @@ -87,11 +95,8 @@ private void migrateConfig() { MainConfig mainConfig = get(MainConfig.class); Config config = mainConfig.getConfig(); - boolean migrated = false; - Map placeholders = config.getValues(false, "placeholders"); if (!placeholders.isEmpty()) { - migrated = true; Map> holders = new HashMap<>(); Pattern placeholderPattern = Pattern.compile("\\s*(\\[.*])?\\s*(.*)\\s*"); for (Map.Entry entry : placeholders.entrySet()) { @@ -124,10 +129,147 @@ private void migrateConfig() { config.set(holders, "holders"); config.set(null, "placeholders"); config.save(); + getLogger().info("The config has been migrated"); + mainConfig.reloadConfig(); } - if (migrated) { - getLogger().info("The config has been migrated"); + List signs = new ArrayList<>(); + File signFile = new File(getDataFolder(), "sign.yml"); + if (signFile.exists()) { + Config signConfig = new BukkitConfig(signFile); + signConfig.setup(); + signs.addAll(CollectionUtils.createStringListFromObject(signConfig.getNormalized("entries"))); + } + + List skulls = new ArrayList<>(); + File skullFile = new File(getDataFolder(), "skull.yml"); + if (skullFile.exists()) { + Config skullConfig = new BukkitConfig(skullFile); + skullConfig.setup(); + skulls.addAll(CollectionUtils.createStringListFromObject(skullConfig.getNormalized("entries"))); + } + + Map> formatters = new HashMap<>(); + config.getValues(false, "formatters") + .forEach( + (key, value) -> + MapUtils.castOptionalStringObjectMap(value).ifPresent(map -> formatters.put(key[0], map)) + ); + + List signLines = Arrays.asList( + "&6&m ", + "&b#{index} &a{name}", + "&a{value} {suffix}", + "&6&m " + ); + Config messageConfig = get(MessageConfig.class).getConfig(); + if (messageConfig.contains("sign-lines")) { + signLines = CollectionUtils.createStringListFromObject(messageConfig.getNormalized("sign-lines")); + } + + boolean blockMigrateSuccess = false; + + if (signs.isEmpty() && skulls.isEmpty()) { + blockMigrateSuccess = true; + } else if (Bukkit.getPluginManager().getPlugin("VarBlocks") != null) { + VarBlocks varBlocks = JavaPlugin.getPlugin(VarBlocks.class); + + TemplateManager templateManager = varBlocks.get(TemplateManager.class); + if (templateManager.getTemplate("topper-sign").isEmpty()) { + List newSignLines = new ArrayList<>(signLines); + newSignLines.replaceAll(s -> s + .replace("uuid", "%topper_{holder};top_key;{index}%") + .replace("name", "%topper_{holder};top_name;{index}%") + .replace("value", "%topper_{holder};top_value;{index};{format}%") + .replace("value_raw", "%topper_{holder};top_value_raw;{index}%") + ); + templateManager.saveTemplate("topper-sign", newSignLines); + } + if (templateManager.getTemplate("topper-skull").isEmpty()) { + templateManager.saveTemplate("topper-skull", Collections.singletonList("%topper_{holder};top_key;{index}%")); + } + + BlockManager blockManager = varBlocks.get(BlockManager.class); + for (int signIndex = 0; signIndex < signs.size(); signIndex++) { + String sign = signs.get(signIndex); + String[] split = sign.split(","); + String world = split[0]; + int x = (int) Double.parseDouble(split[1]); + int y = (int) Double.parseDouble(split[2]); + int z = (int) Double.parseDouble(split[3]); + String holder = split[4]; + int index = Integer.parseInt(split[5]) + 1; + + Map arguments = new LinkedHashMap<>(); + + Map formatter = formatters.getOrDefault(holder, Collections.emptyMap()); + + int fractionDigits = Optional.ofNullable(formatter.get("fraction-digits")).map(String::valueOf).map(Integer::parseInt).orElse(-1); + char decimalSeparator = Optional.ofNullable(formatter.get("decimal-separator")).map(String::valueOf).map(s -> s.charAt(0)).orElse('.'); + char groupSeparator = Optional.ofNullable(formatter.get("group-separator")).map(String::valueOf).map(s -> s.charAt(0)).orElse(','); + boolean showGroupSeparator = Optional.ofNullable(formatter.get("show-group-separator")).map(String::valueOf).map(Boolean::parseBoolean).orElse(true); + StringBuilder decimalFormatBuilder = new StringBuilder(); + if (showGroupSeparator) { + decimalFormatBuilder.append("#").append(groupSeparator).append("###"); + } else { + decimalFormatBuilder.append("0"); + } + if (fractionDigits > 0) { + decimalFormatBuilder.append(decimalSeparator); + for (int i = 0; i < fractionDigits; i++) { + decimalFormatBuilder.append("0"); + } + } + String decimalFormat = decimalFormatBuilder.toString(); + String displayName = Optional.ofNullable(formatter.get("display-name")).map(String::valueOf).orElse(""); + String prefix = Optional.ofNullable(formatter.get("prefix")).map(String::valueOf).orElse(""); + String suffix = Optional.ofNullable(formatter.get("suffix")).map(String::valueOf).orElse(""); + + arguments.put("index", String.valueOf(index)); + arguments.put("display_name", displayName); + arguments.put("prefix", prefix); + arguments.put("suffix", suffix); + arguments.put("format", decimalFormat); + arguments.put("holder", holder); + + BlockEntry blockEntry = new BlockEntry(world, x, y, z, "sign", "topper-sign", arguments); + blockManager.setBlockEntry("topper-sign-" + signIndex, blockEntry); + } + for (int skullIndex = 0; skullIndex < skulls.size(); skullIndex++) { + String skull = skulls.get(skullIndex); + String[] split = skull.split(","); + String world = split[0]; + int x = (int) Double.parseDouble(split[1]); + int y = (int) Double.parseDouble(split[2]); + int z = (int) Double.parseDouble(split[3]); + String holder = split[4]; + int index = Integer.parseInt(split[5]) + 1; + + Map arguments = new LinkedHashMap<>(); + arguments.put("index", String.valueOf(index)); + arguments.put("holder", holder); + + BlockEntry blockEntry = new BlockEntry(world, x, y, z, "skull", "topper-skull", arguments); + blockManager.setBlockEntry("topper-skull-" + skullIndex, blockEntry); + } + + blockMigrateSuccess = true; + } else { + getLogger().warning("The plugin VarBlocks is not found"); + getLogger().warning("Please install it to migrate the block config"); + getLogger().warning("Link: https://www.spigotmc.org/resources/varblocks.120327/"); + } + + if (blockMigrateSuccess) { + getLogger().info("The block config has been migrated"); + if (signFile.exists()) { + signFile.delete(); + } + if (skullFile.exists()) { + skullFile.delete(); + } + config.remove("formatters"); + config.save(); mainConfig.reloadConfig(); } } diff --git a/spigot/plugin/src/main/java/me/hsgamer/topper/spigot/plugin/config/MessageConfig.java b/spigot/plugin/src/main/java/me/hsgamer/topper/spigot/plugin/config/MessageConfig.java index 0154acd..e00e72f 100644 --- a/spigot/plugin/src/main/java/me/hsgamer/topper/spigot/plugin/config/MessageConfig.java +++ b/spigot/plugin/src/main/java/me/hsgamer/topper/spigot/plugin/config/MessageConfig.java @@ -1,5 +1,6 @@ package me.hsgamer.topper.spigot.plugin.config; +import me.hsgamer.hscore.config.Config; import me.hsgamer.hscore.config.annotation.ConfigPath; public interface MessageConfig { @@ -34,4 +35,6 @@ default String getTopHolderNotFound() { } void reloadConfig(); + + Config getConfig(); } diff --git a/spigot/plugin/src/main/resources/plugin.yml b/spigot/plugin/src/main/resources/plugin.yml index 3ee695e..86f7d7b 100644 --- a/spigot/plugin/src/main/resources/plugin.yml +++ b/spigot/plugin/src/main/resources/plugin.yml @@ -4,4 +4,5 @@ main: me.hsgamer.topper.spigot.plugin.TopperPlugin api-version: 1.13 softdepend: - PlaceholderAPI + - VarBlocks folia-supported: true