mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-03 22:15:45 +00:00
cleanup project
This commit is contained in:
1 parent
1231946184
commit
6681c22288
106 files changed
+10
-9601
No files matched your search
@@ -1,421 +1,10 @@
|
||||
package net.draycia.carbon.bukkit;
|
||||
|
||||
import cloud.commandframework.CommandManager;
|
||||
import cloud.commandframework.execution.CommandExecutionCoordinator;
|
||||
import cloud.commandframework.paper.PaperCommandManager;
|
||||
import net.draycia.carbon.api.channels.ChatChannel;
|
||||
import net.draycia.carbon.bukkit.listeners.events.ItemLinkHandler;
|
||||
import net.draycia.carbon.bukkit.listeners.BukkitChatHandler;
|
||||
import net.draycia.carbon.bukkit.messaging.BungeeMessageService;
|
||||
import net.draycia.carbon.bukkit.users.BukkitConsoleUser;
|
||||
import net.draycia.carbon.bukkit.users.BukkitPlayerUser;
|
||||
import net.draycia.carbon.bukkit.util.BungeeChannelApi;
|
||||
import net.draycia.carbon.bukkit.util.CarbonPlaceholders;
|
||||
import net.draycia.carbon.bukkit.util.Metrics;
|
||||
import net.draycia.carbon.api.CarbonChat;
|
||||
import net.draycia.carbon.api.CarbonChatProvider;
|
||||
import net.draycia.carbon.api.config.CarbonTranslations;
|
||||
import net.draycia.carbon.api.adventure.MessageProcessor;
|
||||
import net.draycia.carbon.api.channels.ChannelRegistry;
|
||||
import net.draycia.carbon.api.commands.settings.CommandSettingsRegistry;
|
||||
import net.draycia.carbon.api.config.CarbonSettings;
|
||||
import net.draycia.carbon.api.config.ChannelSettings;
|
||||
import net.draycia.carbon.api.config.MessagingType;
|
||||
import net.draycia.carbon.api.config.ModerationSettings;
|
||||
import net.draycia.carbon.api.config.StorageType;
|
||||
import net.draycia.carbon.api.messaging.MessageService;
|
||||
import net.draycia.carbon.api.users.CarbonUser;
|
||||
import net.draycia.carbon.api.users.PlayerUser;
|
||||
import net.draycia.carbon.common.channels.ChannelManager;
|
||||
import net.draycia.carbon.common.config.ConfigLoader;
|
||||
import net.draycia.carbon.common.messaging.EmptyMessageService;
|
||||
import net.draycia.carbon.api.config.SQLCredentials;
|
||||
import net.draycia.carbon.common.messaging.RedisMessageService;
|
||||
import net.draycia.carbon.common.listeners.events.CapsHandler;
|
||||
import net.draycia.carbon.common.listeners.events.CustomPlaceholderHandler;
|
||||
import net.draycia.carbon.common.listeners.events.IgnoredPlayerHandler;
|
||||
import net.draycia.carbon.common.listeners.events.LegacyFormatHandler;
|
||||
import net.draycia.carbon.common.listeners.events.MuteHandler;
|
||||
import net.draycia.carbon.common.listeners.events.PingHandler;
|
||||
import net.draycia.carbon.bukkit.listeners.events.PlaceholderHandler;
|
||||
import net.draycia.carbon.common.listeners.events.PlayerJoinListener;
|
||||
import net.draycia.carbon.bukkit.listeners.events.RelationalPlaceholderHandler;
|
||||
import net.draycia.carbon.common.listeners.events.ShadowMuteHandler;
|
||||
import net.draycia.carbon.common.listeners.events.UrlLinkHandler;
|
||||
import net.draycia.carbon.common.listeners.events.UserFormattingHandler;
|
||||
import net.draycia.carbon.common.listeners.events.WhisperPingHandler;
|
||||
import net.draycia.carbon.common.adventure.AdventureManager;
|
||||
import net.draycia.carbon.common.commands.misc.CommandRegistrar;
|
||||
import net.draycia.carbon.common.messaging.MessageManager;
|
||||
import net.draycia.carbon.api.users.UserService;
|
||||
import net.draycia.carbon.common.users.JSONUserService;
|
||||
import net.draycia.carbon.common.users.MySQLUserService;
|
||||
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
|
||||
import net.kyori.adventure.text.serializer.craftbukkit.BukkitComponentSerializer;
|
||||
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
|
||||
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.ConsoleCommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.PluginManager;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.bukkit.plugin.messaging.ChannelNotRegisteredException;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.spongepowered.configurate.ConfigurateException;
|
||||
import org.spongepowered.configurate.yaml.YamlConfigurationLoader;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
@SuppressWarnings("initialization.fields.uninitialized")
|
||||
public final class CarbonChatBukkit extends JavaPlugin implements CarbonChat {
|
||||
|
||||
private static final int BSTATS_PLUGIN_ID = 8720;
|
||||
|
||||
private ChannelManager channelManager;
|
||||
private AdventureManager messageProcessor;
|
||||
private CommandSettingsRegistry commandSettings;
|
||||
private ModerationSettings moderationSettings;
|
||||
private CarbonSettings carbonSettings;
|
||||
private ChannelSettings channelSettings;
|
||||
private UserService<BukkitPlayerUser> userService;
|
||||
private MessageManager messageManager;
|
||||
private CarbonTranslations translations;
|
||||
private CommandManager<CarbonUser> commandManager;
|
||||
|
||||
private Logger logger;
|
||||
|
||||
private @NonNull Collection<@NonNull String> proxyPlayerNames = new ArrayList<>();
|
||||
|
||||
public static final LegacyComponentSerializer LEGACY =
|
||||
LegacyComponentSerializer.builder()
|
||||
.extractUrls()
|
||||
.hexColors()
|
||||
.character('§')
|
||||
.useUnusualXRepeatedCharacterHexFormat()
|
||||
.build();
|
||||
|
||||
@Override
|
||||
public void onLoad() {
|
||||
CarbonChatProvider.register(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
this.logger = LoggerFactory.getLogger(this.getName());
|
||||
|
||||
// Setup metrics
|
||||
final Metrics metrics = new Metrics(this, BSTATS_PLUGIN_ID);
|
||||
|
||||
this.loadConfigs();
|
||||
|
||||
// Setup Adventure
|
||||
final BukkitAudiences audiences = BukkitAudiences.create(this);
|
||||
this.messageProcessor = new AdventureManager(audiences);
|
||||
|
||||
// Initialize managers
|
||||
this.channelManager = new ChannelManager(this);
|
||||
|
||||
// Handle messaging service
|
||||
final MessagingType messagingType = this.carbonSettings().messagingType();
|
||||
final MessageService messageService;
|
||||
|
||||
if (messagingType == MessagingType.REDIS) {
|
||||
messageService = new RedisMessageService(this, this.carbonSettings.redisCredentials());
|
||||
} else if (messagingType == MessagingType.BUNGEECORD) {
|
||||
messageService = new BungeeMessageService(this); // TODO: only support this if bukkit
|
||||
} else {
|
||||
messageService = new EmptyMessageService();
|
||||
}
|
||||
|
||||
this.messageManager = new MessageManager(this, messageService);
|
||||
|
||||
new BukkitRunnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
if (Bukkit.getOnlinePlayers().size() > 0) {
|
||||
BungeeChannelApi.of(CarbonChatBukkit.this).getPlayerList("ALL")
|
||||
.thenAccept(value -> CarbonChatBukkit.this.proxyPlayerNames = value);
|
||||
}
|
||||
} catch (final ChannelNotRegisteredException exception) {
|
||||
CarbonChatBukkit.this.logger().info("Exception thrown when gathering proxy player list!");
|
||||
CarbonChatBukkit.this.logger().info("Aborting attempts to communicate to the proxy.");
|
||||
CarbonChatBukkit.this.logger().info("This is merely for command completions, don't worry.");
|
||||
|
||||
this.cancel();
|
||||
}
|
||||
}
|
||||
}.runTaskTimer(this, 20, 5*20);
|
||||
|
||||
// Handle storage service
|
||||
final StorageType storageType = this.carbonSettings().storageType();
|
||||
|
||||
final Supplier<Collection<BukkitPlayerUser>> supplier = () -> {
|
||||
final List<BukkitPlayerUser> users = new ArrayList<>();
|
||||
|
||||
for (final Player player : Bukkit.getOnlinePlayers()) {
|
||||
users.add(this.userService.wrap(player.getUniqueId()));
|
||||
}
|
||||
|
||||
return users;
|
||||
};
|
||||
|
||||
if (storageType == StorageType.MYSQL) {
|
||||
this.logger().info("Enabling MySQL storage!");
|
||||
final SQLCredentials credentials = this.carbonSettings().sqlCredentials();
|
||||
this.userService = new MySQLUserService<>(this, credentials, supplier, () -> this.proxyPlayerNames,
|
||||
BukkitPlayerUser::new, BukkitConsoleUser::new);
|
||||
} else if (storageType == StorageType.JSON) {
|
||||
this.logger().info("Enabling JSON storage!");
|
||||
this.userService = new JSONUserService<>(BukkitPlayerUser.class, this, supplier, () -> this.proxyPlayerNames,
|
||||
BukkitPlayerUser::new, BukkitConsoleUser::new);
|
||||
} else {
|
||||
this.logger().error("Invalid storage type selected! Falling back to JSON.");
|
||||
this.userService = new JSONUserService<>(BukkitPlayerUser.class, this, supplier, () -> this.proxyPlayerNames,
|
||||
BukkitPlayerUser::new, BukkitConsoleUser::new);
|
||||
}
|
||||
|
||||
// Setup listeners
|
||||
this.setupCommands();
|
||||
this.setupListeners();
|
||||
|
||||
// Setup PlaceholderAPI placeholders
|
||||
new CarbonPlaceholders(this).register();
|
||||
|
||||
this.logger().info("CarbonChat startup finished! Enjoy :)");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
this.userService.onDisable();
|
||||
}
|
||||
|
||||
private void setupListeners() {
|
||||
final PluginManager pluginManager = this.getServer().getPluginManager();
|
||||
|
||||
// Register chat listeners
|
||||
pluginManager.registerEvents(new BukkitChatHandler(this), this);
|
||||
|
||||
new CapsHandler();
|
||||
new CustomPlaceholderHandler();
|
||||
new IgnoredPlayerHandler();
|
||||
new ItemLinkHandler();
|
||||
new LegacyFormatHandler();
|
||||
new MuteHandler();
|
||||
new PingHandler();
|
||||
new PlaceholderHandler();
|
||||
new PlayerJoinListener();
|
||||
new RelationalPlaceholderHandler();
|
||||
new ShadowMuteHandler();
|
||||
new UrlLinkHandler();
|
||||
new UserFormattingHandler();
|
||||
new WhisperPingHandler();
|
||||
}
|
||||
|
||||
@SuppressWarnings("return.type.incompatible")
|
||||
private void setupCommands() {
|
||||
try {
|
||||
this.commandManager = new PaperCommandManager<>(this,
|
||||
CommandExecutionCoordinator
|
||||
.simpleCoordinator(), sender -> {
|
||||
if (sender instanceof Player) {
|
||||
return this.userService().wrap(((Player) sender).getUniqueId());
|
||||
} else {
|
||||
return new BukkitConsoleUser((ConsoleCommandSender) sender);
|
||||
}
|
||||
}, user -> {
|
||||
if (user instanceof PlayerUser) {
|
||||
return Bukkit.getPlayer(((PlayerUser) user).uuid());
|
||||
} else {
|
||||
return Bukkit.getConsoleSender();
|
||||
}
|
||||
});
|
||||
|
||||
CommandRegistrar.registerCommands(this.commandManager);
|
||||
} catch (final Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reload() {
|
||||
this.loadConfigs();
|
||||
this.channelManager.reloadChannels();
|
||||
|
||||
final ChatChannel defaultChannel = this.channelManager.defaultChannel();
|
||||
|
||||
for (final PlayerUser user : this.userService().onlineUsers()) {
|
||||
final ChatChannel selectedChannel = user.selectedChannel();
|
||||
|
||||
if (selectedChannel != null) {
|
||||
final ChatChannel channel = this.channelManager.channelOrDefault(selectedChannel.key());
|
||||
|
||||
if (channel != null) {
|
||||
user.selectedChannel(channel);
|
||||
} else if (defaultChannel != null) {
|
||||
user.selectedChannel(defaultChannel);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void loadConfigs() {
|
||||
this.getDataFolder().mkdir();
|
||||
|
||||
try {
|
||||
final ConfigLoader<YamlConfigurationLoader> loader = new ConfigLoader<>(YamlConfigurationLoader.class);
|
||||
|
||||
this.carbonSettings = CarbonSettings.loadFrom(loader.loadConfig("config.yml"));
|
||||
this.commandSettings = CommandSettingsRegistry.loadFrom(loader.loadConfig("commands.yml"));
|
||||
this.channelSettings = ChannelSettings.loadFrom(loader.loadConfig("channels.yml"));
|
||||
this.translations = CarbonTranslations.loadFrom(loader.loadConfig("language.yml"));
|
||||
this.moderationSettings = ModerationSettings.loadFrom(loader.loadConfig("moderation.yml"));
|
||||
} catch(final ConfigurateException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public File dataDirectory() {
|
||||
return this.getDataFolder();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull String resolveName(final @NonNull UUID uuid) {
|
||||
if (this.messageService() instanceof RedisMessageService) {
|
||||
final RedisMessageService redis = (RedisMessageService) this.messageService();
|
||||
|
||||
final String value = redis.get("UUID:" + uuid.toString());
|
||||
|
||||
if (value != null) {
|
||||
return value;
|
||||
} else {
|
||||
final String name = Bukkit.getOfflinePlayer(uuid).getName();
|
||||
|
||||
if (name != null) {
|
||||
redis.set("UUID:" + uuid.toString(), name);
|
||||
|
||||
return name;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
final String name = Bukkit.getOfflinePlayer(uuid).getName();
|
||||
|
||||
if (name != null) {
|
||||
return name;
|
||||
}
|
||||
|
||||
return uuid.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull UUID resolveUUID(final @NonNull String name) {
|
||||
if (this.messageService() instanceof RedisMessageService) {
|
||||
final RedisMessageService redis = (RedisMessageService) this.messageService();
|
||||
|
||||
final String value = redis.get("Name:" + name);
|
||||
|
||||
if (value != null) {
|
||||
return UUID.fromString(value);
|
||||
} else {
|
||||
final UUID uuid = Bukkit.getOfflinePlayer(name).getUniqueId();
|
||||
redis.set("UUID:" + uuid.toString(), name);
|
||||
|
||||
return uuid;
|
||||
}
|
||||
}
|
||||
|
||||
return Bukkit.getOfflinePlayer(name).getUniqueId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cacheUUID(final @NonNull String name, final @NonNull UUID uuid) {
|
||||
if (this.messageService() instanceof RedisMessageService) {
|
||||
((RedisMessageService) this.messageService()).set(name, uuid.toString());
|
||||
}
|
||||
}
|
||||
|
||||
public @NonNull MessageManager messageManager() {
|
||||
return this.messageManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull UserService<BukkitPlayerUser> userService() {
|
||||
return this.userService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull ChannelRegistry channelRegistry() {
|
||||
return this.channelManager.registry();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull CarbonTranslations translations() {
|
||||
return this.translations;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull ModerationSettings moderationSettings() {
|
||||
return this.moderationSettings;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull CarbonSettings carbonSettings() {
|
||||
return this.carbonSettings;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull ChannelSettings channelSettings() {
|
||||
return this.channelSettings;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull CommandSettingsRegistry commandSettings() {
|
||||
return this.commandSettings;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull CommandManager<CarbonUser> commandManager() {
|
||||
return this.commandManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull MessageProcessor messageProcessor() {
|
||||
return this.messageProcessor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull MessageService messageService() {
|
||||
return this.messageManager().messageService();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull Logger logger() {
|
||||
return this.logger;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull Path dataFolder() {
|
||||
return this.getDataFolder().toPath();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull GsonComponentSerializer gsonSerializer() {
|
||||
return BukkitComponentSerializer.gson();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull String version() {
|
||||
return this.getDescription().getVersion();
|
||||
}
|
||||
}
|
||||
-101
@@ -1,101 +0,0 @@
|
||||
package net.draycia.carbon.bukkit.listeners;
|
||||
|
||||
import net.draycia.carbon.api.channels.ChatChannel;
|
||||
import net.draycia.carbon.api.channels.TextChannel;
|
||||
import net.draycia.carbon.api.users.CarbonUser;
|
||||
import net.draycia.carbon.api.users.ConsoleUser;
|
||||
import net.draycia.carbon.bukkit.CarbonChatBukkit;
|
||||
import net.draycia.carbon.api.users.PlayerUser;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.serializer.plain.PlainComponentSerializer;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.AsyncPlayerChatEvent;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class BukkitChatHandler implements Listener {
|
||||
|
||||
private final @NonNull CarbonChatBukkit carbonChat;
|
||||
|
||||
public BukkitChatHandler(final @NonNull CarbonChatBukkit carbonChat) {
|
||||
this.carbonChat = carbonChat;
|
||||
}
|
||||
|
||||
// Chat messages
|
||||
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
|
||||
public void onPlayerChat(final @NonNull AsyncPlayerChatEvent event) {
|
||||
final PlayerUser user = this.carbonChat.userService().wrap(event.getPlayer().getUniqueId());
|
||||
ChatChannel channel = user.selectedChannel();
|
||||
|
||||
if (channel == null) {
|
||||
channel = this.carbonChat.channelRegistry().defaultValue();
|
||||
}
|
||||
|
||||
if (channel.options().shouldCancelChatEvent()) {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
|
||||
for (final ChatChannel entry : this.carbonChat.channelRegistry()) {
|
||||
if (!(entry instanceof TextChannel)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
final TextChannel textChannel = (TextChannel) entry;
|
||||
final String prefix = textChannel.messagePrefix();
|
||||
|
||||
if (prefix == null || prefix.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (event.getMessage().startsWith(prefix)) {
|
||||
if (entry.canPlayerUse(user)) {
|
||||
event.setMessage(event.getMessage().substring(prefix.length()));
|
||||
channel = entry;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
final ChatChannel selectedChannel = channel;
|
||||
|
||||
if (!selectedChannel.canPlayerUse(user)) {
|
||||
return;
|
||||
}
|
||||
|
||||
event.getRecipients().clear();
|
||||
|
||||
if (event.isAsynchronous()) {
|
||||
final Map<CarbonUser, Component> messages =
|
||||
selectedChannel.parseMessage(user, event.getMessage());
|
||||
|
||||
for (final Map.Entry<CarbonUser, Component> entry : messages.entrySet()) {
|
||||
if (entry.getValue().equals(Component.empty())) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (user instanceof ConsoleUser) {
|
||||
event.setFormat(PlainComponentSerializer.plain().serialize(entry.getValue())
|
||||
.replaceAll("(?:[^%]|\\A)%(?:[^%]|\\z)", "%%"));
|
||||
} else {
|
||||
entry.getKey().sendMessage(user.identity(), entry.getValue());
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Bukkit.getScheduler().runTaskAsynchronously(this.carbonChat, () -> {
|
||||
selectedChannel.sendComponentsAndLog(user.identity(),
|
||||
selectedChannel.parseMessage(user, event.getMessage()));
|
||||
|
||||
// if (this.carbonChat.getConfig().getBoolean("show-tips")) {
|
||||
// this.carbonChat.logger().info("Tip: Sync chat event! I cannot set the message format due to this. :(");
|
||||
// this.carbonChat.logger().info("Tip: To 'solve' this, do a binary search and see which plugin is triggering");
|
||||
// this.carbonChat.logger().info("Tip: sync chat events and causing this, and let that plugin author know.");
|
||||
// }
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
-41
@@ -1,41 +0,0 @@
|
||||
package net.draycia.carbon.bukkit.listeners;
|
||||
|
||||
import net.draycia.carbon.api.events.UserEvent;
|
||||
import net.draycia.carbon.api.events.misc.CarbonEvents;
|
||||
import net.draycia.carbon.bukkit.CarbonChatBukkit;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
|
||||
public class BukkitLoginHandler implements Listener {
|
||||
|
||||
private final @NonNull CarbonChatBukkit carbonChat;
|
||||
|
||||
public BukkitLoginHandler(final @NonNull CarbonChatBukkit carbonChat) {
|
||||
this.carbonChat = carbonChat;
|
||||
}
|
||||
|
||||
@SuppressWarnings("argument.type.incompatible")
|
||||
@EventHandler
|
||||
public void onPlayerJoin(final @NonNull PlayerJoinEvent event) {
|
||||
this.carbonChat.cacheUUID(event.getPlayer().getName(), event.getPlayer().getUniqueId());
|
||||
|
||||
this.carbonChat.userService().wrapLater(event.getPlayer().getUniqueId()).thenAcceptAsync(user -> {
|
||||
final UserEvent.Join joinEvent = new UserEvent.Join(user);
|
||||
|
||||
CarbonEvents.post(joinEvent);
|
||||
});
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerLeave(final @NonNull PlayerQuitEvent event) {
|
||||
this.carbonChat.userService().wrapLater(event.getPlayer().getUniqueId()).thenAcceptAsync(user -> {
|
||||
final UserEvent.Leave leaveEvent = new UserEvent.Leave(user);
|
||||
|
||||
CarbonEvents.post(leaveEvent);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
-54
@@ -1,54 +0,0 @@
|
||||
package net.draycia.carbon.bukkit.listeners.events;
|
||||
|
||||
import net.draycia.carbon.api.events.ChatFormatEvent;
|
||||
import net.draycia.carbon.bukkit.util.CarbonUtils;
|
||||
import net.draycia.carbon.api.events.misc.CarbonEvents;
|
||||
import net.draycia.carbon.api.events.ChatComponentEvent;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
import net.kyori.event.PostOrders;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class ItemLinkHandler {
|
||||
|
||||
public ItemLinkHandler() {
|
||||
CarbonEvents.register(ChatComponentEvent.class, PostOrders.FIRST, false, event -> {
|
||||
// Handle item linking placeholders
|
||||
final Player player = Bukkit.getPlayer(event.sender().uuid());
|
||||
|
||||
if (player == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!player.hasPermission("carbonchat.itemlink")) {
|
||||
return;
|
||||
}
|
||||
|
||||
final Component itemComponent = CarbonUtils.createComponent(player);
|
||||
|
||||
if (itemComponent.equals(Component.empty())) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (final Pattern pattern : event.channel().itemLinkPatterns()) {
|
||||
if (pattern.matcher(event.originalMessage()).find()) {
|
||||
final TextComponent component = (TextComponent) event.component()
|
||||
.replaceText(configurer -> configurer.once().match(pattern).replacement(itemComponent));
|
||||
event.component(component);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
CarbonEvents.register(ChatFormatEvent.class, PostOrders.EARLY, false, event -> {
|
||||
for (final Pattern pattern : event.channel().itemLinkPatterns()) {
|
||||
event.message(pattern.matcher(event.message()).replaceAll("<white>$1</white>"));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
-17
@@ -1,17 +0,0 @@
|
||||
package net.draycia.carbon.bukkit.listeners.events;
|
||||
|
||||
import me.clip.placeholderapi.PlaceholderAPI;
|
||||
import net.draycia.carbon.api.events.misc.CarbonEvents;
|
||||
import net.draycia.carbon.api.events.PreChatFormatEvent;
|
||||
import net.kyori.event.PostOrders;
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
public class PlaceholderHandler {
|
||||
|
||||
public PlaceholderHandler() {
|
||||
CarbonEvents.register(PreChatFormatEvent.class, PostOrders.FIRST, false, event -> {
|
||||
event.format(PlaceholderAPI.setPlaceholders(Bukkit.getOfflinePlayer(event.user().uuid()), event.format()));
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
-33
@@ -1,33 +0,0 @@
|
||||
package net.draycia.carbon.bukkit.listeners.events;
|
||||
|
||||
import me.clip.placeholderapi.PlaceholderAPI;
|
||||
import net.draycia.carbon.api.events.misc.CarbonEvents;
|
||||
import net.draycia.carbon.api.events.ChatFormatEvent;
|
||||
import net.draycia.carbon.api.users.CarbonUser;
|
||||
import net.draycia.carbon.api.users.PlayerUser;
|
||||
import net.kyori.event.PostOrders;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class RelationalPlaceholderHandler {
|
||||
|
||||
public RelationalPlaceholderHandler() {
|
||||
CarbonEvents.register(ChatFormatEvent.class, PostOrders.FIRST, false, event -> {
|
||||
final CarbonUser targetUser = event.target();
|
||||
|
||||
if (!(targetUser instanceof PlayerUser)) {
|
||||
return;
|
||||
}
|
||||
|
||||
final Player sender = Bukkit.getPlayer(event.sender().uuid());
|
||||
final Player target = Bukkit.getPlayer(((PlayerUser) targetUser).uuid());
|
||||
|
||||
if (sender == null || target == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
event.format(PlaceholderAPI.setRelationalPlaceholders(sender, target, event.format()));
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
-112
@@ -1,112 +0,0 @@
|
||||
package net.draycia.carbon.bukkit.messaging;
|
||||
|
||||
import com.google.common.io.ByteArrayDataInput;
|
||||
import com.google.common.io.ByteArrayDataOutput;
|
||||
import com.google.common.io.ByteStreams;
|
||||
import net.draycia.carbon.bukkit.CarbonChatBukkit;
|
||||
import net.draycia.carbon.api.messaging.MessageService;
|
||||
import net.draycia.carbon.api.users.PlayerUser;
|
||||
import net.draycia.carbon.bukkit.util.BungeeChannelApi;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class BungeeMessageService implements MessageService {
|
||||
|
||||
private final @NonNull CarbonChatBukkit carbonChat;
|
||||
|
||||
private final @NonNull BungeeChannelApi api;
|
||||
|
||||
private final @NonNull Map<@NonNull String, @NonNull BiConsumer<@NonNull PlayerUser, @NonNull ByteArrayDataInput>> userLoadedListeners = new HashMap<>();
|
||||
|
||||
private final @NonNull Map<@NonNull String, @NonNull BiConsumer<@NonNull UUID, @NonNull ByteArrayDataInput>> userNotLoadedListeners = new HashMap<>();
|
||||
|
||||
private final @NonNull UUID serverUUID = UUID.randomUUID();
|
||||
|
||||
@SuppressWarnings("method.invocation.invalid")
|
||||
public BungeeMessageService(final @NonNull CarbonChatBukkit carbonChat) {
|
||||
this.carbonChat = carbonChat;
|
||||
|
||||
this.carbonChat.getServer().getMessenger().registerOutgoingPluginChannel(carbonChat, "BungeeCord");
|
||||
|
||||
this.api = BungeeChannelApi.of(carbonChat);
|
||||
|
||||
this.api.registerForwardListener((String channel, Player player, byte[] bytes) -> {
|
||||
try {
|
||||
final ByteArrayDataInput input = ByteStreams.newDataInput(bytes);
|
||||
|
||||
// Separated out for ease of debugging.
|
||||
final long mostServer = input.readLong();
|
||||
final long leastServer = input.readLong();
|
||||
|
||||
final UUID messageUUID = new UUID(mostServer, leastServer);
|
||||
|
||||
if (messageUUID.equals(this.serverUUID)) {
|
||||
return;
|
||||
}
|
||||
|
||||
final long mostUser = input.readLong();
|
||||
final long leastUser = input.readLong();
|
||||
|
||||
final UUID userUUID = new UUID(mostUser, leastUser);
|
||||
|
||||
this.receiveMessage(userUUID, channel, input);
|
||||
} catch (final IllegalStateException ignored) {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void receiveMessage(final @NonNull UUID uuid, final @NonNull String key, final @NonNull ByteArrayDataInput value) {
|
||||
final PlayerUser user = this.carbonChat.userService().wrapIfLoaded(uuid);
|
||||
|
||||
if (user != null) {
|
||||
for (final Map.Entry<String, BiConsumer<PlayerUser, ByteArrayDataInput>> listener : this.userLoadedListeners.entrySet()) {
|
||||
if (key.equals(listener.getKey())) {
|
||||
listener.getValue().accept(user, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (final Map.Entry<String, BiConsumer<UUID, ByteArrayDataInput>> listener : this.userNotLoadedListeners.entrySet()) {
|
||||
if (key.equals(listener.getKey())) {
|
||||
listener.getValue().accept(uuid, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerUserMessageListener(final @NonNull String key, final @NonNull BiConsumer<@NonNull PlayerUser, @NonNull ByteArrayDataInput> listener) {
|
||||
this.userLoadedListeners.put(key, listener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerUUIDMessageListener(final @NonNull String key, final @NonNull BiConsumer<@NonNull UUID, @NonNull ByteArrayDataInput> listener) {
|
||||
this.userNotLoadedListeners.put(key, listener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unregisterMessageListener(final @NonNull String key) {
|
||||
this.userLoadedListeners.remove(key);
|
||||
this.userNotLoadedListeners.remove(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendMessage(final @NonNull String key, final @NonNull UUID uuid, final @NonNull Consumer<ByteArrayDataOutput> consumer) {
|
||||
final ByteArrayDataOutput msg = ByteStreams.newDataOutput();
|
||||
|
||||
msg.writeLong(this.serverUUID.getMostSignificantBits());
|
||||
msg.writeLong(this.serverUUID.getLeastSignificantBits());
|
||||
msg.writeLong(uuid.getMostSignificantBits());
|
||||
msg.writeLong(uuid.getLeastSignificantBits());
|
||||
|
||||
consumer.accept(msg);
|
||||
|
||||
this.api.forward("ONLINE", key, msg.toByteArray());
|
||||
}
|
||||
|
||||
}
|
||||
-51
@@ -1,51 +0,0 @@
|
||||
package net.draycia.carbon.bukkit.users;
|
||||
|
||||
import net.draycia.carbon.api.users.ConsoleUser;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.kyori.adventure.audience.ForwardingAudience;
|
||||
import net.kyori.adventure.identity.Identity;
|
||||
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.ConsoleCommandSender;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
|
||||
public class BukkitConsoleUser implements ConsoleUser, ForwardingAudience.Single {
|
||||
|
||||
private final @NonNull Audience audience;
|
||||
|
||||
public BukkitConsoleUser(final @NonNull ConsoleCommandSender sender) {
|
||||
final Plugin plugin = Bukkit.getPluginManager().getPlugin("CarbonChat");
|
||||
|
||||
if (plugin == null) {
|
||||
throw new IllegalArgumentException("CarbonChat plugin cannot be found!");
|
||||
}
|
||||
|
||||
this.audience = BukkitAudiences.create(plugin).sender(sender);
|
||||
}
|
||||
|
||||
public BukkitConsoleUser() {
|
||||
this(Bukkit.getConsoleSender());
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull Identity identity() {
|
||||
return Identity.nil();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull Audience audience() {
|
||||
return this.audience;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPermission(final @NonNull String permission) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull String name() {
|
||||
return "Console";
|
||||
}
|
||||
|
||||
}
|
||||
-470
@@ -1,470 +0,0 @@
|
||||
package net.draycia.carbon.bukkit.users;
|
||||
|
||||
import me.clip.placeholderapi.PlaceholderAPI;
|
||||
import net.draycia.carbon.api.channels.ChatChannel;
|
||||
import net.draycia.carbon.api.users.CarbonUser;
|
||||
import net.draycia.carbon.api.users.PlayerUser;
|
||||
import net.draycia.carbon.api.users.UserChannelSettings;
|
||||
import net.draycia.carbon.bukkit.util.BungeeChannelApi;
|
||||
import net.draycia.carbon.common.channels.CarbonWhisperChannel;
|
||||
import net.draycia.carbon.bukkit.util.FunctionalityConstants;
|
||||
import net.draycia.carbon.bukkit.CarbonChatBukkit;
|
||||
import net.draycia.carbon.api.events.misc.CarbonEvents;
|
||||
import net.draycia.carbon.api.events.ChannelSwitchEvent;
|
||||
import net.draycia.carbon.api.events.PrivateMessageEvent;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.kyori.adventure.audience.ForwardingAudience;
|
||||
import net.kyori.adventure.identity.Identity;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.format.TextColor;
|
||||
import net.luckperms.api.LuckPerms;
|
||||
import net.luckperms.api.LuckPermsProvider;
|
||||
import net.luckperms.api.model.group.Group;
|
||||
import net.luckperms.api.model.user.User;
|
||||
import net.luckperms.api.query.QueryOptions;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
public class BukkitPlayerUser implements PlayerUser, ForwardingAudience.Single {
|
||||
|
||||
private final transient @NonNull CarbonChatBukkit carbonChat;
|
||||
private final @NonNull Map<@NonNull String, @NonNull SimpleUserChannelSettings> channelSettings = new HashMap<>();
|
||||
private final @NonNull List<@NonNull UUID> ignoredUsers = new ArrayList<>();
|
||||
private @NonNull UUID uuid;
|
||||
private boolean muted = false;
|
||||
private boolean shadowMuted = false;
|
||||
private boolean spyingWhispers = false;
|
||||
private @Nullable String nickname = null;
|
||||
private @Nullable UUID replyTarget = null;
|
||||
private @Nullable TextColor customChatColor = null;
|
||||
private @NonNull PingOptions pingOptions = new PingOptions(null, null);
|
||||
|
||||
private @Nullable String selectedChannelKey = null;
|
||||
private transient @Nullable ChatChannel selectedChannel = null;
|
||||
|
||||
private transient @Nullable Audience audience;
|
||||
|
||||
@SuppressWarnings({"initialization.fields.uninitialized", "method.invocation.invalid"})
|
||||
public BukkitPlayerUser() {
|
||||
final CarbonChatBukkit carbonChatBukkit =
|
||||
(CarbonChatBukkit) Bukkit.getPluginManager().getPlugin("CarbonChat");
|
||||
|
||||
if (carbonChatBukkit == null) {
|
||||
throw new IllegalArgumentException("CarbonChat plugin not present.");
|
||||
}
|
||||
|
||||
this.carbonChat = carbonChatBukkit;
|
||||
}
|
||||
|
||||
public BukkitPlayerUser(final @NonNull UUID uuid) {
|
||||
this();
|
||||
this.uuid = uuid;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull Identity identity() {
|
||||
if (this.hasPermission("carbonchat.hideidentity")) {
|
||||
return Identity.nil();
|
||||
}
|
||||
|
||||
return Identity.identity(this.uuid());
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull Audience audience() {
|
||||
if (this.audience == null) {
|
||||
this.audience = this.carbonChat.messageProcessor().audiences().player(this.uuid());
|
||||
}
|
||||
|
||||
return this.audience;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull String parsePlaceholders(final @NonNull String input) {
|
||||
final Player player = Bukkit.getPlayer(this.uuid());
|
||||
|
||||
if (player == null) {
|
||||
return input;
|
||||
}
|
||||
|
||||
return PlaceholderAPI.setPlaceholders(player, input);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull PingOptions pingOptions() {
|
||||
return this.pingOptions;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void pingOptions(final @NonNull PingOptions pingOptions) {
|
||||
this.pingOptions = pingOptions;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean online() {
|
||||
return Bukkit.getPlayer(this.uuid()) != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull UUID uuid() {
|
||||
return this.uuid;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull String nickname() {
|
||||
if (this.nickname != null) {
|
||||
return this.nickname;
|
||||
}
|
||||
|
||||
return this.displayName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPermission(final @NonNull String permission) {
|
||||
final User user = LuckPermsProvider.get().getUserManager().getUser(this.uuid());
|
||||
|
||||
if (user == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return user.getCachedData().getPermissionData().checkPermission(permission).asBoolean();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void nickname(@Nullable String newNickname) {
|
||||
this.nickname = newNickname;
|
||||
|
||||
final OfflinePlayer player = Bukkit.getOfflinePlayer(this.uuid());
|
||||
|
||||
if (player.isOnline()) {
|
||||
if (newNickname != null) {
|
||||
final Component component = this.carbonChat.messageProcessor().processMessage(newNickname);
|
||||
newNickname = CarbonChatBukkit.LEGACY.serialize(component);
|
||||
}
|
||||
|
||||
final Player onlinePlayer = player.getPlayer();
|
||||
|
||||
if (onlinePlayer != null) {
|
||||
onlinePlayer.setDisplayName(newNickname);
|
||||
onlinePlayer.setPlayerListName(newNickname);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull String displayName() {
|
||||
final Player player = Bukkit.getPlayer(this.uuid());
|
||||
|
||||
if (player != null) {
|
||||
return player.getDisplayName();
|
||||
}
|
||||
|
||||
return this.name();
|
||||
}
|
||||
|
||||
public void displayName(final @Nullable String displayName) {
|
||||
final Player player = Bukkit.getPlayer(this.uuid());
|
||||
|
||||
if (player != null) {
|
||||
player.setDisplayName(displayName);
|
||||
}
|
||||
}
|
||||
|
||||
public @NonNull String name() {
|
||||
return this.carbonChat.resolveName(this.uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable TextColor customChatColor() {
|
||||
return this.customChatColor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void customChatColor(final @Nullable TextColor customChatColor) {
|
||||
this.customChatColor = customChatColor;
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("argument.type.incompatible")
|
||||
public @Nullable ChatChannel selectedChannel() {
|
||||
if (this.selectedChannel != null) {
|
||||
return this.selectedChannel;
|
||||
}
|
||||
|
||||
if (this.selectedChannelKey != null) {
|
||||
if (this.selectedChannelKey.equals("whisper") && this.replyTarget != null) {
|
||||
this.selectedChannel = new CarbonWhisperChannel(this,
|
||||
this.carbonChat.userService().wrap(this.replyTarget));
|
||||
|
||||
return this.selectedChannel;
|
||||
}
|
||||
|
||||
return this.carbonChat.channelRegistry().getOrDefault(this.selectedChannelKey);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void selectedChannel(final @NonNull ChatChannel chatChannel) {
|
||||
final String failureMessage = chatChannel.switchFailureMessage();
|
||||
|
||||
final ChannelSwitchEvent event = new ChannelSwitchEvent(chatChannel, this, failureMessage);
|
||||
|
||||
CarbonEvents.post(event);
|
||||
|
||||
if (event.cancelled()) {
|
||||
this.sendMessage(Identity.nil(), this.carbonChat.messageProcessor().processMessage(event.failureMessage(),
|
||||
"channel", chatChannel.name()));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
this.selectedChannelKey = chatChannel.key();
|
||||
this.selectedChannel = chatChannel;
|
||||
|
||||
final OfflinePlayer player = Bukkit.getOfflinePlayer(this.uuid());
|
||||
|
||||
if (player.isOnline()) {
|
||||
this.sendMessage(Identity.nil(), this.carbonChat.messageProcessor().processMessage(chatChannel.switchMessage(),
|
||||
"color", "<" + chatChannel.channelColor(this).toString() + ">",
|
||||
"channel", chatChannel.name()));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearSelectedChannel() {
|
||||
this.selectedChannel(this.carbonChat.channelRegistry().defaultValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean ignoringUser(final @NonNull UUID uuid) {
|
||||
return this.ignoredUsers.contains(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void ignoringUser(final @NonNull UUID uuid, final boolean ignoring) {
|
||||
if (ignoring) {
|
||||
this.ignoredUsers.add(uuid);
|
||||
} else {
|
||||
this.ignoredUsers.remove(uuid);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull Iterable<@NonNull UUID> ignoredUsers() {
|
||||
return this.ignoredUsers;
|
||||
}
|
||||
|
||||
private transient final LuckPerms luckPerms = LuckPermsProvider.get();
|
||||
|
||||
@Override
|
||||
public boolean hasGroup(final @NonNull String groupName) {
|
||||
final Group group = this.luckPerms.getGroupManager().getGroup(groupName);
|
||||
|
||||
if (group == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return this.hasGroup(group);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasGroup(final @NonNull Group group) {
|
||||
return this.groups().contains(group);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull Collection<@NonNull Group> groups() {
|
||||
final User user = this.luckPerms.getUserManager().getUser(this.uuid());
|
||||
|
||||
if (user != null) {
|
||||
return user.getInheritedGroups(QueryOptions.nonContextual());
|
||||
} else {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable Group primaryGroup() {
|
||||
final User user = this.luckPerms.getUserManager().getUser(this.uuid());
|
||||
|
||||
if (user == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return this.luckPerms.getGroupManager().getGroup(user.getPrimaryGroup());
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull Iterable<@NonNull CarbonUser> ignoredChatUsers() {
|
||||
final List<CarbonUser> users = new ArrayList<>();
|
||||
|
||||
for (final UUID uuid : this.ignoredUsers) {
|
||||
users.add(this.carbonChat.userService().wrap(uuid));
|
||||
}
|
||||
|
||||
return users;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void muted(final boolean muted) {
|
||||
this.muted = muted;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean muted() {
|
||||
return this.muted;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void shadowMuted(final boolean shadowMuted) {
|
||||
this.shadowMuted = shadowMuted;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean shadowMuted() {
|
||||
return this.shadowMuted;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable UUID replyTarget() {
|
||||
return this.replyTarget;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void replyTarget(final @Nullable UUID target) {
|
||||
this.replyTarget = target;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull UserChannelSettings channelSettings(final @NonNull ChatChannel channel) {
|
||||
return this.channelSettings.computeIfAbsent(channel.key(), name -> {
|
||||
return new SimpleUserChannelSettings(this.uuid);
|
||||
});
|
||||
}
|
||||
|
||||
public @NonNull Map<@NonNull String, @NonNull ? extends UserChannelSettings> channelSettings() {
|
||||
return this.channelSettings;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void spyingWhispers(final boolean spyingWhispers) {
|
||||
this.spyingWhispers = spyingWhispers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean spyingWhispers() {
|
||||
return this.spyingWhispers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendMessage(final @NonNull PlayerUser sender, final @NonNull String message) {
|
||||
if (this.ignoringUser(sender) || sender.ignoringUser(this)) {
|
||||
return;
|
||||
}
|
||||
|
||||
final String toPlayerFormat = this.carbonChat.carbonSettings().whisperOptions().senderFormat();
|
||||
final String fromPlayerFormat = this.carbonChat.carbonSettings().whisperOptions().receiverFormat();
|
||||
|
||||
final OfflinePlayer offlineSender = Bukkit.getOfflinePlayer(sender.uuid());
|
||||
final String senderName = sender.nickname();
|
||||
|
||||
final OfflinePlayer offlineTarget = Bukkit.getOfflinePlayer(this.uuid());
|
||||
final String targetName = this.nickname();
|
||||
|
||||
final Component toPlayerComponent = this.carbonChat.messageProcessor().processMessage(toPlayerFormat,
|
||||
"message", message, "target", targetName, "receiver", targetName, "sender", senderName);
|
||||
|
||||
final Component fromPlayerComponent = this.carbonChat.messageProcessor().processMessage(fromPlayerFormat,
|
||||
"message", message, "target", targetName, "receiver", targetName, "sender", senderName);
|
||||
|
||||
final PrivateMessageEvent event = new PrivateMessageEvent(sender, this, toPlayerComponent, fromPlayerComponent, message);
|
||||
|
||||
CarbonEvents.post(event);
|
||||
|
||||
if (event.cancelled()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (offlineTarget.isOnline()) {
|
||||
if (offlineSender.isOnline()) {
|
||||
sender.sendMessage(sender.identity(), toPlayerComponent);
|
||||
|
||||
if (sender.shadowMuted()) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.sendMessage(sender.identity(), fromPlayerComponent);
|
||||
|
||||
sender.replyTarget(this);
|
||||
this.replyTarget(sender);
|
||||
}
|
||||
} else if (offlineSender.isOnline()) {
|
||||
final String targetNameFinal = targetName;
|
||||
final String senderNameFinal = senderName;
|
||||
|
||||
if (!FunctionalityConstants.HAS_PROXY) {
|
||||
final String playerOfflineFormat = this.carbonChat.translations().otherPlayerOffline();
|
||||
|
||||
final Component playerOfflineComponent = this.carbonChat.messageProcessor().processMessage(playerOfflineFormat,
|
||||
"message", message, "target", targetNameFinal, "sender", senderNameFinal);
|
||||
|
||||
sender.sendMessage(Identity.nil(), playerOfflineComponent);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
BungeeChannelApi.of(this.carbonChat).getPlayerList("ALL").thenAccept(list -> {
|
||||
final OfflinePlayer player = Bukkit.getOfflinePlayer(this.uuid());
|
||||
final String name = player.getName();
|
||||
|
||||
if (name == null || !list.contains(name)) {
|
||||
final String playerOfflineFormat = this.carbonChat.translations().otherPlayerOffline();
|
||||
|
||||
final Component playerOfflineComponent = this.carbonChat.messageProcessor().processMessage(playerOfflineFormat,
|
||||
"message", message, "target", targetNameFinal, "sender", senderNameFinal);
|
||||
|
||||
sender.sendMessage(Identity.nil(), playerOfflineComponent);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
sender.sendMessage(sender.identity(), toPlayerComponent);
|
||||
|
||||
this.carbonChat.messageManager().sendMessage("whisper-component", sender.uuid(), byteArray -> {
|
||||
byteArray.writeLong(this.uuid().getMostSignificantBits());
|
||||
byteArray.writeLong(this.uuid().getLeastSignificantBits());
|
||||
byteArray.writeUTF(this.carbonChat.gsonSerializer().serialize(fromPlayerComponent));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
for (final Player player : Bukkit.getOnlinePlayers()) {
|
||||
final PlayerUser user = this.carbonChat.userService().wrap(player.getUniqueId());
|
||||
|
||||
if (!user.spyingWhispers()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (user.uuid().equals(sender.uuid()) || user.uuid().equals(this.uuid())) {
|
||||
continue;
|
||||
}
|
||||
|
||||
user.sendMessage(sender.identity(), this.carbonChat.messageProcessor()
|
||||
.processMessage(this.carbonChat.translations().spyWhispers(), "message", message,
|
||||
"target", targetName, "sender", senderName));
|
||||
}
|
||||
}
|
||||
}
|
||||
-68
@@ -1,68 +0,0 @@
|
||||
package net.draycia.carbon.bukkit.users;
|
||||
|
||||
import net.draycia.carbon.api.CarbonChat;
|
||||
import net.draycia.carbon.api.CarbonChatProvider;
|
||||
import net.draycia.carbon.api.users.CarbonUser;
|
||||
import net.draycia.carbon.api.users.UserChannelSettings;
|
||||
import net.kyori.adventure.text.format.TextColor;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public class SimpleUserChannelSettings implements UserChannelSettings {
|
||||
|
||||
private final transient @NonNull CarbonChat carbonChat = CarbonChatProvider.carbonChat();
|
||||
private boolean spying;
|
||||
private boolean ignored;
|
||||
private @Nullable String color;
|
||||
private @NonNull UUID uuid;
|
||||
|
||||
@SuppressWarnings("initialization.fields.uninitialized")
|
||||
public SimpleUserChannelSettings(final @NonNull UUID uuid) {
|
||||
this.uuid = uuid;
|
||||
}
|
||||
|
||||
private @NonNull CarbonUser user() {
|
||||
return this.carbonChat.userService().wrap(this.uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean spying() {
|
||||
return this.spying;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void spying(final boolean spying) {
|
||||
this.spying = spying;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean ignored() {
|
||||
return this.ignored;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void ignoring(final boolean ignored) {
|
||||
this.ignored = ignored;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable TextColor color() {
|
||||
if (this.color == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return TextColor.fromHexString(this.color);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void color(final @Nullable TextColor color) {
|
||||
if (color == null) {
|
||||
this.color = null;
|
||||
} else {
|
||||
this.color = color.asHexString();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,546 +0,0 @@
|
||||
/**
|
||||
* https://github.com/leonardosnt/bungeechannelapi
|
||||
*
|
||||
* Copyright (C) 2017 leonardosnt <[email protected]>
|
||||
* Licensed under the MIT License. See LICENSE file in the project root for full license information.
|
||||
*/
|
||||
|
||||
package net.draycia.carbon.bukkit.util;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Queue;
|
||||
import java.util.WeakHashMap;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.bukkit.plugin.messaging.Messenger;
|
||||
import org.bukkit.plugin.messaging.PluginMessageListener;
|
||||
|
||||
import com.google.common.collect.Iterables;
|
||||
import com.google.common.io.ByteArrayDataInput;
|
||||
import com.google.common.io.ByteArrayDataOutput;
|
||||
import com.google.common.io.ByteStreams;
|
||||
|
||||
/**
|
||||
* Documentation copied from https://www.spigotmc.org/wiki/bukkit-bungee-plugin-messaging-channel/
|
||||
*
|
||||
* @author leonardosnt ([email protected])
|
||||
* @see <a href="https://www.spigotmc.org/wiki/bukkit-bungee-plugin-messaging-channel/">https://www.spigotmc.org/wiki/bukkit-bungee-plugin-messaging-channel/</a>
|
||||
*/
|
||||
@SuppressWarnings("all")
|
||||
public class BungeeChannelApi {
|
||||
|
||||
private static WeakHashMap<Plugin, BungeeChannelApi> registeredInstances = new WeakHashMap<>();
|
||||
|
||||
private final PluginMessageListener messageListener;
|
||||
private final Plugin plugin;
|
||||
private final Map<String, Queue<CompletableFuture<?>>> callbackMap;
|
||||
|
||||
private Map<String, ForwardConsumer> forwardListeners;
|
||||
private ForwardConsumer globalForwardListener;
|
||||
|
||||
/**
|
||||
* Get or create new BungeeChannelApi instance
|
||||
*
|
||||
* @param plugin the plugin instance.
|
||||
* @return the BungeeChannelApi instance for the {@code plugin}
|
||||
* @throws NullPointerException if the {@code plugin} is {@code null}
|
||||
*/
|
||||
public synchronized static BungeeChannelApi of(Plugin plugin) {
|
||||
return registeredInstances.compute(plugin, (k, v) -> {
|
||||
if (v == null) v = new BungeeChannelApi(plugin);
|
||||
return v;
|
||||
});
|
||||
}
|
||||
|
||||
public BungeeChannelApi(Plugin plugin) {
|
||||
this.plugin = Objects.requireNonNull(plugin, "plugin cannot be null");
|
||||
this.callbackMap = new HashMap<>();
|
||||
|
||||
// Prevent dev's from registering multiple channel listeners,
|
||||
// by unregistering the old instance.
|
||||
synchronized (registeredInstances) {
|
||||
registeredInstances.compute(plugin, (k, oldInstance) -> {
|
||||
if (oldInstance != null) oldInstance.unregister();
|
||||
return this;
|
||||
});
|
||||
}
|
||||
|
||||
this.messageListener = this::onPluginMessageReceived;
|
||||
|
||||
Messenger messenger = Bukkit.getServer().getMessenger();
|
||||
messenger.registerOutgoingPluginChannel(plugin, "BungeeCord");
|
||||
messenger.registerIncomingPluginChannel(plugin, "BungeeCord", messageListener);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a global listener for all 'forwarded' messages.
|
||||
*
|
||||
* @param globalListener the listener
|
||||
* @see <a href="https://www.spigotmc.org/wiki/bukkit-bungee-plugin-messaging-channel/#forward">https://www.spigotmc.org/wiki/bukkit-bungee-plugin-messaging-channel/#forward</a>
|
||||
*/
|
||||
public void registerForwardListener(ForwardConsumer globalListener) {
|
||||
this.globalForwardListener = globalListener;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a listener for all 'forwarded' messages in a specific subchannel.
|
||||
*
|
||||
* @param channelName the subchannel name
|
||||
* @param listener the listener
|
||||
* @see <a href="https://www.spigotmc.org/wiki/bukkit-bungee-plugin-messaging-channel/#forward">https://www.spigotmc.org/wiki/bukkit-bungee-plugin-messaging-channel/#forward</a>
|
||||
*/
|
||||
public void registerForwardListener(String channelName, ForwardConsumer listener) {
|
||||
if (forwardListeners == null) {
|
||||
forwardListeners = new HashMap<>();
|
||||
}
|
||||
synchronized (forwardListeners) {
|
||||
forwardListeners.put(channelName, listener);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the amount of players on a certain server, or on ALL the servers.
|
||||
*
|
||||
* @param serverName the server name of the server to get the player count of, or ALL to get the global player count
|
||||
* @return A {@link CompletableFuture} that, when completed, will return
|
||||
* the amount of players on a certain server, or on ALL the servers.
|
||||
* @throws IllegalArgumentException if there is no players online.
|
||||
*/
|
||||
public CompletableFuture<Integer> getPlayerCount(String serverName) {
|
||||
Player player = getFirstPlayer();
|
||||
CompletableFuture<Integer> future = new CompletableFuture<>();
|
||||
|
||||
synchronized (callbackMap) {
|
||||
callbackMap.compute("PlayerCount-" + serverName, this.computeQueueValue(future));
|
||||
}
|
||||
|
||||
ByteArrayDataOutput output = ByteStreams.newDataOutput();
|
||||
output.writeUTF("PlayerCount");
|
||||
output.writeUTF(serverName);
|
||||
player.sendPluginMessage(this.plugin, "BungeeCord", output.toByteArray());
|
||||
return future;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of players connected on a certain server, or on ALL the servers.
|
||||
*
|
||||
* @param serverName the name of the server to get the list of connected players, or ALL for global online player list
|
||||
* @return A {@link CompletableFuture} that, when completed, will return a
|
||||
* list of players connected on a certain server, or on ALL the servers.
|
||||
* @throws IllegalArgumentException if there is no players online.
|
||||
*/
|
||||
public CompletableFuture<List<String>> getPlayerList(String serverName) {
|
||||
Player player = getFirstPlayer();
|
||||
CompletableFuture<List<String>> future = new CompletableFuture<>();
|
||||
|
||||
synchronized (callbackMap) {
|
||||
callbackMap.compute("PlayerList-" + serverName, this.computeQueueValue(future));
|
||||
}
|
||||
|
||||
ByteArrayDataOutput output = ByteStreams.newDataOutput();
|
||||
output.writeUTF("PlayerList");
|
||||
output.writeUTF(serverName);
|
||||
player.sendPluginMessage(this.plugin, "BungeeCord", output.toByteArray());
|
||||
return future;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of server name strings, as defined in BungeeCord's config.yml.
|
||||
*
|
||||
* @return A {@link CompletableFuture} that, when completed, will return a
|
||||
* list of server name strings, as defined in BungeeCord's config.yml.
|
||||
* @throws IllegalArgumentException if there is no players online.
|
||||
*/
|
||||
public CompletableFuture<List<String>> getServers() {
|
||||
Player player = getFirstPlayer();
|
||||
CompletableFuture<List<String>> future = new CompletableFuture<>();
|
||||
|
||||
synchronized (callbackMap) {
|
||||
callbackMap.compute("GetServers", this.computeQueueValue(future));
|
||||
}
|
||||
|
||||
ByteArrayDataOutput output = ByteStreams.newDataOutput();
|
||||
output.writeUTF("GetServers");
|
||||
player.sendPluginMessage(this.plugin, "BungeeCord", output.toByteArray());
|
||||
return future;
|
||||
}
|
||||
|
||||
/**
|
||||
* Connects a player to said subserver.
|
||||
*
|
||||
* @param player the player you want to teleport.
|
||||
* @param serverName the name of server to connect to, as defined in BungeeCord config.yml.
|
||||
*/
|
||||
public void connect(Player player, String serverName) {
|
||||
ByteArrayDataOutput output = ByteStreams.newDataOutput();
|
||||
output.writeUTF("Connect");
|
||||
output.writeUTF(serverName);
|
||||
player.sendPluginMessage(this.plugin, "BungeeCord", output.toByteArray());
|
||||
}
|
||||
|
||||
/**
|
||||
* Connect a named player to said subserver.
|
||||
*
|
||||
* @param playerName name of the player to teleport.
|
||||
* @param server name of server to connect to, as defined in BungeeCord config.yml.
|
||||
* @throws IllegalArgumentException if there is no players online.
|
||||
*/
|
||||
public void connectOther(String playerName, String server) {
|
||||
Player player = getFirstPlayer();
|
||||
ByteArrayDataOutput output = ByteStreams.newDataOutput();
|
||||
|
||||
output.writeUTF("ConnectOther");
|
||||
output.writeUTF(playerName);
|
||||
output.writeUTF(server);
|
||||
|
||||
player.sendPluginMessage(this.plugin, "BungeeCord", output.toByteArray());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the (real) IP of a player.
|
||||
*
|
||||
* @param player The player you wish to get the IP of.
|
||||
* @return A {@link CompletableFuture} that, when completed, will return the (real) IP of {@code player}.
|
||||
*/
|
||||
public CompletableFuture<InetSocketAddress> getIp(Player player) {
|
||||
CompletableFuture<InetSocketAddress> future = new CompletableFuture<>();
|
||||
|
||||
synchronized (callbackMap) {
|
||||
callbackMap.compute("IP", this.computeQueueValue(future));
|
||||
}
|
||||
|
||||
ByteArrayDataOutput output = ByteStreams.newDataOutput();
|
||||
output.writeUTF("IP");
|
||||
player.sendPluginMessage(this.plugin, "BungeeCord", output.toByteArray());
|
||||
return future;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a message (as in, a chat message) to the specified player.
|
||||
*
|
||||
* @param playerName the name of the player to send the chat message.
|
||||
* @param message the message to send to the player.
|
||||
* @throws IllegalArgumentException if there is no players online.
|
||||
*/
|
||||
public void sendMessage(String playerName, String message) {
|
||||
Player player = getFirstPlayer();
|
||||
ByteArrayDataOutput output = ByteStreams.newDataOutput();
|
||||
|
||||
output.writeUTF("Message");
|
||||
output.writeUTF(playerName);
|
||||
output.writeUTF(message);
|
||||
player.sendPluginMessage(this.plugin, "BungeeCord", output.toByteArray());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get this server's name, as defined in BungeeCord's config.yml
|
||||
*
|
||||
* @return A {@link CompletableFuture} that, when completed, will return
|
||||
* the {@code server's} name, as defined in BungeeCord's config.yml.
|
||||
* @throws IllegalArgumentException if there is no players online.
|
||||
*/
|
||||
public CompletableFuture<String> getServer() {
|
||||
Player player = getFirstPlayer();
|
||||
CompletableFuture<String> future = new CompletableFuture<>();
|
||||
|
||||
synchronized (callbackMap) {
|
||||
callbackMap.compute("GetServer", this.computeQueueValue(future));
|
||||
}
|
||||
|
||||
ByteArrayDataOutput output = ByteStreams.newDataOutput();
|
||||
output.writeUTF("GetServer");
|
||||
player.sendPluginMessage(this.plugin, "BungeeCord", output.toByteArray());
|
||||
return future;
|
||||
}
|
||||
|
||||
/**
|
||||
* Request the UUID of this player.
|
||||
*
|
||||
* @param player The player whose UUID you requested.
|
||||
* @return A {@link CompletableFuture} that, when completed, will return the UUID of {@code player}.
|
||||
*/
|
||||
public CompletableFuture<String> getUUID(Player player) {
|
||||
CompletableFuture<String> future = new CompletableFuture<>();
|
||||
|
||||
synchronized (callbackMap) {
|
||||
callbackMap.compute("UUID", this.computeQueueValue(future));
|
||||
}
|
||||
|
||||
ByteArrayDataOutput output = ByteStreams.newDataOutput();
|
||||
output.writeUTF("UUID");
|
||||
player.sendPluginMessage(this.plugin, "BungeeCord", output.toByteArray());
|
||||
return future;
|
||||
}
|
||||
|
||||
/**
|
||||
* Request the UUID of any player connected to the BungeeCord proxy.
|
||||
*
|
||||
* @param playerName the name of the player whose UUID you would like.
|
||||
* @return A {@link CompletableFuture} that, when completed, will return the UUID of {@code playerName}.
|
||||
* @throws IllegalArgumentException if there is no players online.
|
||||
*/
|
||||
public CompletableFuture<String> getUUID(String playerName) {
|
||||
Player player = getFirstPlayer();
|
||||
CompletableFuture<String> future = new CompletableFuture<>();
|
||||
|
||||
synchronized (callbackMap) {
|
||||
callbackMap.compute("UUIDOther-" + playerName, this.computeQueueValue(future));
|
||||
}
|
||||
|
||||
ByteArrayDataOutput output = ByteStreams.newDataOutput();
|
||||
output.writeUTF("UUIDOther");
|
||||
output.writeUTF(playerName);
|
||||
player.sendPluginMessage(this.plugin, "BungeeCord", output.toByteArray());
|
||||
return future;
|
||||
}
|
||||
|
||||
/**
|
||||
* Request the IP of any server on this proxy.
|
||||
*
|
||||
* @param serverName the name of the server.
|
||||
* @return A {@link CompletableFuture} that, when completed, will return the requested ip.
|
||||
* @throws IllegalArgumentException if there is no players online.
|
||||
*/
|
||||
public CompletableFuture<InetSocketAddress> getServerIp(String serverName) {
|
||||
Player player = getFirstPlayer();
|
||||
CompletableFuture<InetSocketAddress> future = new CompletableFuture<>();
|
||||
|
||||
synchronized (callbackMap) {
|
||||
callbackMap.compute("ServerIP-" + serverName, this.computeQueueValue(future));
|
||||
}
|
||||
|
||||
ByteArrayDataOutput output = ByteStreams.newDataOutput();
|
||||
output.writeUTF("ServerIP");
|
||||
output.writeUTF(serverName);
|
||||
player.sendPluginMessage(this.plugin, "BungeeCord", output.toByteArray());
|
||||
return future;
|
||||
}
|
||||
|
||||
/**
|
||||
* Kick any player on this proxy.
|
||||
*
|
||||
* @param playerName the name of the player.
|
||||
* @param kickMessage the reason the player is kicked with.
|
||||
* @throws IllegalArgumentException if there is no players online.
|
||||
*/
|
||||
public void kickPlayer(String playerName, String kickMessage) {
|
||||
Player player = getFirstPlayer();
|
||||
CompletableFuture<InetSocketAddress> future = new CompletableFuture<>();
|
||||
|
||||
synchronized (callbackMap) {
|
||||
callbackMap.compute("KickPlayer", this.computeQueueValue(future));
|
||||
}
|
||||
|
||||
ByteArrayDataOutput output = ByteStreams.newDataOutput();
|
||||
output.writeUTF("KickPlayer");
|
||||
output.writeUTF(playerName);
|
||||
output.writeUTF(kickMessage);
|
||||
player.sendPluginMessage(this.plugin, "BungeeCord", output.toByteArray());
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a custom plugin message to said server. This is one of the most useful channels ever.
|
||||
* <b>Remember, the sending and receiving server(s) need to have a player online.</b>
|
||||
*
|
||||
* @param server the name of the server to send to,
|
||||
* ALL to send to every server (except the one sending the plugin message),
|
||||
* or ONLINE to send to every server that's online (except the one sending the plugin message).
|
||||
*
|
||||
* @param channelName Subchannel for plugin usage.
|
||||
* @param data data to send.
|
||||
* @throws IllegalArgumentException if there is no players online.
|
||||
*/
|
||||
public void forward(String server, String channelName, byte[] data) {
|
||||
Player player = getFirstPlayer();
|
||||
|
||||
ByteArrayDataOutput output = ByteStreams.newDataOutput();
|
||||
output.writeUTF("Forward");
|
||||
output.writeUTF(server);
|
||||
output.writeUTF(channelName);
|
||||
output.writeShort(data.length);
|
||||
output.write(data);
|
||||
player.sendPluginMessage(this.plugin, "BungeeCord", output.toByteArray());
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a custom plugin message to specific player.
|
||||
*
|
||||
* @param playerName the name of the player to send to.
|
||||
* @param channelName Subchannel for plugin usage.
|
||||
* @param data data to send.
|
||||
* @throws IllegalArgumentException if there is no players online.
|
||||
*/
|
||||
public void forwardToPlayer(String playerName, String channelName, byte[] data) {
|
||||
Player player = getFirstPlayer();
|
||||
|
||||
ByteArrayDataOutput output = ByteStreams.newDataOutput();
|
||||
output.writeUTF("ForwardToPlayer");
|
||||
output.writeUTF(playerName);
|
||||
output.writeUTF(channelName);
|
||||
output.writeShort(data.length);
|
||||
output.write(data);
|
||||
player.sendPluginMessage(this.plugin, "BungeeCord", output.toByteArray());
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private void onPluginMessageReceived(String channel, Player player, byte[] message) {
|
||||
if (!channel.equalsIgnoreCase("BungeeCord")) return;
|
||||
|
||||
ByteArrayDataInput input = ByteStreams.newDataInput(message);
|
||||
String subchannel = input.readUTF();
|
||||
|
||||
synchronized (callbackMap) {
|
||||
Queue<CompletableFuture<?>> callbacks;
|
||||
|
||||
if (subchannel.equals("PlayerCount") || subchannel.equals("PlayerList") ||
|
||||
subchannel.equals("UUIDOther") || subchannel.equals("ServerIP")) {
|
||||
String identifier = input.readUTF(); // Server/player name
|
||||
callbacks = callbackMap.get(subchannel + "-" + identifier);
|
||||
|
||||
if (callbacks == null || callbacks.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
CompletableFuture<?> callback = callbacks.poll();
|
||||
|
||||
try {
|
||||
switch (subchannel) {
|
||||
case "PlayerCount":
|
||||
((CompletableFuture<Integer>) callback).complete(Integer.valueOf(input.readInt()));
|
||||
break;
|
||||
|
||||
case "PlayerList":
|
||||
((CompletableFuture<List<String>>) callback).complete(Arrays.asList(input.readUTF().split(", ")));
|
||||
break;
|
||||
|
||||
case "UUIDOther":
|
||||
((CompletableFuture<String>) callback).complete(input.readUTF());
|
||||
break;
|
||||
|
||||
case "ServerIP": {
|
||||
String ip = input.readUTF();
|
||||
int port = input.readUnsignedShort();
|
||||
((CompletableFuture<InetSocketAddress>) callback).complete(new InetSocketAddress(ip, port));
|
||||
break;
|
||||
}
|
||||
}
|
||||
} catch(Exception ex) {
|
||||
callback.completeExceptionally(ex);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
callbacks = callbackMap.get(subchannel);
|
||||
|
||||
if (callbacks == null) {
|
||||
short dataLength = input.readShort();
|
||||
byte[] data = new byte[dataLength];
|
||||
input.readFully(data);
|
||||
|
||||
if (globalForwardListener != null) {
|
||||
globalForwardListener.accept(subchannel, player, data);
|
||||
}
|
||||
|
||||
if (forwardListeners != null) {
|
||||
synchronized (forwardListeners) {
|
||||
ForwardConsumer listener = forwardListeners.get(subchannel);
|
||||
if (listener != null) {
|
||||
listener.accept(subchannel, player, data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (callbacks.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
final CompletableFuture<?> callback = callbacks.poll();
|
||||
|
||||
try {
|
||||
switch (subchannel) {
|
||||
case "GetServers":
|
||||
((CompletableFuture<List<String>>) callback).complete(Arrays.asList(input.readUTF().split(", ")));
|
||||
break;
|
||||
|
||||
case "GetServer":
|
||||
case "UUID":
|
||||
((CompletableFuture<String>) callback).complete(input.readUTF());
|
||||
break;
|
||||
|
||||
case "IP": {
|
||||
String ip = input.readUTF();
|
||||
int port = input.readInt();
|
||||
((CompletableFuture<InetSocketAddress>) callback).complete(new InetSocketAddress(ip, port));
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
} catch(Exception ex) {
|
||||
callback.completeExceptionally(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Unregister message channels.
|
||||
*/
|
||||
public void unregister() {
|
||||
Messenger messenger = Bukkit.getServer().getMessenger();
|
||||
messenger.unregisterIncomingPluginChannel(plugin, "BungeeCord", messageListener);
|
||||
messenger.unregisterOutgoingPluginChannel(plugin);
|
||||
callbackMap.clear();
|
||||
}
|
||||
|
||||
private BiFunction<String, Queue<CompletableFuture<?>>, Queue<CompletableFuture<?>>> computeQueueValue(CompletableFuture<?> queueValue) {
|
||||
return (key, value) -> {
|
||||
if (value == null) value = new ArrayDeque<CompletableFuture<?>>();
|
||||
value.add(queueValue);
|
||||
return value;
|
||||
};
|
||||
}
|
||||
|
||||
private Player getFirstPlayer() {
|
||||
/**
|
||||
* if Bukkit Version < 1.7.10 then Bukkit.getOnlinePlayers() will return
|
||||
* a Player[] otherwise it'll return a Collection<? extends Player>.
|
||||
*/
|
||||
Player firstPlayer = getFirstPlayer0(Bukkit.getOnlinePlayers());
|
||||
|
||||
if (firstPlayer == null) {
|
||||
throw new IllegalArgumentException("Bungee Messaging Api requires at least one player to be online.");
|
||||
}
|
||||
|
||||
return firstPlayer;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private Player getFirstPlayer0(Player[] playerArray) {
|
||||
return playerArray.length > 0 ? playerArray[0] : null;
|
||||
}
|
||||
|
||||
private Player getFirstPlayer0(Collection<? extends Player> playerCollection) {
|
||||
return Iterables.getFirst(playerCollection, null);
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
public interface ForwardConsumer {
|
||||
void accept(String channel, Player player, byte[] data);
|
||||
}
|
||||
}
|
||||
-87
@@ -1,87 +0,0 @@
|
||||
package net.draycia.carbon.bukkit.util;
|
||||
|
||||
import net.draycia.carbon.api.channels.ChatChannel;
|
||||
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
|
||||
import net.draycia.carbon.bukkit.CarbonChatBukkit;
|
||||
import net.draycia.carbon.api.users.PlayerUser;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
|
||||
public class CarbonPlaceholders extends PlaceholderExpansion {
|
||||
|
||||
private final @NonNull CarbonChatBukkit carbonChat;
|
||||
|
||||
public CarbonPlaceholders(final @NonNull CarbonChatBukkit carbonChat) {
|
||||
this.carbonChat = carbonChat;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull String getIdentifier() {
|
||||
return "carbonchat";
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull String getAuthor() {
|
||||
return "Draycia (Vicarious#0001)";
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull String getVersion() {
|
||||
return "1.0.0";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean persist() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull String onPlaceholderRequest(final @NonNull Player player, final @NonNull String identifier) {
|
||||
final String key = identifier.toLowerCase();
|
||||
|
||||
if (key.startsWith("can_use_")) {
|
||||
final String value = key.replace("can_use_", "");
|
||||
|
||||
final ChatChannel channel = this.carbonChat.channelRegistry().get(value);
|
||||
|
||||
if (channel == null) {
|
||||
return "false";
|
||||
}
|
||||
|
||||
final PlayerUser user = this.carbonChat.userService().wrap(player.getUniqueId());
|
||||
|
||||
return String.valueOf(channel.canPlayerUse(user));
|
||||
} else if (key.startsWith("can_see_")) {
|
||||
final String value = key.replace("can_see_", "");
|
||||
|
||||
final ChatChannel channel = this.carbonChat.channelRegistry().get(value);
|
||||
|
||||
if (channel == null) {
|
||||
return "false";
|
||||
}
|
||||
|
||||
final PlayerUser user = this.carbonChat.userService().wrap(player.getUniqueId());
|
||||
|
||||
return String.valueOf(channel.canPlayerSee(user, true));
|
||||
} else if (key.startsWith("ignoring_channel_")) {
|
||||
final String value = key.replace("ignoring_channel_", "");
|
||||
|
||||
final ChatChannel channel = this.carbonChat.channelRegistry().get(value);
|
||||
|
||||
if (channel == null) {
|
||||
return "false";
|
||||
}
|
||||
|
||||
final PlayerUser user = this.carbonChat.userService().wrap(player.getUniqueId());
|
||||
|
||||
return String.valueOf(user.channelSettings(channel).ignored());
|
||||
} else if (key.startsWith("selected_channel")) {
|
||||
final ChatChannel channel = this.carbonChat.userService().wrap(player.getUniqueId()).selectedChannel();
|
||||
|
||||
return channel == null ? this.carbonChat.channelRegistry().defaultValue().name() : channel.name();
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -21,14 +21,8 @@
|
||||
package net.draycia.carbon.bukkit.util;
|
||||
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.serializer.bungeecord.BungeeComponentSerializer;
|
||||
import net.md_5.bungee.api.ChatColor;
|
||||
import net.md_5.bungee.api.chat.ComponentBuilder;
|
||||
import net.md_5.bungee.api.chat.HoverEvent;
|
||||
import net.md_5.bungee.api.chat.TextComponent;
|
||||
import net.md_5.bungee.api.chat.TranslatableComponent;
|
||||
import net.md_5.bungee.api.chat.hover.content.Content;
|
||||
import org.bukkit.Bukkit;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
@@ -46,26 +40,20 @@ public final class CarbonUtils {
|
||||
return net.kyori.adventure.text.Component.empty();
|
||||
}
|
||||
|
||||
final Content content = Bukkit.getItemFactory().hoverContentOf(itemStack);
|
||||
final HoverEvent event = new HoverEvent(HoverEvent.Action.SHOW_ITEM, content);
|
||||
final TextComponent.Builder builder = Component.text();
|
||||
|
||||
final ComponentBuilder component = new ComponentBuilder();
|
||||
component.event(event); // Let this be inherited by all coming components.
|
||||
component.color(ChatColor.WHITE).append("[");
|
||||
builder.hoverEvent(itemStack); // Let this be inherited by all coming components.
|
||||
builder.append(Component.text("[", NamedTextColor.WHITE));
|
||||
|
||||
if (itemStack.getItemMeta().hasDisplayName()) {
|
||||
component.append(TextComponent.fromLegacyText(itemStack.getItemMeta().getDisplayName()));
|
||||
builder.append(itemStack.getItemMeta().displayName());
|
||||
} else {
|
||||
// As of 1.13, Material is 1:1 with MC's names
|
||||
final String prefix = itemStack.getType().isBlock() ? "block" : "item";
|
||||
final String name = prefix + ".minecraft." + itemStack.getType().name().toLowerCase();
|
||||
|
||||
component.append(new TranslatableComponent(name));
|
||||
builder.append(Component.translatable(itemStack.getType().getTranslationKey()));
|
||||
}
|
||||
|
||||
component.color(ChatColor.WHITE).append("]");
|
||||
builder.append(Component.text("]", NamedTextColor.WHITE));
|
||||
|
||||
return BungeeComponentSerializer.get().deserialize(component.create());
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
}
|
||||
-43
@@ -1,43 +0,0 @@
|
||||
package net.draycia.carbon.bukkit.util;
|
||||
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
@SuppressWarnings("argument.type.incompatible")
|
||||
public final class FunctionalityConstants {
|
||||
|
||||
public static final boolean HAS_PROXY;
|
||||
|
||||
static {
|
||||
boolean hasProxy = false;
|
||||
|
||||
try {
|
||||
final Class<?> spigotConfig = Class.forName("org.spigotmc.SpigotConfig");
|
||||
final Field bungee = spigotConfig.getField("bungee");
|
||||
|
||||
hasProxy = bungee.getBoolean(null);
|
||||
} catch (final ClassNotFoundException | NoSuchFieldException | IllegalAccessException ignored) {
|
||||
}
|
||||
|
||||
HAS_PROXY = hasProxy;
|
||||
}
|
||||
|
||||
private FunctionalityConstants() {
|
||||
|
||||
}
|
||||
|
||||
private static boolean noThrow(final @NonNull ThrowingRunnable runnable) {
|
||||
try {
|
||||
runnable.run();
|
||||
return true;
|
||||
} catch (final Exception ex) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
private interface ThrowingRunnable {
|
||||
void run() throws Exception;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user