mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-02 06:28:54 +00:00
GRAAAAAAAAAAAAAAAAADLE
This commit is contained in:
1 parent
40777efc9e
commit
4bcf9a5605
57 files changed
+73
-40
No files matched your search
@@ -0,0 +1,130 @@
|
||||
package net.draycia.carbon;
|
||||
|
||||
import net.draycia.carbon.channels.contexts.impl.DistanceContext;
|
||||
import net.draycia.carbon.listeners.*;
|
||||
import net.draycia.carbon.managers.*;
|
||||
import net.draycia.carbon.storage.UserService;
|
||||
import net.draycia.carbon.storage.impl.JSONUserService;
|
||||
import net.draycia.carbon.storage.impl.MySQLUserService;
|
||||
import net.draycia.carbon.util.ItemStackUtils;
|
||||
import net.milkbowl.vault.permission.Permission;
|
||||
import org.bukkit.plugin.PluginManager;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
public final class CarbonChat extends JavaPlugin {
|
||||
|
||||
private Permission permission;
|
||||
|
||||
private PluginMessageManager pluginMessageManager;
|
||||
private CommandManager commandManager;
|
||||
private ChannelManager channelManager;
|
||||
private RedisManager redisManager = null;
|
||||
private AdventureManager adventureManager;
|
||||
private ContextManager contextManager;
|
||||
|
||||
private UserService userService;
|
||||
|
||||
private ItemStackUtils itemStackUtils;
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
// Setup Adventure
|
||||
adventureManager = new AdventureManager(this);
|
||||
|
||||
try {
|
||||
itemStackUtils = new ItemStackUtils();
|
||||
} catch (ClassNotFoundException | NoSuchMethodException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
// Setup vault and permissions
|
||||
permission = getServer().getServicesManager().getRegistration(Permission.class).getProvider();
|
||||
|
||||
// Ensure config is present to be modified by the user
|
||||
saveDefaultConfig();
|
||||
|
||||
// Initialize managers
|
||||
pluginMessageManager = new PluginMessageManager(this);
|
||||
commandManager = new CommandManager(this);
|
||||
channelManager = new ChannelManager(this);
|
||||
contextManager = new ContextManager();
|
||||
|
||||
if (getConfig().getBoolean("redis.enabled")) {
|
||||
redisManager = new RedisManager(this);
|
||||
}
|
||||
|
||||
String storageType = getConfig().getString("storage.type");
|
||||
|
||||
if (storageType.equalsIgnoreCase("mysql")) {
|
||||
userService = new MySQLUserService(this);
|
||||
} else if (storageType.equalsIgnoreCase("json")) {
|
||||
userService = new JSONUserService(this);
|
||||
} else {
|
||||
getLogger().warning("Invalid storage type selected! Falling back to JSON.");
|
||||
userService = new JSONUserService(this);
|
||||
}
|
||||
|
||||
// Setup listeners
|
||||
setupListeners();
|
||||
registerContexts();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
userService.cleanUp();
|
||||
}
|
||||
|
||||
private void setupListeners() {
|
||||
PluginManager pluginManager = getServer().getPluginManager();
|
||||
|
||||
// Register chat listeners
|
||||
pluginManager.registerEvents(new BukkitChatListener(this), this);
|
||||
pluginManager.registerEvents(new ItemLinkHandler(this), this);
|
||||
pluginManager.registerEvents(new LegacyFormatHandler(), this);
|
||||
pluginManager.registerEvents(new OfflineNameHandler(), this);
|
||||
pluginManager.registerEvents(new PingHandler(this), this);
|
||||
pluginManager.registerEvents(new PlaceholderHandler(this), this);
|
||||
pluginManager.registerEvents(new EmptyChatHandler(this), this);
|
||||
pluginManager.registerEvents(new UserFormattingListener(), this);
|
||||
}
|
||||
|
||||
private void registerContexts() {
|
||||
getContextManager().register("distance", new DistanceContext());
|
||||
}
|
||||
|
||||
public Permission getPermission() {
|
||||
return permission;
|
||||
}
|
||||
|
||||
public CommandManager getCommandManager() {
|
||||
return commandManager;
|
||||
}
|
||||
|
||||
public ChannelManager getChannelManager() {
|
||||
return channelManager;
|
||||
}
|
||||
|
||||
public PluginMessageManager getPluginMessageManager() {
|
||||
return pluginMessageManager;
|
||||
}
|
||||
|
||||
public RedisManager getRedisManager() {
|
||||
return redisManager;
|
||||
}
|
||||
|
||||
public UserService getUserService() {
|
||||
return userService;
|
||||
}
|
||||
|
||||
public ItemStackUtils getItemStackUtils() {
|
||||
return itemStackUtils;
|
||||
}
|
||||
|
||||
public AdventureManager getAdventureManager() {
|
||||
return adventureManager;
|
||||
}
|
||||
|
||||
public ContextManager getContextManager() {
|
||||
return contextManager;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package net.draycia.carbon.channels;
|
||||
|
||||
import co.aikar.commands.CommandManager;
|
||||
import net.draycia.carbon.CarbonChat;
|
||||
import net.draycia.carbon.commands.AliasedChannelCommand;
|
||||
import net.draycia.carbon.util.Registry;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class ChannelRegistry implements Registry<ChatChannel> {
|
||||
|
||||
private final Map<String, ChatChannel> registry = new HashMap<>();
|
||||
private final CarbonChat carbonChat;
|
||||
|
||||
public ChannelRegistry(CarbonChat carbonChat) {
|
||||
this.carbonChat = carbonChat;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean register(@NotNull String key, @NotNull ChatChannel value) {
|
||||
boolean registerSuccessful = registry.putIfAbsent(key, value) == null;
|
||||
|
||||
if (registerSuccessful) {
|
||||
CommandManager commandManager = carbonChat.getCommandManager().getCommandManager();
|
||||
|
||||
commandManager.getCommandReplacements().addReplacement("channelName", value.getAliases());
|
||||
commandManager.registerCommand(new AliasedChannelCommand(value));
|
||||
|
||||
if (value instanceof Listener) {
|
||||
Bukkit.getPluginManager().registerEvents((Listener)value, carbonChat);
|
||||
}
|
||||
}
|
||||
|
||||
return registerSuccessful;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public Collection<ChatChannel> values() {
|
||||
return registry.values();
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public ChatChannel get(String key) {
|
||||
return registry.get(key);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
package net.draycia.carbon.channels;
|
||||
|
||||
import net.draycia.carbon.events.ChatFormatEvent;
|
||||
import net.draycia.carbon.storage.ChatUser;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.format.TextColor;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public abstract class ChatChannel {
|
||||
|
||||
/**
|
||||
* @return The color that represents this channel. Optionally used in formatting.
|
||||
*/
|
||||
public abstract TextColor getColor();
|
||||
|
||||
/**
|
||||
* @return The MiniMessage styled format for the group in this channel.
|
||||
*/
|
||||
public abstract String getFormat(String group);
|
||||
|
||||
/**
|
||||
* @return If this is the default (typically Global) channel players use when they're in no other channel.
|
||||
*/
|
||||
public abstract Boolean isDefault();
|
||||
|
||||
/**
|
||||
* @return If this channel can be toggled off and if players can ignore player messages in this channel.
|
||||
*/
|
||||
public abstract Boolean isIgnorable();
|
||||
|
||||
public abstract Boolean shouldBungee();
|
||||
|
||||
/**
|
||||
* @return The name of this channel.
|
||||
*/
|
||||
public abstract String getName();
|
||||
|
||||
public abstract String getKey();
|
||||
|
||||
public abstract String getAliases();
|
||||
|
||||
/**
|
||||
* @return The message to be sent to the player when switching to this channel.
|
||||
*/
|
||||
public abstract String getSwitchMessage();
|
||||
|
||||
public abstract String getSwitchOtherMessage();
|
||||
|
||||
/**
|
||||
* @return The message to be send to the player when toggling this channel off.
|
||||
*/
|
||||
|
||||
public abstract String getToggleOffMessage();
|
||||
|
||||
/**
|
||||
* @return The message to be send to the player when toggling this channel on.
|
||||
*/
|
||||
|
||||
public abstract String getToggleOnMessage();
|
||||
|
||||
public abstract String getToggleOtherOnMessage();
|
||||
|
||||
public abstract String getToggleOtherOffMessage();
|
||||
|
||||
public abstract String getCannotUseMessage();
|
||||
|
||||
public abstract Boolean firstMatchingGroup();
|
||||
|
||||
public abstract boolean testContext(ChatUser sender, ChatUser target);
|
||||
|
||||
public abstract Object getContext(String key);
|
||||
|
||||
/**
|
||||
* @return If the player can use this channel.
|
||||
*/
|
||||
public abstract Boolean canPlayerUse(ChatUser user);
|
||||
|
||||
public abstract Boolean canPlayerSee(ChatUser sender, ChatUser target, boolean checkSpying);
|
||||
|
||||
public abstract List<ChatUser> getAudience(ChatUser user);
|
||||
|
||||
/**
|
||||
* @return If the channel should forward its formatting / formatted message to other servers
|
||||
*/
|
||||
public Boolean shouldForwardFormatting() {
|
||||
return true;
|
||||
}
|
||||
|
||||
public abstract List<Pattern> getItemLinkPatterns();
|
||||
|
||||
/**
|
||||
* Parses the specified message, calls a {@link ChatFormatEvent}, and sends the message to everyone who can view this channel.
|
||||
* @param user The player who is saying the message.
|
||||
* @param message The message to be sent.
|
||||
*/
|
||||
public abstract void sendMessage(ChatUser user, String message, boolean fromBungee);
|
||||
|
||||
public abstract void sendComponent(ChatUser user, Component component);
|
||||
|
||||
public String processPlaceholders(ChatUser user, String input) { return input; }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package net.draycia.carbon.channels.contexts;
|
||||
|
||||
import net.draycia.carbon.channels.ChatChannel;
|
||||
import net.draycia.carbon.storage.ChatUser;
|
||||
|
||||
public interface MessageContext {
|
||||
ChatUser getSender();
|
||||
ChatUser getTarget();
|
||||
ChatChannel getChatChannel();
|
||||
Object getValue();
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package net.draycia.carbon.channels.contexts.impl;
|
||||
|
||||
import net.draycia.carbon.channels.contexts.MessageContext;
|
||||
import org.bukkit.Location;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
public class DistanceContext implements Function<MessageContext, Boolean> {
|
||||
|
||||
@Override
|
||||
public Boolean apply(MessageContext context) {
|
||||
if (!context.getSender().isOnline() || !context.getTarget().isOnline()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Location senderLocation = context.getSender().asPlayer().getLocation();
|
||||
Location targetLocation = context.getTarget().asPlayer().getLocation();
|
||||
|
||||
if (!senderLocation.getWorld().equals(targetLocation.getWorld())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Double value = (Double) context.getValue();
|
||||
|
||||
if (value > 0) {
|
||||
return senderLocation.distance(targetLocation) <= value;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
package net.draycia.carbon.channels.contexts.impl;
|
||||
|
||||
import net.draycia.carbon.channels.ChatChannel;
|
||||
import net.draycia.carbon.channels.contexts.MessageContext;
|
||||
import net.draycia.carbon.storage.ChatUser;
|
||||
|
||||
public class SimpleMessageContext implements MessageContext {
|
||||
private final ChatUser sender;
|
||||
private final ChatUser target;
|
||||
private final ChatChannel chatChannel;
|
||||
private final Object value;
|
||||
|
||||
public SimpleMessageContext(ChatUser sender, ChatUser target, ChatChannel chatChannel, Object value) {
|
||||
this.sender = sender;
|
||||
this.target = target;
|
||||
this.chatChannel = chatChannel;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChatUser getSender() {
|
||||
return sender;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChatUser getTarget() {
|
||||
return target;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChatChannel getChatChannel() {
|
||||
return chatChannel;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getValue() {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,413 @@
|
||||
package net.draycia.carbon.channels.impls;
|
||||
|
||||
import net.draycia.carbon.CarbonChat;
|
||||
import net.draycia.carbon.channels.ChatChannel;
|
||||
import net.draycia.carbon.events.ChatComponentEvent;
|
||||
import net.draycia.carbon.events.ChatFormatEvent;
|
||||
import net.draycia.carbon.storage.ChatUser;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
import net.kyori.adventure.text.format.TextColor;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class CarbonChatChannel extends ChatChannel {
|
||||
|
||||
private final String key;
|
||||
|
||||
private final CarbonChat carbonChat;
|
||||
private final ConfigurationSection config;
|
||||
|
||||
public CarbonChatChannel(String key, CarbonChat carbonChat, ConfigurationSection config) {
|
||||
this.key = key;
|
||||
this.carbonChat = carbonChat;
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
public CarbonChat getCarbonChat() {
|
||||
return carbonChat;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean testContext(ChatUser sender, ChatUser target) {
|
||||
return carbonChat.getContextManager().testContext(sender, target, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean canPlayerUse(ChatUser user) {
|
||||
return user.asPlayer().hasPermission("carbonchat.channels." + getName() + ".use");
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ChatUser> getAudience(ChatUser user) {
|
||||
List<ChatUser> audience = new ArrayList<>();
|
||||
|
||||
for (Player player : Bukkit.getOnlinePlayers()) {
|
||||
if (canPlayerSee(user, carbonChat.getUserService().wrap(player), false)) {
|
||||
audience.add(carbonChat.getUserService().wrap(player));
|
||||
}
|
||||
}
|
||||
|
||||
return audience;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendMessage(ChatUser user, String message, boolean fromBungee) {
|
||||
// Get player's formatting
|
||||
String messageFormat = getFormat(user);
|
||||
|
||||
// Call custom chat event
|
||||
ChatFormatEvent formatEvent = new ChatFormatEvent(user, this, messageFormat, message);
|
||||
|
||||
Bukkit.getPluginManager().callEvent(formatEvent);
|
||||
|
||||
if (formatEvent.isCancelled()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Get formatted message
|
||||
TextComponent formattedMessage = (TextComponent) carbonChat.getAdventureManager().processMessage(formatEvent.getFormat(),
|
||||
"br", "\n",
|
||||
"color", "<color:" + getColor().toString() + ">",
|
||||
"phase", Long.toString(System.currentTimeMillis() % 25),
|
||||
"server", carbonChat.getConfig().getString("server-name", "Server"),
|
||||
"message", formatEvent.getMessage());
|
||||
|
||||
// Call custom chat event
|
||||
ChatComponentEvent componentEvent = new ChatComponentEvent(user, this, formattedMessage,
|
||||
formatEvent.getMessage(), getAudience(user));
|
||||
|
||||
Bukkit.getPluginManager().callEvent(componentEvent);
|
||||
|
||||
// Send message as normal
|
||||
for (Player onlinePlayer : Bukkit.getOnlinePlayers()) {
|
||||
ChatUser chatUser = carbonChat.getUserService().wrap(onlinePlayer);
|
||||
|
||||
TextColor userColor = chatUser.getChannelSettings(this).getColor();
|
||||
|
||||
String prefix = processPlaceholders(user, carbonChat.getConfig().getString("spy-prefix"));
|
||||
|
||||
if (userColor == null) {
|
||||
if (isUserSpying(user, chatUser)) {
|
||||
prefix = prefix.replace("<color>", "<color:" + getColor() + ">");
|
||||
|
||||
chatUser.sendMessage(carbonChat.getAdventureManager().processMessage(prefix).append(componentEvent.getComponent()));
|
||||
} else if (componentEvent.getRecipients().contains(chatUser)) {
|
||||
chatUser.sendMessage(componentEvent.getComponent());
|
||||
}
|
||||
} else {
|
||||
prefix = prefix.replace("<color>", "<color:" +
|
||||
chatUser.getChannelSettings(this).getColor().asHexString() + ">");
|
||||
String format = formatEvent.getFormat();
|
||||
|
||||
TextComponent newFormat = (TextComponent) carbonChat.getAdventureManager().processMessage(format,
|
||||
"br", "\n",
|
||||
"color", "<color:" + userColor.toString() + ">",
|
||||
"phase", Long.toString(System.currentTimeMillis() % 25),
|
||||
"server", carbonChat.getConfig().getString("server-name", "Server"),
|
||||
"message", formatEvent.getMessage());
|
||||
|
||||
if (isUserSpying(user, chatUser)) {
|
||||
newFormat = (TextComponent) carbonChat.getAdventureManager().processMessage(prefix, "br", "\n")
|
||||
.append(newFormat);
|
||||
} else if (!componentEvent.getRecipients().contains(chatUser)) {
|
||||
return;
|
||||
}
|
||||
|
||||
ChatComponentEvent newEvent = new ChatComponentEvent(user, this, newFormat,
|
||||
formatEvent.getMessage(), Collections.singletonList(chatUser), true);
|
||||
|
||||
Bukkit.getPluginManager().callEvent(newEvent);
|
||||
|
||||
chatUser.sendMessage(newEvent.getComponent());
|
||||
}
|
||||
}
|
||||
|
||||
// Log message to console
|
||||
String sm = user.isShadowMuted() ? "[SM] " : "";
|
||||
System.out.println(sm + LegacyComponentSerializer.legacy().serialize(componentEvent.getComponent()));
|
||||
|
||||
// Route message to bungee / discord (if message originates from this server)
|
||||
// Use instanceof and not isOnline, if this message originates from another then the instanceof will
|
||||
// fail, but isOnline may succeed if the player is online on both servers (somehow).
|
||||
if (user.isOnline() && fromBungee) {
|
||||
if (shouldForwardFormatting() && shouldBungee()) {
|
||||
sendMessageToBungee(user.asPlayer(), componentEvent.getComponent());
|
||||
} else if (shouldBungee()) {
|
||||
sendMessageToBungee(user.asPlayer(), message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isUserSpying(ChatUser sender, ChatUser target) {
|
||||
if (!canPlayerSee(sender, target, false)) {
|
||||
return target.getChannelSettings(this).isSpying();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendComponent(ChatUser player, Component component) {
|
||||
for (ChatUser user : getAudience(player)) {
|
||||
user.sendMessage(component);
|
||||
}
|
||||
|
||||
System.out.println(LegacyComponentSerializer.legacy().serialize(component));
|
||||
}
|
||||
|
||||
public void sendMessageToBungee(Player player, Component component) {
|
||||
carbonChat.getPluginMessageManager().sendComponent(this, player, component);
|
||||
}
|
||||
|
||||
public void sendMessageToBungee(Player player, String message) {
|
||||
carbonChat.getPluginMessageManager().sendMessage(this, player, message);
|
||||
}
|
||||
|
||||
private String getFormat(ChatUser user) {
|
||||
String group = "default";
|
||||
|
||||
if (!firstMatchingGroup()) {
|
||||
if (user.isOnline()) {
|
||||
group = carbonChat.getPermission().getPrimaryGroup(user.asPlayer());
|
||||
} else {
|
||||
group = carbonChat.getPermission().getPrimaryGroup(null, user.asOfflinePlayer());
|
||||
}
|
||||
} else {
|
||||
String[] groups;
|
||||
|
||||
// TODO: this can't be done on main thread :(
|
||||
if (user.isOnline()) {
|
||||
groups = carbonChat.getPermission().getPlayerGroups(user.asPlayer());
|
||||
} else {
|
||||
groups = carbonChat.getPermission().getPlayerGroups(null, user.asOfflinePlayer());
|
||||
}
|
||||
|
||||
for (String entry : groups) {
|
||||
if (getFormats().containsKey(entry.toLowerCase())) {
|
||||
group = entry;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return getFormat(group);
|
||||
}
|
||||
|
||||
public Boolean canPlayerSee(ChatUser sender, ChatUser target, boolean checkSpying) {
|
||||
Player targetPlayer = target.asPlayer();
|
||||
|
||||
if (checkSpying && targetPlayer.hasPermission("carbonchat.spy." + getName())) {
|
||||
if (target.getChannelSettings(this).isSpying()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!targetPlayer.hasPermission("carbonchat.channels." + getName() + ".see")) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (isIgnorable()) {
|
||||
if (target.isIgnoringUser(sender) && !targetPlayer.hasPermission("carbonchat.ignore.exempt")) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (target.getChannelSettings(this).isIgnored()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public Map<String, String> getFormats() {
|
||||
HashMap<String, String> formats = new HashMap<>();
|
||||
|
||||
ConfigurationSection formatSection = config.getConfigurationSection("formats");
|
||||
|
||||
if (formatSection == null) {
|
||||
formatSection = carbonChat.getConfig().getConfigurationSection("default").getConfigurationSection("formats");
|
||||
}
|
||||
|
||||
for (String key : formatSection.getKeys(false)) {
|
||||
formats.put(key, formatSection.getString(key));
|
||||
}
|
||||
|
||||
return formats;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TextColor getColor() {
|
||||
return TextColor.fromHexString(getString("color"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFormat(String group) {
|
||||
return getFormats().getOrDefault(group, getFormats().getOrDefault("default",
|
||||
"<white><%player_displayname%<white>> <message>"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean isDefault() {
|
||||
return getBoolean("default");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean isIgnorable() {
|
||||
return getBoolean("ignorable");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean shouldBungee() {
|
||||
return getBoolean("should-bungee");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
String name = getString("name");
|
||||
return name == null ? key : name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSwitchMessage() {
|
||||
return getString("switch-message");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSwitchOtherMessage() {
|
||||
return getString("switch-other-message");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getToggleOffMessage() {
|
||||
return getString("toggle-off-message");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getToggleOnMessage() {
|
||||
return getString("toggle-on-message");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getToggleOtherOnMessage() {
|
||||
return getString("toggle-other-on");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getToggleOtherOffMessage() {
|
||||
return getString("toggle-other-off");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCannotUseMessage() {
|
||||
return getString("cannot-use-channel");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean shouldForwardFormatting() {
|
||||
return getBoolean("forward-format");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean firstMatchingGroup() {
|
||||
return getBoolean("first-matching-group");
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Pattern> getItemLinkPatterns() {
|
||||
ArrayList<Pattern> itemPatterns = new ArrayList<>();
|
||||
|
||||
for (String entry : carbonChat.getConfig().getStringList("item-link-placeholders")) {
|
||||
itemPatterns.add(Pattern.compile(Pattern.quote(entry)));
|
||||
}
|
||||
|
||||
return itemPatterns;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getContext(String key) {
|
||||
ConfigurationSection section = config.getConfigurationSection("contexts");
|
||||
|
||||
if (section == null) {
|
||||
ConfigurationSection defaultSection = carbonChat.getConfig().getConfigurationSection("default");
|
||||
|
||||
if (defaultSection == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
ConfigurationSection defaultContexts = defaultSection.getConfigurationSection("contexts");
|
||||
|
||||
if (defaultContexts == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return defaultContexts.get(key);
|
||||
}
|
||||
|
||||
return section.get(key);
|
||||
}
|
||||
|
||||
private String getString(String key) {
|
||||
if (config != null && config.contains(key)) {
|
||||
return config.getString(key);
|
||||
}
|
||||
|
||||
ConfigurationSection defaultSection = carbonChat.getConfig().getConfigurationSection("default");
|
||||
|
||||
if (defaultSection != null && defaultSection.contains(key)) {
|
||||
return defaultSection.getString(key);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private boolean getBoolean(String key) {
|
||||
if (config != null && config.contains(key)) {
|
||||
return config.getBoolean(key);
|
||||
}
|
||||
|
||||
ConfigurationSection defaultSection = carbonChat.getConfig().getConfigurationSection("default");
|
||||
|
||||
if (defaultSection != null && defaultSection.contains(key)) {
|
||||
return defaultSection.getBoolean(key);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private double getDouble(String key) {
|
||||
if (config != null && config.contains(key)) {
|
||||
return config.getDouble(key);
|
||||
}
|
||||
|
||||
ConfigurationSection defaultSection = carbonChat.getConfig().getConfigurationSection("default");
|
||||
|
||||
if (defaultSection != null && defaultSection.contains(key)) {
|
||||
return defaultSection.getDouble(key);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAliases() {
|
||||
String aliases = getString("aliases");
|
||||
|
||||
if (aliases == null) {
|
||||
return getKey();
|
||||
}
|
||||
|
||||
return aliases;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package net.draycia.carbon.commands;
|
||||
|
||||
import co.aikar.commands.BaseCommand;
|
||||
import co.aikar.commands.ConditionFailedException;
|
||||
import co.aikar.commands.annotation.*;
|
||||
import net.draycia.carbon.CarbonChat;
|
||||
import net.draycia.carbon.channels.ChatChannel;
|
||||
import net.draycia.carbon.storage.ChatUser;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
@CommandAlias("%channelName")
|
||||
@CommandPermission("carbonchat.switch")
|
||||
public class AliasedChannelCommand extends BaseCommand {
|
||||
|
||||
@Dependency
|
||||
private CarbonChat carbonChat;
|
||||
|
||||
private final ChatChannel chatChannel;
|
||||
|
||||
public AliasedChannelCommand(ChatChannel chatChannel) {
|
||||
this.chatChannel = chatChannel;
|
||||
}
|
||||
|
||||
@Default
|
||||
public void baseCommand(Player player, @Optional String[] args) {
|
||||
ChatUser user = carbonChat.getUserService().wrap(player);
|
||||
|
||||
if (!chatChannel.canPlayerUse(user)) {
|
||||
throw new ConditionFailedException(chatChannel.getCannotUseMessage());
|
||||
}
|
||||
|
||||
if (args == null || args.length == 0) {
|
||||
user.setSelectedChannel(getChatChannel());
|
||||
|
||||
user.sendMessage(carbonChat.getAdventureManager().processMessage(getChatChannel().getSwitchMessage(),
|
||||
"color", "<" + getChatChannel().getColor().toString() + ">",
|
||||
"channel", getChatChannel().getName()));
|
||||
} else {
|
||||
Bukkit.getScheduler().runTaskAsynchronously(carbonChat, () -> {
|
||||
getChatChannel().sendMessage(user, String.join(" ", args), false);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public ChatChannel getChatChannel() {
|
||||
return chatChannel;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package net.draycia.carbon.commands;
|
||||
|
||||
import co.aikar.commands.BaseCommand;
|
||||
import co.aikar.commands.annotation.*;
|
||||
import net.draycia.carbon.CarbonChat;
|
||||
import net.draycia.carbon.channels.ChatChannel;
|
||||
import net.draycia.carbon.storage.ChatUser;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
@CommandAlias("ch|channel|switch")
|
||||
@CommandPermission("carbonchat.switch")
|
||||
public class ChannelCommand extends BaseCommand {
|
||||
|
||||
@Dependency
|
||||
private CarbonChat carbonChat;
|
||||
|
||||
@Default
|
||||
@CommandCompletion("@chatchannel")
|
||||
public void baseCommand(Player player, @Conditions("canuse:true") ChatChannel channel, @Optional String[] args) {
|
||||
ChatUser user = carbonChat.getUserService().wrap(player);
|
||||
|
||||
if (args == null || args.length == 0) {
|
||||
user.setSelectedChannel(channel);
|
||||
|
||||
user.sendMessage(carbonChat.getAdventureManager().processMessageWithPapi(player, channel.getSwitchMessage(),
|
||||
"br", "\n",
|
||||
"color", "<" + channel.getColor().toString() + ">",
|
||||
"channel", channel.getName()));
|
||||
} else {
|
||||
Bukkit.getScheduler().runTaskAsynchronously(carbonChat, () -> {
|
||||
channel.sendMessage(user, String.join(" ", args), false);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@CommandPermission("carbonchat.switch.others")
|
||||
@Subcommand("other")
|
||||
@CommandCompletion("@chatchannel @players")
|
||||
public void baseCommand(CommandSender sender, ChatChannel channel, ChatUser user) {
|
||||
user.setSelectedChannel(channel);
|
||||
|
||||
String message = channel.getSwitchMessage();
|
||||
String otherMessage = channel.getSwitchOtherMessage();
|
||||
|
||||
user.sendMessage(carbonChat.getAdventureManager().processMessage(message, "br", "\n",
|
||||
"color", "<color:" + channel.getColor().toString() + ">", "channel", channel.getName()));
|
||||
|
||||
carbonChat.getAdventureManager().getAudiences().audience(sender).sendMessage(carbonChat.getAdventureManager().processMessage(otherMessage, "br", "\n",
|
||||
"color", "<color:" + channel.getColor().toString() + ">", "channel", channel.getName(),
|
||||
"player", user.asOfflinePlayer().getName()));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package net.draycia.carbon.commands;
|
||||
|
||||
import co.aikar.commands.BaseCommand;
|
||||
import co.aikar.commands.annotation.CommandAlias;
|
||||
import co.aikar.commands.annotation.CommandPermission;
|
||||
import co.aikar.commands.annotation.Default;
|
||||
import co.aikar.commands.annotation.Dependency;
|
||||
import net.draycia.carbon.CarbonChat;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
@CommandAlias("chatreload|creload")
|
||||
@CommandPermission("carbonchat.reload")
|
||||
public class ChatReloadCommand extends BaseCommand {
|
||||
|
||||
@Dependency
|
||||
private CarbonChat carbonChat;
|
||||
|
||||
@Default
|
||||
public void baseCommand(CommandSender sender) {
|
||||
carbonChat.reloadConfig();
|
||||
|
||||
Component message = carbonChat.getAdventureManager().processMessage(carbonChat.getConfig().getString("language.reloaded"),
|
||||
"br", "\n");
|
||||
|
||||
carbonChat.getAdventureManager().getAudiences().audience(sender).sendMessage(message);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package net.draycia.carbon.commands;
|
||||
|
||||
import co.aikar.commands.BaseCommand;
|
||||
import co.aikar.commands.annotation.CommandAlias;
|
||||
import co.aikar.commands.annotation.CommandCompletion;
|
||||
import co.aikar.commands.annotation.CommandPermission;
|
||||
import co.aikar.commands.annotation.Default;
|
||||
import co.aikar.commands.annotation.Dependency;
|
||||
import net.draycia.carbon.CarbonChat;
|
||||
import net.draycia.carbon.storage.ChatUser;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.milkbowl.vault.permission.Permission;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
@CommandAlias("ignore")
|
||||
@CommandPermission("carbonchat.ignore")
|
||||
public class IgnoreCommand extends BaseCommand {
|
||||
|
||||
@Dependency
|
||||
private CarbonChat carbonChat;
|
||||
|
||||
@Default
|
||||
@CommandCompletion("@players")
|
||||
public void baseCommand(Player player, ChatUser targetUser) {
|
||||
ChatUser user = carbonChat.getUserService().wrap(player);
|
||||
|
||||
if (user.isIgnoringUser(targetUser)) {
|
||||
user.setIgnoringUser(targetUser, false);
|
||||
user.sendMessage(processMessage(player, carbonChat.getConfig().getString("language.not-ignoring-user")));
|
||||
} else {
|
||||
Bukkit.getScheduler().runTaskAsynchronously(carbonChat, () -> {
|
||||
Permission permission = carbonChat.getPermission();
|
||||
|
||||
if (permission.playerHas(null, targetUser.asOfflinePlayer(), "carbonchat.ignore.exempt")) {
|
||||
user.sendMessage(processMessage(player, carbonChat.getConfig().getString("language.ignore-exempt")));
|
||||
} else {
|
||||
user.setIgnoringUser(targetUser, true);
|
||||
user.sendMessage(processMessage(player, carbonChat.getConfig().getString("language.ignoring-user")));
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private Component processMessage(Player player, String message) {
|
||||
return carbonChat.getAdventureManager().processMessageWithPapi(player, message, "br", "\n");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package net.draycia.carbon.commands;
|
||||
|
||||
import co.aikar.commands.BaseCommand;
|
||||
import co.aikar.commands.annotation.CommandAlias;
|
||||
import co.aikar.commands.annotation.CommandPermission;
|
||||
import co.aikar.commands.annotation.Default;
|
||||
import co.aikar.commands.annotation.Dependency;
|
||||
import me.clip.placeholderapi.PlaceholderAPI;
|
||||
import net.draycia.carbon.CarbonChat;
|
||||
import net.draycia.carbon.storage.ChatUser;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
@CommandAlias("me|rp")
|
||||
@CommandPermission("carbonchat.me")
|
||||
public class MeCommand extends BaseCommand {
|
||||
|
||||
@Dependency
|
||||
private CarbonChat carbonChat;
|
||||
|
||||
@Default
|
||||
public void baseCommand(Player player, String... args) {
|
||||
String message = String.join(" ", args).replace("</pre>", "");
|
||||
String format = PlaceholderAPI.setPlaceholders(player, carbonChat.getConfig().getString("language.me"));
|
||||
Component component = carbonChat.getAdventureManager().processMessage(format, "br", "\n", "message", message);
|
||||
|
||||
ChatUser user = carbonChat.getUserService().wrap(player);
|
||||
|
||||
if (user.isShadowMuted()) {
|
||||
user.sendMessage(component);
|
||||
} else {
|
||||
for (Player onlinePlayer : Bukkit.getOnlinePlayers()) {
|
||||
if (carbonChat.getUserService().wrap(onlinePlayer).isIgnoringUser(user)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
carbonChat.getAdventureManager().getAudiences().player(onlinePlayer).sendMessage(component);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package net.draycia.carbon.commands;
|
||||
|
||||
import co.aikar.commands.BaseCommand;
|
||||
import co.aikar.commands.annotation.CommandAlias;
|
||||
import co.aikar.commands.annotation.CommandCompletion;
|
||||
import co.aikar.commands.annotation.CommandPermission;
|
||||
import co.aikar.commands.annotation.Default;
|
||||
import co.aikar.commands.annotation.Dependency;
|
||||
import net.draycia.carbon.CarbonChat;
|
||||
import net.draycia.carbon.storage.ChatUser;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
@CommandAlias("msg|whisper|message|w")
|
||||
@CommandPermission("carbonchat.message")
|
||||
public class MessageCommand extends BaseCommand {
|
||||
|
||||
@Dependency
|
||||
private CarbonChat carbonChat;
|
||||
|
||||
@Default
|
||||
@CommandCompletion("@players")
|
||||
public void baseCommand(Player player, ChatUser targetUser, String... args) {
|
||||
if (args.length == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
ChatUser sender = carbonChat.getUserService().wrap(player);
|
||||
targetUser.sendMessage(sender, String.join(" ", args));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package net.draycia.carbon.commands;
|
||||
|
||||
import co.aikar.commands.BaseCommand;
|
||||
import co.aikar.commands.annotation.CommandAlias;
|
||||
import co.aikar.commands.annotation.CommandPermission;
|
||||
import co.aikar.commands.annotation.Default;
|
||||
import co.aikar.commands.annotation.Dependency;
|
||||
import net.draycia.carbon.CarbonChat;
|
||||
import net.draycia.carbon.storage.ChatUser;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
@CommandAlias("reply|r")
|
||||
@CommandPermission("carbonchat.reply")
|
||||
public class ReplyCommand extends BaseCommand {
|
||||
|
||||
@Dependency
|
||||
private CarbonChat carbonChat;
|
||||
|
||||
@Default
|
||||
public void baseCommand(Player player, String... args) {
|
||||
if (args.length == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
ChatUser user = carbonChat.getUserService().wrap(player);
|
||||
ChatUser targetUser = carbonChat.getUserService().wrap(user.getReplyTarget());
|
||||
|
||||
if (user.getReplyTarget() == null) {
|
||||
String message = carbonChat.getConfig().getString("language.no-reply-target");
|
||||
Component component = carbonChat.getAdventureManager().processMessage(message, "br", "\n");
|
||||
user.sendMessage(component);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
targetUser.sendMessage(user, String.join(" ", args));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package net.draycia.carbon.commands;
|
||||
|
||||
import co.aikar.commands.BaseCommand;
|
||||
import co.aikar.commands.ConditionFailedException;
|
||||
import co.aikar.commands.annotation.*;
|
||||
import net.draycia.carbon.CarbonChat;
|
||||
import net.draycia.carbon.channels.ChatChannel;
|
||||
import net.draycia.carbon.storage.ChatUser;
|
||||
import net.draycia.carbon.storage.UserChannelSettings;
|
||||
import net.kyori.adventure.text.format.TextColor;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
@CommandAlias("setcolor")
|
||||
@CommandPermission("carbonchat.setcolor")
|
||||
public class SetColorCommand extends BaseCommand {
|
||||
|
||||
@Dependency
|
||||
private CarbonChat carbonChat;
|
||||
|
||||
@Default
|
||||
@CommandCompletion("@chatchannel")
|
||||
public void baseCommand(Player player, @Conditions("canuse:true") ChatChannel channel, String color) {
|
||||
if (channel == null || color == null) {
|
||||
throw new ConditionFailedException("Channel or Color not supplied!");
|
||||
}
|
||||
|
||||
ChatUser user = carbonChat.getUserService().wrap(player);
|
||||
|
||||
if (!player.hasPermission("carbonchat.setcolor." + channel.getName())) {
|
||||
throw new ConditionFailedException("You can't set colors for this channel!");
|
||||
}
|
||||
|
||||
UserChannelSettings settings = user.getChannelSettings(channel);
|
||||
|
||||
settings.setColor(TextColor.fromHexString(color));
|
||||
|
||||
user.sendMessage(carbonChat.getAdventureManager().processMessageWithPapi(player, carbonChat.getConfig().getString("language.channel-color-set"),
|
||||
"br", "\n", "color", "<color:" + color + ">", "channel", channel.getName(), "hex", color));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package net.draycia.carbon.commands;
|
||||
|
||||
import co.aikar.commands.BaseCommand;
|
||||
import co.aikar.commands.annotation.*;
|
||||
import net.draycia.carbon.CarbonChat;
|
||||
import net.draycia.carbon.channels.ChatChannel;
|
||||
import net.draycia.carbon.storage.ChatUser;
|
||||
import net.draycia.carbon.storage.UserChannelSettings;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
@CommandAlias("spych|spychannel|spy")
|
||||
@CommandPermission("carbonchat.spy")
|
||||
public class SpyChannelCommand extends BaseCommand {
|
||||
|
||||
@Dependency
|
||||
private CarbonChat carbonChat;
|
||||
|
||||
@Default
|
||||
@CommandCompletion("@channel")
|
||||
public void baseCommand(Player player, @Conditions("exists:true") ChatChannel channel) {
|
||||
ChatUser user = carbonChat.getUserService().wrap(player);
|
||||
|
||||
String message;
|
||||
|
||||
UserChannelSettings settings = user.getChannelSettings(channel);
|
||||
|
||||
if (settings.isSpying()) {
|
||||
settings.setSpying(false);
|
||||
message = carbonChat.getConfig().getString("language.spy-toggled-off");
|
||||
} else {
|
||||
settings.setSpying(true);
|
||||
message = carbonChat.getConfig().getString("language.spy-toggled-on");
|
||||
}
|
||||
|
||||
user.sendMessage(carbonChat.getAdventureManager().processMessageWithPapi(player, message, "br", "\n",
|
||||
"color", "<color:" + channel.getColor().toString() + ">", "channel", channel.getName()));
|
||||
}
|
||||
|
||||
@Subcommand("whispers")
|
||||
public void whispersCommand(Player player) {
|
||||
ChatUser user = carbonChat.getUserService().wrap(player);
|
||||
|
||||
String message;
|
||||
|
||||
if (user.isSpyingWhispers()) {
|
||||
user.setSpyingWhispers(false);
|
||||
message = carbonChat.getConfig().getString("language.spy-whispers-off");
|
||||
} else {
|
||||
user.setSpyingWhispers(true);
|
||||
message = carbonChat.getConfig().getString("language.spy-whispers-on");
|
||||
}
|
||||
|
||||
user.sendMessage(carbonChat.getAdventureManager().processMessageWithPapi(player, message, "br", "\n"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package net.draycia.carbon.commands;
|
||||
|
||||
import co.aikar.commands.BaseCommand;
|
||||
import co.aikar.commands.annotation.*;
|
||||
import net.draycia.carbon.CarbonChat;
|
||||
import net.draycia.carbon.channels.ChatChannel;
|
||||
import net.draycia.carbon.storage.ChatUser;
|
||||
import net.draycia.carbon.storage.UserChannelSettings;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
@CommandAlias("tch|togglec|togglechannel|toggle")
|
||||
@CommandPermission("carbonchat.toggle")
|
||||
public class ToggleCommand extends BaseCommand {
|
||||
|
||||
@Dependency
|
||||
private CarbonChat carbonChat;
|
||||
|
||||
@Default
|
||||
@CommandCompletion("@chatchannel @players")
|
||||
public void baseCommand(Player player, @Conditions("canuse:true") ChatChannel channel) {
|
||||
ChatUser user = carbonChat.getUserService().wrap(player);
|
||||
|
||||
String message;
|
||||
|
||||
UserChannelSettings settings = user.getChannelSettings(channel);
|
||||
|
||||
if (settings.isIgnored()) {
|
||||
settings.setIgnoring(false);
|
||||
message = channel.getToggleOffMessage();
|
||||
} else {
|
||||
settings.setIgnoring(true);
|
||||
message = channel.getToggleOnMessage();
|
||||
}
|
||||
|
||||
user.sendMessage(carbonChat.getAdventureManager().processMessageWithPapi(player, message, "br", "\n",
|
||||
"color", "<color:" + channel.getColor().toString() + ">", "channel", channel.getName()));
|
||||
}
|
||||
|
||||
@CommandPermission("carbonchat.toggle.others")
|
||||
@Subcommand("other")
|
||||
@CommandCompletion("@chatchannel @players")
|
||||
public void baseCommand(CommandSender sender, @Conditions("canuse:true") ChatChannel channel, ChatUser user) {
|
||||
String message;
|
||||
String otherMessage;
|
||||
|
||||
UserChannelSettings settings = user.getChannelSettings(channel);
|
||||
|
||||
if (settings.isIgnored()) {
|
||||
settings.setIgnoring(false);
|
||||
message = channel.getToggleOffMessage();
|
||||
otherMessage = channel.getToggleOtherOffMessage();
|
||||
} else {
|
||||
settings.setIgnoring(true);
|
||||
message = channel.getToggleOnMessage();
|
||||
otherMessage = channel.getToggleOtherOnMessage();
|
||||
}
|
||||
|
||||
user.sendMessage(carbonChat.getAdventureManager().processMessage(message, "br", "\n",
|
||||
"color", "<color:" + channel.getColor().toString() + ">", "channel", channel.getName()));
|
||||
|
||||
carbonChat.getAdventureManager().getAudiences().audience(sender).sendMessage(carbonChat.getAdventureManager().processMessage(otherMessage,
|
||||
"br", "\n", "color", "<color:" + channel.getColor().toString() + ">",
|
||||
"channel", channel.getName(), "player", user.asOfflinePlayer().getName()));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package net.draycia.carbon.events;
|
||||
|
||||
import net.draycia.carbon.channels.ChatChannel;
|
||||
import net.draycia.carbon.util.Registry;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.event.Event;
|
||||
import org.bukkit.event.HandlerList;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class ChannelRegisterEvent extends Event {
|
||||
|
||||
/** Bukkit event stuff **/
|
||||
private static final HandlerList handlers = new HandlerList();
|
||||
|
||||
@Override
|
||||
public @NotNull HandlerList getHandlers() {
|
||||
return handlers;
|
||||
}
|
||||
|
||||
public static HandlerList getHandlerList() {
|
||||
return handlers;
|
||||
}
|
||||
|
||||
/** Relevant stuff **/
|
||||
private List<ChatChannel> registeredChannels;
|
||||
private Registry<ChatChannel> registry;
|
||||
|
||||
public ChannelRegisterEvent(List<ChatChannel> registeredChannels, Registry<ChatChannel> registry) {
|
||||
super(!Bukkit.isPrimaryThread());
|
||||
|
||||
this.registeredChannels = registeredChannels;
|
||||
this.registry = registry;
|
||||
}
|
||||
|
||||
public void register(ChatChannel chatChannel) {
|
||||
registry.register(chatChannel.getKey(), chatChannel);
|
||||
}
|
||||
|
||||
public List<ChatChannel> getRegisteredChannels() {
|
||||
return registeredChannels;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package net.draycia.carbon.events;
|
||||
|
||||
import net.draycia.carbon.channels.ChatChannel;
|
||||
import net.draycia.carbon.storage.ChatUser;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.event.Cancellable;
|
||||
import org.bukkit.event.Event;
|
||||
import org.bukkit.event.HandlerList;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class ChannelSwitchEvent extends Event implements Cancellable {
|
||||
|
||||
/** Bukkit event stuff **/
|
||||
private static final HandlerList handlers = new HandlerList();
|
||||
private boolean cancelled = false;
|
||||
|
||||
@Override
|
||||
public @NotNull HandlerList getHandlers() {
|
||||
return handlers;
|
||||
}
|
||||
|
||||
public static HandlerList getHandlerList() {
|
||||
return handlers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCancelled() {
|
||||
return cancelled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCancelled(boolean cancelled) {
|
||||
this.cancelled = cancelled;
|
||||
}
|
||||
|
||||
/** Relevant stuff **/
|
||||
private ChatChannel channel;
|
||||
private ChatUser user;
|
||||
private String failureMessage;
|
||||
|
||||
public ChannelSwitchEvent(ChatChannel channel, ChatUser user, String failureMessage) {
|
||||
super(!Bukkit.isPrimaryThread());
|
||||
|
||||
this.channel = channel;
|
||||
this.user = user;
|
||||
this.failureMessage = failureMessage;
|
||||
}
|
||||
|
||||
public ChatUser getUser() {
|
||||
return user;
|
||||
}
|
||||
|
||||
public ChatChannel getChannel() {
|
||||
return channel;
|
||||
}
|
||||
|
||||
public String getFailureMessage() {
|
||||
return failureMessage;
|
||||
}
|
||||
|
||||
public void setFailureMessage(String failureMessage) {
|
||||
this.failureMessage = failureMessage;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
package net.draycia.carbon.events;
|
||||
|
||||
import net.draycia.carbon.channels.ChatChannel;
|
||||
import net.draycia.carbon.storage.ChatUser;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.event.Cancellable;
|
||||
import org.bukkit.event.Event;
|
||||
import org.bukkit.event.HandlerList;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class ChatComponentEvent extends Event implements Cancellable {
|
||||
|
||||
private static final HandlerList HANDLERS_LIST = new HandlerList();
|
||||
private boolean isCancelled = false;
|
||||
|
||||
private ChatUser user;
|
||||
private ChatChannel chatChannel;
|
||||
private TextComponent component;
|
||||
private String originalMessage;
|
||||
private List<ChatUser> recipients;
|
||||
private boolean customPlayerFormat;
|
||||
|
||||
public ChatComponentEvent(ChatUser user, ChatChannel chatChannel, TextComponent component, String originalMessage, List<ChatUser> recipients) {
|
||||
this(user, chatChannel, component, originalMessage, recipients, false);
|
||||
}
|
||||
|
||||
public ChatComponentEvent(ChatUser user, ChatChannel chatChannel, TextComponent component, String originalMessage, List<ChatUser> recipients, boolean customPlayerFormat) {
|
||||
super(!Bukkit.isPrimaryThread());
|
||||
|
||||
this.user = user;
|
||||
this.chatChannel = chatChannel;
|
||||
this.component = component;
|
||||
this.originalMessage = originalMessage;
|
||||
this.recipients = recipients;
|
||||
this.customPlayerFormat = customPlayerFormat;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCancelled() {
|
||||
return isCancelled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCancelled(boolean cancelled) {
|
||||
this.isCancelled = cancelled;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public HandlerList getHandlers() {
|
||||
return HANDLERS_LIST;
|
||||
}
|
||||
|
||||
public static HandlerList getHandlerList() {
|
||||
return HANDLERS_LIST;
|
||||
}
|
||||
|
||||
public ChatUser getUser() {
|
||||
return user;
|
||||
}
|
||||
|
||||
public ChatChannel getChatChannel() {
|
||||
return chatChannel;
|
||||
}
|
||||
|
||||
public void setChatChannel(ChatChannel chatChannel) {
|
||||
this.chatChannel = chatChannel;
|
||||
}
|
||||
|
||||
public TextComponent getComponent() {
|
||||
return component;
|
||||
}
|
||||
|
||||
public void setComponent(TextComponent component) {
|
||||
this.component = component;
|
||||
}
|
||||
|
||||
public List<ChatUser> getRecipients() {
|
||||
return recipients;
|
||||
}
|
||||
|
||||
public String getOriginalMessage() {
|
||||
return originalMessage;
|
||||
}
|
||||
|
||||
public boolean isCustomPlayerFormat() {
|
||||
return customPlayerFormat;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package net.draycia.carbon.events;
|
||||
|
||||
import net.draycia.carbon.channels.ChatChannel;
|
||||
import net.draycia.carbon.storage.ChatUser;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.event.Cancellable;
|
||||
import org.bukkit.event.Event;
|
||||
import org.bukkit.event.HandlerList;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class ChatFormatEvent extends Event implements Cancellable {
|
||||
|
||||
private static final HandlerList HANDLERS_LIST = new HandlerList();
|
||||
private boolean isCancelled = false;
|
||||
|
||||
private final ChatUser user;
|
||||
private ChatChannel chatChannel;
|
||||
private String format;
|
||||
private String message;
|
||||
|
||||
public ChatFormatEvent(ChatUser user, ChatChannel chatChannel, String format, String message) {
|
||||
super(!Bukkit.isPrimaryThread());
|
||||
|
||||
this.user = user;
|
||||
this.chatChannel = chatChannel;
|
||||
this.format = format;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCancelled() {
|
||||
return isCancelled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCancelled(boolean cancelled) {
|
||||
this.isCancelled = cancelled;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public HandlerList getHandlers() {
|
||||
return HANDLERS_LIST;
|
||||
}
|
||||
|
||||
public static HandlerList getHandlerList() {
|
||||
return HANDLERS_LIST;
|
||||
}
|
||||
|
||||
public ChatUser getUser() {
|
||||
return user;
|
||||
}
|
||||
|
||||
public ChatChannel getChatChannel() {
|
||||
return chatChannel;
|
||||
}
|
||||
|
||||
public void setChatChannel(ChatChannel chatChannel) {
|
||||
this.chatChannel = chatChannel;
|
||||
}
|
||||
|
||||
public String getFormat() {
|
||||
return format;
|
||||
}
|
||||
|
||||
public void setFormat(String format) {
|
||||
this.format = format;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package net.draycia.carbon.events;
|
||||
|
||||
import net.draycia.carbon.storage.ChatUser;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.event.Event;
|
||||
import org.bukkit.event.HandlerList;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class PrivateMessageEvent extends Event {
|
||||
|
||||
/** Bukkit event stuff **/
|
||||
private static final HandlerList handlers = new HandlerList();
|
||||
|
||||
@Override
|
||||
public @NotNull HandlerList getHandlers() {
|
||||
return handlers;
|
||||
}
|
||||
|
||||
public static HandlerList getHandlerList() {
|
||||
return handlers;
|
||||
}
|
||||
|
||||
/** Relevant stuff **/
|
||||
private final ChatUser sender;
|
||||
private final ChatUser target;
|
||||
private final Component senderComponent;
|
||||
private final Component targetComponent;
|
||||
private final String message;
|
||||
|
||||
public PrivateMessageEvent(ChatUser sender, ChatUser target, Component senderComponent, Component targetComponent, String message) {
|
||||
super(!Bukkit.isPrimaryThread());
|
||||
|
||||
this.sender = sender;
|
||||
this.target = target;
|
||||
this.senderComponent = senderComponent;
|
||||
this.targetComponent = targetComponent;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public ChatUser getSender() {
|
||||
return sender;
|
||||
}
|
||||
|
||||
public ChatUser getTarget() {
|
||||
return target;
|
||||
}
|
||||
|
||||
public Component getSenderComponent() {
|
||||
return senderComponent;
|
||||
}
|
||||
|
||||
public Component getTargetComponent() {
|
||||
return targetComponent;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package net.draycia.carbon.listeners;
|
||||
|
||||
import net.draycia.carbon.CarbonChat;
|
||||
import net.draycia.carbon.storage.ChatUser;
|
||||
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;
|
||||
|
||||
public class BukkitChatListener implements Listener {
|
||||
|
||||
private final CarbonChat carbonChat;
|
||||
|
||||
public BukkitChatListener(CarbonChat carbonChat) {
|
||||
this.carbonChat = carbonChat;
|
||||
}
|
||||
|
||||
// Chat messages
|
||||
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
|
||||
public void onPlayerchat(AsyncPlayerChatEvent event) {
|
||||
event.getRecipients().clear();
|
||||
|
||||
ChatUser user = carbonChat.getUserService().wrap(event.getPlayer());
|
||||
|
||||
if (event.isAsynchronous()) {
|
||||
user.getSelectedChannel().sendMessage(user, event.getMessage(), false);
|
||||
} else {
|
||||
Bukkit.getScheduler().runTaskAsynchronously(carbonChat, () -> {
|
||||
user.getSelectedChannel().sendMessage(user, event.getMessage(), false);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package net.draycia.carbon.listeners;
|
||||
|
||||
import net.draycia.carbon.CarbonChat;
|
||||
import net.draycia.carbon.events.ChatComponentEvent;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
|
||||
public class EmptyChatHandler implements Listener {
|
||||
|
||||
private final CarbonChat carbonChat;
|
||||
|
||||
public EmptyChatHandler(CarbonChat carbonChat) {
|
||||
this.carbonChat = carbonChat;
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.MONITOR)
|
||||
public void onComponent(ChatComponentEvent event) {
|
||||
if (event.isCustomPlayerFormat()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (event.getRecipients().size() == 1 && event.getUser().equals(event.getRecipients().get(0))) {
|
||||
String message = carbonChat.getConfig().getString("language.empty-channel");
|
||||
|
||||
if (message == null || message.isEmpty() || message.equalsIgnoreCase("NONE")) {
|
||||
return;
|
||||
}
|
||||
|
||||
event.getUser().sendMessage(carbonChat.getAdventureManager().processMessage(message));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package net.draycia.carbon.listeners;
|
||||
|
||||
import net.draycia.carbon.CarbonChat;
|
||||
import net.draycia.carbon.events.ChatComponentEvent;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class ItemLinkHandler implements Listener {
|
||||
|
||||
private final CarbonChat carbonChat;
|
||||
|
||||
public ItemLinkHandler(CarbonChat carbonChat) {
|
||||
this.carbonChat = carbonChat;
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.LOWEST)
|
||||
public void onItemLink(ChatComponentEvent event) {
|
||||
// Handle item linking placeholders
|
||||
if (event.getUser().isOnline()) {
|
||||
for (Pattern pattern : event.getChatChannel().getItemLinkPatterns()) {
|
||||
String patternContent = pattern.toString().replace("\\Q", "").replace("\\E", "");
|
||||
|
||||
if (event.getOriginalMessage().contains(patternContent)) {
|
||||
TextComponent component = event.getComponent().replaceFirst(pattern, (input) -> {
|
||||
return TextComponent.builder().append(carbonChat.getItemStackUtils().createComponent(event.getUser().asPlayer()));
|
||||
});
|
||||
|
||||
event.setComponent(component);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package net.draycia.carbon.listeners;
|
||||
|
||||
import net.draycia.carbon.events.ChatFormatEvent;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
|
||||
public class LegacyFormatHandler implements Listener {
|
||||
|
||||
@EventHandler(priority = EventPriority.LOWEST)
|
||||
public void onLegacyFormat(ChatFormatEvent event) {
|
||||
Component component = LegacyComponentSerializer.legacy('&').deserialize(event.getFormat());
|
||||
event.setFormat(MiniMessage.get().serialize(component));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package net.draycia.carbon.listeners;
|
||||
|
||||
import net.draycia.carbon.events.ChatFormatEvent;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
|
||||
public class OfflineNameHandler implements Listener {
|
||||
|
||||
@EventHandler(priority = EventPriority.HIGHEST)
|
||||
public void onOfflineMessage(ChatFormatEvent event) {
|
||||
// If the player isn't online (cross server message), use their normal name
|
||||
if (!event.getUser().isOnline()) {
|
||||
event.setFormat(event.getFormat().replace("%player_displayname%", "%player_name%"));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package net.draycia.carbon.listeners;
|
||||
|
||||
import net.draycia.carbon.CarbonChat;
|
||||
import net.draycia.carbon.events.ChatComponentEvent;
|
||||
import net.draycia.carbon.storage.ChatUser;
|
||||
import net.kyori.adventure.key.Key;
|
||||
import net.kyori.adventure.sound.Sound;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
|
||||
public class PingHandler implements Listener {
|
||||
|
||||
private CarbonChat carbonChat;
|
||||
|
||||
public PingHandler(CarbonChat carbonChat) {
|
||||
this.carbonChat = carbonChat;
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.MONITOR)
|
||||
public void onPing(ChatComponentEvent event) {
|
||||
for (ChatUser user : event.getRecipients()) {
|
||||
if (user.getUUID().equals(event.getUser().getUUID())) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (event.getOriginalMessage().contains(user.asPlayer().getName())) {
|
||||
if (carbonChat.getConfig().getBoolean("pings.enabled")) {
|
||||
Key key = Key.of(carbonChat.getConfig().getString("pings.sound"));
|
||||
Sound.Source source = Sound.Source.valueOf(carbonChat.getConfig().getString("pings.source"));
|
||||
float volume = (float) carbonChat.getConfig().getDouble("pings.volume");
|
||||
float pitch = (float) carbonChat.getConfig().getDouble("pings.pitch");
|
||||
|
||||
user.playSound(Sound.of(key, source, volume, pitch));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package net.draycia.carbon.listeners;
|
||||
|
||||
import me.clip.placeholderapi.PlaceholderAPI;
|
||||
import net.draycia.carbon.CarbonChat;
|
||||
import net.draycia.carbon.events.ChatFormatEvent;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
|
||||
public class PlaceholderHandler implements Listener {
|
||||
|
||||
private final CarbonChat carbonChat;
|
||||
|
||||
public PlaceholderHandler(CarbonChat carbonChat) {
|
||||
this.carbonChat = carbonChat;
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.LOWEST)
|
||||
public void onChatPlaceholder(ChatFormatEvent event) {
|
||||
ConfigurationSection replacements = carbonChat.getConfig().getConfigurationSection("replacements");
|
||||
|
||||
if (replacements != null) {
|
||||
for (String key : replacements.getKeys(false)) {
|
||||
event.setMessage(event.getMessage().replace(key, replacements.getString(key)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.LOWEST)
|
||||
public void onPapiPlaceholder(ChatFormatEvent event) {
|
||||
event.setFormat(PlaceholderAPI.setPlaceholders(event.getUser().asOfflinePlayer(), event.getFormat()));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package net.draycia.carbon.listeners;
|
||||
|
||||
import net.draycia.carbon.events.ChatFormatEvent;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
|
||||
public class UserFormattingListener implements Listener {
|
||||
|
||||
@EventHandler
|
||||
public void onFormat(ChatFormatEvent event) {
|
||||
if (event.getUser().isOnline() && event.getUser().asPlayer().hasPermission("carbonchat.formatting")) {
|
||||
event.setFormat(event.getFormat()
|
||||
.replace("<pre><message></pre>", "<message>")
|
||||
.replace("<pre><message>", "<message>")
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package net.draycia.carbon.managers;
|
||||
|
||||
import de.themoep.minedown.adventure.MineDown;
|
||||
import me.clip.placeholderapi.PlaceholderAPI;
|
||||
import net.draycia.carbon.CarbonChat;
|
||||
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class AdventureManager {
|
||||
|
||||
private final CarbonChat carbonChat;
|
||||
private final BukkitAudiences audiences;
|
||||
|
||||
public AdventureManager(CarbonChat carbonChat) {
|
||||
this.carbonChat = carbonChat;
|
||||
this.audiences = BukkitAudiences.create(carbonChat);
|
||||
}
|
||||
|
||||
public Component processMessageWithPapi(Player player, String input, String... placeholders) {
|
||||
return processMessage(PlaceholderAPI.setPlaceholders(player, input), placeholders);
|
||||
}
|
||||
|
||||
public Component processMessage(String input, String... placeholders) {
|
||||
switch (carbonChat.getConfig().getString("formatting.type", "minimessage").toLowerCase()) {
|
||||
case "minedown":
|
||||
return processMineDown(input, placeholders);
|
||||
case "minimessage-markdown":
|
||||
return MiniMessage.markdown().parse(input, placeholders);
|
||||
case "minimessage":
|
||||
default:
|
||||
return MiniMessage.get().parse(input, placeholders);
|
||||
}
|
||||
}
|
||||
|
||||
private Component processMineDown(String input, String... placeholders) {
|
||||
return MineDown.parse(input, placeholders);
|
||||
}
|
||||
|
||||
public BukkitAudiences getAudiences() {
|
||||
return audiences;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
package net.draycia.carbon.managers;
|
||||
|
||||
import net.draycia.carbon.CarbonChat;
|
||||
import net.draycia.carbon.channels.ChannelRegistry;
|
||||
import net.draycia.carbon.channels.ChatChannel;
|
||||
import net.draycia.carbon.channels.impls.*;
|
||||
import net.draycia.carbon.events.ChannelRegisterEvent;
|
||||
import net.draycia.carbon.util.Registry;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
public class ChannelManager {
|
||||
|
||||
private final CarbonChat carbonChat;
|
||||
private final ChannelRegistry registry;
|
||||
private String defaultChannelKey = null;
|
||||
|
||||
public ChannelManager(CarbonChat carbonChat) {
|
||||
this.carbonChat = carbonChat;
|
||||
this.registry = new ChannelRegistry(carbonChat);
|
||||
|
||||
for (String key : carbonChat.getConfig().getConfigurationSection("channels").getKeys(false)) {
|
||||
ConfigurationSection section = carbonChat.getConfig().getConfigurationSection("channels").getConfigurationSection(key);
|
||||
|
||||
ChatChannel channel = loadChannel(key, section);
|
||||
|
||||
if (channel != null) {
|
||||
if (registerChannel(channel)) {
|
||||
carbonChat.getLogger().info("Registering channel: " + channel.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public ChatChannel loadChannel(String key, ConfigurationSection section) {
|
||||
ChatChannel channel = new CarbonChatChannel(key, carbonChat, section);
|
||||
|
||||
String name = section.getString("name");
|
||||
|
||||
if (name != null && name.length() > 16) {
|
||||
carbonChat.getLogger().warning("Channel name [" + name + "] too long! Max length: 16.");
|
||||
carbonChat.getLogger().warning("Skipping channel, please check your settings!");
|
||||
return null;
|
||||
}
|
||||
|
||||
return channel;
|
||||
}
|
||||
|
||||
public boolean registerChannel(ChatChannel channel) {
|
||||
boolean success = getRegistry().register(channel.getKey(), channel);
|
||||
|
||||
if (success) {
|
||||
if (channel.isDefault() && defaultChannelKey == null) {
|
||||
carbonChat.getLogger().info("Default channel registered: " + channel.getName());
|
||||
defaultChannelKey = channel.getKey();
|
||||
}
|
||||
|
||||
Bukkit.getPluginManager().callEvent(new ChannelRegisterEvent(Collections.singletonList(channel), getRegistry()));
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
public Registry<ChatChannel> getRegistry() {
|
||||
return registry;
|
||||
}
|
||||
|
||||
public ChatChannel getDefaultChannel() {
|
||||
if (defaultChannelKey != null) {
|
||||
return registry.get(defaultChannelKey);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public ChatChannel getChannelOrDefault(String key) {
|
||||
if (key == null) {
|
||||
return getDefaultChannel();
|
||||
}
|
||||
|
||||
ChatChannel channel = registry.get(key);
|
||||
|
||||
if (channel == null) {
|
||||
return getDefaultChannel();
|
||||
}
|
||||
|
||||
return channel;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
package net.draycia.carbon.managers;
|
||||
|
||||
import co.aikar.commands.*;
|
||||
import net.draycia.carbon.CarbonChat;
|
||||
import net.draycia.carbon.channels.ChatChannel;
|
||||
import net.draycia.carbon.commands.*;
|
||||
import net.draycia.carbon.storage.ChatUser;
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class CommandManager {
|
||||
|
||||
private final BukkitCommandManager commandManager;
|
||||
|
||||
public CommandManager(CarbonChat carbonChat) {
|
||||
|
||||
commandManager = new BukkitCommandManager(carbonChat);
|
||||
|
||||
commandManager.enableUnstableAPI("help");
|
||||
|
||||
commandManager.getCommandCompletions().registerCompletion("chatchannel", (context) -> {
|
||||
ChatUser user = carbonChat.getUserService().wrap(context.getPlayer());
|
||||
|
||||
List<String> completions = new ArrayList<>();
|
||||
|
||||
for (ChatChannel chatChannel : carbonChat.getChannelManager().getRegistry().values()) {
|
||||
if (chatChannel.canPlayerUse(user)) {
|
||||
completions.add(chatChannel.getName());
|
||||
}
|
||||
}
|
||||
|
||||
return completions;
|
||||
});
|
||||
|
||||
commandManager.getCommandCompletions().registerCompletion("channel", (context) -> {
|
||||
List<String> completions = new ArrayList<>();
|
||||
|
||||
for (ChatChannel chatChannel : carbonChat.getChannelManager().getRegistry().values()) {
|
||||
completions.add(chatChannel.getName());
|
||||
}
|
||||
|
||||
return completions;
|
||||
});
|
||||
|
||||
commandManager.getCommandContexts().registerContext(ChatChannel.class, (context) -> {
|
||||
String name = context.popFirstArg();
|
||||
|
||||
for (ChatChannel chatChannel : carbonChat.getChannelManager().getRegistry().values()) {
|
||||
if (chatChannel.getName().equalsIgnoreCase(name)) {
|
||||
return chatChannel;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
});
|
||||
|
||||
commandManager.getCommandContexts().registerContext(ChatUser.class, (context) -> {
|
||||
return carbonChat.getUserService().wrap(Bukkit.getOfflinePlayer(context.popFirstArg()));
|
||||
});
|
||||
|
||||
commandManager.getCommandConditions().addCondition(ChatChannel.class,"canuse", (context, execution, value) -> {
|
||||
if (value == null) {
|
||||
throw new ConditionFailedException(carbonChat.getConfig().getString("language.cannot-use-channel"));
|
||||
}
|
||||
|
||||
ChatUser user = carbonChat.getUserService().wrap(context.getIssuer().getPlayer());
|
||||
|
||||
if (!value.canPlayerUse(user)) {
|
||||
throw new ConditionFailedException(value.getCannotUseMessage());
|
||||
}
|
||||
});
|
||||
|
||||
commandManager.getCommandConditions().addCondition(ChatChannel.class,"exists", (context, execution, value) -> {
|
||||
if (value == null) {
|
||||
throw new ConditionFailedException(carbonChat.getConfig().getString("language.cannot-use-channel"));
|
||||
}
|
||||
});
|
||||
|
||||
setupCommands(commandManager);
|
||||
}
|
||||
|
||||
private void setupCommands(BukkitCommandManager manager) {
|
||||
manager.registerCommand(new ToggleCommand());
|
||||
manager.registerCommand(new ChannelCommand());
|
||||
manager.registerCommand(new IgnoreCommand());
|
||||
manager.registerCommand(new ChatReloadCommand());
|
||||
manager.registerCommand(new MeCommand());
|
||||
manager.registerCommand(new MessageCommand());
|
||||
manager.registerCommand(new ReplyCommand());
|
||||
manager.registerCommand(new SetColorCommand());
|
||||
manager.registerCommand(new SpyChannelCommand());
|
||||
}
|
||||
|
||||
public co.aikar.commands.CommandManager getCommandManager() {
|
||||
return commandManager;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package net.draycia.carbon.managers;
|
||||
|
||||
import net.draycia.carbon.channels.ChatChannel;
|
||||
import net.draycia.carbon.channels.contexts.MessageContext;
|
||||
import net.draycia.carbon.channels.contexts.impl.SimpleMessageContext;
|
||||
import net.draycia.carbon.storage.ChatUser;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class ContextManager {
|
||||
|
||||
private final Map<String, Function<MessageContext, Boolean>> handlers = new HashMap<>();
|
||||
|
||||
public boolean register(String key, Function<MessageContext, Boolean> handler) {
|
||||
if (this.handlers.containsKey(key)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
this.handlers.put(key, handler);
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean testContext(ChatUser user, ChatUser target, ChatChannel channel) {
|
||||
for (Map.Entry<String, Function<MessageContext, Boolean>> handler : handlers.entrySet()) {
|
||||
String key = handler.getKey();
|
||||
|
||||
Object value = channel.getContext(key);
|
||||
|
||||
if (value == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
MessageContext context = new SimpleMessageContext(user, target, channel, value);
|
||||
|
||||
return handler.getValue().apply(context);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,171 @@
|
||||
package net.draycia.carbon.managers;
|
||||
|
||||
import com.google.common.io.ByteArrayDataInput;
|
||||
import com.google.common.io.ByteArrayDataOutput;
|
||||
import com.google.common.io.ByteStreams;
|
||||
import io.github.leonardosnt.bungeechannelapi.BungeeChannelApi;
|
||||
import net.draycia.carbon.CarbonChat;
|
||||
import net.draycia.carbon.channels.ChatChannel;
|
||||
import net.draycia.carbon.storage.ChatUser;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.messaging.PluginMessageListener;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public class PluginMessageManager implements PluginMessageListener {
|
||||
|
||||
private final CarbonChat carbonChat;
|
||||
private final BungeeChannelApi api;
|
||||
|
||||
public PluginMessageManager(CarbonChat carbonChat) {
|
||||
this.carbonChat = carbonChat;
|
||||
|
||||
carbonChat.getServer().getMessenger().registerOutgoingPluginChannel(carbonChat, "BungeeCord");
|
||||
carbonChat.getServer().getMessenger().registerIncomingPluginChannel(carbonChat, "BungeeCord", this);
|
||||
|
||||
api = BungeeChannelApi.of(carbonChat);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPluginMessageReceived(String channel, @NotNull Player player, @NotNull byte[] message) {
|
||||
if (!channel.equals("BungeeCord")) {
|
||||
return;
|
||||
}
|
||||
|
||||
ByteArrayDataInput input = ByteStreams.newDataInput(message);
|
||||
|
||||
String subchannel = input.readUTF();
|
||||
short length = input.readShort();
|
||||
byte[] msgbytes = new byte[length];
|
||||
input.readFully(msgbytes);
|
||||
|
||||
ByteArrayDataInput msg = ByteStreams.newDataInput(msgbytes);
|
||||
|
||||
switch (subchannel) {
|
||||
case "carbonchat:message":
|
||||
onMessageReceived(msg);
|
||||
break;
|
||||
case "carbonchat:component":
|
||||
onComponentReceived(msg);
|
||||
break;
|
||||
case "carbonchat:whisper":
|
||||
onWhisperReceived(msg);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void onMessageReceived(ByteArrayDataInput in) {
|
||||
String chatChannelName = in.readUTF();
|
||||
|
||||
ChatChannel chatChannel = carbonChat.getChannelManager().getRegistry().get(chatChannelName);
|
||||
|
||||
if (chatChannel == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
UUID playerUUID;
|
||||
|
||||
try {
|
||||
playerUUID = UUID.fromString(in.readUTF());
|
||||
} catch (IllegalArgumentException e) {
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
|
||||
ChatUser user = carbonChat.getUserService().wrap(playerUUID);
|
||||
|
||||
String chatMessage = in.readUTF();
|
||||
|
||||
Bukkit.getScheduler().runTaskAsynchronously(carbonChat, () -> {
|
||||
chatChannel.sendMessage(user, chatMessage, true);
|
||||
});
|
||||
}
|
||||
|
||||
public void onComponentReceived(ByteArrayDataInput in) {
|
||||
String chatChannelName = in.readUTF();
|
||||
|
||||
ChatChannel chatChannel = carbonChat.getChannelManager().getRegistry().get(chatChannelName);
|
||||
|
||||
if (chatChannel == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
UUID playerUUID;
|
||||
|
||||
try {
|
||||
playerUUID = UUID.fromString(in.readUTF());
|
||||
} catch (IllegalArgumentException e) {
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
|
||||
ChatUser user = carbonChat.getUserService().wrap(playerUUID);
|
||||
|
||||
String chatMessage = in.readUTF();
|
||||
|
||||
Bukkit.getScheduler().scheduleAsyncDelayedTask(carbonChat, () -> {
|
||||
chatChannel.sendComponent(user, carbonChat.getAdventureManager().processMessage(chatMessage, "br", "\n"));
|
||||
});
|
||||
}
|
||||
|
||||
private void onWhisperReceived(ByteArrayDataInput in) {
|
||||
UUID targetUUID;
|
||||
|
||||
try {
|
||||
targetUUID = UUID.fromString(in.readUTF());
|
||||
} catch (IllegalArgumentException e) {
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
|
||||
String chatMessage = in.readUTF();
|
||||
|
||||
ChatUser target = carbonChat.getUserService().wrap(targetUUID);
|
||||
target.sendMessage(carbonChat.getAdventureManager().processMessage(chatMessage, "br", "\n"));
|
||||
}
|
||||
|
||||
public void sendMessage(ChatChannel chatChannel, Player player, String message) {
|
||||
ByteArrayDataOutput msg = ByteStreams.newDataOutput();
|
||||
|
||||
msg.writeUTF(chatChannel.getName());
|
||||
msg.writeUTF(player.getUniqueId().toString());
|
||||
msg.writeUTF(message);
|
||||
|
||||
api.forward("ALL", "carbonchat:message", msg.toByteArray());
|
||||
}
|
||||
|
||||
public void sendComponent(ChatChannel chatChannel, Player player, Component component) {
|
||||
ByteArrayDataOutput msg = ByteStreams.newDataOutput();
|
||||
|
||||
msg.writeUTF(chatChannel.getName());
|
||||
msg.writeUTF(player.getUniqueId().toString());
|
||||
msg.writeUTF(MiniMessage.get().serialize(component));
|
||||
|
||||
api.forward("ALL", "carbonchat:component", msg.toByteArray());
|
||||
}
|
||||
|
||||
public void sendComponentToPlayer(ChatUser sender, ChatUser target, Component toComponent, Component fromComponent) {
|
||||
api.getPlayerList("ALL").whenComplete((result, error) -> {
|
||||
if (!result.contains(target.asOfflinePlayer().getName())) {
|
||||
return;
|
||||
}
|
||||
|
||||
sender.sendMessage(toComponent);
|
||||
|
||||
if (sender.isShadowMuted()) {
|
||||
return;
|
||||
}
|
||||
|
||||
ByteArrayDataOutput msg = ByteStreams.newDataOutput();
|
||||
|
||||
msg.writeUTF(target.getUUID().toString());
|
||||
msg.writeUTF(MiniMessage.get().serialize(fromComponent));
|
||||
|
||||
api.forward("ALL", "carbonchat:whisper", msg.toByteArray());
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
package net.draycia.carbon.managers;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import io.lettuce.core.RedisClient;
|
||||
import io.lettuce.core.RedisURI;
|
||||
import io.lettuce.core.pubsub.RedisPubSubListener;
|
||||
import io.lettuce.core.pubsub.StatefulRedisPubSubConnection;
|
||||
import io.lettuce.core.pubsub.api.sync.RedisPubSubCommands;
|
||||
import net.draycia.carbon.CarbonChat;
|
||||
import net.draycia.carbon.storage.impl.CarbonChatUser;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.UUID;
|
||||
|
||||
public class RedisManager {
|
||||
|
||||
private final RedisClient client;
|
||||
private final StatefulRedisPubSubConnection<String, String> connection;
|
||||
private final RedisPubSubCommands<String, String> sync;
|
||||
|
||||
private final Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
||||
private final Type userType = new TypeToken<CarbonChatUser>() {}.getType();
|
||||
|
||||
public RedisManager(CarbonChat carbonChat) {
|
||||
|
||||
String host = carbonChat.getConfig().getString("redis.host");
|
||||
int port = carbonChat.getConfig().getInt("redis.port");
|
||||
int database = carbonChat.getConfig().getInt("redis.database");
|
||||
|
||||
RedisURI uri = RedisURI.Builder.redis(host, port)
|
||||
.withDatabase(database)
|
||||
.build();
|
||||
|
||||
this.client = RedisClient.create(uri);
|
||||
this.connection = client.connectPubSub();
|
||||
this.sync = connection.sync();
|
||||
|
||||
connection.addListener(new RedisPubSubListener<String, String>() {
|
||||
@Override
|
||||
public void message(String channel, String message) {
|
||||
if (channel.equalsIgnoreCase("refreshuser")) {
|
||||
carbonChat.getUserService().refreshUser(UUID.fromString(message));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void message(String pattern, String channel, String message) { }
|
||||
|
||||
@Override
|
||||
public void subscribed(String channel, long count) { }
|
||||
|
||||
@Override
|
||||
public void psubscribed(String pattern, long count) { }
|
||||
|
||||
@Override
|
||||
public void unsubscribed(String channel, long count) { }
|
||||
|
||||
@Override
|
||||
public void punsubscribed(String pattern, long count) { }
|
||||
});
|
||||
|
||||
sync.subscribe("userdata");
|
||||
}
|
||||
|
||||
public void publishUser(CarbonChatUser user) {
|
||||
String data = gson.toJson(user, userType);
|
||||
|
||||
sync.set(user.getUUID().toString(), data);
|
||||
sync.publish("refreshuser", user.getUUID().toString());
|
||||
}
|
||||
|
||||
public CarbonChatUser getUser(UUID uuid) {
|
||||
String value = sync.get(uuid.toString());
|
||||
|
||||
if (value == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return gson.fromJson(value, userType);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package net.draycia.carbon.storage;
|
||||
|
||||
import net.draycia.carbon.channels.ChatChannel;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public interface ChatUser extends Audience {
|
||||
|
||||
Player asPlayer();
|
||||
OfflinePlayer asOfflinePlayer();
|
||||
UUID getUUID();
|
||||
|
||||
boolean isOnline();
|
||||
|
||||
ChatChannel getSelectedChannel();
|
||||
void setSelectedChannel(ChatChannel channel);
|
||||
|
||||
void clearSelectedChannel();
|
||||
|
||||
UserChannelSettings getChannelSettings(ChatChannel channel);
|
||||
|
||||
void setSpyingWhispers(boolean spyingWhispers);
|
||||
boolean isSpyingWhispers();
|
||||
|
||||
void setMuted(boolean muted);
|
||||
boolean isMuted();
|
||||
|
||||
void setShadowMuted(boolean shadowMuted);
|
||||
boolean isShadowMuted();
|
||||
|
||||
@Nullable UUID getReplyTarget();
|
||||
void setReplyTarget(@Nullable UUID target);
|
||||
void setReplyTarget(@Nullable ChatUser user);
|
||||
|
||||
boolean isIgnoringUser(UUID uuid);
|
||||
boolean isIgnoringUser(ChatUser user);
|
||||
void setIgnoringUser(UUID uuid, boolean ignoring);
|
||||
void setIgnoringUser(ChatUser user, boolean ignoring);
|
||||
|
||||
void sendMessage(ChatUser sender, String message);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package net.draycia.carbon.storage;
|
||||
|
||||
import net.kyori.adventure.text.format.TextColor;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public interface UserChannelSettings {
|
||||
|
||||
boolean isSpying();
|
||||
void setSpying(boolean spying);
|
||||
|
||||
boolean isIgnored();
|
||||
void setIgnoring(boolean ignored);
|
||||
|
||||
@Nullable TextColor getColor();
|
||||
void setColor(@Nullable TextColor color);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package net.draycia.carbon.storage;
|
||||
|
||||
import org.bukkit.OfflinePlayer;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public abstract class UserService {
|
||||
|
||||
public abstract ChatUser wrap(OfflinePlayer player);
|
||||
public abstract ChatUser wrap(UUID uuid);
|
||||
|
||||
public abstract void refreshUser(UUID uuid);
|
||||
|
||||
public abstract void cleanUp();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,270 @@
|
||||
package net.draycia.carbon.storage.impl;
|
||||
|
||||
import net.draycia.carbon.CarbonChat;
|
||||
import net.draycia.carbon.channels.ChatChannel;
|
||||
import net.draycia.carbon.events.ChannelSwitchEvent;
|
||||
import net.draycia.carbon.events.PrivateMessageEvent;
|
||||
import net.draycia.carbon.storage.ChatUser;
|
||||
import net.draycia.carbon.storage.UserChannelSettings;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.kyori.adventure.audience.ForwardingAudience;
|
||||
import net.kyori.adventure.key.Key;
|
||||
import net.kyori.adventure.sound.Sound;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
|
||||
import javax.annotation.CheckForNull;
|
||||
import java.util.*;
|
||||
|
||||
public class CarbonChatUser implements ChatUser, ForwardingAudience {
|
||||
|
||||
private final transient CarbonChat carbonChat;
|
||||
|
||||
private UUID uuid;
|
||||
|
||||
private String selectedChannel = null;
|
||||
private final Map<String, SimpleUserChannelSettings> channelSettings = new HashMap<>();
|
||||
private final List<UUID> ignoredUsers = new ArrayList<>();
|
||||
private boolean muted = false;
|
||||
private boolean shadowMuted = false;
|
||||
private boolean spyingWhispers = false;
|
||||
|
||||
private transient UUID replyTarget = null;
|
||||
|
||||
public CarbonChatUser() {
|
||||
this.carbonChat = (CarbonChat)Bukkit.getPluginManager().getPlugin("CarbonChat");
|
||||
}
|
||||
|
||||
public CarbonChatUser(UUID uuid) {
|
||||
this.carbonChat = (CarbonChat)Bukkit.getPluginManager().getPlugin("CarbonChat");
|
||||
this.uuid = uuid;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull Iterable<? extends Audience> audiences() {
|
||||
return Collections.singleton(carbonChat.getAdventureManager().getAudiences().player(uuid));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Player asPlayer() {
|
||||
return Bukkit.getPlayer(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public OfflinePlayer asOfflinePlayer() {
|
||||
return Bukkit.getOfflinePlayer(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isOnline() {
|
||||
return asOfflinePlayer().isOnline();
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID getUUID() {
|
||||
return uuid;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChatChannel getSelectedChannel() {
|
||||
return carbonChat.getChannelManager().getChannelOrDefault(selectedChannel);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSelectedChannel(ChatChannel chatChannel) {
|
||||
String failureMessage = carbonChat.getConfig().getString("language.channel-switch-failure");
|
||||
ChannelSwitchEvent event = new ChannelSwitchEvent(chatChannel, this, failureMessage);
|
||||
|
||||
Bukkit.getPluginManager().callEvent(event);
|
||||
|
||||
if (event.isCancelled()) {
|
||||
sendMessage(carbonChat.getAdventureManager().processMessage(event.getFailureMessage(),
|
||||
"channel", chatChannel.getName()));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
this.selectedChannel = chatChannel.getKey();
|
||||
syncToRedis();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearSelectedChannel() {
|
||||
setSelectedChannel(carbonChat.getChannelManager().getDefaultChannel());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isIgnoringUser(UUID uuid) {
|
||||
return ignoredUsers.contains(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isIgnoringUser(ChatUser user) {
|
||||
return ignoredUsers.contains(user.getUUID());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setIgnoringUser(UUID uuid, boolean ignoring) {
|
||||
if (ignoring) {
|
||||
ignoredUsers.add(uuid);
|
||||
} else {
|
||||
ignoredUsers.remove(uuid);
|
||||
}
|
||||
|
||||
syncToRedis();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setIgnoringUser(ChatUser user, boolean ignoring) {
|
||||
setIgnoringUser(user.getUUID(), ignoring);
|
||||
}
|
||||
|
||||
public List<UUID> getIgnoredUsers() {
|
||||
return ignoredUsers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setMuted(boolean muted) {
|
||||
this.muted = muted;
|
||||
|
||||
syncToRedis();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isMuted() {
|
||||
return muted;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setShadowMuted(boolean shadowMuted) {
|
||||
this.shadowMuted = shadowMuted;
|
||||
|
||||
syncToRedis();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isShadowMuted() {
|
||||
return shadowMuted;
|
||||
}
|
||||
|
||||
@CheckForNull
|
||||
@Override
|
||||
public UUID getReplyTarget() {
|
||||
return replyTarget;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setReplyTarget(UUID target) {
|
||||
this.replyTarget = target;
|
||||
|
||||
syncToRedis();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setReplyTarget(ChatUser user) {
|
||||
setReplyTarget(user.getUUID());
|
||||
}
|
||||
|
||||
@Override
|
||||
public UserChannelSettings getChannelSettings(ChatChannel channel) {
|
||||
return channelSettings.computeIfAbsent(channel.getName(), (name) -> new SimpleUserChannelSettings());
|
||||
}
|
||||
|
||||
public Map<String, ? extends UserChannelSettings> getChannelSettings() {
|
||||
return channelSettings;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSpyingWhispers(boolean spyingWhispers) {
|
||||
this.spyingWhispers = spyingWhispers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSpyingWhispers() {
|
||||
return spyingWhispers;
|
||||
}
|
||||
|
||||
public void sendMessage(ChatUser sender, String message) {
|
||||
if (isIgnoringUser(sender) || sender.isIgnoringUser(this)) {
|
||||
return;
|
||||
}
|
||||
|
||||
String toPlayerFormat = carbonChat.getConfig().getString("language.message-to-other");
|
||||
String fromPlayerFormat = carbonChat.getConfig().getString("language.message-from-other");
|
||||
|
||||
String senderName = sender.asOfflinePlayer().getName();
|
||||
String senderOfflineName = senderName;
|
||||
|
||||
String targetName = this.asOfflinePlayer().getName();
|
||||
String targetOfflineName = targetName;
|
||||
|
||||
if (sender.isOnline()) {
|
||||
senderName = sender.asPlayer().getDisplayName();
|
||||
}
|
||||
|
||||
if (this.isOnline()) {
|
||||
targetName = this.asPlayer().getDisplayName();
|
||||
}
|
||||
|
||||
Component toPlayerComponent = carbonChat.getAdventureManager().processMessage(toPlayerFormat, "br", "\n",
|
||||
"message", message,
|
||||
"targetname", targetOfflineName, "sendername", senderOfflineName,
|
||||
"target", targetName, "sender", senderName);
|
||||
|
||||
Component fromPlayerComponent = carbonChat.getAdventureManager().processMessage(fromPlayerFormat, "br", "\n",
|
||||
"message", message,
|
||||
"targetname", targetOfflineName, "sendername", senderOfflineName,
|
||||
"target", targetName, "sender", senderName);
|
||||
|
||||
if (this.isOnline()) {
|
||||
if (sender.isOnline()) {
|
||||
sender.sendMessage(toPlayerComponent);
|
||||
|
||||
if (sender.isShadowMuted()) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.sendMessage(fromPlayerComponent);
|
||||
|
||||
sender.setReplyTarget(this);
|
||||
this.setReplyTarget(sender);
|
||||
|
||||
if (carbonChat.getConfig().getBoolean("pings.on-whisper")) {
|
||||
Key key = Key.of(carbonChat.getConfig().getString("pings.sound"));
|
||||
Sound.Source source = Sound.Source.valueOf(carbonChat.getConfig().getString("pings.source"));
|
||||
float volume = (float) carbonChat.getConfig().getDouble("pings.volume");
|
||||
float pitch = (float) carbonChat.getConfig().getDouble("pings.pitch");
|
||||
|
||||
this.playSound(Sound.of(key, source, volume, pitch));
|
||||
}
|
||||
}
|
||||
} else if (sender.isOnline()) {
|
||||
carbonChat.getPluginMessageManager().sendComponentToPlayer(sender, this, toPlayerComponent, fromPlayerComponent);
|
||||
}
|
||||
|
||||
for (Player player : Bukkit.getOnlinePlayers()) {
|
||||
ChatUser user = carbonChat.getUserService().wrap(player);
|
||||
|
||||
if (user.getUUID().equals(sender.getUUID()) || user.getUUID().equals(getUUID())) {
|
||||
continue;
|
||||
}
|
||||
|
||||
user.sendMessage(carbonChat.getAdventureManager().processMessage(carbonChat.getConfig().getString("language.spy-whispers"), "br", "\n",
|
||||
"message", message,
|
||||
"targetname", targetOfflineName, "sendername", senderOfflineName,
|
||||
"target", targetName, "sender", senderName));
|
||||
}
|
||||
|
||||
Bukkit.getPluginManager().callEvent(new PrivateMessageEvent(sender, this, toPlayerComponent, fromPlayerComponent, message));
|
||||
}
|
||||
|
||||
private void syncToRedis() {
|
||||
if (carbonChat.getRedisManager() != null) {
|
||||
carbonChat.getRedisManager().publishUser(this);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
package net.draycia.carbon.storage.impl;
|
||||
|
||||
import com.google.common.cache.*;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import com.google.gson.stream.JsonReader;
|
||||
import com.google.gson.stream.JsonWriter;
|
||||
import net.draycia.carbon.CarbonChat;
|
||||
import net.draycia.carbon.storage.ChatUser;
|
||||
import net.draycia.carbon.storage.UserService;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class JSONUserService extends UserService {
|
||||
|
||||
private final CarbonChat carbonChat;
|
||||
|
||||
private final LoadingCache<UUID, CarbonChatUser> userCache = CacheBuilder.newBuilder()
|
||||
.expireAfterWrite(10, TimeUnit.MINUTES)
|
||||
.removalListener(this::saveUser)
|
||||
.build(CacheLoader.from(this::loadUser));
|
||||
|
||||
private final Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
||||
private final Type userType = new TypeToken<CarbonChatUser>() {}.getType();
|
||||
|
||||
public JSONUserService(CarbonChat carbonChat) {
|
||||
this.carbonChat = carbonChat;
|
||||
|
||||
Bukkit.getScheduler().scheduleSyncRepeatingTask(carbonChat, userCache::cleanUp, 0L, 20 * 60 * 10);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChatUser wrap(OfflinePlayer player) {
|
||||
return wrap(player.getUniqueId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChatUser wrap(UUID uuid) {
|
||||
try {
|
||||
return userCache.get(uuid);
|
||||
} catch (ExecutionException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void refreshUser(UUID uuid) {
|
||||
userCache.invalidate(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cleanUp() {
|
||||
userCache.invalidateAll();
|
||||
userCache.cleanUp();
|
||||
}
|
||||
|
||||
private CarbonChatUser loadUser(UUID uuid) {
|
||||
if (carbonChat.getRedisManager() != null) {
|
||||
CarbonChatUser user = carbonChat.getRedisManager().getUser(uuid);
|
||||
|
||||
if (user != null) {
|
||||
return user;
|
||||
}
|
||||
}
|
||||
|
||||
File userFile = new File(carbonChat.getDataFolder(), "users/" + uuid.toString() + ".json");
|
||||
ensureFileExists(userFile);
|
||||
|
||||
try (JsonReader reader = gson.newJsonReader(new FileReader(userFile))) {
|
||||
CarbonChatUser user = gson.fromJson(reader, userType);
|
||||
|
||||
if (user != null) {
|
||||
return user;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return new CarbonChatUser(uuid);
|
||||
}
|
||||
|
||||
private void saveUser(RemovalNotification<UUID, CarbonChatUser> notification) {
|
||||
File userFile = new File(carbonChat.getDataFolder(), "users/" + notification.getKey().toString() + ".json");
|
||||
ensureFileExists(userFile);
|
||||
|
||||
try (JsonWriter writer = gson.newJsonWriter(new FileWriter(userFile))) {
|
||||
gson.toJson(notification.getValue(), userType, writer);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private void ensureFileExists(File file) {
|
||||
if (!file.getParentFile().exists()) {
|
||||
file.getParentFile().mkdirs();
|
||||
}
|
||||
|
||||
if (!file.exists()) {
|
||||
try {
|
||||
file.createNewFile();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,208 @@
|
||||
package net.draycia.carbon.storage.impl;
|
||||
|
||||
import co.aikar.idb.*;
|
||||
import com.google.common.cache.CacheBuilder;
|
||||
import com.google.common.cache.CacheLoader;
|
||||
import com.google.common.cache.LoadingCache;
|
||||
import com.google.common.cache.RemovalNotification;
|
||||
import net.draycia.carbon.CarbonChat;
|
||||
import net.draycia.carbon.channels.ChatChannel;
|
||||
import net.draycia.carbon.storage.ChatUser;
|
||||
import net.draycia.carbon.storage.UserChannelSettings;
|
||||
import net.draycia.carbon.storage.UserService;
|
||||
import net.kyori.adventure.text.format.TextColor;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.sql.SQLSyntaxErrorException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class MySQLUserService extends UserService {
|
||||
|
||||
private final LoadingCache<UUID, CarbonChatUser> userCache = CacheBuilder.newBuilder()
|
||||
.expireAfterWrite(10, TimeUnit.MINUTES)
|
||||
.removalListener(this::saveUser)
|
||||
.build(CacheLoader.from(this::loadUser));
|
||||
|
||||
private final CarbonChat carbonChat;
|
||||
private final Database database;
|
||||
|
||||
public MySQLUserService(CarbonChat carbonChat) {
|
||||
this.carbonChat = carbonChat;
|
||||
|
||||
ConfigurationSection section = carbonChat.getConfig().getConfigurationSection("storage");
|
||||
|
||||
String username = section.getString("username");
|
||||
String password = section.getString("password");
|
||||
String dbname = section.getString("database");
|
||||
String hostandport = section.getString("hostname") + ":" + section.getString("port");
|
||||
|
||||
database = new HikariPooledDatabase(BukkitDB.getRecommendedOptions(carbonChat, username, password, dbname, hostandport));
|
||||
|
||||
DB.setGlobalDatabase(database);
|
||||
|
||||
try {
|
||||
database.executeUpdate("CREATE TABLE IF NOT EXISTS sc_users (uuid CHAR(36) PRIMARY KEY," +
|
||||
"channel VARCHAR(16), muted BOOLEAN, shadowmuted BOOLEAN, spyingwhispers BOOLEAN)");
|
||||
|
||||
// Ignore the exception, it's just saying the column already exists
|
||||
try {
|
||||
database.executeUpdate("ALTER TABLE sc_users ADD COLUMN spyingwhispers BOOLEAN DEFAULT false");
|
||||
} catch (SQLSyntaxErrorException syntaxErrorException) {}
|
||||
|
||||
database.executeUpdate("CREATE TABLE IF NOT EXISTS sc_channel_settings (uuid CHAR(36), channel CHAR(16), spying BOOLEAN, ignored BOOLEAN, color TINYTEXT, PRIMARY KEY (uuid, channel))");
|
||||
|
||||
database.executeUpdate("CREATE TABLE IF NOT EXISTS sc_ignored_users (uuid CHAR(36), user CHAR(36), PRIMARY KEY (uuid, user))");
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
Bukkit.getScheduler().scheduleSyncRepeatingTask(carbonChat, userCache::cleanUp, 0L, 20 * 60 * 10);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChatUser wrap(OfflinePlayer player) {
|
||||
return wrap(player.getUniqueId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChatUser wrap(UUID uuid) {
|
||||
try {
|
||||
return userCache.get(uuid);
|
||||
} catch (ExecutionException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cleanUp() {
|
||||
userCache.invalidateAll();
|
||||
userCache.cleanUp();
|
||||
database.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void refreshUser(UUID uuid) {
|
||||
userCache.invalidate(uuid);
|
||||
}
|
||||
|
||||
private CarbonChatUser loadUser(UUID uuid) {
|
||||
if (carbonChat.getRedisManager() != null) {
|
||||
CarbonChatUser user = carbonChat.getRedisManager().getUser(uuid);
|
||||
|
||||
if (user != null) {
|
||||
return user;
|
||||
}
|
||||
}
|
||||
|
||||
CarbonChatUser user = new CarbonChatUser(uuid);
|
||||
|
||||
try (DbStatement statement = database.query("SELECT * from sc_users WHERE uuid = ?;")) {
|
||||
statement.execute(uuid.toString());
|
||||
|
||||
DbRow users = statement.getNextRow();
|
||||
|
||||
if (users == null) {
|
||||
return user;
|
||||
}
|
||||
|
||||
List<DbRow> channelSettings = database.getResults("SELECT * from sc_channel_settings WHERE uuid = ?;", uuid.toString());
|
||||
List<DbRow> ignoredUsers = database.getResults("SELECT * from sc_ignored_users WHERE uuid = ?;", uuid.toString());
|
||||
|
||||
ChatChannel channel = carbonChat.getChannelManager().getChannelOrDefault(users.getString("channel"));
|
||||
|
||||
if (channel != null) {
|
||||
user.setSelectedChannel(channel);
|
||||
}
|
||||
|
||||
user.setMuted(users.<Boolean>get("muted"));
|
||||
user.setShadowMuted(users.<Boolean>get("shadowmuted"));
|
||||
user.setSpyingWhispers(users.<Boolean>get("spyingwhispers"));
|
||||
|
||||
for (DbRow channelSetting : channelSettings) {
|
||||
ChatChannel chatChannel = carbonChat.getChannelManager().getRegistry().get(channelSetting.getString("channel"));
|
||||
|
||||
if (chatChannel != null) {
|
||||
UserChannelSettings settings = user.getChannelSettings(chatChannel);
|
||||
|
||||
settings.setSpying(channelSetting.<Boolean>get("spying"));
|
||||
settings.setIgnoring(channelSetting.<Boolean>get("ignored"));
|
||||
|
||||
String color = channelSetting.getString("color");
|
||||
|
||||
if (color != null) {
|
||||
settings.setColor(TextColor.fromHexString(color));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (DbRow ignoredUser : ignoredUsers) {
|
||||
user.setIgnoringUser(UUID.fromString(ignoredUser.getString("user")), true);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
private void saveUser(RemovalNotification<UUID, CarbonChatUser> notification) {
|
||||
CarbonChatUser user = notification.getValue();
|
||||
|
||||
if (user == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
database.createTransaction(stm -> {
|
||||
// Save user general data
|
||||
String selectedName = null;
|
||||
|
||||
if (user.getSelectedChannel() != null) {
|
||||
selectedName = user.getSelectedChannel().getName();
|
||||
}
|
||||
|
||||
carbonChat.getLogger().info("Saving user data!");
|
||||
stm.executeUpdateQuery("INSERT INTO sc_users (uuid, channel, muted, shadowmuted, spyingwhispers) VALUES (?, ?, ?, ?, ?) " +
|
||||
"ON DUPLICATE KEY UPDATE channel = ?, muted = ?, shadowmuted = ?, spyingwhispers = ?",
|
||||
user.getUUID().toString(), selectedName, user.isMuted(), user.isShadowMuted(), user.isSpyingWhispers(),
|
||||
selectedName, user.isMuted(), user.isShadowMuted(), user.isSpyingWhispers());
|
||||
|
||||
carbonChat.getLogger().info("Saving user channel settings!");
|
||||
// Save user channel settings
|
||||
for (Map.Entry<String, ? extends UserChannelSettings> entry : user.getChannelSettings().entrySet()) {
|
||||
UserChannelSettings value = entry.getValue();
|
||||
|
||||
String colorString = null;
|
||||
|
||||
if (value.getColor() != null) {
|
||||
colorString = value.getColor().asHexString();
|
||||
}
|
||||
|
||||
stm.executeUpdateQuery("INSERT INTO sc_channel_settings (uuid, channel, spying, ignored, color) VALUES (?, ?, ?, ?, ?) " +
|
||||
"ON DUPLICATE KEY UPDATE spying = ?, ignored = ?, color = ?",
|
||||
user.getUUID().toString(), entry.getKey(), value.isSpying(), value.isIgnored(), colorString,
|
||||
value.isSpying(), value.isIgnored(), colorString);
|
||||
}
|
||||
|
||||
carbonChat.getLogger().info("Saving user ignores!");
|
||||
// Save user ignore list (remove old entries then add new ones)
|
||||
// TODO: keep DB up to date with settings as settings are mutated
|
||||
stm.executeUpdateQuery("DELETE FROM sc_ignored_users WHERE uuid = ?", user.getUUID().toString());
|
||||
|
||||
for (UUID entry : user.getIgnoredUsers()) {
|
||||
stm.executeUpdateQuery("INSERT INTO sc_ignored_users (uuid, user) VALUES (?, ?)",
|
||||
user.getUUID().toString(), entry.toString());
|
||||
}
|
||||
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
package net.draycia.carbon.storage.impl;
|
||||
|
||||
import net.draycia.carbon.storage.UserChannelSettings;
|
||||
import net.kyori.adventure.text.format.TextColor;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class SimpleUserChannelSettings implements UserChannelSettings {
|
||||
|
||||
private boolean spying;
|
||||
private boolean ignored;
|
||||
private String color;
|
||||
|
||||
@Override
|
||||
public boolean isSpying() {
|
||||
return this.spying;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSpying(boolean spying) {
|
||||
this.spying = spying;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isIgnored() {
|
||||
return ignored;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setIgnoring(boolean ignored) {
|
||||
this.ignored = ignored;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable TextColor getColor() {
|
||||
if (color == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return TextColor.fromHexString(color);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setColor(@Nullable TextColor color) {
|
||||
if (color == null) {
|
||||
this.color = null;
|
||||
return;
|
||||
}
|
||||
|
||||
this.color = color.asHexString();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
Simple channel based chat plugin for Spigot
|
||||
Copyright (C) 2020 Alexander Söderberg
|
||||
Copyright (C) 2020 Josua Parks (Draycia)
|
||||
|
||||
Modifications made to work with Kyori/Adventure
|
||||
|
||||
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.util;
|
||||
|
||||
import net.kyori.adventure.platform.bukkit.MinecraftComponentSerializer;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
public final class ItemStackUtils {
|
||||
|
||||
private final Method asNMSCopyMethod;
|
||||
private final Method cMethod;
|
||||
|
||||
public ItemStackUtils() throws NoSuchMethodException, ClassNotFoundException {
|
||||
final String version = Bukkit.getServer().getClass().getPackage().getName()
|
||||
.substring("org.bukkit.craftbukkit.".length());
|
||||
|
||||
Class<?> craftItemStackClass = Class.forName("org.bukkit.craftbukkit." + version + ".inventory.CraftItemStack");
|
||||
this.asNMSCopyMethod = craftItemStackClass.getMethod("asNMSCopy", ItemStack.class);
|
||||
Class<?> itemStackClass = Class.forName("net.minecraft.server." + version + ".ItemStack");
|
||||
this.cMethod = itemStackClass.getMethod("C");
|
||||
}
|
||||
|
||||
public Component createComponent(final Player player) {
|
||||
final ItemStack itemStack = player.getInventory().getItemInMainHand();
|
||||
|
||||
if (itemStack == null || itemStack.getType() == Material.AIR) {
|
||||
return TextComponent.empty();
|
||||
}
|
||||
|
||||
try {
|
||||
final Object cbItemStack = this.asNMSCopyMethod.invoke(null, itemStack);
|
||||
final Object mojangComponent = this.cMethod.invoke(cbItemStack);
|
||||
|
||||
return MinecraftComponentSerializer.INSTANCE.deserialize(mojangComponent);
|
||||
} catch (final IllegalAccessException | InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
return TextComponent.empty();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package net.draycia.carbon.util;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
public interface Registry<T> {
|
||||
|
||||
/**
|
||||
* Adds the specified key and value to the registry
|
||||
* @param key The entry's key
|
||||
* @param value The value to insert
|
||||
* @return If successfully registered
|
||||
*/
|
||||
boolean register(@NotNull String key, @NotNull T value);
|
||||
|
||||
/**
|
||||
* Gets the list of registered values
|
||||
* @return The list of registered values
|
||||
*/
|
||||
@NotNull
|
||||
Collection<T> values();
|
||||
|
||||
/**
|
||||
* Obtains an entry associated with the specified key
|
||||
* @param key The key for the entry
|
||||
* @return The entry associated with the key
|
||||
*/
|
||||
@Nullable
|
||||
T get(String key);
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user