Compilation fixes & color util

This commit is contained in:
Draycia committed 2020-10-11 01:07:24 -07:00
1 parent 1ce21395dd
commit 16affaaa35
4 files changed
+55 -14

No files matched your search

@@ -315,13 +315,13 @@ public final class CarbonChatBukkit extends JavaPlugin implements CarbonChat {
} }
return HoconConfigurationLoader.builder() return HoconConfigurationLoader.builder()
.setDefaultOptions(opts -> { .defaultOptions(opts -> {
return opts.withShouldCopyDefaults(true).withSerializers(builder -> { return opts.shouldCopyDefaults(true).serializers(builder -> {
builder.register(Key.class, KeySerializer.INSTANCE) builder.register(Key.class, KeySerializer.INSTANCE)
.register(Sound.class, SoundSerializer.INSTANCE); .register(Sound.class, SoundSerializer.INSTANCE);
}); });
}) })
.setFile(configFile) .file(configFile)
.build(); .build();
} }
@@ -48,7 +48,7 @@ public final class CarbonUtils {
static { static {
final List<String> colorList = new ArrayList<>(); final List<String> colorList = new ArrayList<>();
for (final NamedTextColor color : NamedTextColor.values()) { for (final NamedTextColor color : NamedTextColor.NAMES.values()) {
colorList.add(color.toString()); colorList.add(color.toString());
} }
@@ -54,10 +54,10 @@ public final class SoundSerializer implements TypeSerializer<Sound> {
return null; return null;
} }
final Key name = value.getNode(NAME).getValue(KeySerializer.INSTANCE.type()); final Key name = value.node(NAME).get(KeySerializer.INSTANCE.type());
final Sound.Source source = (Sound.Source) value.getNode(SOURCE).getValue(SOURCE_TYPE); // unsure about this final Sound.Source source = (Sound.Source) value.node(SOURCE).get(SOURCE_TYPE); // unsure about this
final float volume = value.getNode(VOLUME).getFloat(1.0f); final float volume = value.node(VOLUME).getFloat(1.0f);
final float pitch = value.getNode(PITCH).getFloat(1.0f); final float pitch = value.node(PITCH).getFloat(1.0f);
if (name == null || source == null) { if (name == null || source == null) {
throw new ObjectMappingException("A name and source are required to deserialize a Sound"); throw new ObjectMappingException("A name and source are required to deserialize a Sound");
@@ -69,14 +69,14 @@ public final class SoundSerializer implements TypeSerializer<Sound> {
@Override @Override
public void serialize(@NonNull final Type type, @Nullable final Sound obj, @NonNull final ConfigurationNode value) throws ObjectMappingException { public void serialize(@NonNull final Type type, @Nullable final Sound obj, @NonNull final ConfigurationNode value) throws ObjectMappingException {
if (obj == null) { if (obj == null) {
value.setValue(null); value.set(null);
return; return;
} }
value.getNode(NAME).setValue(KeySerializer.INSTANCE.type(), obj.name()); value.node(NAME).set(KeySerializer.INSTANCE.type(), obj.name());
value.getNode(SOURCE).setValue(SOURCE_TYPE, obj.source()); value.node(SOURCE).set(SOURCE_TYPE, obj.source());
value.getNode(VOLUME).setValue(obj.volume()); value.node(VOLUME).set(obj.volume());
value.getNode(PITCH).setValue(obj.pitch()); value.node(PITCH).set(obj.pitch());
} }
} }
@@ -3,9 +3,14 @@ package net.draycia.carbon.common.utils;
import net.draycia.carbon.api.users.ChatUser; import net.draycia.carbon.api.users.ChatUser;
import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextColor; import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.text.format.TextDecoration;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import net.kyori.adventure.text.serializer.legacy.LegacyFormat;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable; import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.regex.Pattern;
public final class ColorUtils { public final class ColorUtils {
private ColorUtils() { private ColorUtils() {
@@ -31,7 +36,7 @@ public final class ColorUtils {
// } // }
// } // }
for (final NamedTextColor namedColor : NamedTextColor.values()) { for (final NamedTextColor namedColor : NamedTextColor.NAMES.values()) {
if (namedColor.toString().equalsIgnoreCase(input)) { if (namedColor.toString().equalsIgnoreCase(input)) {
return namedColor; return namedColor;
} }
@@ -46,4 +51,40 @@ public final class ColorUtils {
return TextColor.fromCSSHexString(input); return TextColor.fromCSSHexString(input);
} }
private static final @NonNull Pattern spigotLegacyRGB =
Pattern.compile("[§&]x[§&]([0-9a-f])[§&]([0-9a-f])[§&]([0-9a-f])[§&]([0-9a-f])[§&]([0-9a-f])[§&]([0-9a-f])");
private static final @NonNull Pattern pluginRGB =
Pattern.compile("[§&]#([0-9a-f])([0-9a-f])([0-9a-f])([0-9a-f])([0-9a-f])([0-9a-f])");
@NonNull
public static String translateAlternateColors(@NonNull final String input) {
// TODO: check if MiniMessage or MineDown
String output = input;
// Legacy RGB
output = spigotLegacyRGB.matcher(output).replaceAll("<#$1$2$3$4$5$6>");
// Alternate RGB, TAB (neznamy) && KiteBoard
output = pluginRGB.matcher(output).replaceAll("<#$1$2$3$4$5$6>");
// Legacy Colors
for (final char c : "0123456789abcdef".toCharArray()) {
final LegacyFormat format = LegacyComponentSerializer.parseChar(c);
final TextColor color = format.color();
output = output.replaceAll("[§&]" + c, "<" + color.asHexString() + ">");
}
// Legacy Formatting
for (final char c : "klmno".toCharArray()) {
final LegacyFormat format = LegacyComponentSerializer.parseChar(c);
final TextDecoration decoration = format.decoration();
output = output.replaceAll("[§&]" + c, "<" + decoration.name() + ">");
}
return output;
}
} }