Skip to content

Commit

Permalink
Merge pull request #236 from 4drian3d/feat/miniplaceholders
Browse files Browse the repository at this point in the history
Reimplement Velocity Module and added MiniPlaceholders support
  • Loading branch information
Draycia authored Apr 15, 2023
2 parents 3dbec83 + 2fe70b6 commit 8c76421
Show file tree
Hide file tree
Showing 27 changed files with 449 additions and 526 deletions.
18 changes: 17 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ hangarPublish.publications.register("plugin") {
platformVersions.add("1.19.4")
dependencies {
url("LuckPerms", "https://luckperms.net/")
url("EssentialsDiscord", "https://essentialsx.net/") {
hangar("EssentialsX", "Essentials") {
required.set(false)
}
url("DiscordSRV", "https://www.spigotmc.org/resources/discordsrv.18494/") {
Expand All @@ -31,6 +31,22 @@ hangarPublish.publications.register("plugin") {
url("PlaceholderAPI", "https://www.spigotmc.org/resources/placeholderapi.6245/") {
required.set(false)
}
hangar("MiniPlaceholders", "MiniPlaceholders") {
required.set(false)
}
}
}
platforms.register(Platforms.VELOCITY) {
jar.set(project(":carbonchat-velocity").the<CarbonPlatformExtension>().jarTask.flatMap { it.archiveFile })
platformVersions.add("3.2")
dependencies {
url("LuckPerms", "https://luckperms.net/")
hangar("MiniPlaceholders", "MiniPlaceholders") {
required.set(false)
}
hangar("4drian3d", "UnSignedVelocity") {
required.set(false)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,10 @@ public <T extends Audience> IMessageRenderer<T, String, Component, Component> me
return this.messagingManager.get().packetService();
}

public boolean isProxy() {
return false;
}

public Injector injector() {
return this.injector;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class ConfigChannelMessageSource implements IMessageSource<SourcedAudienc
The "console" format is what's shown to console.
The "discord" format is what's shown to supported discord integrations.
If PlaceholderAPI is installed, PAPI placeholders (with %) are supported.
If MiniPlaceholders is installed, its placeholders (with <>) are supported.
The keys are group names, the values are chat formats (MiniMessage).
For example:
basic {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import net.draycia.carbon.api.CarbonChat;
import net.draycia.carbon.api.CarbonChatProvider;
import net.draycia.carbon.api.events.CarbonShutdownEvent;
import net.draycia.carbon.common.CarbonChatInternal;
import net.draycia.carbon.common.config.ConfigFactory;
import net.draycia.carbon.common.config.MessagingSettings;
import net.draycia.carbon.common.listeners.PingHandler;
Expand Down Expand Up @@ -83,7 +84,9 @@ public MessagingManager(
final PingHandler pingHandler
) {
if (!configFactory.primaryConfig().messagingSettings().enabled()) {
logger.info("Messaging services disabled in config. Cross-server will not work without this!");
if (!((CarbonChatInternal<?>) carbonChat).isProxy()) {
logger.info("Messaging services disabled in config. Cross-server will not work without this!");
}
this.messagingService = EMPTY_MESSAGING_SERVICE;
this.packetService = null;
this.carbonChat = carbonChat;
Expand Down
13 changes: 13 additions & 0 deletions common/src/main/java/net/draycia/carbon/common/util/Strings.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,26 @@
*/
package net.draycia.carbon.common.util;

import com.google.common.base.Suppliers;
import java.util.function.Supplier;
import java.util.regex.Pattern;
import net.kyori.adventure.text.TextReplacementConfig;
import net.kyori.adventure.text.event.ClickEvent;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.framework.qual.DefaultQualifier;

@DefaultQualifier(NonNull.class)
public final class Strings {

private static final Pattern DEFAULT_URL_PATTERN = Pattern.compile("(?:(https?)://)?([-\\w_.]+\\.\\w{2,})(/\\S*)?");
public static final Supplier<TextReplacementConfig> URL_REPLACEMENT_CONFIG = Suppliers.memoize(
() -> TextReplacementConfig.builder()
.match(DEFAULT_URL_PATTERN)
.replacement(builder -> builder.clickEvent(ClickEvent.clickEvent(ClickEvent.Action.OPEN_URL, builder.content())))
.build()
);

private Strings() {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.Tag;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import net.kyori.moonshine.message.IMessageRenderer;
import org.checkerframework.checker.nullness.qual.NonNull;
Expand Down Expand Up @@ -57,16 +58,11 @@ public Component render(
tagResolver.tag(entry.getKey(), Tag.inserting(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;
this.configFactory.primaryConfig().customPlaceholders().forEach(
(key, value) -> tagResolver.resolver(Placeholder.parsed(key, value))
);

for (final var entry : this.configFactory.primaryConfig().customPlaceholders().entrySet()) {
placeholderResolvedMessage = placeholderResolvedMessage.replace("<" + entry.getKey() + ">",
entry.getValue());
}

return MiniMessage.miniMessage().deserialize(placeholderResolvedMessage, tagResolver.build());
return MiniMessage.miniMessage().deserialize(intermediateMessage, tagResolver.build());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.util.Optional;
import java.util.UUID;
import java.util.function.Consumer;
import java.util.regex.Pattern;
import net.draycia.carbon.api.channels.ChannelRegistry;
import net.draycia.carbon.api.events.CarbonChatEvent;
import net.draycia.carbon.api.users.CarbonPlayer;
Expand All @@ -33,13 +32,13 @@
import net.kyori.adventure.identity.Identity;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextReplacementConfig;
import net.kyori.adventure.text.event.ClickEvent;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.framework.qual.DefaultQualifier;

import static java.util.Objects.requireNonNullElse;
import static net.draycia.carbon.api.util.KeyedRenderer.keyedRenderer;
import static net.draycia.carbon.common.util.Strings.URL_REPLACEMENT_CONFIG;
import static net.kyori.adventure.key.Key.key;
import static net.kyori.adventure.text.Component.empty;
import static net.kyori.adventure.text.Component.text;
Expand All @@ -50,8 +49,6 @@ public class FabricChatListener implements Consumer<ChatCallback.Chat> {
private final CarbonChatFabric carbonChatFabric;
private final ChannelRegistry channelRegistry;

private static final Pattern DEFAULT_URL_PATTERN = Pattern.compile("(?:(https?)://)?([-\\w_.]+\\.\\w{2,})(/\\S*)?");

public FabricChatListener(final CarbonChatFabric carbonChatFabric, final ChannelRegistry channelRegistry) {
this.carbonChatFabric = carbonChatFabric;
this.channelRegistry = channelRegistry;
Expand All @@ -66,10 +63,7 @@ public void accept(final ChatCallback.Chat chat) {
Component eventMessage = text(chat.message());

if (sender.hasPermission("carbon.chatlinks")) {
eventMessage = eventMessage.replaceText(TextReplacementConfig.builder()
.match(DEFAULT_URL_PATTERN)
.replacement(builder -> builder.clickEvent(ClickEvent.clickEvent(ClickEvent.Action.OPEN_URL, builder.content())))
.build());
eventMessage = eventMessage.replaceText(URL_REPLACEMENT_CONFIG.get());
}

for (final var chatChannel : this.channelRegistry) {
Expand Down
14 changes: 13 additions & 1 deletion gradle/libs.versions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ plugins:
net.kyori.blossom: 1.3.0
org.spongepowered.gradle.plugin: 2.1.1
xyz.jpenilla.run-paper: 2.0.1
xyz.jpenilla.run-velocity: 2.0.1
io.papermc.hangar-publish-plugin: 0.0.5

versions:
Expand All @@ -30,14 +31,16 @@ versions:
kyoriMoonshine: 2.0.4
log4j: 2.17.2
guice: 5.1.0
velocityApi: 3.1.1
velocityApi: 3.2.0-SNAPSHOT
geantyref: 1.3.11
fabricMinecraft: 1.19.4
fabricLoader: 0.14.17
adventurePlatformFabric: 5.8.0
luckPermsApi: 5.3
essentialsx: 2.19.3
discordsrv: 1.26.0
placeholderapi: 2.10.9
miniplaceholders: 2.0.0
jdbi: 3.34.0
hikari: 5.0.1
mysql: 8.0.31
Expand Down Expand Up @@ -328,4 +331,13 @@ dependencies:
name: discordsrv
version: { ref: discordsrv }

placeholderapi:
group: me.clip
name: placeholderapi
version: { ref: placeholderapi }
miniplaceholders:
group: io.github.miniplaceholders
name: miniplaceholders-api
version: { ref: miniplaceholders }

bundles:
4 changes: 3 additions & 1 deletion paper/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ dependencies {
implementation(libs.bstatsBukkit)

// Plugins
compileOnly("me.clip:placeholderapi:2.10.9") // TODO: move this to libs.versions.yml
compileOnly(libs.placeholderapi)
compileOnly(libs.miniplaceholders)
compileOnly(libs.essentialsXDiscord)
compileOnly(libs.discordsrv)
}
Expand Down Expand Up @@ -54,6 +55,7 @@ paper {
dependencies += PaperPluginDescription.Dependency("PlaceholderAPI", false)
dependencies += PaperPluginDescription.Dependency("EssentialsDiscord", false)
dependencies += PaperPluginDescription.Dependency("DiscordSRV", false)
dependencies += PaperPluginDescription.Dependency("MiniPlaceholders", false)
website = GITHUB_REPO_URL
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,8 @@ public static boolean papiLoaded() {
return Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI");
}

public static boolean miniPlaceholdersLoaded() {
return Bukkit.getPluginManager().isPluginEnabled("MiniPlaceholders");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import io.papermc.paper.event.player.AsyncChatEvent;
import java.util.ArrayList;
import java.util.Map;
import java.util.regex.Pattern;
import net.draycia.carbon.api.CarbonChat;
import net.draycia.carbon.api.channels.ChannelRegistry;
import net.draycia.carbon.api.events.CarbonChatEvent;
Expand All @@ -37,7 +36,6 @@
import net.kyori.adventure.identity.Identity;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextReplacementConfig;
import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import net.kyori.event.EventSubscriber;
import ninja.egg82.messenger.services.PacketService;
Expand All @@ -50,6 +48,7 @@

import static java.util.Objects.requireNonNullElse;
import static net.draycia.carbon.api.util.KeyedRenderer.keyedRenderer;
import static net.draycia.carbon.common.util.Strings.URL_REPLACEMENT_CONFIG;
import static net.kyori.adventure.key.Key.key;
import static net.kyori.adventure.text.Component.text;

Expand All @@ -60,8 +59,6 @@ public final class PaperChatListener implements Listener {
private final ChannelRegistry registry;
private final CarbonMessages carbonMessages;

private static final Pattern DEFAULT_URL_PATTERN = Pattern.compile("(?:(https?)://)?([-\\w_.]+\\.\\w{2,})(/\\S*)?");

@Inject
public PaperChatListener(final CarbonChat carbonChat, final ChannelRegistry registry, final CarbonMessages carbonMessages) {
this.carbonChat = (CarbonChatPaper) carbonChat;
Expand All @@ -82,10 +79,7 @@ public void onPaperChat(final @NonNull AsyncChatEvent event) {
Component eventMessage = ConfigChatChannel.parseMessageTags(sender, messageContents);

if (sender.hasPermission("carbon.chatlinks")) {
eventMessage = eventMessage.replaceText(TextReplacementConfig.builder()
.match(DEFAULT_URL_PATTERN)
.replacement(builder -> builder.clickEvent(ClickEvent.clickEvent(ClickEvent.Action.OPEN_URL, builder.content())))
.build());
eventMessage = eventMessage.replaceText(URL_REPLACEMENT_CONFIG.get());
}

for (final var chatChannel : this.registry) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import com.google.common.base.Suppliers;
import com.google.inject.Inject;
import io.github.miniplaceholders.api.MiniPlaceholders;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.Map;
Expand All @@ -33,6 +34,7 @@
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.Tag;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import net.kyori.moonshine.message.IMessageRenderer;
import org.bukkit.Bukkit;
Expand All @@ -50,6 +52,8 @@ public class PaperMessageRenderer<T extends Audience> implements IMessageRendere
return null;
});

private final Supplier<Boolean> miniPlaceholdersAvailable = Suppliers.memoize(CarbonChatPaper::miniPlaceholdersLoaded);

private final MiniMessage miniMessage;
private final ConfigFactory configFactory;

Expand All @@ -73,33 +77,44 @@ public Component render(
tagResolver.tag(entry.getKey(), Tag.inserting(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;
this.configFactory.primaryConfig().customPlaceholders().forEach(
(key, value) -> tagResolver.resolver(Placeholder.parsed(key, value))
);

if (this.miniPlaceholdersAvailable.get()) {
tagResolver.resolver(MiniPlaceholders.getGlobalPlaceholders());
}

if (!(receiver instanceof SourcedAudience sourced)) {
return this.miniMessage.deserialize(intermediateMessage, tagResolver.build());
}

for (final var entry : this.configFactory.primaryConfig().customPlaceholders().entrySet()) {
placeholderResolvedMessage = placeholderResolvedMessage.replace("<" + entry.getKey() + ">",
entry.getValue());
if (!(sourced.sender() instanceof CarbonPlayer sender) || sender.online()) {
return this.miniMessage.deserialize(intermediateMessage, tagResolver.build());
}

final Component message;

if (receiver instanceof SourcedAudience sourced && this.parser.get() != null) {
if (sourced.sender() instanceof CarbonPlayer sender && sender.online()) {
if (sourced.recipient() instanceof CarbonPlayer recipient && recipient.online()) {
message = this.parser.get().parseRelational(Bukkit.getPlayer(sender.uuid()),
Bukkit.getPlayer(recipient.uuid()), placeholderResolvedMessage, tagResolver.build());
} else {
message = this.parser.get().parse(Bukkit.getPlayer(sender.uuid()), placeholderResolvedMessage, tagResolver.build());
}
} else {
message = this.miniMessage.deserialize(placeholderResolvedMessage, tagResolver.build());
if (!(sourced.recipient() instanceof CarbonPlayer recipient && recipient.online())) {
if (this.miniPlaceholdersAvailable.get()) {
tagResolver.resolver(MiniPlaceholders.getAudiencePlaceholders(Bukkit.getPlayer(sender.uuid())));
}
} else {
message = this.miniMessage.deserialize(placeholderResolvedMessage, tagResolver.build());
if (this.parser.get() != null) {
return this.parser.get().parse(Bukkit.getPlayer(sender.uuid()), intermediateMessage, tagResolver.build());
}
return this.miniMessage.deserialize(intermediateMessage, tagResolver.build());
}

if (this.miniPlaceholdersAvailable.get()) {
tagResolver.resolver(MiniPlaceholders.getRelationalPlaceholders(
Bukkit.getPlayer(sender.uuid()),
Bukkit.getPlayer(recipient.uuid())
));
}
if (this.parser.get() != null) {
return this.parser.get().parseRelational(Bukkit.getPlayer(sender.uuid()),
Bukkit.getPlayer(recipient.uuid()), intermediateMessage, tagResolver.build());
}

return message;
return this.miniMessage.deserialize(intermediateMessage, tagResolver.build());
}

}
2 changes: 1 addition & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ sequenceOf(
"paper",
// "sponge", // TODO API 10
"fabric",
// "velocity"
"velocity"
).forEach {
include("carbonchat-$it")
project(":carbonchat-$it").projectDir = file(it)
Expand Down
Loading

0 comments on commit 8c76421

Please sign in to comment.