Skip to content

Commit

Permalink
feat(migrator): Provide a luckperms migrator for outdated data
Browse files Browse the repository at this point in the history
  • Loading branch information
CarmJos committed Mar 4, 2024
1 parent 5d27a14 commit 9131f83
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
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;
import dev.rollczi.litecommands.annotations.context.Context;
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;

Expand Down Expand Up @@ -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;
Expand All @@ -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();
}
}

}
Original file line number Diff line number Diff line change
@@ -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.<weight>.<content>
public static final Pattern PATTERN = Pattern.compile("^suffix\\.\\d+\\.(.*)$");

// REGEX for [START-COLOR]<content>[END-COLOR] or [START-COLOR]<content> or <content>[END-COLOR] or <content>
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<Integer> indexList = new HashSet<>();
Set<MigrateCache> 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);
}


}
Original file line number Diff line number Diff line change
@@ -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) {
}
29 changes: 29 additions & 0 deletions src/test/java/MigratorTest.java
Original file line number Diff line number Diff line change
@@ -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("----------------");
}

}

0 comments on commit 9131f83

Please sign in to comment.