Skip to content

Commit

Permalink
fix(color): Fixed color methods
Browse files Browse the repository at this point in the history
  • Loading branch information
CarmJos committed Mar 4, 2024
1 parent e38c4ac commit c365d37
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +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.migrator.ArtEssMigrator;
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 jdk.jfr.Description;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

import java.util.Map;

Expand Down Expand Up @@ -77,11 +77,13 @@ 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) {
@Execute(name = "import")
@Description("Import suffixes from ArtEssentials(outdated).")
@Permission("usersuffix.admin")
void importLuckPerms(@Context CommandSender sender,
@Arg("perms-table") String sourceTable, @Arg("users-table") String usersTable, @Arg boolean purge) {
try {
int count = LuckPermsMigrator.importData(sourceTable, usersTable, purge);
int count = ArtEssMigrator.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!");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.artformgames.plugin.usersuffix.migrator;

import cc.carm.lib.easyplugin.utils.ColorParser;
import cc.carm.lib.easysql.api.SQLQuery;
import com.artformgames.core.ArtCore;
import com.artformgames.core.user.UserKey;
Expand All @@ -12,10 +13,24 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class LuckPermsMigrator {
public class ArtEssMigrator {

public record CachedRecord(UserKey user,
String content, String formatColor) {

public String color() {

Matcher hexMatcher = ColorParser.HEX_PATTERN.matcher(formatColor());
if (hexMatcher.find()) return "#" + hexMatcher.group(1);

Matcher colorMatcher = ColorParser.COLOR_PATTERN.matcher(formatColor());
if (colorMatcher.matches()) {
return formatColor.replace("&", "");
}

return null;
}

}

// REGEX For suffix.<weight>.<content>
Expand Down Expand Up @@ -50,7 +65,7 @@ public static int importData(String sourceTable, String usersTable, boolean purg
if (content == null && formatColor == null) continue;

String username = getUsername(usersTable, uuid);
if (username == null) continue;
if (username == null || username.equals("null")) continue;

UserKey key = importKey(uuid, username);
if (key == null) continue;
Expand All @@ -66,7 +81,7 @@ public static int importData(String sourceTable, String usersTable, boolean purg
for (CachedRecord cache : data) { // Insert data into the new table
SuffixLoader.TABLE.createReplace()
.setColumnNames("user", "content", "color")
.setParams(cache.user().id(), cache.content(), cache.content())
.setParams(cache.user().id(), cache.content(), cache.color())
.execute(null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
public class SuffixAccount extends AbstractUserHandler implements UserHandler {

// Allow colors and hex codes(starting with #)
public static final Pattern ALLOWED_CODES = Pattern.compile("(&[0-9a-fk-or]|#[0-9a-fA-F]{6})");
public static final Pattern ALLOWED_CODES = Pattern.compile("([0-9a-fk-or]|#[0-9a-fA-F]{6})");
public static final NumberFormat FORMAT = NumberFormat.getInstance();

static {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ public SuffixLoader(@NotNull Plugin plugin) {
if (rs.next()) {
String content = rs.getString("content");
String color = rs.getString("color");
return new SuffixAccount(user, content, color == null || !SuffixAccount.validColor(color) ? null : color);
if (color != null && !SuffixAccount.validColor(color)) {
color = null;
}
return new SuffixAccount(user, content, color);
}
}
return null;
Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ softdepend:

permissions:
"usersuffix.use":
default: true
default: true
"usersuffix.admin":
default: op
9 changes: 7 additions & 2 deletions src/test/java/MigratorTest.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import com.artformgames.plugin.usersuffix.migrator.LuckPermsMigrator;
import com.artformgames.plugin.usersuffix.migrator.ArtEssMigrator;
import org.junit.Test;

import java.util.regex.Matcher;
Expand All @@ -16,11 +16,16 @@ public void patternTest() {
}

public void testPattern(String input) {
Matcher matcher = LuckPermsMigrator.PATTERN.matcher(input);
Matcher matcher = ArtEssMigrator.PATTERN.matcher(input);

while (matcher.find()) {
for (int i = 0; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
Matcher c = ArtEssMigrator.CONTENT.matcher(input);
if (c.matches()) {
System.out.println("Content: " + c.group(1));
System.out.println("Color: " + c.group(2));
}
}
}
System.out.println("----------------");
Expand Down

0 comments on commit c365d37

Please sign in to comment.