Skip to content

Commit

Permalink
Adjusted parsers and parsing order
Browse files Browse the repository at this point in the history
  • Loading branch information
OakLoaf committed Sep 30, 2024
1 parent c26c78e commit 9033d92
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,16 @@ public ChatColorHandlerSettings debug(boolean debug) {
private void init() {
initialised = true;

Parsers.register(LegacyHexParser.INSTANCE, 85);
Parsers.register(LegacySpigotParser.INSTANCE, 84);
Parsers.register(HexParser.INSTANCE, 83);
Parsers.register(SpigotParser.INSTANCE, 65);

try {
Class.forName("net.kyori.adventure.text.minimessage.MiniMessage").getMethod("miniMessage");
messenger(new MiniMessageMessenger());

Parsers.register(MiniMessageResolverParser.INSTANCE, 89);
Parsers.register(LegacyHexParser.INSTANCE, 75);
Parsers.register(LegacySpigotParser.INSTANCE, 74);
Parsers.register(MiniMessageColorParser.INSTANCE, 73);
Parsers.register(MiniMessageInteractionParser.INSTANCE, 72);
Parsers.register(MiniMessagePlaceholderParser.INSTANCE, 71);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.lushplugins.chatcolorhandler.parsers.custom;

import org.bukkit.ChatColor;
import org.jetbrains.annotations.NotNull;
import org.lushplugins.chatcolorhandler.parsers.Parser;
import org.lushplugins.chatcolorhandler.parsers.ParserTypes;
Expand All @@ -18,7 +17,7 @@ public String getType() {
@Override
public String parseString(@NotNull String string, @NotNull OutputType outputType) {
return switch (outputType) {
case SPIGOT -> ChatColor.translateAlternateColorCodes('&', string);
case SPIGOT -> string;
case MINI_MESSAGE -> string
// Legacy Ampersand
.replace("§", "&")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ public String parseString(@NotNull String string, @NotNull OutputType outputType
return switch (outputType) {
case SPIGOT -> {
string = string.replace('§', '&');
yield MiniMessageMessenger.LEGACY_COMPONENT_SERIALIZER.serialize(MiniMessageMessenger.MINI_MESSAGE.deserialize(string, BASIC_COLORS))
.replace('&', '§');
yield MiniMessageMessenger.LEGACY_COMPONENT_SERIALIZER.serialize(MiniMessageMessenger.MINI_MESSAGE.deserialize(string, BASIC_COLORS));
}
case MINI_MESSAGE -> string;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ public String parseString(@NotNull String string, @NotNull OutputType outputType
return switch (outputType) {
case SPIGOT -> {
string = string.replace('§', '&');
yield MiniMessageMessenger.LEGACY_COMPONENT_SERIALIZER.serialize(MiniMessageMessenger.MINI_MESSAGE.deserialize(string, INTERACTION))
.replace('&', '§');
yield MiniMessageMessenger.LEGACY_COMPONENT_SERIALIZER.serialize(MiniMessageMessenger.MINI_MESSAGE.deserialize(string, INTERACTION));
}
case MINI_MESSAGE -> string;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ public String parseString(@NotNull String string, @NotNull OutputType outputType
return switch (outputType) {
case SPIGOT -> {
string = string.replace('§', '&');
yield MiniMessageMessenger.LEGACY_COMPONENT_SERIALIZER.serialize(MiniMessageMessenger.MINI_MESSAGE.deserialize(string, VANILLA_PLACEHOLDERS))
.replace('&', '§');
yield MiniMessageMessenger.LEGACY_COMPONENT_SERIALIZER.serialize(MiniMessageMessenger.MINI_MESSAGE.deserialize(string, VANILLA_PLACEHOLDERS));
}
case MINI_MESSAGE -> string;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
import org.lushplugins.chatcolorhandler.parsers.Parser;
import org.lushplugins.chatcolorhandler.parsers.ParserTypes;
import org.lushplugins.chatcolorhandler.parsers.Parsers;
import org.lushplugins.chatcolorhandler.parsers.Resolver;

import java.util.List;
import java.util.Objects;

public class MiniMessageResolverParser implements Parser {
public static final MiniMessageResolverParser INSTANCE = new MiniMessageResolverParser();
Expand All @@ -30,11 +34,17 @@ public String parseString(@NotNull String string, @NotNull OutputType outputType
case SPIGOT -> {
string = string.replace('§', '&');

TagResolver resolver = Parsers.getCombinedResolvers(player instanceof Audience audience ? audience : null);
yield MiniMessageMessenger.LEGACY_COMPONENT_SERIALIZER.serialize(MiniMessageMessenger.MINI_MESSAGE.deserialize(string, resolver))
.replace('&', '§');
TagResolver resolver = Parsers.getCombinedResolvers(player instanceof Audience audience ? audience : null, getPlaceholderResolvers());
yield MiniMessageMessenger.LEGACY_COMPONENT_SERIALIZER.serialize(MiniMessageMessenger.MINI_MESSAGE.deserialize(string, resolver));
}
case MINI_MESSAGE -> string;
};
}

private static List<Resolver> getPlaceholderResolvers() {
return ParserTypes.placeholder().stream()
.map(parser -> parser instanceof Resolver parserResolver ? parserResolver : null)
.filter(Objects::nonNull)
.toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ public String parseString(@NotNull String string, @NotNull OutputType outputType
return switch (outputType) {
case SPIGOT -> {
string = string.replace('§', '&');
yield MiniMessageMessenger.LEGACY_COMPONENT_SERIALIZER.serialize(MiniMessageMessenger.MINI_MESSAGE.deserialize(string, TEXT_FORMATTING))
.replace('&', '§');
yield MiniMessageMessenger.LEGACY_COMPONENT_SERIALIZER.serialize(MiniMessageMessenger.MINI_MESSAGE.deserialize(string, TEXT_FORMATTING));
}
case MINI_MESSAGE -> string;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.lushplugins.chatcolorhandler.parsers.custom;

import org.bukkit.ChatColor;
import org.jetbrains.annotations.NotNull;
import org.lushplugins.chatcolorhandler.parsers.Parser;
import org.lushplugins.chatcolorhandler.parsers.ParserTypes;

public class SpigotParser implements Parser {
public static final SpigotParser INSTANCE = new SpigotParser();

private SpigotParser() {}

@Override
public String getType() {
return ParserTypes.COLOR;
}

@Override
public String parseString(@NotNull String string, @NotNull OutputType outputType) {
return switch (outputType) {
case SPIGOT -> ChatColor.translateAlternateColorCodes('&', string);
case MINI_MESSAGE -> string;
};
}
}

0 comments on commit 9033d92

Please sign in to comment.