mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-01 14:38:48 +00:00
feature - custom placeholders
This commit is contained in:
1 parent
305803199b
commit
4e738f94c6
5 files changed
+61
-8
No files matched your search
@@ -180,7 +180,7 @@ public final class CarbonChatBukkit extends JavaPlugin implements CarbonChat {
|
||||
|
||||
@Override
|
||||
public IMessageRenderer<SourcedAudience, String, Component, Component> messageRenderer() {
|
||||
return new BukkitMessageRenderer();
|
||||
return this.injector.getInstance(BukkitMessageRenderer.class);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package net.draycia.carbon.bukkit.util;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.ArrayList;
|
||||
@@ -7,6 +8,7 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import net.draycia.carbon.api.users.CarbonPlayer;
|
||||
import net.draycia.carbon.api.util.SourcedAudience;
|
||||
import net.draycia.carbon.common.config.PrimaryConfig;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import net.kyori.adventure.text.minimessage.Template;
|
||||
@@ -20,6 +22,12 @@ import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
public class BukkitMessageRenderer implements IMessageRenderer<SourcedAudience, String, Component, Component> {
|
||||
|
||||
private final PlaceholderAPIMiniMessageParser parser = PlaceholderAPIMiniMessageParser.create(MiniMessage.miniMessage());
|
||||
private final PrimaryConfig config;
|
||||
|
||||
@Inject
|
||||
public BukkitMessageRenderer(final PrimaryConfig config) {
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Component render(
|
||||
@@ -35,16 +43,25 @@ public class BukkitMessageRenderer implements IMessageRenderer<SourcedAudience,
|
||||
templates.add(Template.template(entry.getKey(), entry.getValue()));
|
||||
}
|
||||
|
||||
// https://github.com/KyoriPowered/adventure-text-minimessage/issues/131
|
||||
// TLDR: 25/10/21, tags in templates aren't parsed. we want them parsed.
|
||||
String placeholderResolvedMessage = intermediateMessage;
|
||||
|
||||
for (final var entry : this.config.customPlaceholders().entrySet()) {
|
||||
placeholderResolvedMessage = placeholderResolvedMessage.replace("<" + entry.getKey() + ">",
|
||||
entry.getValue());
|
||||
}
|
||||
|
||||
if (receiver.sender() instanceof CarbonPlayer sender && sender.online()) {
|
||||
if (receiver.recipient() instanceof CarbonPlayer recipient && recipient.online()) {
|
||||
return this.parser.parseRelational(Bukkit.getPlayer(sender.uuid()),
|
||||
Bukkit.getPlayer(recipient.uuid()), intermediateMessage, templates);
|
||||
Bukkit.getPlayer(recipient.uuid()), placeholderResolvedMessage, templates);
|
||||
} else {
|
||||
return this.parser.parse(Bukkit.getPlayer(sender.uuid()), intermediateMessage, templates);
|
||||
return this.parser.parse(Bukkit.getPlayer(sender.uuid()), placeholderResolvedMessage, templates);
|
||||
}
|
||||
}
|
||||
|
||||
return MiniMessage.miniMessage().deserialize(intermediateMessage, TemplateResolver.templates(templates));
|
||||
return MiniMessage.miniMessage().deserialize(placeholderResolvedMessage, TemplateResolver.templates(templates));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -43,7 +43,7 @@ public final class ConfigChatChannel implements ChatChannel {
|
||||
|
||||
@Comment("""
|
||||
The channel's key, used to track the channel.
|
||||
The key can stay "carbon".
|
||||
You only need to change the second part of the key. "global" by default.
|
||||
The value is what's used in commands, this is probably what you want to change.
|
||||
""")
|
||||
private Key key = Key.key("carbon", "basic");
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package net.draycia.carbon.common.config;
|
||||
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import net.kyori.adventure.key.Key;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
@@ -14,7 +15,10 @@ public class PrimaryConfig {
|
||||
@Comment("The default locale for plugin messages.")
|
||||
private Locale defaultLocale = Locale.US;
|
||||
|
||||
@Comment("The default channel that new players will be in when they join.")
|
||||
@Comment("""
|
||||
The default channel that new players will be in when they join.
|
||||
If the channel is not found or the player cannot use the channel, they will speak in basic non-channel chat.
|
||||
""")
|
||||
private Key defaultChannel = Key.key("carbon", "basic");
|
||||
|
||||
@Comment("The service that will be used to store and load player information.")
|
||||
@@ -26,6 +30,15 @@ public class PrimaryConfig {
|
||||
@Comment("Various ClearChat command settings.")
|
||||
private ClearChatSettings clearChatSettings = new ClearChatSettings();
|
||||
|
||||
@Comment("""
|
||||
Plugin-wide custom placeholders.
|
||||
These will be parsed in all messages rendered and sent by Carbon.
|
||||
This includes chat, command feedback, and others.
|
||||
Make sure to close your tags so they do not bleed into other formats.
|
||||
Only a single pass is done so custom placeholders will not work within each other.
|
||||
""")
|
||||
private Map<String, String> customPlaceholders = Map.of();
|
||||
|
||||
public Locale defaultLocale() {
|
||||
return this.defaultLocale;
|
||||
}
|
||||
@@ -46,6 +59,10 @@ public class PrimaryConfig {
|
||||
return this.clearChatSettings;
|
||||
}
|
||||
|
||||
public Map<String, String> customPlaceholders() {
|
||||
return this.customPlaceholders;
|
||||
}
|
||||
|
||||
public enum StorageType {
|
||||
JSON,
|
||||
MYSQL,
|
||||
|
||||
@@ -1,14 +1,17 @@
|
||||
package net.draycia.carbon.common.messages;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import net.draycia.carbon.common.config.PrimaryConfig;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import net.kyori.adventure.text.minimessage.Template;
|
||||
import net.kyori.adventure.text.minimessage.template.TemplateResolver;
|
||||
import net.kyori.moonshine.message.IMessageRenderer;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
@@ -16,6 +19,13 @@ import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public class CarbonMessageRenderer implements IMessageRenderer<Audience, String, Component, Component> {
|
||||
|
||||
private final PrimaryConfig config;
|
||||
|
||||
@Inject
|
||||
public CarbonMessageRenderer(final PrimaryConfig config) {
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Component render(
|
||||
final Audience receiver,
|
||||
@@ -27,10 +37,19 @@ public class CarbonMessageRenderer implements IMessageRenderer<Audience, String,
|
||||
final List<Template> templates = new ArrayList<>();
|
||||
|
||||
for (final var entry : resolvedPlaceholders.entrySet()) {
|
||||
templates.add(Template.of(entry.getKey(), entry.getValue()));
|
||||
templates.add(Template.template(entry.getKey(), entry.getValue()));
|
||||
}
|
||||
|
||||
// https://github.com/KyoriPowered/adventure-text-minimessage/issues/131
|
||||
// TLDR: 25/10/21, tags in templates aren't parsed. we want them parsed.
|
||||
String placeholderResolvedMessage = intermediateMessage;
|
||||
|
||||
for (final var entry : this.config.customPlaceholders().entrySet()) {
|
||||
placeholderResolvedMessage = placeholderResolvedMessage.replace("<" + entry.getKey() + ">",
|
||||
entry.getValue());
|
||||
}
|
||||
|
||||
return MiniMessage.miniMessage().parse(intermediateMessage, templates);
|
||||
return MiniMessage.miniMessage().deserialize(placeholderResolvedMessage, TemplateResolver.templates(templates));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user