mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-03 14:38:51 +00:00
Change indentation and improve event
This commit is contained in:
1 parent
73c7f3f78f
commit
591a6606cc
31 files changed
+1072
-974
No files matched your search
@@ -18,51 +18,51 @@ import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
|
||||
public final class CarbonChatBukkit extends CarbonChatCommon {
|
||||
|
||||
private final @NonNull UserManager userManager = new MemoryUserManagerBukkit();
|
||||
private final Logger logger = LogManager.getLogger("CarbonChat");
|
||||
private final CarbonChatBukkitEntry plugin;
|
||||
private final @NonNull UserManager userManager = new MemoryUserManagerBukkit();
|
||||
private final Logger logger = LogManager.getLogger("CarbonChat");
|
||||
private final CarbonChatBukkitEntry plugin;
|
||||
|
||||
CarbonChatBukkit(final @NonNull CarbonChatBukkitEntry plugin) {
|
||||
this.plugin = plugin;
|
||||
CarbonChatBukkit(final @NonNull CarbonChatBukkitEntry plugin) {
|
||||
this.plugin = plugin;
|
||||
|
||||
Injector.provide(CarbonChat.class, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull Logger logger() {
|
||||
return this.logger;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull UserManager userManager() {
|
||||
return this.userManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected @NonNull CommandManager<Commander> createCommandManager() {
|
||||
final PaperCommandManager<Commander> commandManager;
|
||||
try {
|
||||
commandManager = new PaperCommandManager<>(
|
||||
this.plugin,
|
||||
AsynchronousCommandExecutionCoordinator.<Commander>newBuilder().build(),
|
||||
commandSender -> {
|
||||
if (commandSender instanceof Player player) {
|
||||
return new BukkitPlayerCommander(this, player);
|
||||
}
|
||||
return BukkitCommander.from(commandSender);
|
||||
},
|
||||
commander -> ((BukkitCommander) commander).commandSender()
|
||||
);
|
||||
} catch (final Exception ex) {
|
||||
throw new RuntimeException("Failed to initialize command manager.", ex);
|
||||
Injector.provide(CarbonChat.class, this);
|
||||
}
|
||||
commandManager.registerAsynchronousCompletions();
|
||||
commandManager.registerBrigadier();
|
||||
final var brigadierManager = commandManager.brigadierManager();
|
||||
if (brigadierManager != null) {
|
||||
brigadierManager.setNativeNumberSuggestions(false);
|
||||
|
||||
@Override
|
||||
public @NonNull Logger logger() {
|
||||
return this.logger;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull UserManager userManager() {
|
||||
return this.userManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected @NonNull CommandManager<Commander> createCommandManager() {
|
||||
final PaperCommandManager<Commander> commandManager;
|
||||
try {
|
||||
commandManager = new PaperCommandManager<>(
|
||||
this.plugin,
|
||||
AsynchronousCommandExecutionCoordinator.<Commander>newBuilder().build(),
|
||||
commandSender -> {
|
||||
if (commandSender instanceof Player player) {
|
||||
return new BukkitPlayerCommander(this, player);
|
||||
}
|
||||
return BukkitCommander.from(commandSender);
|
||||
},
|
||||
commander -> ((BukkitCommander) commander).commandSender()
|
||||
);
|
||||
} catch (final Exception ex) {
|
||||
throw new RuntimeException("Failed to initialize command manager.", ex);
|
||||
}
|
||||
commandManager.registerAsynchronousCompletions();
|
||||
commandManager.registerBrigadier();
|
||||
final var brigadierManager = commandManager.brigadierManager();
|
||||
if (brigadierManager != null) {
|
||||
brigadierManager.setNativeNumberSuggestions(false);
|
||||
}
|
||||
return commandManager;
|
||||
}
|
||||
return commandManager;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -11,34 +11,34 @@ import java.util.logging.Level;
|
||||
|
||||
public final class CarbonChatBukkitEntry extends JavaPlugin {
|
||||
|
||||
private static final int BSTATS_PLUGIN_ID = 8720;
|
||||
private static final int BSTATS_PLUGIN_ID = 8720;
|
||||
|
||||
private @MonotonicNonNull CarbonChatBukkit carbon;
|
||||
private @MonotonicNonNull CarbonChatBukkit carbon;
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
if (!PaperLib.isPaper()) {
|
||||
this.getLogger().log(Level.SEVERE, "*");
|
||||
this.getLogger().log(Level.SEVERE, "* CarbonChat makes extensive use of APIs added by Paper.");
|
||||
this.getLogger().log(Level.SEVERE, "* For this reason, CarbonChat is not compatible with Spigot or CraftBukkit servers.");
|
||||
this.getLogger().log(Level.SEVERE, "* Upgrade your server to Paper in order to use CarbonChat.");
|
||||
this.getLogger().log(Level.SEVERE, "*");
|
||||
PaperLib.suggestPaper(this, Level.SEVERE);
|
||||
Bukkit.getPluginManager().disablePlugin(this);
|
||||
return;
|
||||
@Override
|
||||
public void onEnable() {
|
||||
if (!PaperLib.isPaper()) {
|
||||
this.getLogger().log(Level.SEVERE, "*");
|
||||
this.getLogger().log(Level.SEVERE, "* CarbonChat makes extensive use of APIs added by Paper.");
|
||||
this.getLogger().log(Level.SEVERE, "* For this reason, CarbonChat is not compatible with Spigot or CraftBukkit servers.");
|
||||
this.getLogger().log(Level.SEVERE, "* Upgrade your server to Paper in order to use CarbonChat.");
|
||||
this.getLogger().log(Level.SEVERE, "*");
|
||||
PaperLib.suggestPaper(this, Level.SEVERE);
|
||||
Bukkit.getPluginManager().disablePlugin(this);
|
||||
return;
|
||||
}
|
||||
|
||||
this.carbon = new CarbonChatBukkit(this);
|
||||
|
||||
final Metrics metrics = new Metrics(this, BSTATS_PLUGIN_ID);
|
||||
|
||||
Bukkit.getPluginManager().registerEvents(new BukkitChatListener(), this);
|
||||
|
||||
this.carbon.initialize();
|
||||
}
|
||||
|
||||
this.carbon = new CarbonChatBukkit(this);
|
||||
|
||||
final Metrics metrics = new Metrics(this, BSTATS_PLUGIN_ID);
|
||||
|
||||
Bukkit.getPluginManager().registerEvents(new BukkitChatListener(), this);
|
||||
|
||||
this.carbon.initialize();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
}
|
||||
@Override
|
||||
public void onDisable() {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -8,29 +8,29 @@ import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
|
||||
public interface BukkitCommander extends Commander, ForwardingAudience.Single {
|
||||
|
||||
@NonNull CommandSender commandSender();
|
||||
@NonNull CommandSender commandSender();
|
||||
|
||||
static @NonNull BukkitCommander from(final @NonNull CommandSender sender) {
|
||||
return new BukkitCommanderImpl(sender);
|
||||
}
|
||||
|
||||
final class BukkitCommanderImpl implements BukkitCommander {
|
||||
|
||||
private final CommandSender commandSender;
|
||||
|
||||
private BukkitCommanderImpl(final @NonNull CommandSender commandSender) {
|
||||
this.commandSender = commandSender;
|
||||
static @NonNull BukkitCommander from(final @NonNull CommandSender sender) {
|
||||
return new BukkitCommanderImpl(sender);
|
||||
}
|
||||
|
||||
public @NonNull CommandSender commandSender() {
|
||||
return this.commandSender;
|
||||
}
|
||||
final class BukkitCommanderImpl implements BukkitCommander {
|
||||
|
||||
@Override
|
||||
public @NonNull Audience audience() {
|
||||
return this.commandSender;
|
||||
}
|
||||
private final CommandSender commandSender;
|
||||
|
||||
}
|
||||
private BukkitCommanderImpl(final @NonNull CommandSender commandSender) {
|
||||
this.commandSender = commandSender;
|
||||
}
|
||||
|
||||
public @NonNull CommandSender commandSender() {
|
||||
return this.commandSender;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull Audience audience() {
|
||||
return this.commandSender;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -11,23 +11,23 @@ import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import static java.util.Objects.requireNonNull;
|
||||
|
||||
public record BukkitPlayerCommander(
|
||||
@NonNull CarbonChat carbon,
|
||||
@NonNull Player player
|
||||
@NonNull CarbonChat carbon,
|
||||
@NonNull Player player
|
||||
) implements PlayerCommander, BukkitCommander {
|
||||
|
||||
@Override
|
||||
public @NonNull CommandSender commandSender() {
|
||||
return this.player;
|
||||
}
|
||||
@Override
|
||||
public @NonNull CommandSender commandSender() {
|
||||
return this.player;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull Audience audience() {
|
||||
return this.player;
|
||||
}
|
||||
@Override
|
||||
public @NonNull Audience audience() {
|
||||
return this.player;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull CarbonPlayer carbonPlayer() {
|
||||
return requireNonNull(this.carbon.userManager().carbonPlayer(this.player.getUniqueId()), "No CarbonPlayer for logged in Player!");
|
||||
}
|
||||
@Override
|
||||
public @NonNull CarbonPlayer carbonPlayer() {
|
||||
return requireNonNull(this.carbon.userManager().carbonPlayer(this.player.getUniqueId()), "No CarbonPlayer for logged in Player!");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,9 +3,10 @@ package net.draycia.carbon.bukkit.listeners;
|
||||
import io.papermc.paper.event.player.AsyncChatEvent;
|
||||
import net.draycia.carbon.api.CarbonChat;
|
||||
import net.draycia.carbon.api.events.CarbonChatEvent;
|
||||
import net.draycia.carbon.api.users.CarbonPlayer;
|
||||
import net.draycia.carbon.api.users.UserManager;
|
||||
import net.draycia.carbon.bukkit.CarbonChatBukkit;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
@@ -19,51 +20,57 @@ import static net.kyori.adventure.key.Key.key;
|
||||
|
||||
public final class BukkitChatListener implements Listener {
|
||||
|
||||
private final CarbonChatBukkit carbonChat = byInject(CarbonChat.class); // lol this is dumb
|
||||
private final UserManager userManager = this.carbonChat.userManager();
|
||||
private final CarbonChatBukkit carbonChat = byInject(CarbonChat.class); // lol this is dumb
|
||||
private final UserManager userManager = this.carbonChat.userManager();
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerChat(final @NonNull AsyncChatEvent event) {
|
||||
final var player = this.userManager.carbonPlayer(event.getPlayer().getUniqueId());
|
||||
@EventHandler
|
||||
public void onPlayerChat(final @NonNull AsyncChatEvent event) {
|
||||
final var sender = this.userManager.carbonPlayer(event.getPlayer().getUniqueId());
|
||||
|
||||
if (player == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
final var recipients = new ArrayList<CarbonPlayer>();
|
||||
|
||||
for (final Player recipient : event.recipients()) {
|
||||
final var bukkitRecipient = this.userManager.carbonPlayer(recipient.getUniqueId());
|
||||
|
||||
if (bukkitRecipient != null) {
|
||||
recipients.add(bukkitRecipient);
|
||||
}
|
||||
}
|
||||
|
||||
final var chatEvent = new CarbonChatEvent(player, event.message(), recipients,
|
||||
Map.of(key("carbon", "default"), this.carbonChat.vanillaChatRenderer));
|
||||
final var result = this.carbonChat.eventHandler().emit(chatEvent);
|
||||
|
||||
if (result.wasSuccessful()) {
|
||||
// TODO: send to channels
|
||||
|
||||
for (final var recipient : chatEvent.recipients()) {
|
||||
var component = chatEvent.message();
|
||||
|
||||
for (final var entry : chatEvent.renderers().entrySet()) {
|
||||
component = entry.getValue().render(player,
|
||||
recipient, chatEvent.message(), chatEvent.originalMessage());
|
||||
if (sender == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (component != null) {
|
||||
recipient.sendMessage(component);
|
||||
}
|
||||
}
|
||||
}
|
||||
final var recipients = new ArrayList<Audience>();
|
||||
final var channel = sender.selectedChannel();
|
||||
|
||||
// There's no guarantee that recipients is mutable.
|
||||
// If it's ever immutable, we yell at who ever is responsible.
|
||||
event.recipients().clear();
|
||||
}
|
||||
for (final Player bukkitRecipient : event.recipients()) {
|
||||
final var recipient = this.userManager.carbonPlayer(bukkitRecipient.getUniqueId());
|
||||
|
||||
// The naming is a little backwards, ChatChannel#mayReceiveMessages
|
||||
// TODO: Change it at some point
|
||||
if (recipient != null && channel.mayReceiveMessages(recipient)) {
|
||||
recipients.add(recipient);
|
||||
}
|
||||
}
|
||||
|
||||
// console too!
|
||||
recipients.add(Bukkit.getConsoleSender());
|
||||
|
||||
final var chatEvent = new CarbonChatEvent(sender, event.message(), recipients,
|
||||
Map.of(key("carbon", "default"), channel.renderer()));
|
||||
final var result = this.carbonChat.eventHandler().emit(chatEvent);
|
||||
|
||||
if (result.wasSuccessful()) {
|
||||
// TODO: send to channels
|
||||
|
||||
for (final var recipient : chatEvent.recipients()) {
|
||||
var component = chatEvent.message();
|
||||
|
||||
for (final var entry : chatEvent.renderers().entrySet()) {
|
||||
component = entry.getValue().render(sender,
|
||||
recipient, chatEvent.message(), chatEvent.originalMessage());
|
||||
}
|
||||
|
||||
if (component != null) {
|
||||
recipient.sendMessage(component);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// There's no guarantee that recipients is mutable.
|
||||
// If it's ever immutable, we yell at who ever is responsible.
|
||||
event.recipients().clear();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -14,73 +14,74 @@ import java.util.UUID;
|
||||
|
||||
public final class CarbonPlayerBukkit extends CarbonPlayerCommon {
|
||||
|
||||
public CarbonPlayerBukkit(
|
||||
final @NonNull String username,
|
||||
final @NonNull Component displayName,
|
||||
final @NonNull UUID uuid
|
||||
) {
|
||||
super(username, displayName, uuid, Identity.identity(uuid));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void displayName(final @Nullable Component displayName) {
|
||||
super.displayName(displayName);
|
||||
|
||||
final Player player = this.player();
|
||||
|
||||
if (player != null) {
|
||||
player.displayName(displayName);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull Audience audience() {
|
||||
final Player player = this.player();
|
||||
|
||||
if (player == null) {
|
||||
return Audience.empty();
|
||||
public CarbonPlayerBukkit(
|
||||
final @NonNull String username,
|
||||
final @NonNull Component displayName,
|
||||
final @NonNull UUID uuid
|
||||
) {
|
||||
super(username, displayName, uuid, Identity.identity(uuid));
|
||||
}
|
||||
|
||||
return player;
|
||||
}
|
||||
@Override
|
||||
public void displayName(final @Nullable Component displayName) {
|
||||
super.displayName(displayName);
|
||||
|
||||
private @Nullable Player player() {
|
||||
return Bukkit.getPlayer(this.uuid);
|
||||
}
|
||||
final Player player = this.player();
|
||||
|
||||
@Override
|
||||
public @NonNull Component createItemHoverComponent() {
|
||||
final Player player = this.player(); // This is temporary (it's not)
|
||||
|
||||
if (player == null) {
|
||||
return Component.empty();
|
||||
if (player != null) {
|
||||
player.displayName(displayName);
|
||||
player.playerListName(displayName);
|
||||
}
|
||||
}
|
||||
|
||||
final ItemStack itemStack;
|
||||
@Override
|
||||
public @NonNull Audience audience() {
|
||||
final Player player = this.player();
|
||||
|
||||
final ItemStack mainHand = player.getInventory().getItemInMainHand();
|
||||
if (player == null) {
|
||||
return Audience.empty();
|
||||
}
|
||||
|
||||
if (!mainHand.getType().isAir()) {
|
||||
itemStack = mainHand;
|
||||
} else {
|
||||
final ItemStack offHand = player.getInventory().getItemInMainHand();
|
||||
|
||||
if (!offHand.getType().isAir()) {
|
||||
itemStack = offHand;
|
||||
} else {
|
||||
itemStack = null;
|
||||
}
|
||||
return player;
|
||||
}
|
||||
|
||||
if (itemStack == null) {
|
||||
return Component.empty();
|
||||
private @Nullable Player player() {
|
||||
return Bukkit.getPlayer(this.uuid);
|
||||
}
|
||||
|
||||
if (itemStack.getType().isAir()) {
|
||||
return Component.empty();
|
||||
}
|
||||
@Override
|
||||
public @NonNull Component createItemHoverComponent() {
|
||||
final Player player = this.player(); // This is temporary (it's not)
|
||||
|
||||
return itemStack.displayName();
|
||||
}
|
||||
if (player == null) {
|
||||
return Component.empty();
|
||||
}
|
||||
|
||||
final ItemStack itemStack;
|
||||
|
||||
final ItemStack mainHand = player.getInventory().getItemInMainHand();
|
||||
|
||||
if (!mainHand.getType().isAir()) {
|
||||
itemStack = mainHand;
|
||||
} else {
|
||||
final ItemStack offHand = player.getInventory().getItemInMainHand();
|
||||
|
||||
if (!offHand.getType().isAir()) {
|
||||
itemStack = offHand;
|
||||
} else {
|
||||
itemStack = null;
|
||||
}
|
||||
}
|
||||
|
||||
if (itemStack == null) {
|
||||
return Component.empty();
|
||||
}
|
||||
|
||||
if (itemStack.getType().isAir()) {
|
||||
return Component.empty();
|
||||
}
|
||||
|
||||
return itemStack.displayName();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -18,40 +18,40 @@ import java.util.UUID;
|
||||
*/
|
||||
public final class MemoryUserManagerBukkit implements UserManager {
|
||||
|
||||
private final @NonNull Map<UUID, @NonNull CarbonPlayer> users = new HashMap<>();
|
||||
private final @NonNull Map<UUID, @NonNull CarbonPlayer> users = new HashMap<>();
|
||||
|
||||
@Override
|
||||
public @Nullable CarbonPlayer carbonPlayer(final @NonNull UUID uuid) {
|
||||
if (this.users.containsKey(uuid)) {
|
||||
return this.users.get(uuid);
|
||||
@Override
|
||||
public @Nullable CarbonPlayer carbonPlayer(final @NonNull UUID uuid) {
|
||||
if (this.users.containsKey(uuid)) {
|
||||
return this.users.get(uuid);
|
||||
}
|
||||
|
||||
final Player player = Bukkit.getPlayer(uuid);
|
||||
|
||||
if (player == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final CarbonPlayer carbonPlayer = new CarbonPlayerBukkit(player.getName(), player.displayName(), player.getUniqueId());
|
||||
|
||||
this.users.put(uuid, carbonPlayer);
|
||||
|
||||
return carbonPlayer;
|
||||
}
|
||||
|
||||
final Player player = Bukkit.getPlayer(uuid);
|
||||
@Override
|
||||
public @Nullable CarbonPlayer carbonPlayer(final @NonNull String username) {
|
||||
final Player player = Bukkit.getPlayer(username);
|
||||
|
||||
if (player == null) {
|
||||
return null;
|
||||
if (player == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (this.users.containsKey(player.getUniqueId())) {
|
||||
return this.users.get(player.getUniqueId());
|
||||
}
|
||||
|
||||
return new CarbonPlayerBukkit(player.getName(), player.displayName(), player.getUniqueId());
|
||||
}
|
||||
|
||||
final CarbonPlayer carbonPlayer = new CarbonPlayerBukkit(player.getName(), player.displayName(), player.getUniqueId());
|
||||
|
||||
this.users.put(uuid, carbonPlayer);
|
||||
|
||||
return carbonPlayer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable CarbonPlayer carbonPlayer(final @NonNull String username) {
|
||||
final Player player = Bukkit.getPlayer(username);
|
||||
|
||||
if (player == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (this.users.containsKey(player.getUniqueId())) {
|
||||
return this.users.get(player.getUniqueId());
|
||||
}
|
||||
|
||||
return new CarbonPlayerBukkit(player.getName(), player.displayName(), player.getUniqueId());
|
||||
}
|
||||
|
||||
}
|
||||
+67
-67
@@ -20,76 +20,76 @@ import static java.util.Collections.emptyList;
|
||||
|
||||
public final class PlaceholderAPIMiniMessageParser {
|
||||
|
||||
private final MiniMessage miniMessage;
|
||||
private final MiniMessage miniMessage;
|
||||
|
||||
private PlaceholderAPIMiniMessageParser(final @NonNull MiniMessage miniMessage) {
|
||||
this.miniMessage = miniMessage;
|
||||
}
|
||||
|
||||
public @NonNull Component parse(final @NonNull OfflinePlayer player, final @NonNull String input, final @NonNull Collection<Template> templates) {
|
||||
return this.parse(
|
||||
PlaceholderAPI.getPlaceholderPattern(),
|
||||
match -> PlaceholderAPI.setPlaceholders(player, match),
|
||||
input,
|
||||
templates
|
||||
);
|
||||
}
|
||||
|
||||
public @NonNull Component parse(final @NonNull OfflinePlayer player, final @NonNull String input) {
|
||||
return this.parse(player, input, emptyList());
|
||||
}
|
||||
|
||||
public @NonNull Component parseRelational(final @NonNull Player one, final @NonNull Player two, final @NonNull String input, final @NonNull Collection<Template> templates) {
|
||||
return this.parse(
|
||||
PlaceholderAPI.getPlaceholderPattern(),
|
||||
match -> PlaceholderAPI.setPlaceholders(one, PlaceholderAPI.setRelationalPlaceholders(one, two, match)),
|
||||
input,
|
||||
templates
|
||||
);
|
||||
}
|
||||
|
||||
public @NonNull Component parseRelational(final @NonNull Player one, final @NonNull Player two, final @NonNull String input) {
|
||||
return this.parseRelational(one, two, input, emptyList());
|
||||
}
|
||||
|
||||
private @NonNull Component parse(
|
||||
final @NonNull Pattern pattern,
|
||||
final @NonNull UnaryOperator<String> placeholderResolver,
|
||||
final @NonNull String input,
|
||||
final @NonNull Collection<Template> originalTemplates
|
||||
) {
|
||||
final Matcher matcher = pattern.matcher(input);
|
||||
final List<Template> templates = new ArrayList<>(originalTemplates);
|
||||
final StringBuilder builder = new StringBuilder();
|
||||
int id = 0;
|
||||
while (matcher.find()) {
|
||||
final String match = matcher.group();
|
||||
final String replaced = placeholderResolver.apply(match);
|
||||
if (match.equals(replaced) || !containsLegacyColorCodes(replaced)) {
|
||||
matcher.appendReplacement(builder, replaced);
|
||||
} else {
|
||||
final String key = "papi_generated_template_" + id;
|
||||
id++;
|
||||
templates.add(Template.of(key, LegacyComponentSerializer.legacySection().deserialize(replaced)));
|
||||
matcher.appendReplacement(builder, "<" + key + ">");
|
||||
}
|
||||
private PlaceholderAPIMiniMessageParser(final @NonNull MiniMessage miniMessage) {
|
||||
this.miniMessage = miniMessage;
|
||||
}
|
||||
matcher.appendTail(builder);
|
||||
return this.miniMessage.parse(builder.toString(), templates);
|
||||
}
|
||||
|
||||
public static @NonNull PlaceholderAPIMiniMessageParser create(final @NonNull MiniMessage backingInstance) {
|
||||
return new PlaceholderAPIMiniMessageParser(backingInstance);
|
||||
}
|
||||
|
||||
private static boolean containsLegacyColorCodes(final @NonNull String string) {
|
||||
final char[] charArray = string.toCharArray();
|
||||
for (int i = 0; i < charArray.length; i++) {
|
||||
if (charArray[i] == LegacyComponentSerializer.SECTION_CHAR) {
|
||||
return true;
|
||||
}
|
||||
public @NonNull Component parse(final @NonNull OfflinePlayer player, final @NonNull String input, final @NonNull Collection<Template> templates) {
|
||||
return this.parse(
|
||||
PlaceholderAPI.getPlaceholderPattern(),
|
||||
match -> PlaceholderAPI.setPlaceholders(player, match),
|
||||
input,
|
||||
templates
|
||||
);
|
||||
}
|
||||
|
||||
public @NonNull Component parse(final @NonNull OfflinePlayer player, final @NonNull String input) {
|
||||
return this.parse(player, input, emptyList());
|
||||
}
|
||||
|
||||
public @NonNull Component parseRelational(final @NonNull Player one, final @NonNull Player two, final @NonNull String input, final @NonNull Collection<Template> templates) {
|
||||
return this.parse(
|
||||
PlaceholderAPI.getPlaceholderPattern(),
|
||||
match -> PlaceholderAPI.setPlaceholders(one, PlaceholderAPI.setRelationalPlaceholders(one, two, match)),
|
||||
input,
|
||||
templates
|
||||
);
|
||||
}
|
||||
|
||||
public @NonNull Component parseRelational(final @NonNull Player one, final @NonNull Player two, final @NonNull String input) {
|
||||
return this.parseRelational(one, two, input, emptyList());
|
||||
}
|
||||
|
||||
private @NonNull Component parse(
|
||||
final @NonNull Pattern pattern,
|
||||
final @NonNull UnaryOperator<String> placeholderResolver,
|
||||
final @NonNull String input,
|
||||
final @NonNull Collection<Template> originalTemplates
|
||||
) {
|
||||
final Matcher matcher = pattern.matcher(input);
|
||||
final List<Template> templates = new ArrayList<>(originalTemplates);
|
||||
final StringBuilder builder = new StringBuilder();
|
||||
int id = 0;
|
||||
while (matcher.find()) {
|
||||
final String match = matcher.group();
|
||||
final String replaced = placeholderResolver.apply(match);
|
||||
if (match.equals(replaced) || !containsLegacyColorCodes(replaced)) {
|
||||
matcher.appendReplacement(builder, replaced);
|
||||
} else {
|
||||
final String key = "papi_generated_template_" + id;
|
||||
id++;
|
||||
templates.add(Template.of(key, LegacyComponentSerializer.legacySection().deserialize(replaced)));
|
||||
matcher.appendReplacement(builder, "<" + key + ">");
|
||||
}
|
||||
}
|
||||
matcher.appendTail(builder);
|
||||
return this.miniMessage.parse(builder.toString(), templates);
|
||||
}
|
||||
|
||||
public static @NonNull PlaceholderAPIMiniMessageParser create(final @NonNull MiniMessage backingInstance) {
|
||||
return new PlaceholderAPIMiniMessageParser(backingInstance);
|
||||
}
|
||||
|
||||
private static boolean containsLegacyColorCodes(final @NonNull String string) {
|
||||
final char[] charArray = string.toCharArray();
|
||||
for (int i = 0; i < charArray.length; i++) {
|
||||
if (charArray[i] == LegacyComponentSerializer.SECTION_CHAR) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user