mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-03 22:15:45 +00:00
Allow custom channel registration and add register event
This commit is contained in:
1 parent
57af9ed327
commit
e290340f9f
4 files changed
+212
-85
No files matched your search
@@ -21,11 +21,21 @@ package net.draycia.carbon.api.channels;
|
|||||||
|
|
||||||
import net.draycia.carbon.api.util.DefaultedKeyValueRegistry;
|
import net.draycia.carbon.api.util.DefaultedKeyValueRegistry;
|
||||||
import net.kyori.adventure.key.Key;
|
import net.kyori.adventure.key.Key;
|
||||||
|
import net.kyori.registry.Registry;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Registry for chat channels.
|
* Registry for chat channels.
|
||||||
*
|
*
|
||||||
* @since 2.0.0
|
* @since 2.0.0
|
||||||
*/
|
*/
|
||||||
public interface ChannelRegistry extends DefaultedKeyValueRegistry<Key, ChatChannel> {
|
public interface ChannelRegistry extends Registry<Key, ChatChannel>, DefaultedKeyValueRegistry<Key, ChatChannel> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Registers ingame commands for the channel.
|
||||||
|
*
|
||||||
|
* @param channel the channel to register commands for
|
||||||
|
* @since 2.1.0
|
||||||
|
*/
|
||||||
|
void registerChannelCommands(final ChatChannel channel);
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
/*
|
||||||
|
* 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.events;
|
||||||
|
|
||||||
|
import net.draycia.carbon.api.channels.ChatChannel;
|
||||||
|
import net.kyori.adventure.key.Key;
|
||||||
|
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||||
|
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@link CarbonEvent} that's called when channels are registered.
|
||||||
|
*
|
||||||
|
* @since 2.1.0
|
||||||
|
*/
|
||||||
|
@DefaultQualifier(NonNull.class)
|
||||||
|
public interface CarbonChannelRegisterEvent extends CarbonEvent {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The channels that were registered.
|
||||||
|
*
|
||||||
|
* @return the registered channels
|
||||||
|
* @since 2.1.0
|
||||||
|
*/
|
||||||
|
Iterable<ChatChannel> channels();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Registers additional channels to the registry. Does not re-emit the event.
|
||||||
|
*
|
||||||
|
* @param key The key the channel is identified with in the registry
|
||||||
|
* @param channel the channel to register
|
||||||
|
* @param registerCommands if commands for the channel should be registered
|
||||||
|
* @since 2.1.0
|
||||||
|
*/
|
||||||
|
void register(final Key key, final ChatChannel channel, final boolean registerCommands);
|
||||||
|
|
||||||
|
}
|
||||||
+84
-84
@@ -53,6 +53,7 @@ import net.draycia.carbon.common.command.Commander;
|
|||||||
import net.draycia.carbon.common.command.PlayerCommander;
|
import net.draycia.carbon.common.command.PlayerCommander;
|
||||||
import net.draycia.carbon.common.config.ConfigFactory;
|
import net.draycia.carbon.common.config.ConfigFactory;
|
||||||
import net.draycia.carbon.common.events.CarbonReloadEvent;
|
import net.draycia.carbon.common.events.CarbonReloadEvent;
|
||||||
|
import net.draycia.carbon.common.events.ChannelRegisterEvent;
|
||||||
import net.draycia.carbon.common.messages.CarbonMessages;
|
import net.draycia.carbon.common.messages.CarbonMessages;
|
||||||
import net.draycia.carbon.common.messaging.packets.ChatMessagePacket;
|
import net.draycia.carbon.common.messaging.packets.ChatMessagePacket;
|
||||||
import net.kyori.adventure.audience.MessageType;
|
import net.kyori.adventure.audience.MessageType;
|
||||||
@@ -60,7 +61,7 @@ import net.kyori.adventure.identity.Identity;
|
|||||||
import net.kyori.adventure.key.Key;
|
import net.kyori.adventure.key.Key;
|
||||||
import net.kyori.adventure.permission.PermissionChecker;
|
import net.kyori.adventure.permission.PermissionChecker;
|
||||||
import net.kyori.adventure.text.Component;
|
import net.kyori.adventure.text.Component;
|
||||||
import net.kyori.registry.DefaultedRegistry;
|
import net.kyori.registry.DefaultedRegistryGetter;
|
||||||
import ninja.egg82.messenger.services.PacketService;
|
import ninja.egg82.messenger.services.PacketService;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
|
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
|
||||||
@@ -80,7 +81,7 @@ import static net.kyori.adventure.text.Component.empty;
|
|||||||
|
|
||||||
@Singleton
|
@Singleton
|
||||||
@DefaultQualifier(NonNull.class)
|
@DefaultQualifier(NonNull.class)
|
||||||
public class CarbonChannelRegistry implements ChannelRegistry, DefaultedRegistry<Key, ChatChannel> {
|
public class CarbonChannelRegistry implements ChannelRegistry, DefaultedRegistryGetter<Key, ChatChannel> {
|
||||||
|
|
||||||
private static @MonotonicNonNull ObjectMapper<ConfigChatChannel> MAPPER;
|
private static @MonotonicNonNull ObjectMapper<ConfigChatChannel> MAPPER;
|
||||||
|
|
||||||
@@ -97,7 +98,7 @@ public class CarbonChannelRegistry implements ChannelRegistry, DefaultedRegistry
|
|||||||
private final Logger logger;
|
private final Logger logger;
|
||||||
private final ConfigFactory configFactory;
|
private final ConfigFactory configFactory;
|
||||||
private @MonotonicNonNull Key defaultKey;
|
private @MonotonicNonNull Key defaultKey;
|
||||||
private @MonotonicNonNull ChatChannel basicChannel;
|
//private @MonotonicNonNull ChatChannel basicChannel;
|
||||||
private final CarbonMessages carbonMessages;
|
private final CarbonMessages carbonMessages;
|
||||||
private final CarbonChat carbonChat;
|
private final CarbonChat carbonChat;
|
||||||
|
|
||||||
@@ -110,7 +111,7 @@ public class CarbonChannelRegistry implements ChannelRegistry, DefaultedRegistry
|
|||||||
final Logger logger,
|
final Logger logger,
|
||||||
final ConfigFactory configFactory,
|
final ConfigFactory configFactory,
|
||||||
final CarbonMessages carbonMessages,
|
final CarbonMessages carbonMessages,
|
||||||
final BasicChatChannel basicChannel,
|
//final BasicChatChannel basicChannel,
|
||||||
final CarbonChat carbonChat
|
final CarbonChat carbonChat
|
||||||
) {
|
) {
|
||||||
this.configChannelDir = dataDirectory.resolve("channels");
|
this.configChannelDir = dataDirectory.resolve("channels");
|
||||||
@@ -118,7 +119,7 @@ public class CarbonChannelRegistry implements ChannelRegistry, DefaultedRegistry
|
|||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
this.configFactory = configFactory;
|
this.configFactory = configFactory;
|
||||||
this.carbonMessages = carbonMessages;
|
this.carbonMessages = carbonMessages;
|
||||||
this.basicChannel = basicChannel;
|
//this.basicChannel = basicChannel;
|
||||||
this.carbonChat = carbonChat;
|
this.carbonChat = carbonChat;
|
||||||
|
|
||||||
carbonChat.eventHandler().subscribe(CarbonReloadEvent.class, event -> {
|
carbonChat.eventHandler().subscribe(CarbonReloadEvent.class, event -> {
|
||||||
@@ -212,9 +213,6 @@ public class CarbonChannelRegistry implements ChannelRegistry, DefaultedRegistry
|
|||||||
|
|
||||||
// otherwise, register all channels found
|
// otherwise, register all channels found
|
||||||
try (final Stream<Path> paths = Files.walk(this.configChannelDir)) {
|
try (final Stream<Path> paths = Files.walk(this.configChannelDir)) {
|
||||||
final CommandManager<Commander> commandManager =
|
|
||||||
this.injector.getInstance(com.google.inject.Key.get(new TypeLiteral<CommandManager<Commander>>() {}));
|
|
||||||
|
|
||||||
paths.forEach(path -> {
|
paths.forEach(path -> {
|
||||||
final String fileName = path.getFileName().toString();
|
final String fileName = path.getFileName().toString();
|
||||||
|
|
||||||
@@ -230,7 +228,7 @@ public class CarbonChannelRegistry implements ChannelRegistry, DefaultedRegistry
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (chatChannel.shouldRegisterCommands()) {
|
if (chatChannel.shouldRegisterCommands()) {
|
||||||
this.registerChannelCommands(carbonMessages, chatChannel, commandManager);
|
this.registerChannelCommands(chatChannel);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -250,6 +248,8 @@ public class CarbonChannelRegistry implements ChannelRegistry, DefaultedRegistry
|
|||||||
} catch (final IOException exception) {
|
} catch (final IOException exception) {
|
||||||
exception.printStackTrace();
|
exception.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.carbonChat.eventHandler().emit(new ChannelRegisterEvent(this.channelMap.values(), this));
|
||||||
}
|
}
|
||||||
|
|
||||||
public @Nullable ChatChannel loadChannel(final Path channelFile) {
|
public @Nullable ChatChannel loadChannel(final Path channelFile) {
|
||||||
@@ -266,27 +266,6 @@ public class CarbonChannelRegistry implements ChannelRegistry, DefaultedRegistry
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public ChatChannel defaultValue() {
|
|
||||||
return Objects.requireNonNull(this.get(this.defaultKey));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Key defaultKey() {
|
|
||||||
return this.defaultKey;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public ChatChannel getOrDefault(final Key key) {
|
|
||||||
final @Nullable ChatChannel channel = this.get(key);
|
|
||||||
|
|
||||||
if (channel != null) {
|
|
||||||
return channel;
|
|
||||||
}
|
|
||||||
|
|
||||||
return this.defaultValue();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void registerDefaultChannel() {
|
private void registerDefaultChannel() {
|
||||||
try {
|
try {
|
||||||
Files.createDirectories(this.configChannelDir);
|
Files.createDirectories(this.configChannelDir);
|
||||||
@@ -325,60 +304,6 @@ public class CarbonChannelRegistry implements ChannelRegistry, DefaultedRegistry
|
|||||||
return channel;
|
return channel;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void registerChannelCommands(
|
|
||||||
final CarbonMessages carbonMessages,
|
|
||||||
final ChatChannel channel,
|
|
||||||
final CommandManager<Commander> commandManager
|
|
||||||
) {
|
|
||||||
var builder = commandManager.commandBuilder(channel.commandName(),
|
|
||||||
channel.commandAliases(), commandManager.createDefaultCommandMeta())
|
|
||||||
.argument(StringArgument.<Commander>newBuilder("message").greedy().asOptional().build());
|
|
||||||
|
|
||||||
if (channel.permission() != null) {
|
|
||||||
builder = builder.permission(channel.permission());
|
|
||||||
|
|
||||||
// Add to LuckPerms permission suggestions... lol
|
|
||||||
this.carbonChat.server().console().get(PermissionChecker.POINTER).ifPresent(checker -> {
|
|
||||||
checker.test(channel.permission());
|
|
||||||
checker.test(channel.permission() + ".see");
|
|
||||||
checker.test(channel.permission() + ".speak");
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
final Key channelKey = channel.key();
|
|
||||||
|
|
||||||
final var command = builder.senderType(PlayerCommander.class)
|
|
||||||
.handler(handler -> {
|
|
||||||
final var sender = ((PlayerCommander) handler.getSender()).carbonPlayer();
|
|
||||||
final @Nullable ChatChannel chatChannel = this.get(channelKey);
|
|
||||||
|
|
||||||
if (sender.muted()) {
|
|
||||||
this.carbonMessages.muteCannotSpeak(sender);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (handler.contains("message")) {
|
|
||||||
final String message = handler.get("message");
|
|
||||||
|
|
||||||
// TODO: trigger platform events related to chat
|
|
||||||
this.sendMessageInChannelAsPlayer(sender, chatChannel, message);
|
|
||||||
} else {
|
|
||||||
sender.selectedChannel(chatChannel);
|
|
||||||
this.carbonMessages.changedChannels(sender, channelKey.value());
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.build();
|
|
||||||
|
|
||||||
commandManager.command(command);
|
|
||||||
|
|
||||||
final var channelCommand = commandManager.commandBuilder("channel", "ch")
|
|
||||||
.literal(channelKey.value())
|
|
||||||
.proxies(command)
|
|
||||||
.build();
|
|
||||||
|
|
||||||
commandManager.command(channelCommand);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void sendMessageInChannelAsPlayer(
|
private void sendMessageInChannelAsPlayer(
|
||||||
final CarbonPlayer sender,
|
final CarbonPlayer sender,
|
||||||
final ChatChannel channel,
|
final ChatChannel channel,
|
||||||
@@ -447,6 +372,60 @@ public class CarbonChannelRegistry implements ChannelRegistry, DefaultedRegistry
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void registerChannelCommands(final ChatChannel channel) {
|
||||||
|
final CommandManager<Commander> commandManager =
|
||||||
|
this.injector.getInstance(com.google.inject.Key.get(new TypeLiteral<CommandManager<Commander>>() {}));
|
||||||
|
|
||||||
|
var builder = commandManager.commandBuilder(channel.commandName(),
|
||||||
|
channel.commandAliases(), commandManager.createDefaultCommandMeta())
|
||||||
|
.argument(StringArgument.<Commander>newBuilder("message").greedy().asOptional().build());
|
||||||
|
|
||||||
|
if (channel.permission() != null) {
|
||||||
|
builder = builder.permission(channel.permission());
|
||||||
|
|
||||||
|
// Add to LuckPerms permission suggestions... lol
|
||||||
|
this.carbonChat.server().console().get(PermissionChecker.POINTER).ifPresent(checker -> {
|
||||||
|
checker.test(channel.permission());
|
||||||
|
checker.test(channel.permission() + ".see");
|
||||||
|
checker.test(channel.permission() + ".speak");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
final Key channelKey = channel.key();
|
||||||
|
|
||||||
|
final var command = builder.senderType(PlayerCommander.class)
|
||||||
|
.handler(handler -> {
|
||||||
|
final var sender = ((PlayerCommander) handler.getSender()).carbonPlayer();
|
||||||
|
final @Nullable ChatChannel chatChannel = this.get(channelKey);
|
||||||
|
|
||||||
|
if (sender.muted()) {
|
||||||
|
this.carbonMessages.muteCannotSpeak(sender);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (handler.contains("message")) {
|
||||||
|
final String message = handler.get("message");
|
||||||
|
|
||||||
|
// TODO: trigger platform events related to chat
|
||||||
|
this.sendMessageInChannelAsPlayer(sender, chatChannel, message);
|
||||||
|
} else {
|
||||||
|
sender.selectedChannel(chatChannel);
|
||||||
|
this.carbonMessages.changedChannels(sender, channelKey.value());
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.build();
|
||||||
|
|
||||||
|
commandManager.command(command);
|
||||||
|
|
||||||
|
final var channelCommand = commandManager.commandBuilder("channel", "ch")
|
||||||
|
.literal(channelKey.value())
|
||||||
|
.proxies(command)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
commandManager.command(channelCommand);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @NonNull ChatChannel register(final @NonNull Key key, final @NonNull ChatChannel value) {
|
public @NonNull ChatChannel register(final @NonNull Key key, final @NonNull ChatChannel value) {
|
||||||
this.channelMap.put(key, value);
|
this.channelMap.put(key, value);
|
||||||
@@ -473,4 +452,25 @@ public class CarbonChannelRegistry implements ChannelRegistry, DefaultedRegistry
|
|||||||
return Iterators.unmodifiableIterator(this.channelMap.values().iterator());
|
return Iterators.unmodifiableIterator(this.channelMap.values().iterator());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ChatChannel defaultValue() {
|
||||||
|
return Objects.requireNonNull(this.get(this.defaultKey));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Key defaultKey() {
|
||||||
|
return this.defaultKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ChatChannel getOrDefault(final Key key) {
|
||||||
|
final @Nullable ChatChannel channel = this.get(key);
|
||||||
|
|
||||||
|
if (channel != null) {
|
||||||
|
return channel;
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.defaultValue();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,64 @@
|
|||||||
|
/*
|
||||||
|
* 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.events;
|
||||||
|
|
||||||
|
import net.draycia.carbon.api.channels.ChatChannel;
|
||||||
|
import net.draycia.carbon.api.events.CarbonChannelRegisterEvent;
|
||||||
|
import net.draycia.carbon.api.events.CarbonEvent;
|
||||||
|
import net.draycia.carbon.common.channels.CarbonChannelRegistry;
|
||||||
|
import net.kyori.adventure.key.Key;
|
||||||
|
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||||
|
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@link CarbonEvent} that's called when channels are registered.
|
||||||
|
*
|
||||||
|
* @since 2.1.0
|
||||||
|
*/
|
||||||
|
@DefaultQualifier(NonNull.class)
|
||||||
|
public class ChannelRegisterEvent implements CarbonChannelRegisterEvent {
|
||||||
|
|
||||||
|
private final Iterable<ChatChannel> channels;
|
||||||
|
private final CarbonChannelRegistry registry;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@link CarbonEvent} that's called when channels are registered.
|
||||||
|
*
|
||||||
|
* @param channels the channels that were registered
|
||||||
|
* @since 2.1.0
|
||||||
|
*/
|
||||||
|
public ChannelRegisterEvent(final Iterable<ChatChannel> channels, final CarbonChannelRegistry registry) {
|
||||||
|
this.channels = channels;
|
||||||
|
this.registry = registry;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Iterable<ChatChannel> channels() {
|
||||||
|
return this.channels;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void register(final Key key, final ChatChannel channel, final boolean registerCommands) {
|
||||||
|
this.registry.register(key, channel);
|
||||||
|
|
||||||
|
if (registerCommands) {
|
||||||
|
this.registry.registerChannelCommands(channel);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in new issue
Block a user