mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-01 22:15:08 +00:00
update to built-in modules style
This commit is contained in:
85 files changed
+455
-702
No files matched your search
@@ -0,0 +1,203 @@
|
||||
package net.draycia.carbon;
|
||||
|
||||
import net.draycia.carbon.channels.contexts.impl.EconomyContext;
|
||||
import net.draycia.carbon.channels.contexts.impl.DistanceContext;
|
||||
import net.draycia.carbon.channels.contexts.impl.TownyContext;
|
||||
import net.draycia.carbon.channels.contexts.impl.WorldGuardContext;
|
||||
import net.draycia.carbon.channels.contexts.impl.mcMMOContext;
|
||||
import net.draycia.carbon.listeners.*;
|
||||
import net.draycia.carbon.managers.*;
|
||||
import net.draycia.carbon.messaging.MessageManager;
|
||||
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.CarbonPlaceholders;
|
||||
import net.draycia.carbon.util.ItemStackUtils;
|
||||
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
|
||||
import net.milkbowl.vault.economy.Economy;
|
||||
import net.milkbowl.vault.permission.Permission;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.configuration.InvalidConfigurationException;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.plugin.PluginManager;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
public final class CarbonChat extends JavaPlugin {
|
||||
|
||||
private Permission permission;
|
||||
|
||||
private CommandManager commandManager;
|
||||
private ChannelManager channelManager;
|
||||
private AdventureManager adventureManager;
|
||||
private ContextManager contextManager;
|
||||
|
||||
private UserService userService;
|
||||
private MessageManager messageManager;
|
||||
|
||||
private ItemStackUtils itemStackUtils;
|
||||
|
||||
private YamlConfiguration modConfig;
|
||||
private YamlConfiguration languageConfig;
|
||||
|
||||
public static final LegacyComponentSerializer LEGACY =
|
||||
LegacyComponentSerializer.builder().extractUrls().character('§').build();
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
// Ensure config is present to be modified by the user
|
||||
if (!(new File(getDataFolder(), "config.yml").exists())) {
|
||||
saveDefaultConfig();
|
||||
}
|
||||
|
||||
if (!(new File(getDataFolder(), "moderation.yml").exists())) {
|
||||
saveResource("moderation.yml", false);
|
||||
}
|
||||
|
||||
if (!(new File(getDataFolder(), "language.yml").exists())) {
|
||||
saveResource("language.yml", false);
|
||||
}
|
||||
|
||||
modConfig = new YamlConfiguration();
|
||||
|
||||
try {
|
||||
modConfig.load(new File(getDataFolder(), "moderation.yml"));
|
||||
} catch (IOException | InvalidConfigurationException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
languageConfig = new YamlConfiguration();
|
||||
|
||||
try {
|
||||
languageConfig.load(new File(getDataFolder(), "language.yml"));
|
||||
} catch (IOException | InvalidConfigurationException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
// 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();
|
||||
|
||||
// Initialize managers
|
||||
commandManager = new CommandManager(this);
|
||||
channelManager = new ChannelManager(this);
|
||||
contextManager = new ContextManager();
|
||||
messageManager = new MessageManager(this);
|
||||
|
||||
String storageType = getConfig().getString("storage.type");
|
||||
|
||||
if (storageType.equalsIgnoreCase("mysql")) {
|
||||
getLogger().info("Enabling MySQL storage!");
|
||||
userService = new MySQLUserService(this);
|
||||
} else if (storageType.equalsIgnoreCase("json")) {
|
||||
getLogger().info("Enabling JSON storage!");
|
||||
userService = new JSONUserService(this);
|
||||
} else {
|
||||
getLogger().warning("Invalid storage type selected! Falling back to JSON.");
|
||||
userService = new JSONUserService(this);
|
||||
}
|
||||
|
||||
// Setup listeners
|
||||
setupListeners();
|
||||
registerContexts();
|
||||
|
||||
new CarbonPlaceholders(this).register();
|
||||
|
||||
if (Bukkit.getPluginManager().isPluginEnabled("Towny")) {
|
||||
getServer().getPluginManager().registerEvents(new TownyContext(this), this);
|
||||
}
|
||||
|
||||
if (Bukkit.getPluginManager().isPluginEnabled("mcMMO")) {
|
||||
getServer().getPluginManager().registerEvents(new mcMMOContext(this), this);
|
||||
}
|
||||
|
||||
if (Bukkit.getPluginManager().isPluginEnabled("WorldGuard")) {
|
||||
getServer().getPluginManager().registerEvents(new WorldGuardContext(this), this);
|
||||
}
|
||||
|
||||
if (Bukkit.getServicesManager().isProvidedFor(Economy.class)) {
|
||||
Bukkit.getPluginManager().registerEvents(new EconomyContext(this), this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
userService.onDisable();
|
||||
}
|
||||
|
||||
private void setupListeners() {
|
||||
PluginManager pluginManager = getServer().getPluginManager();
|
||||
|
||||
// Register chat listeners
|
||||
pluginManager.registerEvents(new BukkitChatListener(this), this);
|
||||
pluginManager.registerEvents(new IgnoredPlayerHandler(), 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);
|
||||
pluginManager.registerEvents(new PlayerJoinListener(this), this);
|
||||
pluginManager.registerEvents(new RelationalPlaceholderHandler(), this);
|
||||
pluginManager.registerEvents(new UserFormattingListener(), this);
|
||||
pluginManager.registerEvents(new WhisperPingHandler(this), this);
|
||||
|
||||
pluginManager.registerEvents(new CapsHandler(this), this);
|
||||
pluginManager.registerEvents(new FilterHandler(this), this);
|
||||
pluginManager.registerEvents(new MuteHandler(), this);
|
||||
pluginManager.registerEvents(new ShadowMuteHandler(this), this);
|
||||
}
|
||||
|
||||
private void registerContexts() {
|
||||
getContextManager().register("distance", new DistanceContext());
|
||||
}
|
||||
|
||||
public YamlConfiguration getModConfig() {
|
||||
return modConfig;
|
||||
}
|
||||
|
||||
public YamlConfiguration getLanguage() {
|
||||
return languageConfig;
|
||||
}
|
||||
|
||||
public Permission getPermission() {
|
||||
return permission;
|
||||
}
|
||||
|
||||
public CommandManager getCommandManager() {
|
||||
return commandManager;
|
||||
}
|
||||
|
||||
public ChannelManager getChannelManager() {
|
||||
return channelManager;
|
||||
}
|
||||
|
||||
public MessageManager getMessageManager() {
|
||||
return messageManager;
|
||||
}
|
||||
|
||||
public UserService getUserService() {
|
||||
return userService;
|
||||
}
|
||||
|
||||
public ItemStackUtils getItemStackUtils() {
|
||||
return itemStackUtils;
|
||||
}
|
||||
|
||||
public AdventureManager getAdventureManager() {
|
||||
return adventureManager;
|
||||
}
|
||||
|
||||
public ContextManager getContextManager() {
|
||||
return contextManager;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package net.draycia.carbon.channels;
|
||||
|
||||
import co.aikar.commands.BukkitCommandManager;
|
||||
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.*;
|
||||
|
||||
public class ChannelRegistry implements Registry<ChatChannel> {
|
||||
|
||||
private final Map<String, ChatChannel> registry = new HashMap<>();
|
||||
private final List<AliasedChannelCommand> channelCommands = new ArrayList<>();
|
||||
|
||||
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) {
|
||||
BukkitCommandManager commandManager = carbonChat.getCommandManager().getCommandManager();
|
||||
commandManager.getCommandReplacements().addReplacement("channelName", value.getAliases());
|
||||
|
||||
AliasedChannelCommand command = new AliasedChannelCommand(value);
|
||||
|
||||
commandManager.registerCommand(command);
|
||||
channelCommands.add(command);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearAll() {
|
||||
registry.clear();
|
||||
|
||||
for (AliasedChannelCommand command : channelCommands) {
|
||||
carbonChat.getLogger().info("Unregistering command for channel: " + command.getChatChannel().getName());
|
||||
carbonChat.getCommandManager().getCommandManager().unregisterCommand(command);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
package net.draycia.carbon.channels;
|
||||
|
||||
import net.draycia.carbon.events.PreChatFormatEvent;
|
||||
import net.draycia.carbon.storage.ChatUser;
|
||||
import net.kyori.adventure.audience.ForwardingAudience;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.format.TextColor;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public abstract class ChatChannel implements ForwardingAudience {
|
||||
|
||||
/**
|
||||
* @return The color that represents this channel. Optionally used in formatting.
|
||||
*/
|
||||
public abstract TextColor getChannelColor(ChatUser user);
|
||||
|
||||
/**
|
||||
* @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();
|
||||
|
||||
@Nullable
|
||||
public abstract String getMessagePrefix();
|
||||
|
||||
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();
|
||||
|
||||
public abstract String getSwitchFailureMessage();
|
||||
|
||||
/**
|
||||
* @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 primaryGroupOnly();
|
||||
|
||||
public abstract Boolean honorsRecipientList();
|
||||
|
||||
public abstract Boolean permissionGroupMatching();
|
||||
|
||||
public abstract boolean testContext(ChatUser sender, ChatUser target);
|
||||
|
||||
public abstract Object getContext(String key);
|
||||
|
||||
public abstract List<String> getGroupOverrides();
|
||||
|
||||
/**
|
||||
* @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 Boolean canPlayerSee(ChatUser target, boolean checkSpying);
|
||||
|
||||
/**
|
||||
* @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 PreChatFormatEvent}, 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 sendMessage(ChatUser user, Collection<ChatUser> recipients, String message, boolean fromBungee);
|
||||
|
||||
public abstract void sendComponent(ChatUser user, Component component);
|
||||
|
||||
public String processPlaceholders(ChatUser user, String input) { return input; }
|
||||
|
||||
public abstract boolean shouldCancelChatEvent();
|
||||
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
package net.draycia.carbon.channels.contexts.impl;
|
||||
|
||||
import net.draycia.carbon.CarbonChat;
|
||||
import net.draycia.carbon.events.PreChatFormatEvent;
|
||||
import net.milkbowl.vault.economy.Economy;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.plugin.RegisteredServiceProvider;
|
||||
|
||||
public class EconomyContext implements Listener {
|
||||
private final CarbonChat carbonChat;
|
||||
private final Economy economy;
|
||||
|
||||
public EconomyContext(CarbonChat carbonChat) {
|
||||
this.carbonChat = carbonChat;
|
||||
this.economy = carbonChat.getServer().getServicesManager().getRegistration(Economy.class).getProvider();
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onChatReqBal(PreChatFormatEvent event) {
|
||||
Object requiredBalObject = event.getChannel().getContext("vault-balance");
|
||||
|
||||
Double requiredBal;
|
||||
|
||||
if (requiredBalObject instanceof Double) {
|
||||
requiredBal = (Double) requiredBalObject;
|
||||
} else if (requiredBalObject instanceof Integer) {
|
||||
requiredBal = Double.valueOf((Integer) requiredBalObject);
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
|
||||
if (requiredBal.equals(0.0)) {
|
||||
return;
|
||||
}
|
||||
|
||||
Player player = event.getUser().asPlayer();
|
||||
|
||||
if (!economy.has(player, requiredBal)) {
|
||||
event.setCancelled(true);
|
||||
|
||||
event.getUser().sendMessage(carbonChat.getAdventureManager()
|
||||
.processMessageWithPapi(player, event.getChannel().getCannotUseMessage()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onChatCost(PreChatFormatEvent event) {
|
||||
Object costObject = event.getChannel().getContext("vault-cost");
|
||||
|
||||
Double cost;
|
||||
|
||||
if (costObject instanceof Double) {
|
||||
cost = (Double) costObject;
|
||||
} else if (costObject instanceof Integer) {
|
||||
cost = Double.valueOf((Integer) costObject);
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
|
||||
if (cost.equals(0.0)) {
|
||||
return;
|
||||
}
|
||||
|
||||
Player player = event.getUser().asPlayer();
|
||||
|
||||
if (!economy.has(player, cost)) {
|
||||
event.setCancelled(true);
|
||||
|
||||
event.getUser().sendMessage(carbonChat.getAdventureManager()
|
||||
.processMessageWithPapi(player, event.getChannel().getCannotUseMessage()));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
economy.withdrawPlayer(player, cost);
|
||||
}
|
||||
}
|
||||
@@ -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,105 @@
|
||||
package net.draycia.carbon.channels.contexts.impl;
|
||||
|
||||
import com.palmergames.bukkit.towny.TownyAPI;
|
||||
import com.palmergames.bukkit.towny.event.TownRemoveResidentEvent;
|
||||
import com.palmergames.bukkit.towny.exceptions.NotRegisteredException;
|
||||
import com.palmergames.bukkit.towny.object.Resident;
|
||||
import net.draycia.carbon.CarbonChat;
|
||||
import net.draycia.carbon.events.ChannelSwitchEvent;
|
||||
import net.draycia.carbon.events.PreChatFormatEvent;
|
||||
import net.draycia.carbon.storage.ChatUser;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
|
||||
public final class TownyContext implements Listener {
|
||||
|
||||
private final CarbonChat carbonChat;
|
||||
private static final String KEY = "towny-town";
|
||||
|
||||
public TownyContext(CarbonChat carbonChat) {
|
||||
this.carbonChat = carbonChat;
|
||||
|
||||
this.carbonChat.getContextManager().register("towny-town", (context) -> {
|
||||
if ((context.getValue() instanceof Boolean) && ((Boolean) context.getValue())) {
|
||||
return isInSameTown(context.getSender(), context.getTarget());
|
||||
}
|
||||
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
@EventHandler(ignoreCancelled = true)
|
||||
public void onChannelSwitch(ChannelSwitchEvent event) {
|
||||
Object town = event.getChannel().getContext(KEY);
|
||||
|
||||
if ((town instanceof Boolean) && ((Boolean) town)) {
|
||||
if (!isInTown(event.getUser())) {
|
||||
event.setCancelled(true);
|
||||
event.setFailureMessage(carbonChat.getConfig().getString("contexts.Towny.cancellation-message"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler(ignoreCancelled = true)
|
||||
public void onChannelMessage(PreChatFormatEvent event) {
|
||||
// TODO: event.setFailureMessage
|
||||
Object town = event.getChannel().getContext(KEY);
|
||||
|
||||
if ((town instanceof Boolean) && ((Boolean) town)) {
|
||||
if (!isInTown(event.getUser())) {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onResidentRemove(TownRemoveResidentEvent event) {
|
||||
String name = event.getResident().getName();
|
||||
ChatUser user = carbonChat.getUserService().wrap(name);
|
||||
Object town = user.getSelectedChannel().getContext(KEY);
|
||||
|
||||
if ((town instanceof Boolean) && ((Boolean) town)) {
|
||||
user.clearSelectedChannel();
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerJoin(PlayerJoinEvent event) {
|
||||
ChatUser user = carbonChat.getUserService().wrap(event.getPlayer());
|
||||
Object town = user.getSelectedChannel().getContext(KEY);
|
||||
|
||||
if ((town instanceof Boolean) && ((Boolean) town) && !isInTown(user)) {
|
||||
user.clearSelectedChannel();
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isInTown(ChatUser user) {
|
||||
try {
|
||||
return TownyAPI.getInstance().getDataSource().getResident(user.asPlayer().getName()).hasTown();
|
||||
} catch (NotRegisteredException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isInSameTown(ChatUser user1, ChatUser user2) {
|
||||
if (!user1.isOnline() || !user2.isOnline()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
Resident resident = TownyAPI.getInstance().getDataSource().getResident(user1.asPlayer().getName());
|
||||
|
||||
if (resident.hasTown()) {
|
||||
return resident.getTown().hasResident(user2.asPlayer().getName());
|
||||
}
|
||||
} catch (NotRegisteredException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
package net.draycia.carbon.channels.contexts.impl;
|
||||
|
||||
import com.sk89q.worldedit.bukkit.BukkitAdapter;
|
||||
import com.sk89q.worldedit.util.Location;
|
||||
import com.sk89q.worldedit.world.World;
|
||||
import com.sk89q.worldguard.WorldGuard;
|
||||
import com.sk89q.worldguard.protection.ApplicableRegionSet;
|
||||
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
|
||||
import com.sk89q.worldguard.protection.regions.RegionContainer;
|
||||
import com.sk89q.worldguard.protection.regions.RegionQuery;
|
||||
import net.draycia.carbon.CarbonChat;
|
||||
import net.draycia.carbon.events.ChannelSwitchEvent;
|
||||
import net.draycia.carbon.events.PreChatFormatEvent;
|
||||
import net.draycia.carbon.storage.ChatUser;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public final class WorldGuardContext implements Listener {
|
||||
|
||||
private static final String KEY = "worldguard-region";
|
||||
|
||||
public WorldGuardContext(CarbonChat carbonChat) {
|
||||
carbonChat.getContextManager().register(KEY, (context) -> {
|
||||
return this.testContext(context.getSender(), context.getTarget(), context.getValue());
|
||||
});
|
||||
}
|
||||
|
||||
@EventHandler(ignoreCancelled = true)
|
||||
public void onChannelSwitch(ChannelSwitchEvent event) {
|
||||
// TODO: cancellation message
|
||||
Object value = event.getChannel().getContext(KEY);
|
||||
|
||||
if ((value instanceof String || value instanceof List) && !isInRegionOrRegions(value, event.getUser())) {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler(ignoreCancelled = true)
|
||||
public void onChannelMessage(PreChatFormatEvent event) {
|
||||
// TODO: cancellation message
|
||||
Object value = event.getChannel().getContext(KEY);
|
||||
|
||||
if ((value instanceof String || value instanceof List) && !isInRegionOrRegions(value, event.getUser())) {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isInRegionOrRegions(Object value, ChatUser user) {
|
||||
if ((value instanceof String)) {
|
||||
return isInRegion((String) value, user);
|
||||
}
|
||||
|
||||
if (value instanceof List) {
|
||||
for (String region : ((List<String>) value)) {
|
||||
if (isInRegion(region, user)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean testContext(ChatUser sender, ChatUser target, Object value) {
|
||||
boolean user1InRegion = false;
|
||||
boolean user2InRegion = false;
|
||||
|
||||
if ((value instanceof Boolean) && ((Boolean) value)) {
|
||||
return isInSameRegion(sender, target);
|
||||
} else if (value instanceof String) {
|
||||
user1InRegion = isInRegion((String)value, sender);
|
||||
user2InRegion = isInRegion((String)value, target);
|
||||
} else if (value instanceof List) {
|
||||
for (String item : (List<String>)value) {
|
||||
if (!user1InRegion) {
|
||||
user1InRegion = isInRegion(item, sender);
|
||||
}
|
||||
|
||||
if (!user2InRegion) {
|
||||
user2InRegion = isInRegion(item, target);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
|
||||
return user1InRegion && user2InRegion;
|
||||
}
|
||||
|
||||
public boolean isInSameRegion(ChatUser user1, ChatUser user2) {
|
||||
if (!user1.isOnline() || !user2.isOnline()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Location user1Location = BukkitAdapter.adapt(user1.asPlayer().getLocation());
|
||||
Location user2Location = BukkitAdapter.adapt(user2.asPlayer().getLocation());
|
||||
|
||||
RegionContainer container = WorldGuard.getInstance().getPlatform().getRegionContainer();
|
||||
RegionQuery regionQuery = container.createQuery();
|
||||
|
||||
ApplicableRegionSet user1Regions = regionQuery.getApplicableRegions(user1Location);
|
||||
ApplicableRegionSet user2Regions = regionQuery.getApplicableRegions(user2Location);
|
||||
|
||||
for (ProtectedRegion region : user1Regions) {
|
||||
if (user2Regions.getRegions().contains(region)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isInRegion(String region, ChatUser user) {
|
||||
RegionContainer container = WorldGuard.getInstance().getPlatform().getRegionContainer();
|
||||
World world = BukkitAdapter.adapt(user.asPlayer().getWorld());
|
||||
ProtectedRegion protection = container.get(world).getRegion(region);
|
||||
|
||||
return isInRegion(protection, user);
|
||||
}
|
||||
|
||||
public boolean isInRegion(ProtectedRegion region, ChatUser user) {
|
||||
Location location = BukkitAdapter.adapt(user.asPlayer().getLocation());
|
||||
|
||||
return region.contains(location.getBlockX(), location.getBlockY(), location.getBlockZ());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
package net.draycia.carbon.channels.contexts.impl;
|
||||
|
||||
import com.gmail.nossr50.api.PartyAPI;
|
||||
import com.gmail.nossr50.events.party.McMMOPartyChangeEvent;
|
||||
import net.draycia.carbon.CarbonChat;
|
||||
import net.draycia.carbon.events.ChannelSwitchEvent;
|
||||
import net.draycia.carbon.events.PreChatFormatEvent;
|
||||
import net.draycia.carbon.storage.ChatUser;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
|
||||
public final class mcMMOContext implements Listener {
|
||||
|
||||
private CarbonChat carbonChat;
|
||||
|
||||
public mcMMOContext(CarbonChat carbonChat) {
|
||||
this.carbonChat = carbonChat;
|
||||
|
||||
this.carbonChat.getContextManager().register("mcmmo-party", (context) -> {
|
||||
if ((context.getValue() instanceof Boolean) && ((Boolean) context.getValue())) {
|
||||
return isInSameParty(context.getSender(), context.getTarget());
|
||||
}
|
||||
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
@EventHandler(ignoreCancelled = true)
|
||||
public void onChannelSwitch(ChannelSwitchEvent event) {
|
||||
Object party = event.getChannel().getContext("mcmmo-party");
|
||||
|
||||
if ((party instanceof Boolean) && ((Boolean) party)) {
|
||||
if (!isInParty(event.getUser())) {
|
||||
event.setCancelled(true);
|
||||
event.setFailureMessage(carbonChat.getConfig().getString("contexts.mcMMO.cancellation-message"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler(ignoreCancelled = true)
|
||||
public void onChannelMessage(PreChatFormatEvent event) {
|
||||
Object party = event.getChannel().getContext("mcmmo-party");
|
||||
|
||||
if ((party instanceof Boolean) && ((Boolean) party)) {
|
||||
if (!isInParty(event.getUser())) {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private final McMMOPartyChangeEvent.EventReason LEFT = McMMOPartyChangeEvent.EventReason.LEFT_PARTY;
|
||||
private final McMMOPartyChangeEvent.EventReason KICKED = McMMOPartyChangeEvent.EventReason.KICKED_FROM_PARTY;
|
||||
|
||||
@EventHandler(ignoreCancelled = true)
|
||||
public void onPartyLeave(McMMOPartyChangeEvent event) {
|
||||
if (event.getReason() != LEFT && event.getReason() != KICKED) {
|
||||
return;
|
||||
}
|
||||
|
||||
ChatUser user = carbonChat.getUserService().wrap(event.getPlayer());
|
||||
Object party = user.getSelectedChannel().getContext("mcmmo-party");
|
||||
|
||||
if ((party instanceof Boolean) && ((Boolean) party)) {
|
||||
user.clearSelectedChannel();
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerJoin(PlayerJoinEvent event) {
|
||||
ChatUser user = carbonChat.getUserService().wrap(event.getPlayer());
|
||||
Object party = user.getSelectedChannel().getContext("mcmmo-party");
|
||||
|
||||
if ((party instanceof Boolean) && ((Boolean) party) && !isInParty(user)) {
|
||||
user.clearSelectedChannel();
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isInParty(ChatUser user) {
|
||||
return PartyAPI.inParty(user.asPlayer());
|
||||
}
|
||||
|
||||
public boolean isInSameParty(ChatUser user1, ChatUser user2) {
|
||||
if (!user1.isOnline() || !user2.isOnline()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return PartyAPI.inSameParty(user1.asPlayer(), user2.asPlayer());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,592 @@
|
||||
package net.draycia.carbon.channels.impls;
|
||||
|
||||
import me.clip.placeholderapi.PlaceholderAPI;
|
||||
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.events.PreChatFormatEvent;
|
||||
import net.draycia.carbon.storage.ChatUser;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import net.kyori.adventure.text.format.TextColor;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
|
||||
import net.kyori.adventure.text.serializer.legacy.LegacyFormat;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
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 @NotNull List<ChatUser> audiences() {
|
||||
List<ChatUser> audience = new ArrayList<>();
|
||||
|
||||
for (Player player : Bukkit.getOnlinePlayers()) {
|
||||
ChatUser playerUser = carbonChat.getUserService().wrap(player);
|
||||
|
||||
if (canPlayerSee(playerUser, true)) {
|
||||
audience.add(playerUser);
|
||||
}
|
||||
}
|
||||
|
||||
return audience;
|
||||
}
|
||||
|
||||
private void updateUserNickname(ChatUser user) {
|
||||
if (user.isOnline()) {
|
||||
if (user.getNickname() != null) {
|
||||
user.asPlayer().setDisplayName(user.getNickname());
|
||||
|
||||
if (carbonChat.getConfig().getBoolean("nicknames-set-tab-name")) {
|
||||
user.asPlayer().setPlayerListName(user.getNickname());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private TextColor getColor(ChatUser user) {
|
||||
TextColor userColor = user.getChannelSettings(this).getColor();
|
||||
|
||||
if (userColor != null) {
|
||||
return userColor;
|
||||
} else {
|
||||
return getChannelColor(user);
|
||||
}
|
||||
}
|
||||
|
||||
public void sendMessage(ChatUser user, Collection<ChatUser> recipients, String message, boolean fromRemote) {
|
||||
updateUserNickname(user);
|
||||
|
||||
// Get player's formatting
|
||||
String messageFormat = getFormat(user);
|
||||
|
||||
// Call custom chat event
|
||||
PreChatFormatEvent preFormatEvent = new PreChatFormatEvent(user, this, messageFormat, message);
|
||||
Bukkit.getPluginManager().callEvent(preFormatEvent);
|
||||
|
||||
// Return if cancelled or message is emptied
|
||||
|
||||
if (preFormatEvent.isCancelled()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (preFormatEvent.getMessage().trim().isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
String displayName;
|
||||
|
||||
if (user.isOnline()) {
|
||||
displayName = user.asPlayer().getDisplayName();
|
||||
} else {
|
||||
displayName = user.asOfflinePlayer().getName();
|
||||
}
|
||||
|
||||
// Iterate through players who should receive messages in this channel
|
||||
for (ChatUser target : recipients) {
|
||||
// Call second format event. Used for relational stuff (placeholders etc)
|
||||
ChatFormatEvent formatEvent = new ChatFormatEvent(user, target, this, preFormatEvent.getFormat(), preFormatEvent.getMessage());
|
||||
Bukkit.getPluginManager().callEvent(formatEvent);
|
||||
|
||||
// Again, return if cancelled or message is emptied
|
||||
if (formatEvent.isCancelled()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (formatEvent.getMessage().trim().isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
TextComponent formattedMessage = (TextComponent) carbonChat.getAdventureManager().processMessage(formatEvent.getFormat(),
|
||||
"br", "\n",
|
||||
"displayname", displayName,
|
||||
"color", "<" + getColor(target).asHexString() + ">",
|
||||
"phase", Long.toString(System.currentTimeMillis() % 25),
|
||||
"server", carbonChat.getConfig().getString("server-name", "Server"),
|
||||
"message", formatEvent.getMessage());
|
||||
|
||||
if (isUserSpying(user, target)) {
|
||||
String prefix = processPlaceholders(user, carbonChat.getConfig().getString("spy-prefix"));
|
||||
|
||||
formattedMessage = (TextComponent) MiniMessage.get().parse(prefix, "color",
|
||||
getColor(target).asHexString()).append(formattedMessage);
|
||||
}
|
||||
|
||||
ChatComponentEvent newEvent = new ChatComponentEvent(user, target, this, formattedMessage,
|
||||
formatEvent.getMessage());
|
||||
|
||||
Bukkit.getPluginManager().callEvent(newEvent);
|
||||
|
||||
target.sendMessage(newEvent.getComponent());
|
||||
}
|
||||
|
||||
ChatFormatEvent consoleFormatEvent = new ChatFormatEvent(user, null, this, preFormatEvent.getFormat(),
|
||||
preFormatEvent.getMessage());
|
||||
|
||||
Bukkit.getPluginManager().callEvent(consoleFormatEvent);
|
||||
|
||||
TextComponent consoleMessage = (TextComponent) carbonChat.getAdventureManager().processMessage(consoleFormatEvent.getFormat(),
|
||||
"br", "\n",
|
||||
"displayname", displayName,
|
||||
"color", "<" + getColor(user).asHexString() + ">",
|
||||
"phase", Long.toString(System.currentTimeMillis() % 25),
|
||||
"server", carbonChat.getConfig().getString("server-name", "Server"),
|
||||
"message", consoleFormatEvent.getMessage());
|
||||
|
||||
ChatComponentEvent consoleEvent = new ChatComponentEvent(user, null, this, consoleMessage,
|
||||
consoleFormatEvent.getMessage());
|
||||
|
||||
Bukkit.getPluginManager().callEvent(consoleEvent);
|
||||
|
||||
// Log message to console
|
||||
System.out.println(CarbonChat.LEGACY.serialize(consoleEvent.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() && !fromRemote && shouldBungee()) {
|
||||
sendMessageToBungee(user.asPlayer(), consoleEvent.getComponent());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendMessage(ChatUser user, String message, boolean fromRemote) {
|
||||
this.sendMessage(user, this.audiences(), message, fromRemote);
|
||||
}
|
||||
|
||||
public String getFormat(ChatUser user) {
|
||||
for (String group : this.getGroupOverrides()) {
|
||||
if (userHasGroup(user, group)) {
|
||||
String format = getFormat(group);
|
||||
|
||||
if (format != null) {
|
||||
return format;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (primaryGroupOnly()) {
|
||||
return getPrimaryGroupFormat(user);
|
||||
} else {
|
||||
return getFirstFoundUserFormat(user);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean userHasGroup(ChatUser user, String group) {
|
||||
if (user.isOnline()) {
|
||||
if (carbonChat.getPermission().playerInGroup(user.asPlayer(), group)) {
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
if (carbonChat.getPermission().playerInGroup(null, user.asOfflinePlayer(), group)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if (user.isOnline() && this.permissionGroupMatching()) {
|
||||
return user.asPlayer().hasPermission("carbonchat.group." + group);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private String getFirstFoundUserFormat(ChatUser user) {
|
||||
String[] playerGroups;
|
||||
|
||||
if (user.isOnline()) {
|
||||
playerGroups = carbonChat.getPermission().getPlayerGroups(user.asPlayer());
|
||||
} else {
|
||||
playerGroups = carbonChat.getPermission().getPlayerGroups(null, user.asOfflinePlayer());
|
||||
}
|
||||
|
||||
for (String group : playerGroups) {
|
||||
String groupFormat = getFormat(group);
|
||||
|
||||
if (groupFormat != null) {
|
||||
return groupFormat;
|
||||
}
|
||||
}
|
||||
|
||||
return getDefaultFormat();
|
||||
}
|
||||
|
||||
private String getPrimaryGroupFormat(ChatUser user) {
|
||||
String primaryGroup;
|
||||
|
||||
if (user.isOnline()) {
|
||||
primaryGroup = carbonChat.getPermission().getPrimaryGroup(user.asPlayer());
|
||||
} else {
|
||||
primaryGroup = carbonChat.getPermission().getPrimaryGroup(null, user.asOfflinePlayer());
|
||||
}
|
||||
|
||||
String primaryGroupFormat = getFormat(primaryGroup);
|
||||
|
||||
if (primaryGroupFormat != null) {
|
||||
return primaryGroupFormat;
|
||||
}
|
||||
|
||||
return getDefaultFormat();
|
||||
}
|
||||
|
||||
private String getDefaultFormat() {
|
||||
return getFormat(getDefaultFormatName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFormat(String group) {
|
||||
return getString("formats." + group);
|
||||
}
|
||||
|
||||
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 : audiences()) {
|
||||
if (!user.isIgnoringUser(player)) {
|
||||
user.sendMessage(component);
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println(CarbonChat.LEGACY.serialize(component));
|
||||
}
|
||||
|
||||
public void sendMessageToBungee(Player player, Component component) {
|
||||
carbonChat.getMessageManager().sendMessage("channel-component", player.getUniqueId(), (byteArray) -> {
|
||||
byteArray.writeUTF(this.getKey());
|
||||
byteArray.writeUTF(carbonChat.getAdventureManager().getAudiences().gsonSerializer().serialize(component));
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean canPlayerSee(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()) {
|
||||
return !target.getChannelSettings(this).isIgnored();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public Boolean canPlayerSee(ChatUser sender, ChatUser target, boolean checkSpying) {
|
||||
Player targetPlayer = target.asPlayer();
|
||||
|
||||
if (canPlayerSee(target, checkSpying)){
|
||||
return true;
|
||||
}
|
||||
|
||||
if (isIgnorable()) {
|
||||
if (target.isIgnoringUser(sender) && !targetPlayer.hasPermission("carbonchat.ignore.exempt")) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return !target.getChannelSettings(this).isIgnored();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TextColor getChannelColor(ChatUser user) {
|
||||
String color = getString("color");
|
||||
|
||||
if (color == null) {
|
||||
color = "white";
|
||||
}
|
||||
|
||||
if (user.isOnline()) {
|
||||
color = PlaceholderAPI.setPlaceholders(user.asPlayer(), color);
|
||||
}
|
||||
|
||||
for (NamedTextColor namedColor : NamedTextColor.values()) {
|
||||
if (namedColor.toString().equalsIgnoreCase(color)) {
|
||||
return namedColor;
|
||||
}
|
||||
}
|
||||
|
||||
if (color.startsWith("&")) {
|
||||
LegacyFormat legacyFormat = LegacyComponentSerializer.parseChar(color.charAt(1));
|
||||
|
||||
if (legacyFormat != null) {
|
||||
TextColor legacyColor = legacyFormat.color();
|
||||
|
||||
if (legacyColor != null) {
|
||||
return legacyColor;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TextColor textColor = TextColor.fromHexString(color);
|
||||
|
||||
if (textColor != null) {
|
||||
return textColor;
|
||||
}
|
||||
|
||||
if (carbonChat.getConfig().getBoolean("show-tips")) {
|
||||
carbonChat.getLogger().warning("Tip: Channel color found (" + color + ") is invalid!");
|
||||
carbonChat.getLogger().warning("Falling back to #FFFFFF");
|
||||
}
|
||||
|
||||
return NamedTextColor.WHITE;
|
||||
}
|
||||
|
||||
private String getDefaultFormatName() {
|
||||
return getString("default-group");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean isDefault() {
|
||||
return getBoolean("default");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean isIgnorable() {
|
||||
return getBoolean("ignorable");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean shouldBungee() {
|
||||
return getBoolean("should-bungee");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean honorsRecipientList() {
|
||||
return getBoolean("honors-recipient-list");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean permissionGroupMatching() {
|
||||
return getBoolean("permission-group-matching");
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getGroupOverrides() {
|
||||
return getStringList("group-overrides");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
String name = getString("name");
|
||||
return name == null ? key : name;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public String getMessagePrefix() {
|
||||
if (config != null && config.contains("message-prefix")) {
|
||||
return config.getString("message-prefix");
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSwitchMessage() {
|
||||
return getString("switch-message");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSwitchOtherMessage() {
|
||||
return getString("switch-other-message");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSwitchFailureMessage() {
|
||||
return getString("switch-failure-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 primaryGroupOnly() {
|
||||
return getBoolean("primary-group-only");
|
||||
}
|
||||
|
||||
@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 List<String> getStringList(String key) {
|
||||
if (config != null && config.contains(key)) {
|
||||
return config.getStringList(key);
|
||||
}
|
||||
|
||||
ConfigurationSection defaultSection = carbonChat.getConfig().getConfigurationSection("default");
|
||||
|
||||
if (defaultSection != null && defaultSection.contains(key)) {
|
||||
return defaultSection.getStringList(key);
|
||||
}
|
||||
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean shouldCancelChatEvent() {
|
||||
return getBoolean("cancel-message-event");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
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
|
||||
@Syntax("[message]")
|
||||
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().getChannelColor(user).toString() + ">",
|
||||
"channel", getChatChannel().getName()));
|
||||
} else {
|
||||
Bukkit.getScheduler().runTaskAsynchronously(carbonChat, () -> {
|
||||
getChatChannel().sendMessage(user, String.join(" ", args), false);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public ChatChannel getChatChannel() {
|
||||
return chatChannel;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
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")
|
||||
@Syntax("<channel> [message]")
|
||||
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.getChannelColor(user).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")
|
||||
@Syntax("<channel> <user>")
|
||||
public void baseCommand(CommandSender sender, @Conditions("exists:true") 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.getChannelColor(user).toString() + ">", "channel", channel.getName()));
|
||||
|
||||
carbonChat.getAdventureManager().getAudiences().audience(sender).sendMessage(carbonChat.getAdventureManager().processMessage(otherMessage, "br", "\n",
|
||||
"color", "<color:" + channel.getChannelColor(user).toString() + ">", "channel", channel.getName(),
|
||||
"player", user.asOfflinePlayer().getName()));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
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.kyori.adventure.text.TextComponent;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.List;
|
||||
|
||||
@CommandAlias("chlist|channellist")
|
||||
@CommandPermission("carbonchat.channellist")
|
||||
public class ChannelListCommand extends BaseCommand {
|
||||
|
||||
@Dependency
|
||||
private CarbonChat carbonChat;
|
||||
|
||||
@Default
|
||||
@Syntax("[player]")
|
||||
public void baseCommand(Player player) {
|
||||
TextComponent.Builder component = TextComponent.builder("");
|
||||
Iterator<ChatChannel> allChannels = carbonChat.getChannelManager().getRegistry().values().iterator();
|
||||
ChatUser user = carbonChat.getUserService().wrap(player);
|
||||
|
||||
getChannelLists(player, user, allChannels, component);
|
||||
user.sendMessage(component.build());
|
||||
}
|
||||
|
||||
@CommandPermission("carbonchat.channellist.others")
|
||||
@Subcommand("user")
|
||||
@CommandCompletion("@players")
|
||||
@Syntax("<player>")
|
||||
public void baseCommand(Player player, ChatUser user) {
|
||||
ChatUser cmdSender = carbonChat.getUserService().wrap(player);
|
||||
TextComponent.Builder component = TextComponent.builder("");
|
||||
Iterator<ChatChannel> allChannels = carbonChat.getChannelManager().getRegistry().values().iterator();
|
||||
|
||||
getChannelLists(player, user, allChannels, component);
|
||||
|
||||
cmdSender.sendMessage(component.build());
|
||||
}
|
||||
|
||||
private void getChannelLists(CommandSender player, ChatUser user, Iterator<ChatChannel> allChannels, TextComponent.Builder component) {
|
||||
ChatChannel channel;
|
||||
List<ChatChannel> canSee = new ArrayList<>();
|
||||
List<ChatChannel> cannotSee = new ArrayList<>();
|
||||
|
||||
while (allChannels.hasNext()) {
|
||||
channel = allChannels.next();
|
||||
|
||||
if (channel.canPlayerSee(user, true)) {
|
||||
canSee.add(channel);
|
||||
} else {
|
||||
cannotSee.add(channel);
|
||||
}
|
||||
}
|
||||
|
||||
Iterator<ChatChannel> visibleChannels = canSee.iterator();
|
||||
|
||||
component.append(TextComponent.of("Available Channels: ").color(NamedTextColor.GREEN));
|
||||
makeList(visibleChannels, component);
|
||||
component.append("\n").build();
|
||||
|
||||
if (player.hasPermission("carbonchat.channellist.bypass")) {
|
||||
Iterator<ChatChannel> invisibleChannels = cannotSee.iterator();
|
||||
|
||||
component.append(TextComponent.of("Unavailable Channels: ").color(NamedTextColor.RED));
|
||||
makeList(invisibleChannels, component);
|
||||
}
|
||||
}
|
||||
|
||||
private void makeList(Iterator<ChatChannel> iterator, TextComponent.Builder component) {
|
||||
|
||||
while (iterator.hasNext()) {
|
||||
ChatChannel chatChannel = iterator.next();
|
||||
|
||||
component.append(TextComponent.of(chatChannel.getName()));
|
||||
|
||||
if (iterator.hasNext()) {
|
||||
component.append(TextComponent.of(", "));
|
||||
} else {
|
||||
component.append(TextComponent.of(". "));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
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();
|
||||
|
||||
carbonChat.getChannelManager().reload();
|
||||
|
||||
Component message = carbonChat.getAdventureManager().processMessage(carbonChat.getLanguage().getString("reloaded"),
|
||||
"br", "\n");
|
||||
|
||||
carbonChat.getAdventureManager().getAudiences().audience(sender).sendMessage(message);
|
||||
|
||||
// TODO: Ensure the command list is updated when reloading.
|
||||
// If new channels are added, their commands should appear to players.
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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.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.Bukkit;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
@CommandAlias("clearchat|cc")
|
||||
@CommandPermission("carbonchat.clearchat.clear")
|
||||
public class ClearChatCommand extends BaseCommand {
|
||||
|
||||
@Dependency
|
||||
private CarbonChat carbonChat;
|
||||
|
||||
@Default
|
||||
public void baseCommand(CommandSender issuer) {
|
||||
String format = carbonChat.getModConfig().getString("clear-chat.message", "");
|
||||
Component component = carbonChat.getAdventureManager().processMessage(format, "br", "\n");
|
||||
|
||||
for (Player player : Bukkit.getOnlinePlayers()) {
|
||||
if (player.hasPermission("carbonchat.clearchat.exempt")) {
|
||||
continue;
|
||||
}
|
||||
|
||||
ChatUser audience = carbonChat.getUserService().wrap(player);
|
||||
|
||||
for (int i = 0; i < carbonChat.getModConfig().getInt("clear-chat.message-count", 100); i++) {
|
||||
audience.sendMessage(component);
|
||||
}
|
||||
|
||||
if (player.hasPermission("carbonchat.clearchat.notify")) {
|
||||
String message = carbonChat.getLanguage().getString("clear-notify");
|
||||
audience.sendMessage(carbonChat.getAdventureManager().processMessage(message, "player", issuer.getName()));
|
||||
}
|
||||
|
||||
if (player.hasPermission("carbonchat.clearchat.exempt")) {
|
||||
String message = carbonChat.getLanguage().getString("clear-exempt");
|
||||
audience.sendMessage(carbonChat.getAdventureManager().processMessage(message, "player", issuer.getName()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package net.draycia.carbon.commands;
|
||||
|
||||
import co.aikar.commands.BaseCommand;
|
||||
import co.aikar.commands.annotation.*;
|
||||
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")
|
||||
@Syntax("<player>")
|
||||
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.getLanguage().getString("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.getLanguage().getString("ignore-exempt")));
|
||||
} else {
|
||||
user.setIgnoringUser(targetUser, true);
|
||||
user.sendMessage(processMessage(player, carbonChat.getLanguage().getString("ignoring-user")));
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private Component processMessage(Player player, String message) {
|
||||
return carbonChat.getAdventureManager().processMessageWithPapi(player, message, "br", "\n");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package net.draycia.carbon.commands;
|
||||
|
||||
import co.aikar.commands.BaseCommand;
|
||||
import co.aikar.commands.annotation.*;
|
||||
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
|
||||
@Syntax("<message>")
|
||||
public void baseCommand(Player player, String... args) {
|
||||
if (args == null || args.length == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
String message = String.join(" ", args).replace("</pre>", "");
|
||||
String format = PlaceholderAPI.setPlaceholders(player, carbonChat.getLanguage().getString("me"));
|
||||
|
||||
if (!player.hasPermission("carbonchat.me.formatting")) {
|
||||
format = format.replace("<message>", "<pre><message></pre>");
|
||||
}
|
||||
|
||||
Component component = carbonChat.getAdventureManager().processMessage(format, "br", "\n",
|
||||
"displayname", player.getDisplayName(), "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,28 @@
|
||||
package net.draycia.carbon.commands;
|
||||
|
||||
import co.aikar.commands.BaseCommand;
|
||||
import co.aikar.commands.annotation.*;
|
||||
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")
|
||||
@Syntax("<player> <message>")
|
||||
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,54 @@
|
||||
package net.draycia.carbon.commands;
|
||||
|
||||
import co.aikar.commands.annotation.*;
|
||||
import co.aikar.commands.BaseCommand;
|
||||
import net.draycia.carbon.CarbonChat;
|
||||
import net.draycia.carbon.storage.ChatUser;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.milkbowl.vault.permission.Permission;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
@CommandAlias("mute")
|
||||
@CommandPermission("carbonchat.mute")
|
||||
public class MuteCommand extends BaseCommand {
|
||||
|
||||
@Dependency
|
||||
private CarbonChat carbonChat;
|
||||
|
||||
@Default
|
||||
@CommandCompletion("@players")
|
||||
@Syntax("<player>")
|
||||
public void baseCommand(CommandSender sender, ChatUser targetUser) {
|
||||
Audience audience = carbonChat.getAdventureManager().getAudiences().audience(sender);
|
||||
|
||||
if (targetUser.isMuted()) {
|
||||
targetUser.setMuted(false);
|
||||
String format = carbonChat.getLanguage().getString("no-longer-muted");
|
||||
|
||||
Component message = carbonChat.getAdventureManager().processMessage(format, "br", "\n",
|
||||
"player", targetUser.asOfflinePlayer().getName());
|
||||
|
||||
audience.sendMessage(message);
|
||||
} else {
|
||||
Bukkit.getScheduler().runTaskAsynchronously(carbonChat, () -> {
|
||||
Permission permission = carbonChat.getPermission();
|
||||
String format;
|
||||
|
||||
if (permission.playerHas(null, targetUser.asOfflinePlayer(), "carbonchat.mute.exempt")) {
|
||||
format = carbonChat.getLanguage().getString("mute-exempt");
|
||||
} else {
|
||||
targetUser.setMuted(true);
|
||||
format = carbonChat.getLanguage().getString("is-now-muted");
|
||||
}
|
||||
|
||||
Component message = carbonChat.getAdventureManager().processMessage(format, "br", "\n",
|
||||
"player", targetUser.asOfflinePlayer().getName());
|
||||
|
||||
audience.sendMessage(message);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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.storage.ChatUser;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
@CommandAlias("nick|nickname")
|
||||
@CommandPermission("carbonchat.nickname")
|
||||
public class NicknameCommand extends BaseCommand {
|
||||
|
||||
@Dependency
|
||||
private CarbonChat carbonChat;
|
||||
|
||||
@Default
|
||||
@Syntax("<nickname> [player]")
|
||||
public void baseCommand(Player player, String nickname, @Optional ChatUser target) {
|
||||
ChatUser sender = carbonChat.getUserService().wrap(player);
|
||||
|
||||
// Reset nickname
|
||||
if (nickname.equalsIgnoreCase("off")) {
|
||||
nickname = null;
|
||||
}
|
||||
|
||||
// Reset other player's nickname
|
||||
if (target != null && nickname != null && nickname.equals(target.asOfflinePlayer().getName())) {
|
||||
nickname = null;
|
||||
}
|
||||
|
||||
// Reset own nickname
|
||||
if (target == null && nickname != null && nickname.equals(sender.asOfflinePlayer().getName())) {
|
||||
nickname = null;
|
||||
}
|
||||
|
||||
if (target != null) {
|
||||
target.setNickname(nickname);
|
||||
|
||||
String message;
|
||||
|
||||
if (nickname == null) {
|
||||
message = carbonChat.getLanguage().getString("other-nickname-reset");
|
||||
} else {
|
||||
message = carbonChat.getLanguage().getString("other-nickname-set");
|
||||
}
|
||||
|
||||
sender.sendMessage(carbonChat.getAdventureManager().processMessage(
|
||||
message, "nickname", nickname == null ? "" : nickname,
|
||||
"user", target.asOfflinePlayer().getName()));
|
||||
} else {
|
||||
sender.setNickname(nickname);
|
||||
|
||||
String message;
|
||||
|
||||
if (nickname == null) {
|
||||
message = carbonChat.getLanguage().getString("nickname-reset");
|
||||
} else {
|
||||
message = carbonChat.getLanguage().getString("nickname-set");
|
||||
}
|
||||
|
||||
sender.sendMessage(carbonChat.getAdventureManager().processMessageWithPapi(player,
|
||||
message, "nickname", nickname == null ? "" : nickname));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package net.draycia.carbon.commands;
|
||||
|
||||
import co.aikar.commands.BaseCommand;
|
||||
import co.aikar.commands.annotation.*;
|
||||
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
|
||||
@Syntax("<message>")
|
||||
public void baseCommand(Player player, String... args) {
|
||||
if (args.length == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
ChatUser user = carbonChat.getUserService().wrap(player);
|
||||
|
||||
if (user.getReplyTarget() == null) {
|
||||
String message = carbonChat.getLanguage().getString("no-reply-target");
|
||||
Component component = carbonChat.getAdventureManager().processMessage(message, "br", "\n");
|
||||
user.sendMessage(component);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
ChatUser targetUser = carbonChat.getUserService().wrap(user.getReplyTarget());
|
||||
|
||||
targetUser.sendMessage(user, String.join(" ", args));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
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")
|
||||
@Syntax("<channel> <color>")
|
||||
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.getKey())) {
|
||||
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.getLanguage().getString("channel-color-set"),
|
||||
"br", "\n", "color", "<color:" + color + ">", "channel", channel.getName(), "hex", color));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package net.draycia.carbon.commands;
|
||||
|
||||
import co.aikar.commands.annotation.*;
|
||||
import co.aikar.commands.BaseCommand;
|
||||
import net.draycia.carbon.CarbonChat;
|
||||
import net.draycia.carbon.storage.ChatUser;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.milkbowl.vault.permission.Permission;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
@CommandAlias("shadowmute|sm|smute")
|
||||
@CommandPermission("carbonchat.shadowmute")
|
||||
public class ShadowMuteCommand extends BaseCommand {
|
||||
|
||||
@Dependency
|
||||
private CarbonChat carbonChat;
|
||||
|
||||
@Default
|
||||
@CommandCompletion("@players")
|
||||
@Syntax("<player>")
|
||||
public void baseCommand(CommandSender sender, ChatUser targetUser) {
|
||||
Audience audience = carbonChat.getAdventureManager().getAudiences().audience(sender);
|
||||
|
||||
if (targetUser.isShadowMuted()) {
|
||||
targetUser.setShadowMuted(false);
|
||||
String format = carbonChat.getLanguage().getString("no-longer-shadow-muted");
|
||||
|
||||
Component message = carbonChat.getAdventureManager().processMessage(format, "br", "\n",
|
||||
"player", targetUser.asOfflinePlayer().getName());
|
||||
|
||||
audience.sendMessage(message);
|
||||
} else {
|
||||
Bukkit.getScheduler().runTaskAsynchronously(carbonChat, () -> {
|
||||
Permission permission = carbonChat.getPermission();
|
||||
String format;
|
||||
|
||||
if (permission.playerHas(null, targetUser.asOfflinePlayer(), "carbonchat.shadowmute.exempt")) {
|
||||
format = carbonChat.getLanguage().getString("shadow-mute-exempt");
|
||||
} else {
|
||||
targetUser.setShadowMuted(true);
|
||||
format = carbonChat.getLanguage().getString("is-now-shadow-muted");
|
||||
}
|
||||
|
||||
Component message = carbonChat.getAdventureManager().processMessage(format, "br", "\n",
|
||||
"player", targetUser.asOfflinePlayer().getName());
|
||||
|
||||
audience.sendMessage(message);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
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")
|
||||
@Syntax("<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.getLanguage().getString("spy-toggled-off");
|
||||
} else {
|
||||
settings.setSpying(true);
|
||||
message = carbonChat.getLanguage().getString("spy-toggled-on");
|
||||
}
|
||||
|
||||
user.sendMessage(carbonChat.getAdventureManager().processMessageWithPapi(player, message, "br", "\n",
|
||||
"color", "<color:" + channel.getChannelColor(user).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.getLanguage().getString("spy-whispers-off");
|
||||
} else {
|
||||
user.setSpyingWhispers(true);
|
||||
message = carbonChat.getLanguage().getString("spy-whispers-on");
|
||||
}
|
||||
|
||||
user.sendMessage(carbonChat.getAdventureManager().processMessageWithPapi(player, message, "br", "\n"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
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")
|
||||
@Syntax("<channel>")
|
||||
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.getChannelColor(user).toString() + ">", "channel", channel.getName()));
|
||||
}
|
||||
|
||||
@CommandPermission("carbonchat.toggle.others")
|
||||
@Subcommand("other")
|
||||
@CommandCompletion("@chatchannel @players")
|
||||
@Syntax("<channel> <player>")
|
||||
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.getChannelColor(user).toString() + ">", "channel", channel.getName()));
|
||||
|
||||
carbonChat.getAdventureManager().getAudiences().audience(sender).sendMessage(carbonChat.getAdventureManager().processMessage(otherMessage,
|
||||
"br", "\n", "color", "<color:" + channel.getChannelColor(user).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 final List<ChatChannel> registeredChannels;
|
||||
private final 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 final ChatChannel channel;
|
||||
private final 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,84 @@
|
||||
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 org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class ChatComponentEvent extends Event implements Cancellable {
|
||||
|
||||
private static final HandlerList HANDLERS_LIST = new HandlerList();
|
||||
private boolean isCancelled = false;
|
||||
|
||||
private @NotNull final ChatUser sender;
|
||||
private @Nullable final ChatUser target;
|
||||
private ChatChannel chatChannel;
|
||||
private TextComponent component;
|
||||
private final String originalMessage;
|
||||
|
||||
public ChatComponentEvent(@NotNull ChatUser sender, @Nullable ChatUser target, ChatChannel chatChannel, TextComponent component, String originalMessage) {
|
||||
super(!Bukkit.isPrimaryThread());
|
||||
|
||||
this.sender = sender;
|
||||
this.target = target;
|
||||
this.chatChannel = chatChannel;
|
||||
this.component = component;
|
||||
this.originalMessage = originalMessage;
|
||||
}
|
||||
|
||||
@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;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public ChatUser getSender() {
|
||||
return sender;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public ChatUser getTarget() {
|
||||
return target;
|
||||
}
|
||||
|
||||
public ChatChannel getChannel() {
|
||||
return chatChannel;
|
||||
}
|
||||
|
||||
public void setChannel(ChatChannel chatChannel) {
|
||||
this.chatChannel = chatChannel;
|
||||
}
|
||||
|
||||
public TextComponent getComponent() {
|
||||
return component;
|
||||
}
|
||||
|
||||
public void setComponent(TextComponent component) {
|
||||
this.component = component;
|
||||
}
|
||||
|
||||
public String getOriginalMessage() {
|
||||
return originalMessage;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
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 sender;
|
||||
private final ChatUser target;
|
||||
|
||||
private ChatChannel chatChannel;
|
||||
private String format;
|
||||
private String message;
|
||||
|
||||
public ChatFormatEvent(ChatUser sender, ChatUser target, ChatChannel chatChannel, String format, String message) {
|
||||
super(!Bukkit.isPrimaryThread());
|
||||
|
||||
this.sender = sender;
|
||||
this.target = target;
|
||||
|
||||
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 getSender() {
|
||||
return sender;
|
||||
}
|
||||
|
||||
public ChatUser getTarget() {
|
||||
return target;
|
||||
}
|
||||
|
||||
public ChatChannel getChannel() {
|
||||
return chatChannel;
|
||||
}
|
||||
|
||||
public void setChannel(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,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 PreChatFormatEvent 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 PreChatFormatEvent(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 getChannel() {
|
||||
return chatChannel;
|
||||
}
|
||||
|
||||
public void setChannel(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,73 @@
|
||||
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.Cancellable;
|
||||
import org.bukkit.event.Event;
|
||||
import org.bukkit.event.HandlerList;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class PrivateMessageEvent 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 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,79 @@
|
||||
package net.draycia.carbon.listeners;
|
||||
|
||||
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;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.AsyncPlayerChatEvent;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
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) {
|
||||
ChatUser user = carbonChat.getUserService().wrap(event.getPlayer());
|
||||
ChatChannel channel = user.getSelectedChannel();
|
||||
|
||||
if (channel.shouldCancelChatEvent()) {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
|
||||
for (ChatChannel entry : carbonChat.getChannelManager().getRegistry().values()) {
|
||||
if (entry.getMessagePrefix() == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (event.getMessage().startsWith(entry.getMessagePrefix())) {
|
||||
if (entry.canPlayerUse(user)) {
|
||||
event.setMessage(event.getMessage().substring(entry.getMessagePrefix().length()));
|
||||
channel = entry;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
final ChatChannel selectedChannel = channel;
|
||||
|
||||
if (!selectedChannel.canPlayerUse(user)) {
|
||||
return;
|
||||
}
|
||||
|
||||
final Collection<ChatUser> recipients;
|
||||
|
||||
if (selectedChannel.honorsRecipientList()) {
|
||||
recipients = new HashSet<>();
|
||||
|
||||
for (Player recipient : event.getRecipients()) {
|
||||
recipients.add(carbonChat.getUserService().wrap(recipient));
|
||||
}
|
||||
} else {
|
||||
recipients = (List<ChatUser>)selectedChannel.audiences();
|
||||
}
|
||||
|
||||
event.getRecipients().clear();
|
||||
|
||||
if (event.isAsynchronous()) {
|
||||
selectedChannel.sendMessage(user, recipients, event.getMessage(), false);
|
||||
} else {
|
||||
Bukkit.getScheduler().runTaskAsynchronously(carbonChat, () -> {
|
||||
selectedChannel.sendMessage(user, recipients, event.getMessage(), false);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package net.draycia.carbon.listeners;
|
||||
|
||||
import net.draycia.carbon.CarbonChat;
|
||||
import net.draycia.carbon.events.PreChatFormatEvent;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
|
||||
public class CapsHandler implements Listener {
|
||||
|
||||
private final CarbonChat carbonChat;
|
||||
|
||||
public CapsHandler(CarbonChat carbonChat) {
|
||||
this.carbonChat = carbonChat;
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
|
||||
public void onMessage(PreChatFormatEvent event) {
|
||||
if (!carbonChat.getModConfig().getBoolean("caps-protection.enabled")) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!(event.getMessage().length() >= carbonChat.getModConfig().getInt("caps-protection.minimum-length"))) {
|
||||
return;
|
||||
}
|
||||
|
||||
int amountOfCaps = 0;
|
||||
|
||||
for (char letter : event.getMessage().toCharArray()) {
|
||||
if (Character.isUpperCase(letter)) {
|
||||
amountOfCaps++;
|
||||
}
|
||||
}
|
||||
|
||||
double capsPercentage = (amountOfCaps * 100.0) / event.getMessage().length();
|
||||
|
||||
if (!(capsPercentage >= carbonChat.getModConfig().getDouble("caps-protection.percent-caps"))) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (carbonChat.getModConfig().getBoolean("block-message")) {
|
||||
event.setCancelled(true);
|
||||
} else {
|
||||
event.setMessage(event.getMessage().toLowerCase());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
package net.draycia.carbon.listeners;
|
||||
|
||||
import net.draycia.carbon.CarbonChat;
|
||||
import net.draycia.carbon.channels.ChatChannel;
|
||||
import net.draycia.carbon.events.PreChatFormatEvent;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class FilterHandler implements Listener {
|
||||
|
||||
private final CarbonChat carbonChat;
|
||||
|
||||
private final Map<String, List<Pattern>> patternReplacements = new HashMap<>();
|
||||
private final List<Pattern> blockedWords = new ArrayList<>();
|
||||
|
||||
public FilterHandler(CarbonChat carbonChat) {
|
||||
this.carbonChat = carbonChat;
|
||||
|
||||
FileConfiguration config = carbonChat.getModConfig();
|
||||
|
||||
ConfigurationSection filters = config.getConfigurationSection("filters.filters");
|
||||
|
||||
for (String replacement : filters.getKeys(false)) {
|
||||
List<Pattern> patterns = new ArrayList<>();
|
||||
|
||||
for (String word : filters.getStringList(replacement)) {
|
||||
if (carbonChat.getModConfig().getBoolean("filters.case-sensitive")) {
|
||||
patterns.add(Pattern.compile(word));
|
||||
} else {
|
||||
patterns.add(Pattern.compile(word, Pattern.CASE_INSENSITIVE));
|
||||
}
|
||||
}
|
||||
|
||||
patternReplacements.put(replacement, patterns);
|
||||
}
|
||||
|
||||
for (String replacement : config.getStringList("filters.blocked-words")) {
|
||||
for (String word : filters.getStringList(replacement)) {
|
||||
if (carbonChat.getModConfig().getBoolean("filters.case-sensitive")) {
|
||||
blockedWords.add(Pattern.compile(word));
|
||||
} else {
|
||||
blockedWords.add(Pattern.compile(word, Pattern.CASE_INSENSITIVE));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
|
||||
public void onFilter(PreChatFormatEvent event) {
|
||||
if (!carbonChat.getModConfig().getBoolean("filters.enabled")) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!channelUsesFilter(event.getChannel())) {
|
||||
return;
|
||||
}
|
||||
|
||||
String message = event.getMessage();
|
||||
//Matcher matcher;
|
||||
|
||||
for (Map.Entry<String, List<Pattern>> entry : patternReplacements.entrySet()) {
|
||||
for (Pattern pattern : entry.getValue()) {
|
||||
Matcher matcher = pattern.matcher(message);
|
||||
|
||||
if (entry.getKey().equals("_")) {
|
||||
message = matcher.replaceAll("");
|
||||
} else {
|
||||
message = matcher.replaceAll(entry.getKey());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
event.setMessage(message);
|
||||
|
||||
for (Pattern blockedWord : blockedWords) {
|
||||
if (blockedWord.matcher(event.getMessage()).find()) {
|
||||
event.setCancelled(true);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean channelUsesFilter(ChatChannel chatChannel) {
|
||||
Object filter = chatChannel.getContext("filter");
|
||||
|
||||
return filter instanceof Boolean && ((Boolean) filter);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
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 IgnoredPlayerHandler implements Listener {
|
||||
|
||||
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
|
||||
public void onPapiPlaceholder(ChatFormatEvent event) {
|
||||
if (event.getTarget() == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (event.getTarget().isIgnoringUser(event.getSender())) {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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, ignoreCancelled = true)
|
||||
public void onItemLink(ChatComponentEvent event) {
|
||||
// Handle item linking placeholders
|
||||
if (event.getSender().isOnline()) {
|
||||
for (Pattern pattern : event.getChannel().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.getSender().asPlayer()));
|
||||
});
|
||||
|
||||
event.setComponent(component);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package net.draycia.carbon.listeners;
|
||||
|
||||
import net.draycia.carbon.CarbonChat;
|
||||
import net.draycia.carbon.events.PreChatFormatEvent;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
|
||||
public class LegacyFormatHandler implements Listener {
|
||||
|
||||
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
|
||||
public void onLegacyFormat(PreChatFormatEvent event) {
|
||||
Component component = CarbonChat.LEGACY.deserialize(event.getFormat());
|
||||
event.setFormat(MiniMessage.get().serialize(component));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package net.draycia.carbon.listeners;
|
||||
|
||||
import net.draycia.carbon.events.PreChatFormatEvent;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
|
||||
public class MuteHandler implements Listener {
|
||||
|
||||
@EventHandler
|
||||
public void onMute(PreChatFormatEvent event) {
|
||||
if (event.getUser().isMuted()) {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package net.draycia.carbon.listeners;
|
||||
|
||||
import net.draycia.carbon.events.PreChatFormatEvent;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
|
||||
public class OfflineNameHandler implements Listener {
|
||||
|
||||
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
|
||||
public void onOfflineMessage(PreChatFormatEvent 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,48 @@
|
||||
package net.draycia.carbon.listeners;
|
||||
|
||||
import net.draycia.carbon.CarbonChat;
|
||||
import net.draycia.carbon.events.ChatComponentEvent;
|
||||
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 final CarbonChat carbonChat;
|
||||
|
||||
public PingHandler(CarbonChat carbonChat) {
|
||||
this.carbonChat = carbonChat;
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
|
||||
public void onPing(ChatComponentEvent event) {
|
||||
if (event.getTarget() == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (event.getSender().getUUID().equals(event.getTarget().getUUID())) {
|
||||
return;
|
||||
}
|
||||
|
||||
String senderName = event.getSender().asOfflinePlayer().getName();
|
||||
String prefix = carbonChat.getConfig().getString("pings.prefix", "");
|
||||
|
||||
if (senderName == null || !event.getOriginalMessage().contains(prefix + senderName)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!carbonChat.getConfig().getBoolean("pings.enabled")) {
|
||||
return;
|
||||
}
|
||||
|
||||
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");
|
||||
|
||||
event.getSender().playSound(Sound.of(key, source, volume, pitch));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package net.draycia.carbon.listeners;
|
||||
|
||||
import me.clip.placeholderapi.PlaceholderAPI;
|
||||
import net.draycia.carbon.events.PreChatFormatEvent;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
|
||||
public class PlaceholderHandler implements Listener {
|
||||
|
||||
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
|
||||
public void onPapiPlaceholder(PreChatFormatEvent event) {
|
||||
event.setFormat(PlaceholderAPI.setPlaceholders(event.getUser().asOfflinePlayer(), event.getFormat()));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package net.draycia.carbon.listeners;
|
||||
|
||||
import net.draycia.carbon.CarbonChat;
|
||||
import net.draycia.carbon.storage.ChatUser;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
|
||||
public class PlayerJoinListener implements Listener {
|
||||
|
||||
private final CarbonChat carbonChat;
|
||||
|
||||
public PlayerJoinListener(CarbonChat carbonChat) {
|
||||
this.carbonChat = carbonChat;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerJoin(PlayerJoinEvent event) {
|
||||
ChatUser user = carbonChat.getUserService().wrap(event.getPlayer());
|
||||
|
||||
carbonChat.getUserService().validate(user);
|
||||
|
||||
if (user.getNickname() != null) {
|
||||
user.setNickname(user.getNickname());
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerQuit(PlayerQuitEvent event) {
|
||||
ChatUser user = carbonChat.getUserService().wrapIfLoaded(event.getPlayer());
|
||||
|
||||
if (user != null) {
|
||||
carbonChat.getUserService().invalidate(user);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package net.draycia.carbon.listeners;
|
||||
|
||||
import me.clip.placeholderapi.PlaceholderAPI;
|
||||
import net.draycia.carbon.events.ChatFormatEvent;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
|
||||
public class RelationalPlaceholderHandler implements Listener {
|
||||
|
||||
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
|
||||
public void onPapiPlaceholder(ChatFormatEvent event) {
|
||||
if (event.getTarget() == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!event.getSender().isOnline() || !event.getTarget().isOnline()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Player sender = event.getSender().asPlayer();
|
||||
Player target = event.getTarget().asPlayer();
|
||||
|
||||
event.setFormat(PlaceholderAPI.setRelationalPlaceholders(sender, target, event.getFormat()));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package net.draycia.carbon.listeners;
|
||||
|
||||
import net.draycia.carbon.CarbonChat;
|
||||
import net.draycia.carbon.events.ChatComponentEvent;
|
||||
import net.draycia.carbon.events.ChatFormatEvent;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
|
||||
public class ShadowMuteHandler implements Listener {
|
||||
|
||||
private final CarbonChat carbonChat;
|
||||
|
||||
public ShadowMuteHandler(CarbonChat carbonChat) {
|
||||
this.carbonChat = carbonChat;
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.LOWEST)
|
||||
public void onComponent(ChatComponentEvent event) {
|
||||
if (event.getSender().isShadowMuted()) {
|
||||
if (!event.getSender().equals(event.getTarget())) {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void on(ChatFormatEvent event) {
|
||||
if (event.getTarget() != null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!event.getSender().isShadowMuted()) {
|
||||
return;
|
||||
}
|
||||
|
||||
String prefix = carbonChat.getModConfig().getString("shadow-mute-prefix", "[SM] ");
|
||||
|
||||
event.setFormat(prefix + event.getFormat());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package net.draycia.carbon.listeners;
|
||||
|
||||
import net.draycia.carbon.events.PreChatFormatEvent;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
|
||||
public class UserFormattingListener implements Listener {
|
||||
|
||||
@EventHandler(ignoreCancelled = true)
|
||||
public void onFormat(PreChatFormatEvent event) {
|
||||
if (!event.getUser().isOnline()) {
|
||||
suppressFormatting(event);
|
||||
return;
|
||||
}
|
||||
|
||||
Player player = event.getUser().asPlayer();
|
||||
|
||||
if (!(player.hasPermission("carbonchat.formatting") ||
|
||||
player.hasPermission("carbonchat.channels." + event.getChannel().getKey() + ".formatting"))) {
|
||||
suppressFormatting(event);
|
||||
} else {
|
||||
// Swap the &-style codes for minimessage-compatible strings
|
||||
event.setMessage(MiniMessage.get().serialize(
|
||||
LegacyComponentSerializer.legacyAmpersand().deserialize(event.getMessage())));
|
||||
}
|
||||
}
|
||||
|
||||
private void suppressFormatting(PreChatFormatEvent event) {
|
||||
event.setFormat(event.getFormat().replace("<message>", "<pre><message></pre>"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package net.draycia.carbon.listeners;
|
||||
|
||||
import net.draycia.carbon.CarbonChat;
|
||||
import net.draycia.carbon.events.PrivateMessageEvent;
|
||||
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 WhisperPingHandler implements Listener {
|
||||
|
||||
private final CarbonChat carbonChat;
|
||||
|
||||
public WhisperPingHandler(CarbonChat carbonChat) {
|
||||
this.carbonChat = carbonChat;
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
|
||||
public void onPing(PrivateMessageEvent event) {
|
||||
if (event.getTarget() == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (event.getSender().getUUID().equals(event.getTarget().getUUID())) {
|
||||
return;
|
||||
}
|
||||
|
||||
String senderName = event.getSender().asOfflinePlayer().getName();
|
||||
|
||||
if (senderName == null || !event.getMessage().contains(senderName)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!carbonChat.getConfig().getBoolean("whisper.pings.enabled")) {
|
||||
return;
|
||||
}
|
||||
|
||||
Key key = Key.of(carbonChat.getConfig().getString("whisper.pings.sound"));
|
||||
Sound.Source source = Sound.Source.valueOf(carbonChat.getConfig().getString("whisper.pings.source"));
|
||||
float volume = (float) carbonChat.getConfig().getDouble("whisper.pings.volume");
|
||||
float pitch = (float) carbonChat.getConfig().getDouble("whisper.pings.pitch");
|
||||
|
||||
event.getSender().playSound(Sound.of(key, source, volume, pitch));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
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.getLanguage().getString("formatting.type", "minimessage").toLowerCase()) {
|
||||
case "minedown":
|
||||
return processMineDown(input, placeholders);
|
||||
case "mojang":
|
||||
case "mojangson":
|
||||
case "json":
|
||||
return processMojang(input, placeholders);
|
||||
case "minimessage-markdown":
|
||||
return MiniMessage.markdown().parse(input, placeholders);
|
||||
case "minimessage":
|
||||
default:
|
||||
return MiniMessage.get().parse(input, placeholders);
|
||||
}
|
||||
}
|
||||
|
||||
private Component processMojang(String input, String... placeholders) {
|
||||
for (int i = 0; i < placeholders.length; i += 2) {
|
||||
String placeholder = placeholders[i];
|
||||
String replacement = placeholders[i+1];
|
||||
|
||||
input = input.replace("<" + placeholder + ">", replacement);
|
||||
}
|
||||
|
||||
return getAudiences().gsonSerializer().deserialize(input);
|
||||
}
|
||||
|
||||
private Component processMineDown(String input, String... placeholders) {
|
||||
return MineDown.parse(input, placeholders);
|
||||
}
|
||||
|
||||
public BukkitAudiences getAudiences() {
|
||||
return audiences;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
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);
|
||||
|
||||
reload();
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public void reload() {
|
||||
registry.clearAll();
|
||||
|
||||
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)) {
|
||||
String prefix = channel.getMessagePrefix();
|
||||
|
||||
if (prefix != null && !prefix.trim().isEmpty()) {
|
||||
prefix = ("(" + prefix + ") ");
|
||||
} else {
|
||||
prefix = "";
|
||||
}
|
||||
|
||||
carbonChat.getLogger().info("Registering channel: " + prefix + channel.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
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 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.getKey());
|
||||
}
|
||||
}
|
||||
|
||||
return completions;
|
||||
});
|
||||
|
||||
commandManager.getCommandCompletions().registerCompletion("channel", (context) -> {
|
||||
List<String> completions = new ArrayList<>();
|
||||
|
||||
for (ChatChannel chatChannel : carbonChat.getChannelManager().getRegistry().values()) {
|
||||
completions.add(chatChannel.getKey());
|
||||
}
|
||||
|
||||
return completions;
|
||||
});
|
||||
|
||||
commandManager.getCommandContexts().registerContext(ChatChannel.class, (context) -> {
|
||||
String name = context.popFirstArg();
|
||||
|
||||
for (ChatChannel chatChannel : carbonChat.getChannelManager().getRegistry().values()) {
|
||||
if (chatChannel.getKey().equalsIgnoreCase(name)) {
|
||||
return chatChannel;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
});
|
||||
|
||||
commandManager.getCommandContexts().registerContext(ChatUser.class, (context) -> {
|
||||
return carbonChat.getUserService().wrap(context.popFirstArg());
|
||||
});
|
||||
|
||||
commandManager.getCommandConditions().addCondition(ChatChannel.class,"canuse", (context, execution, value) -> {
|
||||
if (value == null) {
|
||||
throw new ConditionFailedException(carbonChat.getLanguage().getString("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.getLanguage().getString("cannot-use-channel"));
|
||||
}
|
||||
});
|
||||
|
||||
reloadCommands();
|
||||
}
|
||||
|
||||
public void reloadCommands() {
|
||||
this.commandManager.registerCommand(new ToggleCommand());
|
||||
this.commandManager.registerCommand(new ChannelCommand());
|
||||
this.commandManager.registerCommand(new ChannelListCommand());
|
||||
this.commandManager.registerCommand(new IgnoreCommand());
|
||||
this.commandManager.registerCommand(new ChatReloadCommand());
|
||||
this.commandManager.registerCommand(new MeCommand());
|
||||
this.commandManager.registerCommand(new MessageCommand());
|
||||
this.commandManager.registerCommand(new NicknameCommand());
|
||||
this.commandManager.registerCommand(new ReplyCommand());
|
||||
this.commandManager.registerCommand(new SetColorCommand());
|
||||
this.commandManager.registerCommand(new SpyChannelCommand());
|
||||
|
||||
this.commandManager.registerCommand(new ClearChatCommand());
|
||||
this.commandManager.registerCommand(new MuteCommand());
|
||||
this.commandManager.registerCommand(new ShadowMuteCommand());
|
||||
}
|
||||
|
||||
public BukkitCommandManager 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,152 @@
|
||||
package net.draycia.carbon.messaging;
|
||||
|
||||
import com.google.common.io.ByteArrayDataOutput;
|
||||
import net.draycia.carbon.CarbonChat;
|
||||
import net.draycia.carbon.channels.ChatChannel;
|
||||
import net.draycia.carbon.messaging.impl.BungeeMessageService;
|
||||
import net.draycia.carbon.messaging.impl.EmptyMessageService;
|
||||
import net.draycia.carbon.messaging.impl.RedisMessageService;
|
||||
import net.draycia.carbon.storage.ChatUser;
|
||||
import net.kyori.adventure.text.format.TextColor;
|
||||
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
|
||||
|
||||
import java.util.UUID;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class MessageManager {
|
||||
|
||||
private final CarbonChat carbonChat;
|
||||
private final MessageService messageService;
|
||||
|
||||
private final GsonComponentSerializer gsonSerializer;
|
||||
|
||||
public MessageManager(CarbonChat carbonChat) {
|
||||
this.carbonChat = carbonChat;
|
||||
this.gsonSerializer = carbonChat.getAdventureManager().getAudiences().gsonSerializer();
|
||||
|
||||
String messageSystem = carbonChat.getConfig().getString("message-system", "none");
|
||||
|
||||
if (messageSystem == null) {
|
||||
messageSystem = "none";
|
||||
}
|
||||
|
||||
switch(messageSystem.toLowerCase()) {
|
||||
case "bungee":
|
||||
carbonChat.getLogger().info("Using Bungee Plugin Messaging for message forwarding!");
|
||||
messageService = new BungeeMessageService(carbonChat);
|
||||
break;
|
||||
case "redis":
|
||||
carbonChat.getLogger().info("Using Redis for message forwarding!");
|
||||
messageService = new RedisMessageService(carbonChat);
|
||||
break;
|
||||
case "none":
|
||||
messageService = new EmptyMessageService();
|
||||
break;
|
||||
default:
|
||||
carbonChat.getLogger().info("Invalid message service selected! Disabling syncing until next restart!");
|
||||
messageService = new EmptyMessageService();
|
||||
break;
|
||||
}
|
||||
|
||||
this.registerDefaultListeners();
|
||||
}
|
||||
|
||||
private void registerDefaultListeners() {
|
||||
getMessageService().registerUserMessageListener("nickname", (user, byteArray) -> {
|
||||
String nickname = byteArray.readUTF();
|
||||
|
||||
user.setNickname(nickname, true);
|
||||
|
||||
if (user.isOnline()) {
|
||||
String message = carbonChat.getLanguage().getString("nickname-set");
|
||||
|
||||
user.sendMessage(carbonChat.getAdventureManager().processMessageWithPapi(user.asPlayer(),
|
||||
message, "nickname", nickname));
|
||||
}
|
||||
});
|
||||
|
||||
getMessageService().registerUserMessageListener("nickname-reset", (user, byteArray) -> {
|
||||
user.setNickname(null, true);
|
||||
|
||||
if (user.isOnline()) {
|
||||
String message = carbonChat.getLanguage().getString("nickname-reset");
|
||||
|
||||
user.sendMessage(carbonChat.getAdventureManager().processMessageWithPapi(user.asPlayer(), message));
|
||||
}
|
||||
});
|
||||
|
||||
getMessageService().registerUserMessageListener("selected-channel", (user, byteArray) -> {
|
||||
user.setSelectedChannel(carbonChat.getChannelManager().getRegistry().get(byteArray.readUTF()), true);
|
||||
});
|
||||
|
||||
getMessageService().registerUserMessageListener("spying-whispers", (user, byteArray) -> {
|
||||
user.setSpyingWhispers(byteArray.readBoolean(), true);
|
||||
});
|
||||
|
||||
getMessageService().registerUserMessageListener("muted", (user, byteArray) -> {
|
||||
user.setMuted(byteArray.readBoolean(), true);
|
||||
});
|
||||
|
||||
getMessageService().registerUserMessageListener("shadow-muted", (user, byteArray) -> {
|
||||
user.setShadowMuted(byteArray.readBoolean(), true);
|
||||
});
|
||||
|
||||
getMessageService().registerUserMessageListener("reply-target", (user, byteArray) -> {
|
||||
user.setReplyTarget(new UUID(byteArray.readLong(), byteArray.readLong()), true);
|
||||
});
|
||||
|
||||
getMessageService().registerUserMessageListener("ignoring-user", (user, byteArray) -> {
|
||||
user.setIgnoringUser(new UUID(byteArray.readLong(), byteArray.readLong()), byteArray.readBoolean(), true);
|
||||
});
|
||||
|
||||
getMessageService().registerUserMessageListener("ignoring-channel", (user, byteArray) -> {
|
||||
user.getChannelSettings(carbonChat.getChannelManager().getRegistry().get(byteArray.readUTF()))
|
||||
.setIgnoring(byteArray.readBoolean(), true);
|
||||
});
|
||||
|
||||
getMessageService().registerUserMessageListener("spying-channel", (user, byteArray) -> {
|
||||
user.getChannelSettings(carbonChat.getChannelManager().getRegistry().get(byteArray.readUTF()))
|
||||
.setSpying(byteArray.readBoolean(), true);
|
||||
});
|
||||
|
||||
getMessageService().registerUserMessageListener("channel-color", (user, byteArray) -> {
|
||||
user.getChannelSettings(carbonChat.getChannelManager().getRegistry().get(byteArray.readUTF()))
|
||||
.setColor(TextColor.fromHexString(byteArray.readUTF()), true);
|
||||
});
|
||||
|
||||
getMessageService().registerUserMessageListener("channel-color-reset", (user, byteArray) -> {
|
||||
user.getChannelSettings(carbonChat.getChannelManager().getRegistry().get(byteArray.readUTF()))
|
||||
.setColor(null, true);
|
||||
});
|
||||
|
||||
getMessageService().registerUUIDMessageListener("channel-component", (uuid, byteArray) -> {
|
||||
ChatChannel channel = carbonChat.getChannelManager().getRegistry().get(byteArray.readUTF());
|
||||
ChatUser user = carbonChat.getUserService().wrap(uuid);
|
||||
|
||||
if (channel != null) {
|
||||
channel.sendComponent(user, gsonSerializer.deserialize(byteArray.readUTF()));
|
||||
}
|
||||
});
|
||||
|
||||
getMessageService().registerUUIDMessageListener("whisper-component", (uuid, byteArray) -> {
|
||||
UUID recipient = new UUID(byteArray.readLong(), byteArray.readLong());
|
||||
|
||||
ChatUser target = carbonChat.getUserService().wrap(recipient);
|
||||
String message = byteArray.readUTF();
|
||||
|
||||
if (!target.isIgnoringUser(uuid)) {
|
||||
target.setReplyTarget(uuid);
|
||||
target.sendMessage(gsonSerializer.deserialize(message));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public MessageService getMessageService() {
|
||||
return messageService;
|
||||
}
|
||||
|
||||
public void sendMessage(String key, UUID uuid, Consumer<ByteArrayDataOutput> consumer) {
|
||||
getMessageService().sendMessage(key, uuid, consumer);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package net.draycia.carbon.messaging;
|
||||
|
||||
import com.google.common.io.ByteArrayDataInput;
|
||||
import com.google.common.io.ByteArrayDataOutput;
|
||||
import net.draycia.carbon.storage.ChatUser;
|
||||
|
||||
import java.util.UUID;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public interface MessageService {
|
||||
|
||||
void registerUserMessageListener(String key, BiConsumer<ChatUser, ByteArrayDataInput> listener);
|
||||
void registerUUIDMessageListener(String key, BiConsumer<UUID, ByteArrayDataInput> listener);
|
||||
|
||||
void unregisterMessageListener(String key);
|
||||
|
||||
void sendMessage(String key, UUID uuid, Consumer<ByteArrayDataOutput> consumer);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
package net.draycia.carbon.messaging.impl;
|
||||
|
||||
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.messaging.MessageService;
|
||||
import net.draycia.carbon.storage.ChatUser;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class BungeeMessageService implements MessageService {
|
||||
|
||||
private final CarbonChat carbonChat;
|
||||
private final BungeeChannelApi api;
|
||||
|
||||
private final Map<String, BiConsumer<ChatUser, ByteArrayDataInput>> userLoadedListeners = new HashMap<>();
|
||||
private final Map<String, BiConsumer<UUID, ByteArrayDataInput>> userNotLoadedListeners = new HashMap<>();
|
||||
|
||||
private final UUID serverUUID = UUID.randomUUID();
|
||||
|
||||
public BungeeMessageService(CarbonChat carbonChat) {
|
||||
this.carbonChat = carbonChat;
|
||||
|
||||
carbonChat.getServer().getMessenger().registerOutgoingPluginChannel(carbonChat, "BungeeCord");
|
||||
|
||||
api = BungeeChannelApi.of(carbonChat);
|
||||
|
||||
api.registerForwardListener((String channel, Player player, byte[] bytes) -> {
|
||||
try {
|
||||
ByteArrayDataInput input = ByteStreams.newDataInput(bytes);
|
||||
|
||||
// Separated out for ease of debugging.
|
||||
long mostServer = input.readLong();
|
||||
long leastServer = input.readLong();
|
||||
|
||||
UUID messageUUID = new UUID(mostServer, leastServer);
|
||||
|
||||
if (messageUUID.equals(serverUUID)) {
|
||||
return;
|
||||
}
|
||||
|
||||
long mostUser = input.readLong();
|
||||
long leastUser = input.readLong();
|
||||
|
||||
UUID userUUID = new UUID(mostUser, leastUser);
|
||||
|
||||
this.receiveMessage(userUUID, channel, input);
|
||||
} catch (IllegalStateException ignored) {}
|
||||
});
|
||||
}
|
||||
|
||||
private void receiveMessage(UUID uuid, String key, ByteArrayDataInput value) {
|
||||
ChatUser user = carbonChat.getUserService().wrapIfLoaded(uuid);
|
||||
|
||||
if (user != null) {
|
||||
for (Map.Entry<String, BiConsumer<ChatUser, ByteArrayDataInput>> listener : userLoadedListeners.entrySet()) {
|
||||
if (key.equals(listener.getKey())) {
|
||||
listener.getValue().accept(user, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (Map.Entry<String, BiConsumer<UUID, ByteArrayDataInput>> listener : userNotLoadedListeners.entrySet()) {
|
||||
if (key.equals(listener.getKey())) {
|
||||
listener.getValue().accept(uuid, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerUserMessageListener(String key, BiConsumer<ChatUser, ByteArrayDataInput> listener) {
|
||||
userLoadedListeners.put(key, listener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerUUIDMessageListener(String key, BiConsumer<UUID, ByteArrayDataInput> listener) {
|
||||
userNotLoadedListeners.put(key, listener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unregisterMessageListener(String key) {
|
||||
userLoadedListeners.remove(key);
|
||||
userNotLoadedListeners.remove(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendMessage(String key, UUID uuid, Consumer<ByteArrayDataOutput> consumer) {
|
||||
ByteArrayDataOutput msg = ByteStreams.newDataOutput();
|
||||
|
||||
msg.writeLong(serverUUID.getMostSignificantBits());
|
||||
msg.writeLong(serverUUID.getLeastSignificantBits());
|
||||
msg.writeLong(uuid.getMostSignificantBits());
|
||||
msg.writeLong(uuid.getLeastSignificantBits());
|
||||
|
||||
consumer.accept(msg);
|
||||
|
||||
api.forward("ALL", key, msg.toByteArray());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package net.draycia.carbon.messaging.impl;
|
||||
|
||||
import com.google.common.io.ByteArrayDataInput;
|
||||
import com.google.common.io.ByteArrayDataOutput;
|
||||
import net.draycia.carbon.messaging.MessageService;
|
||||
import net.draycia.carbon.storage.ChatUser;
|
||||
|
||||
import java.util.UUID;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class EmptyMessageService implements MessageService {
|
||||
|
||||
@Override
|
||||
public void registerUserMessageListener(String key, BiConsumer<ChatUser, ByteArrayDataInput> listener) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerUUIDMessageListener(String key, BiConsumer<UUID, ByteArrayDataInput> listener) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unregisterMessageListener(String key) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendMessage(String key, UUID uuid, Consumer<ByteArrayDataOutput> consumer) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
package net.draycia.carbon.messaging.impl;
|
||||
|
||||
import com.google.common.io.ByteArrayDataInput;
|
||||
import com.google.common.io.ByteArrayDataOutput;
|
||||
import com.google.common.io.ByteStreams;
|
||||
import io.lettuce.core.RedisClient;
|
||||
import io.lettuce.core.RedisURI;
|
||||
import io.lettuce.core.pubsub.StatefulRedisPubSubConnection;
|
||||
import io.lettuce.core.pubsub.api.sync.RedisPubSubCommands;
|
||||
import net.draycia.carbon.CarbonChat;
|
||||
import net.draycia.carbon.messaging.MessageService;
|
||||
import net.draycia.carbon.storage.ChatUser;
|
||||
import net.draycia.carbon.util.RedisListener;
|
||||
|
||||
import java.util.Base64;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class RedisMessageService implements MessageService {
|
||||
|
||||
private Map<String, BiConsumer<ChatUser, ByteArrayDataInput>> userLoadedListeners = new HashMap<>();
|
||||
private Map<String, BiConsumer<UUID, ByteArrayDataInput>> userNotLoadedListeners = new HashMap<>();
|
||||
|
||||
private final RedisPubSubCommands<String, String> subscribeSync;
|
||||
private final RedisPubSubCommands<String, String> publishSync;
|
||||
|
||||
private final UUID serverUUID = UUID.randomUUID();
|
||||
private final CarbonChat carbonChat;
|
||||
|
||||
public RedisMessageService(CarbonChat carbonChat) {
|
||||
this.carbonChat = carbonChat;
|
||||
|
||||
String host = carbonChat.getConfig().getString("redis.host");
|
||||
String password = carbonChat.getConfig().getString("redis.password");
|
||||
int port = carbonChat.getConfig().getInt("redis.port");
|
||||
int database = carbonChat.getConfig().getInt("redis.database");
|
||||
|
||||
RedisURI.Builder builder = RedisURI.Builder.redis(host, port)
|
||||
.withDatabase(database);
|
||||
|
||||
if (password != null) {
|
||||
builder.withPassword(password.toCharArray());
|
||||
}
|
||||
|
||||
RedisClient client = RedisClient.create(builder.build());
|
||||
|
||||
StatefulRedisPubSubConnection<String, String> subscribeConnection = client.connectPubSub();
|
||||
this.subscribeSync = subscribeConnection.sync();
|
||||
|
||||
StatefulRedisPubSubConnection<String, String> publishConnection = client.connectPubSub();
|
||||
this.publishSync = publishConnection.sync();
|
||||
|
||||
subscribeConnection.addListener((RedisListener)(channel, message) -> {
|
||||
ByteArrayDataInput input = ByteStreams.newDataInput(Base64.getDecoder().decode(message));
|
||||
|
||||
UUID messageUUID = new UUID(input.readLong(), input.readLong());
|
||||
|
||||
if (messageUUID.equals(serverUUID)) {
|
||||
return;
|
||||
}
|
||||
|
||||
UUID uuid = new UUID(input.readLong(), input.readLong());
|
||||
|
||||
this.receiveMessage(uuid, channel, input);
|
||||
});
|
||||
}
|
||||
|
||||
private void receiveMessage(UUID uuid, String key, ByteArrayDataInput value) {
|
||||
ChatUser user = carbonChat.getUserService().wrapIfLoaded(uuid);
|
||||
|
||||
if (user != null) {
|
||||
for (Map.Entry<String, BiConsumer<ChatUser, ByteArrayDataInput>> listener : userLoadedListeners.entrySet()) {
|
||||
if (key.equals(listener.getKey())) {
|
||||
listener.getValue().accept(user, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (Map.Entry<String, BiConsumer<UUID, ByteArrayDataInput>> listener : userNotLoadedListeners.entrySet()) {
|
||||
if (key.equals(listener.getKey())) {
|
||||
listener.getValue().accept(uuid, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerUserMessageListener(String key, BiConsumer<ChatUser, ByteArrayDataInput> listener) {
|
||||
userLoadedListeners.put(key, listener);
|
||||
subscribeSync.subscribe(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerUUIDMessageListener(String key, BiConsumer<UUID, ByteArrayDataInput> listener) {
|
||||
userNotLoadedListeners.put(key, listener);
|
||||
subscribeSync.subscribe(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unregisterMessageListener(String key) {
|
||||
userLoadedListeners.remove(key);
|
||||
userNotLoadedListeners.remove(key);
|
||||
|
||||
subscribeSync.unsubscribe(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendMessage(String key, UUID uuid, Consumer<ByteArrayDataOutput> consumer) {
|
||||
ByteArrayDataOutput msg = ByteStreams.newDataOutput();
|
||||
|
||||
msg.writeLong(serverUUID.getMostSignificantBits());
|
||||
msg.writeLong(serverUUID.getLeastSignificantBits());
|
||||
msg.writeLong(uuid.getMostSignificantBits());
|
||||
msg.writeLong(uuid.getLeastSignificantBits());
|
||||
|
||||
consumer.accept(msg);
|
||||
|
||||
publishSync.publish(key, Base64.getEncoder().encodeToString(msg.toByteArray()));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
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();
|
||||
|
||||
String getNickname();
|
||||
void setNickname(String nickname, boolean fromRemote);
|
||||
default void setNickname(String nickname) {
|
||||
this.setNickname(nickname, false);
|
||||
}
|
||||
|
||||
ChatChannel getSelectedChannel();
|
||||
void setSelectedChannel(ChatChannel channel, boolean fromRemote);
|
||||
default void setSelectedChannel(ChatChannel channel) {
|
||||
this.setSelectedChannel(channel, false);
|
||||
}
|
||||
|
||||
void clearSelectedChannel();
|
||||
|
||||
UserChannelSettings getChannelSettings(ChatChannel channel);
|
||||
|
||||
boolean isSpyingWhispers();
|
||||
void setSpyingWhispers(boolean spyingWhispers, boolean fromRemote);
|
||||
default void setSpyingWhispers(boolean spyingWhispers) {
|
||||
this.setSpyingWhispers(spyingWhispers, false);
|
||||
}
|
||||
|
||||
boolean isMuted();
|
||||
void setMuted(boolean muted, boolean fromRemote);
|
||||
default void setMuted(boolean muted) {
|
||||
this.setMuted(muted, false);
|
||||
}
|
||||
|
||||
boolean isShadowMuted();
|
||||
void setShadowMuted(boolean shadowMuted, boolean fromRemote);
|
||||
default void setShadowMuted(boolean shadowMuted) {
|
||||
this.setShadowMuted(shadowMuted, false);
|
||||
}
|
||||
|
||||
@Nullable UUID getReplyTarget();
|
||||
void setReplyTarget(@Nullable UUID target, boolean fromRemote);
|
||||
default void setReplyTarget(@Nullable UUID target) {
|
||||
this.setReplyTarget(target, false);
|
||||
}
|
||||
|
||||
default void setReplyTarget(@Nullable ChatUser user, boolean fromRemote) {
|
||||
this.setReplyTarget(user.getUUID(), fromRemote);
|
||||
}
|
||||
default void setReplyTarget(@Nullable ChatUser user) {
|
||||
this.setReplyTarget(user.getUUID(), false);
|
||||
}
|
||||
|
||||
boolean isIgnoringUser(UUID uuid);
|
||||
void setIgnoringUser(UUID uuid, boolean ignoring, boolean fromRemote);
|
||||
default void setIgnoringUser(UUID uuid, boolean ignoring) {
|
||||
this.setIgnoringUser(uuid, ignoring, false);
|
||||
}
|
||||
|
||||
default boolean isIgnoringUser(ChatUser user) {
|
||||
return this.isIgnoringUser(user.getUUID());
|
||||
}
|
||||
default void setIgnoringUser(ChatUser user, boolean ignoring, boolean fromRemote) {
|
||||
this.setIgnoringUser(user.getUUID(), ignoring, fromRemote);
|
||||
}
|
||||
default void setIgnoringUser(ChatUser user, boolean ignoring) {
|
||||
this.setIgnoringUser(user.getUUID(), ignoring, false);
|
||||
}
|
||||
|
||||
void sendMessage(ChatUser sender, String message);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
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 fromRemote);
|
||||
default void setSpying(boolean spying) {
|
||||
this.setSpying(spying, false);
|
||||
}
|
||||
|
||||
boolean isIgnored();
|
||||
void setIgnoring(boolean ignored, boolean fromRemote);
|
||||
default void setIgnoring(boolean ignored) {
|
||||
this.setIgnoring(ignored, false);
|
||||
}
|
||||
|
||||
@Nullable TextColor getColor();
|
||||
void setColor(@Nullable TextColor color, boolean fromRemote);
|
||||
default void setColor(@Nullable TextColor color) {
|
||||
this.setColor(color, false);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package net.draycia.carbon.storage;
|
||||
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public interface UserService {
|
||||
|
||||
ChatUser wrap(String name);
|
||||
ChatUser wrap(OfflinePlayer player);
|
||||
ChatUser wrap(UUID uuid);
|
||||
|
||||
@Nullable
|
||||
default ChatUser wrapIfLoaded(OfflinePlayer player) {
|
||||
return wrapIfLoaded(player.getUniqueId());
|
||||
}
|
||||
|
||||
@Nullable
|
||||
ChatUser wrapIfLoaded(UUID uuid);
|
||||
|
||||
ChatUser refreshUser(UUID uuid);
|
||||
|
||||
void onDisable();
|
||||
|
||||
void invalidate(ChatUser user);
|
||||
void validate(ChatUser user);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,334 @@
|
||||
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 String nickname = null;
|
||||
|
||||
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 String getNickname() {
|
||||
return nickname;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setNickname(String nickname, boolean fromRemote) {
|
||||
if (nickname != null) {
|
||||
Component component = carbonChat.getAdventureManager().processMessage(nickname);
|
||||
nickname = CarbonChat.LEGACY.serialize(component);
|
||||
}
|
||||
|
||||
final String newNickname = nickname;
|
||||
this.nickname = newNickname;
|
||||
|
||||
if (isOnline()) {
|
||||
this.asPlayer().setDisplayName(newNickname);
|
||||
|
||||
if (carbonChat.getConfig().getBoolean("nicknames-set-tab-name")) {
|
||||
this.asPlayer().setPlayerListName(newNickname);
|
||||
}
|
||||
}
|
||||
|
||||
if (!fromRemote) {
|
||||
if (nickname == null) {
|
||||
carbonChat.getMessageManager().sendMessage("nickname-reset", this.getUUID(), (byteArray) -> {});
|
||||
} else {
|
||||
carbonChat.getMessageManager().sendMessage("nickname", this.getUUID(), (byteArray) -> {
|
||||
byteArray.writeUTF(newNickname);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChatChannel getSelectedChannel() {
|
||||
return carbonChat.getChannelManager().getChannelOrDefault(selectedChannel);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSelectedChannel(ChatChannel chatChannel, boolean fromRemote) {
|
||||
String failureMessage = chatChannel.getSwitchFailureMessage();
|
||||
|
||||
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();
|
||||
|
||||
if (!fromRemote) {
|
||||
carbonChat.getMessageManager().sendMessage("selected-channel", this.getUUID(), (byteArray) -> {
|
||||
byteArray.writeUTF(chatChannel.getKey());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearSelectedChannel() {
|
||||
setSelectedChannel(carbonChat.getChannelManager().getDefaultChannel());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isIgnoringUser(UUID uuid) {
|
||||
return ignoredUsers.contains(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setIgnoringUser(UUID uuid, boolean ignoring, boolean fromRemote) {
|
||||
if (ignoring) {
|
||||
ignoredUsers.add(uuid);
|
||||
} else {
|
||||
ignoredUsers.remove(uuid);
|
||||
}
|
||||
|
||||
if (!fromRemote) {
|
||||
carbonChat.getMessageManager().sendMessage("ignoring-user", this.getUUID(), (byteArray) -> {
|
||||
byteArray.writeLong(uuid.getMostSignificantBits());
|
||||
byteArray.writeLong(uuid.getLeastSignificantBits());
|
||||
byteArray.writeBoolean(ignoring);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public List<UUID> getIgnoredUsers() {
|
||||
return ignoredUsers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setMuted(boolean muted, boolean fromRemote) {
|
||||
this.muted = muted;
|
||||
|
||||
if (!fromRemote) {
|
||||
carbonChat.getMessageManager().sendMessage("muted", this.getUUID(), (byteArray) -> {
|
||||
byteArray.writeBoolean(muted);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isMuted() {
|
||||
return muted;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setShadowMuted(boolean shadowMuted, boolean fromRemote) {
|
||||
this.shadowMuted = shadowMuted;
|
||||
|
||||
if (!fromRemote) {
|
||||
carbonChat.getMessageManager().sendMessage("shadow-muted", this.getUUID(), (byteArray) -> {
|
||||
byteArray.writeBoolean(shadowMuted);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isShadowMuted() {
|
||||
return shadowMuted;
|
||||
}
|
||||
|
||||
@CheckForNull
|
||||
@Override
|
||||
public UUID getReplyTarget() {
|
||||
return replyTarget;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setReplyTarget(UUID target, boolean fromRemote) {
|
||||
this.replyTarget = target;
|
||||
|
||||
if (!fromRemote) {
|
||||
carbonChat.getMessageManager().sendMessage("reply-target", this.getUUID(), (byteArray) -> {
|
||||
byteArray.writeLong(target.getMostSignificantBits());
|
||||
byteArray.writeLong(target.getLeastSignificantBits());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public UserChannelSettings getChannelSettings(ChatChannel channel) {
|
||||
return channelSettings.computeIfAbsent(channel.getKey(), (name) -> {
|
||||
return new SimpleUserChannelSettings(uuid, channel.getKey());
|
||||
});
|
||||
}
|
||||
|
||||
public Map<String, ? extends UserChannelSettings> getChannelSettings() {
|
||||
return channelSettings;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSpyingWhispers(boolean spyingWhispers, boolean fromRemote) {
|
||||
this.spyingWhispers = spyingWhispers;
|
||||
|
||||
if (!fromRemote) {
|
||||
carbonChat.getMessageManager().sendMessage("spying-whispers", this.getUUID(), (byteArray) -> {
|
||||
byteArray.writeBoolean(spyingWhispers);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSpyingWhispers() {
|
||||
return spyingWhispers;
|
||||
}
|
||||
|
||||
public void sendMessage(ChatUser sender, String message) {
|
||||
if (isIgnoringUser(sender) || sender.isIgnoringUser(this)) {
|
||||
return;
|
||||
}
|
||||
|
||||
String toPlayerFormat = carbonChat.getLanguage().getString("message-to-other");
|
||||
String fromPlayerFormat = carbonChat.getLanguage().getString("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);
|
||||
|
||||
PrivateMessageEvent event = new PrivateMessageEvent(sender, this, toPlayerComponent, fromPlayerComponent, message);
|
||||
|
||||
Bukkit.getPluginManager().callEvent(event);
|
||||
|
||||
if (event.isCancelled()) {
|
||||
return;
|
||||
}
|
||||
|
||||
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()) {
|
||||
sender.sendMessage(toPlayerComponent);
|
||||
|
||||
// TODO: make sure this works lol
|
||||
carbonChat.getMessageManager().sendMessage("whisper-component", sender.getUUID(), (byteArray) -> {
|
||||
byteArray.writeLong(this.getUUID().getMostSignificantBits());
|
||||
byteArray.writeLong(this.getUUID().getLeastSignificantBits());
|
||||
byteArray.writeUTF(carbonChat.getAdventureManager().getAudiences().gsonSerializer().serialize(fromPlayerComponent));
|
||||
});
|
||||
}
|
||||
|
||||
for (Player player : Bukkit.getOnlinePlayers()) {
|
||||
ChatUser user = carbonChat.getUserService().wrap(player);
|
||||
|
||||
if (!user.isSpyingWhispers()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (user.getUUID().equals(sender.getUUID()) || user.getUUID().equals(getUUID())) {
|
||||
continue;
|
||||
}
|
||||
|
||||
user.sendMessage(carbonChat.getAdventureManager().processMessage(carbonChat.getLanguage().getString("spy-whispers"),
|
||||
"br", "\n", "message", message,
|
||||
"targetname", targetOfflineName, "sendername", senderOfflineName,
|
||||
"target", targetName, "sender", senderName));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
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 org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
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;
|
||||
|
||||
public class JSONUserService implements UserService {
|
||||
|
||||
private final CarbonChat carbonChat;
|
||||
|
||||
private final LoadingCache<UUID, CarbonChatUser> userCache = CacheBuilder.newBuilder()
|
||||
.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 void onDisable() {
|
||||
userCache.invalidateAll();
|
||||
userCache.cleanUp();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChatUser wrap(String name) {
|
||||
Player player = Bukkit.getPlayer(name);
|
||||
|
||||
if (player != null) {
|
||||
return wrap(player);
|
||||
}
|
||||
|
||||
return wrap(Bukkit.getOfflinePlayer(name));
|
||||
}
|
||||
|
||||
@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
|
||||
@Nullable
|
||||
public ChatUser wrapIfLoaded(UUID uuid) {
|
||||
return userCache.getIfPresent(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChatUser refreshUser(UUID uuid) {
|
||||
userCache.invalidate(uuid);
|
||||
|
||||
return this.wrap(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invalidate(ChatUser user) {
|
||||
userCache.invalidate(user.getUUID());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void validate(ChatUser user) {
|
||||
userCache.put(user.getUUID(), (CarbonChatUser)user);
|
||||
}
|
||||
|
||||
private CarbonChatUser loadUser(UUID uuid) {
|
||||
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,239 @@
|
||||
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 org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
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;
|
||||
|
||||
public class MySQLUserService implements UserService {
|
||||
|
||||
private final LoadingCache<UUID, CarbonChatUser> userCache = CacheBuilder.newBuilder()
|
||||
.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");
|
||||
|
||||
if (section == null) {
|
||||
throw new IllegalStateException("Missing Database Credentials!");
|
||||
}
|
||||
|
||||
String username = section.getString("username", "username");
|
||||
String password = section.getString("password", "password");
|
||||
String dbname = section.getString("database", "0");
|
||||
String hostandport = section.getString("hostname", "hostname") + ":" + section.getString("port", "0");
|
||||
|
||||
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," +
|
||||
"nickname VARCHAR(512))");
|
||||
|
||||
// Ignore the exception, it's just saying the column already exists
|
||||
try {
|
||||
database.executeUpdate("ALTER TABLE sc_users ADD COLUMN spyingwhispers BOOLEAN DEFAULT false");
|
||||
database.executeUpdate("ALTER TABLE sc_users ADD COLUMN nickname VARCHAR(512) 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();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
userCache.invalidateAll();
|
||||
userCache.cleanUp();
|
||||
database.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChatUser wrap(String name) {
|
||||
Player player = Bukkit.getPlayer(name);
|
||||
|
||||
if (player != null) {
|
||||
return wrap(player);
|
||||
}
|
||||
|
||||
return wrap(Bukkit.getOfflinePlayer(name));
|
||||
}
|
||||
|
||||
@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
|
||||
@Nullable
|
||||
public ChatUser wrapIfLoaded(UUID uuid) {
|
||||
return userCache.getIfPresent(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChatUser refreshUser(UUID uuid) {
|
||||
userCache.invalidate(uuid);
|
||||
|
||||
return this.wrap(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invalidate(ChatUser user) {
|
||||
userCache.invalidate(user.getUUID());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void validate(ChatUser user) {
|
||||
userCache.put(user.getUUID(), (CarbonChatUser)user);
|
||||
}
|
||||
|
||||
private CarbonChatUser loadUser(UUID uuid) {
|
||||
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, true);
|
||||
}
|
||||
|
||||
String nickname = users.getString("nickname");
|
||||
|
||||
if (nickname != null) {
|
||||
user.setNickname(nickname, true);
|
||||
}
|
||||
|
||||
user.setMuted(users.<Boolean>get("muted"), true);
|
||||
user.setShadowMuted(users.<Boolean>get("shadowmuted"), true);
|
||||
user.setSpyingWhispers(users.<Boolean>get("spyingwhispers"), true);
|
||||
|
||||
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"), true);
|
||||
settings.setIgnoring(channelSetting.<Boolean>get("ignored"), true);
|
||||
|
||||
String color = channelSetting.getString("color");
|
||||
|
||||
if (color != null) {
|
||||
settings.setColor(TextColor.fromHexString(color), true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (DbRow ignoredUser : ignoredUsers) {
|
||||
user.setIgnoringUser(UUID.fromString(ignoredUser.getString("user")), true, 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().getKey();
|
||||
}
|
||||
|
||||
carbonChat.getLogger().info("Saving user data!");
|
||||
stm.executeUpdateQuery("INSERT INTO sc_users (uuid, channel, muted, shadowmuted, spyingwhispers, nickname) VALUES (?, ?, ?, ?, ?, ?) " +
|
||||
"ON DUPLICATE KEY UPDATE channel = ?, muted = ?, shadowmuted = ?, spyingwhispers = ?, nickname =?",
|
||||
user.getUUID().toString(), selectedName, user.isMuted(), user.isShadowMuted(), user.isSpyingWhispers(), user.getNickname(),
|
||||
selectedName, user.isMuted(), user.isShadowMuted(), user.isSpyingWhispers(), user.getNickname());
|
||||
|
||||
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;
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
package net.draycia.carbon.storage.impl;
|
||||
|
||||
import net.draycia.carbon.CarbonChat;
|
||||
import net.draycia.carbon.storage.UserChannelSettings;
|
||||
import net.kyori.adventure.text.format.TextColor;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public class SimpleUserChannelSettings implements UserChannelSettings {
|
||||
|
||||
private boolean spying;
|
||||
private boolean ignored;
|
||||
private String color;
|
||||
|
||||
private final UUID uuid;
|
||||
private final String channel;
|
||||
|
||||
private transient CarbonChat carbonChat;
|
||||
|
||||
public SimpleUserChannelSettings(UUID uuid, String channel) {
|
||||
this.uuid = uuid;
|
||||
this.channel = channel;
|
||||
|
||||
carbonChat = (CarbonChat)Bukkit.getPluginManager().getPlugin("CarbonChat");
|
||||
}
|
||||
|
||||
private CarbonChatUser getUser() {
|
||||
return (CarbonChatUser)carbonChat.getUserService().wrap(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSpying() {
|
||||
return this.spying;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSpying(boolean spying, boolean fromRemote) {
|
||||
this.spying = spying;
|
||||
|
||||
if (!fromRemote) {
|
||||
carbonChat.getMessageManager().sendMessage("spying-channel", uuid, (byteArray) -> {
|
||||
byteArray.writeUTF(channel);
|
||||
byteArray.writeBoolean(spying);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isIgnored() {
|
||||
return ignored;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setIgnoring(boolean ignored, boolean fromRemote) {
|
||||
this.ignored = ignored;
|
||||
|
||||
if (!fromRemote) {
|
||||
carbonChat.getMessageManager().sendMessage("ignoring-channel", uuid, (byteArray) -> {
|
||||
byteArray.writeUTF(channel);
|
||||
byteArray.writeBoolean(ignored);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable TextColor getColor() {
|
||||
if (color == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return TextColor.fromHexString(color);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setColor(@Nullable TextColor color, boolean fromRemote) {
|
||||
if (color == null) {
|
||||
this.color = null;
|
||||
} else {
|
||||
this.color = color.asHexString();
|
||||
}
|
||||
|
||||
if (!fromRemote) {
|
||||
carbonChat.getMessageManager().sendMessage("channel-color", uuid, (byteArray) -> {
|
||||
byteArray.writeUTF(color.asHexString());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
package net.draycia.carbon.util;
|
||||
|
||||
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
|
||||
import net.draycia.carbon.CarbonChat;
|
||||
import net.draycia.carbon.channels.ChatChannel;
|
||||
import net.draycia.carbon.storage.ChatUser;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class CarbonPlaceholders extends PlaceholderExpansion {
|
||||
|
||||
private final CarbonChat carbonChat;
|
||||
|
||||
public CarbonPlaceholders(CarbonChat carbonChat) {
|
||||
this.carbonChat = carbonChat;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getIdentifier() {
|
||||
return "carbonchat";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAuthor() {
|
||||
return "Draycia (Vicarious#0001)";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getVersion() {
|
||||
return "1.0.0";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean persist() {
|
||||
return true;
|
||||
}
|
||||
|
||||
public String onPlaceholderRequest(final Player player, final String identifier) {
|
||||
String key = identifier.toLowerCase();
|
||||
|
||||
if (key.startsWith("can_use_")) {
|
||||
String value = key.replace("can_use_", "");
|
||||
|
||||
ChatChannel channel = carbonChat.getChannelManager().getRegistry().get(value);
|
||||
|
||||
if (channel == null) {
|
||||
return "false";
|
||||
}
|
||||
|
||||
ChatUser user = carbonChat.getUserService().wrap(player);
|
||||
|
||||
return channel.canPlayerUse(user) ? "true" : "false";
|
||||
} else if (key.startsWith("can_see_")) {
|
||||
String value = key.replace("can_see_", "");
|
||||
|
||||
ChatChannel channel = carbonChat.getChannelManager().getRegistry().get(value);
|
||||
|
||||
if (channel == null) {
|
||||
return "false";
|
||||
}
|
||||
|
||||
ChatUser user = carbonChat.getUserService().wrap(player);
|
||||
|
||||
return channel.canPlayerSee(user, true) ? "true" : "false";
|
||||
} else if (key.startsWith("ignoring_channel_")) {
|
||||
String value = key.replace("ignoring_channel_", "");
|
||||
|
||||
ChatChannel channel = carbonChat.getChannelManager().getRegistry().get(value);
|
||||
|
||||
if (channel == null) {
|
||||
return "false";
|
||||
}
|
||||
|
||||
ChatUser user = carbonChat.getUserService().wrap(player);
|
||||
|
||||
return user.getChannelSettings(channel).isIgnored() ? "true" : "false";
|
||||
} else if (key.startsWith("selected_channel")) {
|
||||
ChatChannel channel = carbonChat.getUserService().wrap(player).getSelectedChannel();
|
||||
|
||||
return channel == null ? carbonChat.getChannelManager().getDefaultChannel().getName() : channel.getName();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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,25 @@
|
||||
package net.draycia.carbon.util;
|
||||
|
||||
import io.lettuce.core.pubsub.RedisPubSubListener;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface RedisListener extends RedisPubSubListener<String, String> {
|
||||
|
||||
void message(String channel, String message);
|
||||
|
||||
@Override
|
||||
default void message(String pattern, String channel, String message) { }
|
||||
|
||||
@Override
|
||||
default void subscribed(String channel, long count) { }
|
||||
|
||||
@Override
|
||||
default void psubscribed(String pattern, long count) { }
|
||||
|
||||
@Override
|
||||
default void unsubscribed(String channel, long count) { }
|
||||
|
||||
@Override
|
||||
default void punsubscribed(String pattern, long count) { }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
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);
|
||||
|
||||
/**
|
||||
* Clears all registered entries from the registry
|
||||
*/
|
||||
void clearAll();
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user