From 9131f837d7a2a0df69823be9caec7f04efc9ca21 Mon Sep 17 00:00:00 2001 From: Carm Date: Mon, 4 Mar 2024 14:04:21 +0800 Subject: [PATCH] feat(migrator): Provide a luckperms migrator for outdated data --- .../command/UserSuffixCommands.java | 20 ++++ .../migrator/LuckPermsMigrator.java | 99 +++++++++++++++++++ .../usersuffix/migrator/MigrateCache.java | 11 +++ src/test/java/MigratorTest.java | 29 ++++++ 4 files changed, 159 insertions(+) create mode 100644 src/main/java/com/artformgames/plugin/usersuffix/migrator/LuckPermsMigrator.java create mode 100644 src/main/java/com/artformgames/plugin/usersuffix/migrator/MigrateCache.java create mode 100644 src/test/java/MigratorTest.java diff --git a/src/main/java/com/artformgames/plugin/usersuffix/command/UserSuffixCommands.java b/src/main/java/com/artformgames/plugin/usersuffix/command/UserSuffixCommands.java index 65203f5..5fee975 100644 --- a/src/main/java/com/artformgames/plugin/usersuffix/command/UserSuffixCommands.java +++ b/src/main/java/com/artformgames/plugin/usersuffix/command/UserSuffixCommands.java @@ -4,6 +4,7 @@ import com.artformgames.core.ArtCore; import com.artformgames.plugin.usersuffix.conf.PluginConfig; import com.artformgames.plugin.usersuffix.conf.PluginMessages; +import com.artformgames.plugin.usersuffix.migrator.LuckPermsMigrator; import com.artformgames.plugin.usersuffix.user.SuffixAccount; import dev.rollczi.litecommands.annotations.argument.Arg; import dev.rollczi.litecommands.annotations.command.Command; @@ -11,7 +12,9 @@ import dev.rollczi.litecommands.annotations.execute.Execute; import dev.rollczi.litecommands.annotations.join.Join; import dev.rollczi.litecommands.annotations.permission.Permission; +import org.bukkit.command.ConsoleCommandSender; import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; import java.util.Map; @@ -60,6 +63,11 @@ void setContent(@Context Player player, @Arg("format-color") String formatColor, return; } + if (content.length() > 200) { + PluginMessages.TOO_LONG.send(player, 200); + return; + } + if (ColorParser.clear(content).isBlank()) { PluginMessages.TOO_SHORT.send(player); return; @@ -69,4 +77,16 @@ void setContent(@Context Player player, @Arg("format-color") String formatColor, PluginMessages.SUCCESS.send(player, account.getSuffix()); } + @Execute(name = "migrate luckperms") + void importLuckPerms(@Context ConsoleCommandSender sender, + @NotNull String sourceTable, @NotNull String usersTable, boolean purge) { + try { + int count = LuckPermsMigrator.importData(sourceTable, usersTable, purge); + sender.sendMessage("Successfully imported " + count + " suffixes from LuckPerms."); + } catch (Exception ex) { + sender.sendMessage("An error occurred while importing suffixes from LuckPerms!"); + ex.printStackTrace(); + } + } + } diff --git a/src/main/java/com/artformgames/plugin/usersuffix/migrator/LuckPermsMigrator.java b/src/main/java/com/artformgames/plugin/usersuffix/migrator/LuckPermsMigrator.java new file mode 100644 index 0000000..4d7aae3 --- /dev/null +++ b/src/main/java/com/artformgames/plugin/usersuffix/migrator/LuckPermsMigrator.java @@ -0,0 +1,99 @@ +package com.artformgames.plugin.usersuffix.migrator; + +import cc.carm.lib.easysql.api.SQLQuery; +import com.artformgames.core.ArtCore; +import com.artformgames.core.user.UserKey; +import com.artformgames.plugin.usersuffix.user.SuffixAccount; +import com.artformgames.plugin.usersuffix.user.SuffixLoader; + +import java.sql.ResultSet; +import java.util.HashSet; +import java.util.Set; +import java.util.UUID; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class LuckPermsMigrator { + + // REGEX For suffix.. + public static final Pattern PATTERN = Pattern.compile("^suffix\\.\\d+\\.(.*)$"); + + // REGEX for [START-COLOR][END-COLOR] or [START-COLOR] or [END-COLOR] or + public static final Pattern CONTENT = Pattern.compile( + "^(.*?)((?:&[0-9a-fk-or]|&\\(#[0-9a-fA-F]{6}\\))?)$" + ); + + public static int importData(String sourceTable, String usersTable, boolean purge) { + Set indexList = new HashSet<>(); + Set data = new HashSet<>(); + try (SQLQuery query = ArtCore.getSQLManager().createQuery().inTable(sourceTable) + .selectColumns("id", "uuid", "permission") + .build().execute()) { + ResultSet rs = query.getResultSet(); + while (rs.next()) { + String perm = rs.getString("permission"); + Matcher matcher = PATTERN.matcher(perm); + if (!matcher.matches()) continue; + + UUID uuid = UUID.fromString(rs.getString("uuid")); + String contentString = matcher.group(1); + + Matcher contentMatcher = CONTENT.matcher(contentString); + if (!contentMatcher.matches()) continue; + + String content = contentMatcher.group(1); + String formatColor = contentMatcher.group(2); + + if (content == null && formatColor == null) continue; + + String username = getUsername(usersTable, uuid); + if (username == null) continue; + + indexList.add(rs.getInt("id"); + data.add(new MigrateCache(importKey(uuid, username), content, formatColor)); + } + } catch (Exception ex) { + ex.printStackTrace(); + return -1; + } + + for (MigrateCache cache : data) { // Insert data into the new table + SuffixLoader.TABLE.createReplace() + .setColumnNames("user", "content", "color") + .setParams(cache.user().id(), cache.content(), cache.content()) + .execute(null); + } + + if (purge) { + for (Integer id : indexList) { + ArtCore.getSQLManager().createDelete(sourceTable) + .addCondition("id", id) + .setLimit(1).build().execute(null); + } + } + + return data.size(); + } + + public static String getUsername(String playersTable, UUID uuid) { + try (SQLQuery query = ArtCore.getSQLManager().createQuery().inTable(playersTable) + .selectColumns("uuid", "username") + .addCondition("uuid", uuid.toString()) + .setLimit(1).build().execute()) { + ResultSet rs = query.getResultSet(); + if (rs.next()) { + return rs.getString("username"); + } + return null; + } catch (Exception ex) { + ex.printStackTrace(); + return null; + } + } + + public static UserKey importKey(UUID uuid, String username) { + return ArtCore.getUserManager().getKey(uuid); + } + + +} diff --git a/src/main/java/com/artformgames/plugin/usersuffix/migrator/MigrateCache.java b/src/main/java/com/artformgames/plugin/usersuffix/migrator/MigrateCache.java new file mode 100644 index 0000000..2b0f794 --- /dev/null +++ b/src/main/java/com/artformgames/plugin/usersuffix/migrator/MigrateCache.java @@ -0,0 +1,11 @@ +package com.artformgames.plugin.usersuffix.migrator; + +import com.artformgames.core.user.UserKey; +import org.checkerframework.checker.units.qual.N; +import org.jetbrains.annotations.NotNull; + +import java.util.UUID; + +public record MigrateCache(UserKey user, + String content, String formatColor) { +} diff --git a/src/test/java/MigratorTest.java b/src/test/java/MigratorTest.java new file mode 100644 index 0000000..36b5904 --- /dev/null +++ b/src/test/java/MigratorTest.java @@ -0,0 +1,29 @@ +import com.artformgames.plugin.usersuffix.migrator.LuckPermsMigrator; +import org.junit.Test; + +import java.util.regex.Matcher; + +public class MigratorTest { + + @Test + public void patternTest() { + testPattern("suffix.100.&(#B2DFEE)凛冬之国的艾莎&(#B2DFEE)"); + testPattern("suffix.100.&fQwQ&(#B2DFEE)"); + testPattern("suffix.100.&fQwQ&f"); + testPattern("suffix.100.QwQ&f"); + testPattern("suffix.100.&fQwQ"); + testPattern("suffix.100.QwQ"); + } + + public void testPattern(String input) { + Matcher matcher = LuckPermsMigrator.PATTERN.matcher(input); + + while (matcher.find()) { + for (int i = 0; i <= matcher.groupCount(); i++) { + System.out.println("Group " + i + ": " + matcher.group(i)); + } + } + System.out.println("----------------"); + } + +}