generated from ArtformGames/TemplateSinglePlugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(migrator): Provide a luckperms migrator for outdated data
- Loading branch information
Showing
4 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
src/main/java/com/artformgames/plugin/usersuffix/migrator/LuckPermsMigrator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
|
||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/artformgames/plugin/usersuffix/migrator/MigrateCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("----------------"); | ||
} | ||
|
||
} |