Add permission to allow PAPI placeholders in messages

Fix chat placeholders not being properly used
This commit is contained in:
Draycia committed 2023-07-05 22:45:11 -07:00
1 parent f459d47bdb
commit 83e5439de2
9 files changed
+199 -68

No files matched your search

@@ -39,6 +39,7 @@ import net.draycia.carbon.common.messaging.MessagingManager;
import net.draycia.carbon.common.users.ProfileCache;
import net.draycia.carbon.common.users.ProfileResolver;
import net.draycia.carbon.paper.hooks.DSRVChatHook;
import net.draycia.carbon.paper.hooks.PAPIChatHook;
import net.draycia.carbon.paper.listeners.DiscordMessageListener;
import net.draycia.carbon.paper.listeners.PaperChatListener;
import net.draycia.carbon.paper.listeners.PaperPlayerJoinListener;
@@ -124,9 +125,12 @@ public final class CarbonChatPaper extends CarbonChatInternal<CarbonPlayerPaper>
}
if (Bukkit.getPluginManager().isPluginEnabled("DiscordSRV")) {
this.logger().info("DiscordSRV found! Enabling hook.");
DiscordSRV.getPlugin().getPluginHooks().add(this.injector().getInstance(DSRVChatHook.class));
}
if (papiLoaded()) {
this.injector().getInstance(PAPIChatHook.class);
}
}
void onDisable() {
@@ -34,12 +34,13 @@ import net.draycia.carbon.api.event.CarbonEventHandler;
import net.draycia.carbon.api.event.events.CarbonChatEvent;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.common.channels.CarbonChannelRegistry;
import net.draycia.carbon.common.channels.ConfigChatChannel;
import net.draycia.carbon.common.users.WrappedCarbonPlayer;
import net.draycia.carbon.common.util.ChannelUtils;
import net.draycia.carbon.paper.users.CarbonPlayerPaper;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.logging.log4j.Logger;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
@@ -54,10 +55,12 @@ public final class DSRVChatHook implements ChatHook {
private DSRVChatHook(
final CarbonEventHandler events,
final CarbonChannelRegistry channelRegistry,
final JavaPlugin plugin
final JavaPlugin plugin,
final Logger logger
) {
this.channelRegistry = channelRegistry;
this.plugin = plugin;
logger.info("DiscordSRV found! Enabling hook.");
final Cache<ImmutablePair<CarbonPlayer, ChatChannel>, Component> awaitingEvent = Caffeine.newBuilder()
.expireAfterWrite(Duration.ofMillis(25))
@@ -75,12 +78,18 @@ public final class DSRVChatHook implements ChatHook {
}
final String messageContents = PlainTextComponentSerializer.plainText().serialize(messageComponent);
final Component parsedMessage = ConfigChatChannel.parseMessageTags(carbonPlayer, messageContents);
final Component eventMessage;
if (carbonPlayer instanceof WrappedCarbonPlayer wrapped) {
eventMessage = wrapped.parseMessageTags(messageContents);
} else {
eventMessage = WrappedCarbonPlayer.parseMessageTags(messageContents, carbonPlayer::hasPermission);
}
DiscordSRV.debug(Debug.MINECRAFT_TO_DISCORD, "Received a CarbonChatEvent (player: " + carbonPlayer.username() + ")");
final @Nullable Player player = ((CarbonPlayerPaper) carbonPlayer).bukkitPlayer();
final String message = PlainTextComponentSerializer.plainText().serialize(parsedMessage);
final String message = PlainTextComponentSerializer.plainText().serialize(eventMessage);
if (player != null) {
DiscordSRV.getPlugin().processChatMessage(player, message, chatChannel.commandName(), event.cancelled());
@@ -0,0 +1,57 @@
/*
* CarbonChat
*
* Copyright (c) 2023 Josua Parks (Vicarious)
* Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.draycia.carbon.paper.hooks;
import com.google.inject.Inject;
import me.clip.placeholderapi.PlaceholderAPI;
import net.draycia.carbon.api.event.CarbonEventHandler;
import net.draycia.carbon.api.util.ColorUtils;
import net.draycia.carbon.common.event.events.CarbonEarlyChatEvent;
import net.draycia.carbon.common.listeners.Listener;
import net.draycia.carbon.paper.CarbonChatPaper;
import net.draycia.carbon.paper.users.CarbonPlayerPaper;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.framework.qual.DefaultQualifier;
@DefaultQualifier(NonNull.class)
public class PAPIChatHook implements Listener {
@Inject
public PAPIChatHook(final CarbonEventHandler events) {
events.subscribe(CarbonEarlyChatEvent.class, 0, false, event -> {
if (!CarbonChatPaper.papiLoaded()) {
return;
}
if (!event.sender().hasPermission("carbon.chatplaceholders")) {
return;
}
if (!(event.sender() instanceof CarbonPlayerPaper playerPaper)) {
return;
}
final String papiParsed = PlaceholderAPI.setPlaceholders(playerPaper.bukkitPlayer(), event.message());
event.message(ColorUtils.legacyToMiniMessage(papiParsed));
});
}
}