Add support for DSRV

This commit is contained in:
Draycia committed 2022-04-16 13:38:50 -07:00
1 parent 4b09232e57
commit f13e7732db
12 files changed
+396 -3

No files matched your search

@@ -40,6 +40,8 @@ import org.checkerframework.framework.qual.DefaultQualifier;
@DefaultQualifier(NonNull.class)
public interface CarbonPlayer extends Audience, Identified {
CarbonPlayer EMPTY = new EmptyCarbonPlayer();
/**
* Returns the appropriate {@link Component} to represent the player's name.
*
@@ -0,0 +1,187 @@
/*
* CarbonChat
*
* Copyright (c) 2021 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.api.users;
import java.util.List;
import java.util.Locale;
import java.util.UUID;
import net.draycia.carbon.api.channels.ChatChannel;
import net.draycia.carbon.api.util.InventorySlot;
import net.kyori.adventure.identity.Identity;
import net.kyori.adventure.text.Component;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.framework.qual.DefaultQualifier;
import org.jetbrains.annotations.NotNull;
@DefaultQualifier(NonNull.class)
class EmptyCarbonPlayer implements CarbonPlayer {
@Override
public String username() {
return "";
}
@Override
public boolean hasCustomDisplayName() {
return false;
}
@Override
public @Nullable Component displayName() {
return null;
}
@Override
public void displayName(final @Nullable Component displayName) {
}
@Override
public UUID uuid() {
return new UUID(0, 0);
}
@Override
public @Nullable Component createItemHoverComponent(final InventorySlot slot) {
return null;
}
@Override
public @Nullable Locale locale() {
return null;
}
@Override
public @Nullable ChatChannel selectedChannel() {
return null;
}
@Override
public void selectedChannel(final @Nullable ChatChannel chatChannel) {
}
@Override
public boolean hasPermission(final String permission) {
return false;
}
@Override
public String primaryGroup() {
return "";
}
@Override
public List<String> groups() {
return List.of();
}
@Override
public boolean muted() {
return false;
}
@Override
public void muted(final boolean muted) {
}
@Override
public boolean ignoring(final CarbonPlayer sender) {
return false;
}
@Override
public void ignoring(final CarbonPlayer player, final boolean nowIgnoring) {
}
@Override
public boolean deafened() {
return false;
}
@Override
public void deafened(final boolean deafened) {
}
@Override
public boolean spying() {
return false;
}
@Override
public void spying(final boolean spying) {
}
@Override
public void sendMessageAsPlayer(final String message) {
}
@Override
public boolean speechPermitted(final String message) {
return true;
}
@Override
public boolean online() {
return false;
}
@Override
public @Nullable UUID whisperReplyTarget() {
return null;
}
@Override
public void whisperReplyTarget(final @Nullable UUID uuid) {
}
@Override
public @Nullable UUID lastWhisperTarget() {
return null;
}
@Override
public void lastWhisperTarget(final @Nullable UUID uuid) {
}
@Override
public boolean vanished() {
return false;
}
@Override
public boolean awareOf(final CarbonPlayer other) {
return true;
}
@Override
public @NotNull Identity identity() {
return Identity.nil();
}
}
+2 -1
View File
@@ -20,6 +20,7 @@ dependencies {
// Plugins
compileOnly("me.clip:placeholderapi:2.10.9") // TODO: move this to libs.versions.yml
compileOnly(libs.essentialsXDiscord)
compileOnly(libs.discordsrv)
}
carbonShadowPlatform {
@@ -44,7 +45,7 @@ bukkit {
apiVersion = "1.18"
author = "Draycia"
depend = listOf("LuckPerms")
softDepend = listOf("PlaceholderAPI", "EssentialsDiscord")
softDepend = listOf("PlaceholderAPI", "EssentialsDiscord", "DiscordSRV")
website = GITHUB_REPO_URL
permissions {
register("carbon.clearchat") {
@@ -37,6 +37,7 @@ import net.draycia.carbon.api.util.RenderedMessage;
import net.draycia.carbon.bukkit.listeners.BukkitChatListener;
import net.draycia.carbon.bukkit.listeners.BukkitPlayerJoinListener;
import net.draycia.carbon.bukkit.listeners.DiscordMessageListener;
import net.draycia.carbon.bukkit.listeners.DiscordSRVListener;
import net.draycia.carbon.bukkit.util.BukkitMessageRenderer;
import net.draycia.carbon.common.channels.CarbonChannelRegistry;
import net.draycia.carbon.common.messages.CarbonMessageService;
@@ -135,6 +136,10 @@ public final class CarbonChatBukkit extends JavaPlugin implements CarbonChat {
Bukkit.getPluginManager().registerEvents(discordMessageListener, this);
discordMessageListener.init();
}
if (Bukkit.getPluginManager().isPluginEnabled("DiscordSRV")) {
this.injector.getInstance(DiscordSRVListener.class);
}
}
@Override
@@ -123,6 +123,7 @@ public final class BukkitChatListener implements Listener {
try {
event.viewers().clear();
event.setCancelled(true);
} catch (final UnsupportedOperationException exception) {
exception.printStackTrace();
}
@@ -29,6 +29,7 @@ import net.essentialsx.api.v2.events.discord.DiscordRelayEvent;
import net.essentialsx.api.v2.services.discord.DiscordService;
import net.essentialsx.api.v2.services.discord.MessageType;
import net.kyori.adventure.key.Key;
import org.apache.logging.log4j.Logger;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
@@ -37,11 +38,15 @@ import org.checkerframework.checker.nullness.qual.Nullable;
public class DiscordMessageListener implements Listener {
private final CarbonChat carbonChat;
private Map<Key, MessageType> channelMessageTypes = new HashMap<>();
private final Map<Key, MessageType> channelMessageTypes = new HashMap<>();
@Inject
public DiscordMessageListener(final CarbonChat carbonChat) {
public DiscordMessageListener(
final CarbonChat carbonChat,
final Logger logger
) {
this.carbonChat = carbonChat;
logger.info("EssentialsXDiscord found! Enabling hook.");
}
// Minecraft -> Discord
@@ -0,0 +1,55 @@
/*
* CarbonChat
*
* Copyright (c) 2021 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.bukkit.listeners;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import github.scarsz.discordsrv.DiscordSRV;
import github.scarsz.discordsrv.api.Subscribe;
import github.scarsz.discordsrv.api.events.DiscordReadyEvent;
import github.scarsz.discordsrv.api.events.GameChatMessagePostProcessEvent;
import net.draycia.carbon.bukkit.util.CarbonChatHook;
import org.apache.logging.log4j.Logger;
@Singleton
public final class DiscordSRVListener {
private final Logger logger;
@Inject
public DiscordSRVListener(final Logger logger) {
this.logger = logger;
this.logger.info("DiscordSRV found! Enabling hook.");
DiscordSRV.api.subscribe(this);
}
@Subscribe
public void onDiscordReady(final DiscordReadyEvent event) {
DiscordSRV.getPlugin().getPluginHooks().add(new CarbonChatHook());
this.logger.info("Successfully enabled DiscordSRV hook!");
}
@Subscribe
public void onGameChat(final GameChatMessagePostProcessEvent event) {
//event.setCancelled(true);
}
}
@@ -158,4 +158,8 @@ public final class CarbonPlayerBukkit extends WrappedCarbonPlayer implements For
.anyMatch(MetadataValue::asBoolean);
}
public @Nullable Player bukkitPlayer() {
return Bukkit.getPlayer(this.uuid());
}
}
@@ -0,0 +1,67 @@
/*
* CarbonChat
*
* Copyright (c) 2021 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.bukkit.util;
import github.scarsz.discordsrv.Debug;
import github.scarsz.discordsrv.DiscordSRV;
import github.scarsz.discordsrv.dependencies.kyori.adventure.text.minimessage.MiniMessage;
import github.scarsz.discordsrv.hooks.chat.ChatHook;
import github.scarsz.discordsrv.util.PluginUtil;
import net.draycia.carbon.api.CarbonChatProvider;
import net.draycia.carbon.api.channels.ChatChannel;
import net.draycia.carbon.api.events.CarbonChatEvent;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.bukkit.users.CarbonPlayerBukkit;
import net.draycia.carbon.common.util.ChannelUtils;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.checkerframework.checker.nullness.qual.Nullable;
public class CarbonChatHook implements ChatHook {
public CarbonChatHook() {
CarbonChatProvider.carbonChat().eventHandler().subscribe(CarbonChatEvent.class, event -> {
final ChatChannel chatChannel = event.chatChannel();
final CarbonPlayer carbonPlayer = event.sender();
DiscordSRV.debug(Debug.MINECRAFT_TO_DISCORD, "Received a CarbonChatEvent (player: " + carbonPlayer.username() + ")");
final @Nullable Player player = ((CarbonPlayerBukkit) carbonPlayer).bukkitPlayer();
final String message = PlainTextComponentSerializer.plainText().serialize(event.message());
if (player != null) {
DiscordSRV.getPlugin().processChatMessage(player, message, chatChannel.commandName(), event.result().cancelled());
}
});
}
@Override
public void broadcastMessageToChannel(final String channel, final github.scarsz.discordsrv.dependencies.kyori.adventure.text.Component message) {
final String mmFormattedMessage = MiniMessage.get().serialize(message);
ChannelUtils.broadcastMessageToChannel(mmFormattedMessage, ChannelUtils.locateChannel(channel));
}
@Override
public Plugin getPlugin() {
return PluginUtil.getPlugin("CarbonChat");
}
}
@@ -0,0 +1,58 @@
/*
* CarbonChat
*
* Copyright (c) 2021 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.common.util;
import net.draycia.carbon.api.CarbonChatProvider;
import net.draycia.carbon.api.channels.ChatChannel;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.kyori.adventure.audience.MessageType;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.checkerframework.checker.nullness.qual.Nullable;
public final class ChannelUtils {
private ChannelUtils() {
}
public static void broadcastMessageToChannel(final String mmFormattedMessage, final ChatChannel channel) {
final Component originalMessage = MiniMessage.miniMessage().deserialize(mmFormattedMessage);
// TODO: Emit events
for (final CarbonPlayer recipient : CarbonChatProvider.carbonChat().server().players()) {
if (channel.hearingPermitted(recipient).permitted()) {
recipient.sendMessage(originalMessage, MessageType.CHAT);
}
}
}
public static @Nullable ChatChannel locateChannel(final String channelName) {
for (final ChatChannel channel : CarbonChatProvider.carbonChat().channelRegistry()) {
if (channel.commandName().equalsIgnoreCase(channelName)) {
return channel;
}
}
return null;
}
}
+5
View File
@@ -36,6 +36,7 @@ versions:
adventurePlatformFabric: 5.2.0
luckPermsApi: 5.3
essentialsx: 2.19.3
discordsrv: 1.25.0
dependencies:
adventureBom:
@@ -216,5 +217,9 @@ dependencies:
group: net.essentialsx
name: EssentialsXDiscord
version: { ref: essentialsx }
discordsrv:
group: com.discordsrv
name: discordsrv
version: { ref: discordsrv }
bundles:
+3
View File
@@ -44,6 +44,9 @@ dependencyResolutionManagement {
includeGroup("net.essentialsx")
}
}
// DiscordSRV
maven("https://m2.dv8tion.net/releases")
maven("https://nexus.scarsz.me/content/groups/public/")
}
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
}