mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-01 22:15:08 +00:00
Hide Event lib from API
This commit is contained in:
1 parent
43d2754018
commit
09054b7332
15 files changed
+213
-114
No files matched your search
@@ -180,7 +180,6 @@ public abstract class CarbonChatInternal<C extends CarbonPlayer> implements Carb
|
||||
return this.channelRegistry;
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T extends Audience> IMessageRenderer<T, String, Component, Component> messageRenderer() {
|
||||
return (IMessageRenderer<T, String, Component, Component>) this.renderer;
|
||||
|
||||
+18
-14
@@ -19,6 +19,7 @@
|
||||
*/
|
||||
package net.draycia.carbon.common.channels;
|
||||
|
||||
import cloud.commandframework.Command;
|
||||
import cloud.commandframework.CommandManager;
|
||||
import cloud.commandframework.arguments.standard.StringArgument;
|
||||
import com.google.common.collect.BiMap;
|
||||
@@ -39,7 +40,9 @@ import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Stream;
|
||||
import net.draycia.carbon.api.CarbonChat;
|
||||
import net.draycia.carbon.api.channels.ChannelRegistry;
|
||||
@@ -53,6 +56,7 @@ import net.draycia.carbon.common.DataDirectory;
|
||||
import net.draycia.carbon.common.command.Commander;
|
||||
import net.draycia.carbon.common.command.PlayerCommander;
|
||||
import net.draycia.carbon.common.config.ConfigFactory;
|
||||
import net.draycia.carbon.common.event.events.CarbonChatEventImpl;
|
||||
import net.draycia.carbon.common.event.events.CarbonReloadEvent;
|
||||
import net.draycia.carbon.common.event.events.ChannelRegisterEventImpl;
|
||||
import net.draycia.carbon.common.messages.CarbonMessages;
|
||||
@@ -257,7 +261,7 @@ public class CarbonChannelRegistry implements ChannelRegistry, DefaultedRegistry
|
||||
final ConfigurationLoader<?> loader = this.configFactory.configurationLoader(channelFile);
|
||||
|
||||
try {
|
||||
final var loaded = updateNode(loader.load());
|
||||
final ConfigurationNode loaded = updateNode(loader.load());
|
||||
loader.save(loaded);
|
||||
return MAPPER.load(loaded);
|
||||
} catch (final ConfigurateException exception) {
|
||||
@@ -273,10 +277,10 @@ public class CarbonChannelRegistry implements ChannelRegistry, DefaultedRegistry
|
||||
|
||||
final Path configFile = this.configChannelDir.resolve("global.conf");
|
||||
|
||||
final var loader = this.configFactory.configurationLoader(configFile);
|
||||
final var node = loader.load();
|
||||
final ConfigurationLoader<?> loader = this.configFactory.configurationLoader(configFile);
|
||||
final ConfigurationNode node = loader.load();
|
||||
|
||||
final var configChannel = this.injector.getInstance(ConfigChatChannel.class);
|
||||
final ConfigChatChannel configChannel = this.injector.getInstance(ConfigChatChannel.class);
|
||||
|
||||
node.set(ConfigChatChannel.class, configChannel);
|
||||
loader.save(node);
|
||||
@@ -310,9 +314,9 @@ public class CarbonChannelRegistry implements ChannelRegistry, DefaultedRegistry
|
||||
final ChatChannel channel,
|
||||
final String plainMessage
|
||||
) {
|
||||
final var recipients = channel.recipients(sender);
|
||||
final List<Audience> recipients = channel.recipients(sender);
|
||||
|
||||
final var renderers = new ArrayList<KeyedRenderer>();
|
||||
final List<KeyedRenderer> renderers = new ArrayList<>();
|
||||
renderers.add(keyedRenderer(Key.key("carbon", "default"), channel));
|
||||
|
||||
final Component eventMessage;
|
||||
@@ -323,7 +327,7 @@ public class CarbonChannelRegistry implements ChannelRegistry, DefaultedRegistry
|
||||
eventMessage = WrappedCarbonPlayer.parseMessageTags(plainMessage, sender::hasPermission);
|
||||
}
|
||||
|
||||
final var chatEvent = new CarbonChatEvent(sender, eventMessage, recipients, renderers, channel, null);
|
||||
final CarbonChatEvent chatEvent = new CarbonChatEventImpl(sender, eventMessage, recipients, renderers, channel, null);
|
||||
this.eventHandler.emit(chatEvent);
|
||||
|
||||
if (chatEvent.cancelled()) {
|
||||
@@ -333,10 +337,10 @@ public class CarbonChannelRegistry implements ChannelRegistry, DefaultedRegistry
|
||||
final CarbonChatInternal<?> carbonChat = (CarbonChatInternal<?>) this.carbonChatProvider.get();
|
||||
|
||||
for (final Audience recipient : chatEvent.recipients()) {
|
||||
final var recipientUUID = recipient.get(Identity.UUID);
|
||||
final Optional<UUID> recipientUUID = recipient.get(Identity.UUID);
|
||||
final Audience recipientViewer;
|
||||
|
||||
var renderedMessage = chatEvent.message();
|
||||
Component renderedMessage = chatEvent.message();
|
||||
|
||||
if (recipientUUID.isPresent()) {
|
||||
recipientViewer = carbonChat.userManager().user(recipient.get(Identity.UUID).orElseThrow()).join();
|
||||
@@ -344,7 +348,7 @@ public class CarbonChannelRegistry implements ChannelRegistry, DefaultedRegistry
|
||||
recipientViewer = recipient;
|
||||
}
|
||||
|
||||
for (final var renderer : chatEvent.renderers()) {
|
||||
for (final KeyedRenderer renderer : chatEvent.renderers()) {
|
||||
renderedMessage = renderer.render(sender, recipientViewer, renderedMessage, chatEvent.originalMessage());
|
||||
}
|
||||
recipient.sendMessage(renderedMessage);
|
||||
@@ -366,7 +370,7 @@ public class CarbonChannelRegistry implements ChannelRegistry, DefaultedRegistry
|
||||
final CommandManager<Commander> commandManager =
|
||||
this.injector.getInstance(com.google.inject.Key.get(new TypeLiteral<CommandManager<Commander>>() {}));
|
||||
|
||||
var builder = commandManager.commandBuilder(channel.commandName(),
|
||||
Command.Builder<Commander> builder = commandManager.commandBuilder(channel.commandName(),
|
||||
channel.commandAliases(), commandManager.createDefaultCommandMeta())
|
||||
.argument(StringArgument.<Commander>builder("message").greedy().asOptional().build());
|
||||
|
||||
@@ -383,9 +387,9 @@ public class CarbonChannelRegistry implements ChannelRegistry, DefaultedRegistry
|
||||
|
||||
final Key channelKey = channel.key();
|
||||
|
||||
final var command = builder.senderType(PlayerCommander.class)
|
||||
final Command<Commander> command = builder.senderType(PlayerCommander.class)
|
||||
.handler(handler -> {
|
||||
final var sender = ((PlayerCommander) handler.getSender()).carbonPlayer();
|
||||
final CarbonPlayer sender = ((PlayerCommander) handler.getSender()).carbonPlayer();
|
||||
final @Nullable ChatChannel chatChannel = this.get(channelKey);
|
||||
|
||||
if (sender.muted()) {
|
||||
@@ -410,7 +414,7 @@ public class CarbonChannelRegistry implements ChannelRegistry, DefaultedRegistry
|
||||
|
||||
commandManager.command(command);
|
||||
|
||||
final var channelCommand = commandManager.commandBuilder("channel", "ch")
|
||||
final Command<Commander> channelCommand = commandManager.commandBuilder("channel", "ch")
|
||||
.literal(channelKey.value())
|
||||
.proxies(command)
|
||||
.build();
|
||||
|
||||
@@ -31,6 +31,7 @@ import net.draycia.carbon.api.CarbonChat;
|
||||
import net.draycia.carbon.api.channels.ChatChannel;
|
||||
import net.draycia.carbon.api.users.CarbonPlayer;
|
||||
import net.draycia.carbon.api.util.SourcedAudience;
|
||||
import net.draycia.carbon.common.CarbonChatInternal;
|
||||
import net.draycia.carbon.common.channels.messages.ConfigChannelMessageSource;
|
||||
import net.draycia.carbon.common.channels.messages.ConfigChannelMessages;
|
||||
import net.draycia.carbon.common.messages.SourcedMessageSender;
|
||||
@@ -212,7 +213,7 @@ public final class ConfigChatChannel implements ChatChannel {
|
||||
return Moonshine.<ConfigChannelMessages, SourcedAudience>builder(new TypeToken<ConfigChannelMessages>() {})
|
||||
.receiverLocatorResolver(serverReceiverResolver, 0)
|
||||
.sourced(this.messageSource)
|
||||
.rendered(this.carbonChat.messageRenderer())
|
||||
.rendered(((CarbonChatInternal<?>) this.carbonChat).messageRenderer())
|
||||
.sent(carbonMessageSender)
|
||||
.resolvingWithStrategy(new StandardPlaceholderResolverStrategy<>(new StandardSupertypeThenInterfaceSupertypeStrategy(false)))
|
||||
.weightedPlaceholderResolver(Component.class, componentPlaceholderResolver, 0)
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* 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.common.event;
|
||||
|
||||
import net.draycia.carbon.api.event.Cancellable;
|
||||
|
||||
public class CancellableImpl implements Cancellable, com.seiama.event.Cancellable {
|
||||
|
||||
private boolean cancelled = false;
|
||||
|
||||
@Override
|
||||
public boolean cancelled() {
|
||||
return this.cancelled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cancelled(final boolean cancelled) {
|
||||
this.cancelled = cancelled;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -56,7 +56,7 @@ public final class CarbonEventHandlerImpl implements CarbonEventHandler {
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T extends CarbonEvent> CarbonEventSubscription subscribe(
|
||||
public <T extends CarbonEvent> CarbonEventSubscription<T> subscribe(
|
||||
final Class<T> eventClass,
|
||||
final CarbonEventSubscriber<T> subscriber
|
||||
) {
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
/*
|
||||
* 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.common.event.events;
|
||||
|
||||
import java.util.List;
|
||||
import net.draycia.carbon.api.channels.ChatChannel;
|
||||
import net.draycia.carbon.api.event.events.CarbonChatEvent;
|
||||
import net.draycia.carbon.api.users.CarbonPlayer;
|
||||
import net.draycia.carbon.api.util.KeyedRenderer;
|
||||
import net.draycia.carbon.common.event.CancellableImpl;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.kyori.adventure.chat.SignedMessage;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
|
||||
/**
|
||||
* Event that's called when chat components are rendered for online players.
|
||||
*
|
||||
* @since 2.0.0
|
||||
*/
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public class CarbonChatEventImpl extends CancellableImpl implements CarbonChatEvent {
|
||||
|
||||
private final List<KeyedRenderer> renderers;
|
||||
private final CarbonPlayer sender;
|
||||
private final Component originalMessage;
|
||||
private final List<? extends Audience> recipients;
|
||||
private final @MonotonicNonNull ChatChannel chatChannel;
|
||||
private final @MonotonicNonNull SignedMessage signedMessage;
|
||||
private Component message;
|
||||
|
||||
public CarbonChatEventImpl(
|
||||
final CarbonPlayer sender,
|
||||
final Component originalMessage,
|
||||
final List<? extends Audience> recipients,
|
||||
final List<KeyedRenderer> renderers,
|
||||
final @Nullable ChatChannel chatChannel,
|
||||
final @Nullable SignedMessage signedMessage
|
||||
) {
|
||||
this.sender = sender;
|
||||
this.originalMessage = originalMessage;
|
||||
this.message = originalMessage;
|
||||
this.recipients = recipients;
|
||||
this.renderers = renderers;
|
||||
this.chatChannel = chatChannel;
|
||||
this.signedMessage = signedMessage;
|
||||
}
|
||||
|
||||
public List<KeyedRenderer> renderers() {
|
||||
return this.renderers;
|
||||
}
|
||||
|
||||
public @MonotonicNonNull SignedMessage signedMessage() {
|
||||
return this.signedMessage;
|
||||
}
|
||||
|
||||
public CarbonPlayer sender() {
|
||||
return this.sender;
|
||||
}
|
||||
|
||||
public Component originalMessage() {
|
||||
return this.originalMessage;
|
||||
}
|
||||
|
||||
public Component message() {
|
||||
return this.message;
|
||||
}
|
||||
|
||||
public void message(final Component message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public @MonotonicNonNull ChatChannel chatChannel() {
|
||||
return this.chatChannel;
|
||||
}
|
||||
|
||||
public List<? extends Audience> recipients() {
|
||||
return this.recipients;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -27,6 +27,7 @@ import net.draycia.carbon.api.event.events.CarbonChatEvent;
|
||||
import net.draycia.carbon.api.users.CarbonPlayer;
|
||||
import net.draycia.carbon.api.util.KeyedRenderer;
|
||||
import net.draycia.carbon.common.config.ConfigFactory;
|
||||
import net.draycia.carbon.common.event.events.CarbonChatEventImpl;
|
||||
import net.draycia.carbon.common.event.events.CarbonEarlyChatEvent;
|
||||
import net.draycia.carbon.common.messages.CarbonMessages;
|
||||
import net.draycia.carbon.common.users.WrappedCarbonPlayer;
|
||||
@@ -82,7 +83,7 @@ public abstract class ChatListenerInternal {
|
||||
|
||||
final List<Audience> recipients = channel.recipients(sender);
|
||||
|
||||
final CarbonChatEvent chatEvent = new CarbonChatEvent(sender, message, recipients, renderers, channel, signedMessage);
|
||||
final CarbonChatEvent chatEvent = new CarbonChatEventImpl(sender, message, recipients, renderers, channel, signedMessage);
|
||||
|
||||
this.carbonChat.eventHandler().emit(chatEvent);
|
||||
|
||||
|
||||
+2
-1
@@ -26,6 +26,7 @@ import net.draycia.carbon.api.channels.ChatChannel;
|
||||
import net.draycia.carbon.api.event.events.CarbonChatEvent;
|
||||
import net.draycia.carbon.api.users.CarbonPlayer;
|
||||
import net.draycia.carbon.api.util.KeyedRenderer;
|
||||
import net.draycia.carbon.common.event.events.CarbonChatEventImpl;
|
||||
import net.draycia.carbon.common.messaging.packets.ChatMessagePacket;
|
||||
import net.draycia.carbon.common.messaging.packets.SaveCompletedPacket;
|
||||
import net.draycia.carbon.common.users.UserManagerInternal;
|
||||
@@ -76,7 +77,7 @@ public final class CarbonChatPacketHandler extends AbstractMessagingHandler {
|
||||
final List<KeyedRenderer> renderers = new ArrayList<>();
|
||||
|
||||
final List<Audience> recipients = channel.recipients(sender);
|
||||
final CarbonChatEvent chatEvent = new CarbonChatEvent(sender, messagePacket.message(), recipients, renderers, channel, null);
|
||||
final CarbonChatEvent chatEvent = new CarbonChatEventImpl(sender, messagePacket.message(), recipients, renderers, channel, null);
|
||||
this.carbonChat.eventHandler().emit(chatEvent);
|
||||
|
||||
for (final Audience recipient : recipients) {
|
||||
|
||||
@@ -0,0 +1,123 @@
|
||||
/*
|
||||
* 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.common.util;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import net.kyori.adventure.text.format.TextColor;
|
||||
import net.kyori.adventure.text.format.TextDecoration;
|
||||
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
|
||||
import net.kyori.adventure.text.serializer.legacy.LegacyFormat;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
|
||||
/**
|
||||
* Basic color related utilities.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public final class ColorUtils {
|
||||
|
||||
private static final Pattern spigotLegacyRGB =
|
||||
Pattern.compile("[§&]x[§&]([0-9a-fA-F])[§&]([0-9a-fA-F])[§&]([0-9a-fA-F])[§&]([0-9a-fA-F])[§&]([0-9a-fA-F])[§&]([0-9a-fA-F])");
|
||||
private static final Pattern pluginRGB =
|
||||
Pattern.compile("[§&]#([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])");
|
||||
private static final String hexReplacement = "<#$1$2$3$4$5$6>";
|
||||
|
||||
private ColorUtils() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the input into a color.<br>
|
||||
* Supports named colors, legacy, and hex inputs.
|
||||
*
|
||||
* @param input the color input
|
||||
* @return the color
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public static @Nullable TextColor parseColor(String input) {
|
||||
if (input.isEmpty()) {
|
||||
return NamedTextColor.WHITE;
|
||||
}
|
||||
|
||||
for (final NamedTextColor namedColor : NamedTextColor.NAMES.values()) {
|
||||
if (namedColor.toString().equalsIgnoreCase(input)) {
|
||||
return namedColor;
|
||||
}
|
||||
}
|
||||
|
||||
if (input.contains("&") || input.contains("§")) {
|
||||
input = input.replace("&", "§");
|
||||
|
||||
return LegacyComponentSerializer.legacySection().deserialize(input).color();
|
||||
}
|
||||
|
||||
return TextColor.fromCSSHexString(input);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the input legacy, legacy rgb, and alternate color formats to MiniMessage color tags.
|
||||
*
|
||||
* @param input the message to convert
|
||||
* @return the converted message
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public static String legacyToMiniMessage(final String input) {
|
||||
String output = input;
|
||||
|
||||
// Legacy RGB
|
||||
output = spigotLegacyRGB.matcher(output).replaceAll(hexReplacement);
|
||||
|
||||
// Alternate RGB, TAB (neznamy) && KiteBoard
|
||||
output = pluginRGB.matcher(output).replaceAll(hexReplacement);
|
||||
|
||||
// Legacy Colors
|
||||
for (final char c : "0123456789abcdefABCDEF".toCharArray()) {
|
||||
final @Nullable LegacyFormat format = LegacyComponentSerializer.parseChar(Character.toLowerCase(c));
|
||||
|
||||
if (format != null) {
|
||||
final @Nullable TextColor color = format.color();
|
||||
|
||||
if (color != null) {
|
||||
output = output.replaceAll("[§&]" + c, "<" + color.asHexString() + ">");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Legacy Formatting
|
||||
for (final char c : "klmnoKLMNO".toCharArray()) {
|
||||
final @Nullable LegacyFormat format = LegacyComponentSerializer.parseChar(Character.toLowerCase(c));
|
||||
|
||||
if (format != null) {
|
||||
final @Nullable TextDecoration decoration = format.decoration();
|
||||
|
||||
if (decoration != null) {
|
||||
output = output.replaceAll("[§&]" + c, "<" + decoration.name() + ">");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user