Skip to content

Commit

Permalink
add migrate to VarBlocks
Browse files Browse the repository at this point in the history
close #110
  • Loading branch information
HSGamer committed Oct 23, 2024
1 parent 1faa519 commit 0fbc3b3
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 5 deletions.
4 changes: 4 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@
<id>codemc-repo</id>
<url>https://repo.codemc.io/repository/maven-public/</url>
</repository>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

<dependencies>
Expand Down
6 changes: 6 additions & 0 deletions spigot/plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -183,5 +183,11 @@
<version>2.11.6</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.github.HSGamer</groupId>
<artifactId>VarBlocks</artifactId>
<version>7ef04127f5</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.*;
Expand Down Expand Up @@ -87,11 +95,8 @@ private void migrateConfig() {
MainConfig mainConfig = get(MainConfig.class);
Config config = mainConfig.getConfig();

boolean migrated = false;

Map<String[], Object> placeholders = config.getValues(false, "placeholders");
if (!placeholders.isEmpty()) {
migrated = true;
Map<String, Map<String, Object>> holders = new HashMap<>();
Pattern placeholderPattern = Pattern.compile("\\s*(\\[.*])?\\s*(.*)\\s*");
for (Map.Entry<String[], Object> entry : placeholders.entrySet()) {
Expand Down Expand Up @@ -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<String> 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<String> 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<String, Map<String, Object>> formatters = new HashMap<>();
config.getValues(false, "formatters")
.forEach(
(key, value) ->
MapUtils.castOptionalStringObjectMap(value).ifPresent(map -> formatters.put(key[0], map))
);

List<String> 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<String> 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<String, String> arguments = new LinkedHashMap<>();

Map<String, Object> 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<String, String> 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();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -34,4 +35,6 @@ default String getTopHolderNotFound() {
}

void reloadConfig();

Config getConfig();
}
1 change: 1 addition & 0 deletions spigot/plugin/src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ main: me.hsgamer.topper.spigot.plugin.TopperPlugin
api-version: 1.13
softdepend:
- PlaceholderAPI
- VarBlocks
folia-supported: true

0 comments on commit 0fbc3b3

Please sign in to comment.