Adjust item link handler and move custom placeholders

This commit is contained in:
Draycia committed 2020-10-21 00:48:29 -07:00
1 parent 2eff673caa
commit 3314a51df8
4 files changed
+27 -22

No files matched your search

@@ -11,9 +11,7 @@ import org.spongepowered.configurate.objectmapping.meta.Setting;
import org.spongepowered.configurate.serialize.SerializationException; import org.spongepowered.configurate.serialize.SerializationException;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ConfigSerializable @ConfigSerializable
@@ -74,16 +72,6 @@ public final class CarbonSettings {
@Comment("The credentials necessary to connect to your redis server") @Comment("The credentials necessary to connect to your redis server")
private @NonNull RedisCredentials redisCredentials = new RedisCredentials(); private @NonNull RedisCredentials redisCredentials = new RedisCredentials();
@Setting
@Comment("Used for message formats - custom tags that are replaced in descending order" +
"\nFor example, in the default config, <group> wil be replaced with <green>%vault_group%")
private @NonNull Map<@Nullable String, @NonNull String> customPlaceholders = new HashMap<String, String>() {
{
this.put("prefix", "<gray>[<group><gray>]");
this.put("group", "<green>%vault_group%");
}
};
@Setting @Setting
@Comment("Plays a sound and highlights the message when someone types your name") @Comment("Plays a sound and highlights the message when someone types your name")
private @NonNull ChannelPings channelPings = new ChannelPings(); private @NonNull ChannelPings channelPings = new ChannelPings();
@@ -137,10 +125,6 @@ public final class CarbonSettings {
return this.redisCredentials; return this.redisCredentials;
} }
public Map<String, String> customPlaceholders() {
return this.customPlaceholders;
}
public @Nullable String channelOnJoin() { public @Nullable String channelOnJoin() {
return this.channelOnJoin; return this.channelOnJoin;
} }
@@ -1,5 +1,7 @@
package net.draycia.carbon.api.config; package net.draycia.carbon.api.config;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.configurate.CommentedConfigurationNode; import org.spongepowered.configurate.CommentedConfigurationNode;
import org.spongepowered.configurate.objectmapping.ConfigSerializable; import org.spongepowered.configurate.objectmapping.ConfigSerializable;
import org.spongepowered.configurate.objectmapping.ObjectMapper; import org.spongepowered.configurate.objectmapping.ObjectMapper;
@@ -8,6 +10,7 @@ import org.spongepowered.configurate.objectmapping.meta.Setting;
import org.spongepowered.configurate.serialize.SerializationException; import org.spongepowered.configurate.serialize.SerializationException;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ConfigSerializable @ConfigSerializable
@@ -40,6 +43,16 @@ public final class ChannelSettings {
private Map<String, ChannelOptions> channelOptions = private Map<String, ChannelOptions> channelOptions =
Collections.singletonMap("global", ChannelOptions.defaultChannel()); Collections.singletonMap("global", ChannelOptions.defaultChannel());
@Setting
@Comment("Used for message formats - custom tags that are replaced in descending order" +
"\nFor example, in the default config, <group> wil be replaced with <green>%vault_group%")
private @NonNull Map<@Nullable String, @NonNull String> customPlaceholders = new HashMap<String, String>() {
{
this.put("prefix", "<gray>[<group><gray>]");
this.put("group", "<green>%vault_group%");
}
};
public SharedChannelOptions defaultChannelOptions() { public SharedChannelOptions defaultChannelOptions() {
return this.sharedChannelOptions; return this.sharedChannelOptions;
} }
@@ -48,6 +61,10 @@ public final class ChannelSettings {
return this.channelOptions; return this.channelOptions;
} }
public Map<String, String> customPlaceholders() {
return this.customPlaceholders;
}
public ChannelOptions channelOptions(final String name) { public ChannelOptions channelOptions(final String name) {
return this.channelOptions.get(name); return this.channelOptions.get(name);
} }
@@ -1,5 +1,6 @@
package net.draycia.carbon.bukkit.listeners.events; package net.draycia.carbon.bukkit.listeners.events;
import net.draycia.carbon.api.events.ChatFormatEvent;
import net.draycia.carbon.bukkit.util.CarbonUtils; import net.draycia.carbon.bukkit.util.CarbonUtils;
import net.draycia.carbon.api.events.misc.CarbonEvents; import net.draycia.carbon.api.events.misc.CarbonEvents;
import net.draycia.carbon.api.events.ChatComponentEvent; import net.draycia.carbon.api.events.ChatComponentEvent;
@@ -33,18 +34,21 @@ public class ItemLinkHandler {
} }
for (final Pattern pattern : event.channel().itemLinkPatterns()) { for (final Pattern pattern : event.channel().itemLinkPatterns()) {
final String patternContent = pattern.toString().replace("\\Q", "") if (pattern.matcher(event.originalMessage()).find()) {
.replace("\\E", "");
if (event.originalMessage().contains(patternContent)) {
final TextComponent component = (TextComponent) event.component() final TextComponent component = (TextComponent) event.component()
.replaceFirstText(pattern, input -> itemComponent); .replaceFirstText(pattern, input -> itemComponent);
event.component(component); event.component(component);
break; break;
} }
} }
}); });
CarbonEvents.register(ChatFormatEvent.class, PostOrders.EARLY, false, event -> {
for (final Pattern pattern : event.channel().itemLinkPatterns()) {
event.message(pattern.matcher(event.message()).replaceAll("<white>$1</white>"));
}
});
} }
} }
@@ -14,7 +14,7 @@ public class CustomPlaceholderHandler {
final CarbonChat carbonChat = CarbonChatProvider.carbonChat(); final CarbonChat carbonChat = CarbonChatProvider.carbonChat();
CarbonEvents.register(PreChatFormatEvent.class, PostOrders.FIRST, false, event -> { CarbonEvents.register(PreChatFormatEvent.class, PostOrders.FIRST, false, event -> {
for (final Map.Entry<String, String> entry : carbonChat.carbonSettings().customPlaceholders().entrySet()) { for (final Map.Entry<String, String> entry : carbonChat.channelSettings().customPlaceholders().entrySet()) {
event.format(event.format().replace("<" + entry.getKey() + ">", entry.getValue())); event.format(event.format().replace("<" + entry.getKey() + ">", entry.getValue()));
} }
}); });