mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-01 22:15:08 +00:00
Begin (broken) modularization
This commit is contained in:
1 parent
b3a0d71ba8
commit
ad8089855c
56 files changed
+553
-369
No files matched your search
@@ -1,167 +0,0 @@
|
||||
package net.draycia.simplechat;
|
||||
|
||||
import net.draycia.simplechat.channels.ChatChannel;
|
||||
import net.draycia.simplechat.listeners.*;
|
||||
import net.draycia.simplechat.managers.*;
|
||||
import net.draycia.simplechat.storage.UserService;
|
||||
import net.draycia.simplechat.storage.impl.JSONUserService;
|
||||
import net.draycia.simplechat.storage.impl.MySQLUserService;
|
||||
import net.draycia.simplechat.util.ItemStackUtils;
|
||||
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
|
||||
import net.milkbowl.vault.permission.Permission;
|
||||
import org.bukkit.plugin.PluginManager;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public final class SimpleChat extends JavaPlugin {
|
||||
|
||||
private ArrayList<ChatChannel> channels = new ArrayList<>();
|
||||
private ArrayList<Pattern> itemPatterns = new ArrayList<>();
|
||||
|
||||
private Permission permission;
|
||||
|
||||
private PluginMessageManager pluginMessageManager;
|
||||
private CommandManager commandManager;
|
||||
private ChannelManager channelManager;
|
||||
private RedisManager redisManager = null;
|
||||
|
||||
private UserService userService;
|
||||
|
||||
private ItemStackUtils itemStackUtils;
|
||||
|
||||
private BukkitAudiences audiences;
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
// Setup Adventure
|
||||
audiences = BukkitAudiences.create(this);
|
||||
|
||||
try {
|
||||
itemStackUtils = new ItemStackUtils();
|
||||
} catch (ClassNotFoundException | NoSuchMethodException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
// Setup vault and permissions
|
||||
permission = getServer().getServicesManager().getRegistration(Permission.class).getProvider();
|
||||
|
||||
// Ensure config is present to be modified by the user
|
||||
saveDefaultConfig();
|
||||
|
||||
// Initialize managers
|
||||
pluginMessageManager = new PluginMessageManager(this);
|
||||
commandManager = new CommandManager(this);
|
||||
channelManager = new ChannelManager(this);
|
||||
|
||||
if (getConfig().getBoolean("redis.enabled")) {
|
||||
redisManager = new RedisManager(this);
|
||||
}
|
||||
|
||||
String storageType = getConfig().getString("storage.type");
|
||||
|
||||
if (storageType.equalsIgnoreCase("mysql")) {
|
||||
userService = new MySQLUserService(this);
|
||||
} else if (storageType.equalsIgnoreCase("json")) {
|
||||
userService = new JSONUserService(this);
|
||||
} else {
|
||||
getLogger().warning("Invalid storage type selected! Falling back to JSON.");
|
||||
userService = new JSONUserService(this);
|
||||
}
|
||||
|
||||
// Setup listeners
|
||||
setupListeners();
|
||||
reloadPatterns();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
userService.cleanUp();
|
||||
}
|
||||
|
||||
private void setupListeners() {
|
||||
PluginManager pluginManager = getServer().getPluginManager();
|
||||
|
||||
// Register chat listeners
|
||||
pluginManager.registerEvents(new BukkitChatListener(this), this);
|
||||
pluginManager.registerEvents(new FilterHandler(this), this);
|
||||
pluginManager.registerEvents(new ItemLinkHandler(this), this);
|
||||
pluginManager.registerEvents(new LegacyFormatHandler(), this);
|
||||
pluginManager.registerEvents(new MuteHandler(), this);
|
||||
pluginManager.registerEvents(new OfflineNameHandler(), this);
|
||||
pluginManager.registerEvents(new PingHandler(this), this);
|
||||
pluginManager.registerEvents(new PlaceholderHandler(this), this);
|
||||
pluginManager.registerEvents(new ShadowMuteHandler(), this);
|
||||
pluginManager.registerEvents(new EmptyChatHandler(this), this);
|
||||
pluginManager.registerEvents(new CapsHandler(this), this);
|
||||
}
|
||||
|
||||
public void reloadPatterns() {
|
||||
itemPatterns.clear();
|
||||
|
||||
for (String entry : getConfig().getStringList("item-link-placeholders")) {
|
||||
itemPatterns.add(Pattern.compile(Pattern.quote(entry)));
|
||||
}
|
||||
}
|
||||
|
||||
public ChatChannel getDefaultChannel() {
|
||||
for (ChatChannel channel : channels) {
|
||||
if (channel.isDefault()) {
|
||||
return channel;
|
||||
}
|
||||
}
|
||||
|
||||
return channels.get(0);
|
||||
}
|
||||
|
||||
public ChatChannel getChannel(String name) {
|
||||
if (name == null) {
|
||||
return getDefaultChannel();
|
||||
}
|
||||
|
||||
for (ChatChannel chatChannel : channels) {
|
||||
if (chatChannel.getName().equalsIgnoreCase(name)) {
|
||||
return chatChannel;
|
||||
}
|
||||
}
|
||||
|
||||
return getDefaultChannel();
|
||||
}
|
||||
|
||||
public List<ChatChannel> getChannels() {
|
||||
return channels;
|
||||
}
|
||||
|
||||
public Permission getPermission() {
|
||||
return permission;
|
||||
}
|
||||
|
||||
public CommandManager getCommandManager() {
|
||||
return commandManager;
|
||||
}
|
||||
|
||||
public ChannelManager getChannelManager() {
|
||||
return channelManager;
|
||||
}
|
||||
|
||||
public PluginMessageManager getPluginMessageManager() {
|
||||
return pluginMessageManager;
|
||||
}
|
||||
|
||||
public UserService getUserService() {
|
||||
return userService;
|
||||
}
|
||||
|
||||
public BukkitAudiences getAudiences() {
|
||||
return audiences;
|
||||
}
|
||||
|
||||
public ItemStackUtils getItemStackUtils() {
|
||||
return itemStackUtils;
|
||||
}
|
||||
|
||||
public RedisManager getRedisManager() {
|
||||
return redisManager;
|
||||
}
|
||||
}
|
||||
@@ -1,131 +0,0 @@
|
||||
package net.draycia.simplechat.channels;
|
||||
|
||||
import net.draycia.simplechat.SimpleChat;
|
||||
import net.draycia.simplechat.events.ChatFormatEvent;
|
||||
import net.draycia.simplechat.storage.ChatUser;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.format.TextColor;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public abstract class ChatChannel {
|
||||
|
||||
/**
|
||||
* @return The color that represents this channel. Optionally used in formatting.
|
||||
*/
|
||||
public abstract TextColor getColor();
|
||||
|
||||
/**
|
||||
* @return The MiniMessage styled format for the group in this channel.
|
||||
*/
|
||||
public abstract String getFormat(String group);
|
||||
|
||||
/**
|
||||
* @return If this is the default (typically Global) channel players use when they're in no other channel.
|
||||
*/
|
||||
public abstract boolean isDefault();
|
||||
|
||||
/**
|
||||
* @return If this channel can be toggled off and if players can ignore player messages in this channel.
|
||||
*/
|
||||
public abstract boolean isIgnorable();
|
||||
|
||||
public abstract boolean shouldBungee();
|
||||
|
||||
/**
|
||||
* @return The name of this channel.
|
||||
*/
|
||||
public abstract String getName();
|
||||
|
||||
/**
|
||||
* @return The distance other players must be within to the sender to see messages in this channel.
|
||||
*/
|
||||
public abstract double getDistance();
|
||||
|
||||
/**
|
||||
* @return The message to be sent to the player when switching to this channel.
|
||||
*/
|
||||
public abstract String getSwitchMessage();
|
||||
|
||||
public abstract String getSwitchOtherMessage();
|
||||
|
||||
/**
|
||||
* @return The message to be send to the player when toggling this channel off.
|
||||
*/
|
||||
|
||||
public abstract String getToggleOffMessage();
|
||||
|
||||
/**
|
||||
* @return The message to be send to the player when toggling this channel on.
|
||||
*/
|
||||
|
||||
public abstract String getToggleOnMessage();
|
||||
|
||||
public abstract String getToggleOtherOnMessage();
|
||||
|
||||
public abstract String getToggleOtherOffMessage();
|
||||
|
||||
public abstract boolean filterEnabled();
|
||||
|
||||
/**
|
||||
* @return If this channel is Towny's town chat.
|
||||
*/
|
||||
public boolean isTownChat() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return If this channel is Towny's nation chat.
|
||||
*/
|
||||
public boolean isNationChat() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return If this channel is a Towny alliance chat.
|
||||
*/
|
||||
public boolean isAllianceChat() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return If this channel is mcMMO's party chat.
|
||||
*/
|
||||
public boolean isPartyChat() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public abstract boolean firstMatchingGroup();
|
||||
|
||||
/**
|
||||
* @return If the player can use this channel.
|
||||
*/
|
||||
public abstract boolean canPlayerUse(ChatUser user);
|
||||
|
||||
public abstract boolean canPlayerSee(ChatUser sender, ChatUser target, boolean checkSpying);
|
||||
|
||||
public abstract List<ChatUser> getAudience(ChatUser user);
|
||||
|
||||
/**
|
||||
* @return If the channel should forward its formatting / formatted message to other servers
|
||||
*/
|
||||
public boolean shouldForwardFormatting() {
|
||||
return true;
|
||||
}
|
||||
|
||||
public abstract List<Pattern> getItemLinkPatterns();
|
||||
|
||||
/**
|
||||
* Parses the specified message, calls a {@link ChatFormatEvent}, and sends the message to everyone who can view this channel.
|
||||
* @param user The player who is saying the message.
|
||||
* @param message The message to be sent.
|
||||
*/
|
||||
public abstract void sendMessage(ChatUser user, String message, boolean fromBungee);
|
||||
|
||||
public abstract void sendComponent(ChatUser user, Component component);
|
||||
|
||||
public String processPlaceholders(ChatUser user, String input) { return input; }
|
||||
|
||||
}
|
||||
@@ -1,56 +0,0 @@
|
||||
package net.draycia.simplechat.channels.impls;
|
||||
|
||||
import com.palmergames.bukkit.towny.TownyAPI;
|
||||
import com.palmergames.bukkit.towny.event.NationRemoveTownEvent;
|
||||
import com.palmergames.bukkit.towny.event.TownRemoveResidentEvent;
|
||||
import com.palmergames.bukkit.towny.exceptions.NotRegisteredException;
|
||||
import com.palmergames.bukkit.towny.object.Resident;
|
||||
import net.draycia.simplechat.SimpleChat;
|
||||
import net.draycia.simplechat.storage.ChatUser;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
|
||||
public class AllianceChatChannel extends NationChatChannel implements Listener {
|
||||
|
||||
public AllianceChatChannel(String name, SimpleChat simpleChat) {
|
||||
super(name, simpleChat);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canPlayerSee(ChatUser sender, ChatUser target, boolean checkSpying) {
|
||||
if (checkSpying && target.asPlayer().hasPermission("simplechat.spy." + getName())) {
|
||||
if (target.getChannelSettings(this).isSpying()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if (super.canPlayerSee(sender, target, false) && sender != null) {
|
||||
try {
|
||||
Resident resident = TownyAPI.getInstance().getDataSource().getResident(target.asPlayer().getName());
|
||||
Resident targetResident = TownyAPI.getInstance().getDataSource().getResident(sender.asPlayer().getName());
|
||||
|
||||
if (resident.isAlliedWith(targetResident)) {
|
||||
return true;
|
||||
}
|
||||
} catch (NotRegisteredException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAllianceChat() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isNationChat() {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,125 +0,0 @@
|
||||
package net.draycia.simplechat.channels.impls;
|
||||
|
||||
import com.palmergames.bukkit.towny.TownyAPI;
|
||||
import com.palmergames.bukkit.towny.event.NationRemoveTownEvent;
|
||||
import com.palmergames.bukkit.towny.event.TownRemoveResidentEvent;
|
||||
import com.palmergames.bukkit.towny.exceptions.NotRegisteredException;
|
||||
import com.palmergames.bukkit.towny.object.Nation;
|
||||
import com.palmergames.bukkit.towny.object.Resident;
|
||||
import net.draycia.simplechat.SimpleChat;
|
||||
import net.draycia.simplechat.storage.ChatUser;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
|
||||
public class NationChatChannel extends SimpleChatChannel implements Listener {
|
||||
|
||||
public NationChatChannel(String name, SimpleChat simpleChat) {
|
||||
super(name, simpleChat);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String processPlaceholders(ChatUser user, String input) {
|
||||
return input.replace("<nation>", getNation(user));
|
||||
}
|
||||
|
||||
private String getNation(ChatUser user) {
|
||||
try {
|
||||
Resident resident = TownyAPI.getInstance().getDataSource().getResident(user.asPlayer().getName());
|
||||
|
||||
if (resident.hasNation()) {
|
||||
return resident.getTown().getNation().getFormattedName();
|
||||
}
|
||||
} catch (NotRegisteredException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canPlayerSee(ChatUser sender, ChatUser target, boolean checkSpying) {
|
||||
if (checkSpying && target.asPlayer().hasPermission("simplechat.spy." + getName())) {
|
||||
if (target.getChannelSettings(this).isSpying()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if (super.canPlayerSee(sender, target, false) && sender != null) {
|
||||
try {
|
||||
Resident resident = TownyAPI.getInstance().getDataSource().getResident(sender.asPlayer().getName());
|
||||
|
||||
if (resident.hasNation()) {
|
||||
Nation nation = resident.getTown().getNation();
|
||||
|
||||
if (nation.hasResident(target.asPlayer().getName())) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
} catch (NotRegisteredException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canPlayerUse(ChatUser user) {
|
||||
return super.canPlayerUse(user) && isInNation(user);
|
||||
}
|
||||
|
||||
public boolean isInNation(ChatUser user) {
|
||||
try {
|
||||
return TownyAPI.getInstance().getDataSource().getResident(user.asPlayer().getName()).hasNation();
|
||||
} catch (NotRegisteredException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onResidentRemove(TownRemoveResidentEvent event) {
|
||||
String name = event.getResident().getName();
|
||||
ChatUser user = getSimpleChat().getUserService().wrap(Bukkit.getOfflinePlayer(name));
|
||||
|
||||
if (user.getSelectedChannel().isAllianceChat()) {
|
||||
user.clearSelectedChannel();
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onNationRemoveTown(NationRemoveTownEvent event) {
|
||||
for (Resident resident : event.getTown().getResidents()) {
|
||||
OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(resident.getName());
|
||||
|
||||
if (!offlinePlayer.isOnline()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
ChatUser user = getSimpleChat().getUserService().wrap(offlinePlayer);
|
||||
|
||||
if (user.getSelectedChannel().isAllianceChat()) {
|
||||
user.clearSelectedChannel();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerJoin(PlayerJoinEvent event) {
|
||||
ChatUser user = getSimpleChat().getUserService().wrap(event.getPlayer());
|
||||
|
||||
if (user.getSelectedChannel().isAllianceChat() && !isInNation(user)) {
|
||||
user.clearSelectedChannel();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isNationChat() {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,82 +0,0 @@
|
||||
package net.draycia.simplechat.channels.impls;
|
||||
|
||||
import com.gmail.nossr50.api.PartyAPI;
|
||||
import com.gmail.nossr50.events.party.McMMOPartyChangeEvent;
|
||||
import net.draycia.simplechat.SimpleChat;
|
||||
import net.draycia.simplechat.storage.ChatUser;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
|
||||
public class PartyChatChannel extends SimpleChatChannel implements Listener {
|
||||
|
||||
public PartyChatChannel(String name, SimpleChat simpleChat) {
|
||||
super(name, simpleChat);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String processPlaceholders(ChatUser user, String input) {
|
||||
String partyName = PartyAPI.getPartyName(user.asPlayer());
|
||||
|
||||
if (partyName == null) {
|
||||
partyName = "NotLoaded";
|
||||
}
|
||||
|
||||
return input.replace("<party>", partyName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canPlayerSee(ChatUser sender, ChatUser target, boolean checkSpying) {
|
||||
if (checkSpying && target.asPlayer().hasPermission("simplechat.spy." + getName())) {
|
||||
if (target.getChannelSettings(this).isSpying()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if (super.canPlayerSee(sender, target, false) && sender != null) {
|
||||
return PartyAPI.inSameParty(sender.asPlayer(), target.asPlayer());
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canPlayerUse(ChatUser user) {
|
||||
return super.canPlayerUse(user) && isInParty(user);
|
||||
}
|
||||
|
||||
private McMMOPartyChangeEvent.EventReason LEFT = McMMOPartyChangeEvent.EventReason.LEFT_PARTY;
|
||||
private McMMOPartyChangeEvent.EventReason KICKED = McMMOPartyChangeEvent.EventReason.KICKED_FROM_PARTY;
|
||||
|
||||
@EventHandler
|
||||
public void onPartyLeave(McMMOPartyChangeEvent event) {
|
||||
if (event.getReason() != LEFT && event.getReason() != KICKED) {
|
||||
return;
|
||||
}
|
||||
|
||||
ChatUser user = getSimpleChat().getUserService().wrap(event.getPlayer());
|
||||
|
||||
if (user.getSelectedChannel().isPartyChat()) {
|
||||
user.clearSelectedChannel();
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerJoin(PlayerJoinEvent event) {
|
||||
ChatUser user = getSimpleChat().getUserService().wrap(event.getPlayer());
|
||||
|
||||
if (user.getSelectedChannel().isPartyChat() && !isInParty(user)) {
|
||||
user.clearSelectedChannel();
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isInParty(ChatUser user) {
|
||||
return PartyAPI.inParty(user.asPlayer());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPartyChat() {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,388 +0,0 @@
|
||||
package net.draycia.simplechat.channels.impls;
|
||||
|
||||
import net.draycia.simplechat.SimpleChat;
|
||||
import net.draycia.simplechat.channels.ChatChannel;
|
||||
import net.draycia.simplechat.events.ChatComponentEvent;
|
||||
import net.draycia.simplechat.events.ChatFormatEvent;
|
||||
import net.draycia.simplechat.storage.ChatUser;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
import net.kyori.adventure.text.format.TextColor;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class SimpleChatChannel extends ChatChannel {
|
||||
|
||||
private String key;
|
||||
|
||||
private SimpleChat simpleChat;
|
||||
|
||||
private SimpleChatChannel() { }
|
||||
|
||||
public SimpleChatChannel(String key, SimpleChat simpleChat) {
|
||||
this.key = key;
|
||||
this.simpleChat = simpleChat;
|
||||
}
|
||||
|
||||
SimpleChat getSimpleChat() {
|
||||
return simpleChat;
|
||||
}
|
||||
|
||||
private String getDiscordFormatting() {
|
||||
return getFormats().getOrDefault("discord-to-mc", "<gray>[<blue>Discord<gray>] <username><white>: <message>");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canPlayerUse(ChatUser user) {
|
||||
return user.asPlayer().hasPermission("simplechat.channels." + getName() + ".use");
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ChatUser> getAudience(ChatUser user) {
|
||||
List<ChatUser> audience = new ArrayList<>();
|
||||
|
||||
for (Player player : Bukkit.getOnlinePlayers()) {
|
||||
if (canPlayerSee(user, simpleChat.getUserService().wrap(player), false)) {
|
||||
audience.add(simpleChat.getUserService().wrap(player));
|
||||
}
|
||||
}
|
||||
|
||||
return audience;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendMessage(ChatUser user, String message, boolean fromBungee) {
|
||||
// Get player's formatting
|
||||
String messageFormat = getFormat(user);
|
||||
|
||||
// Call custom chat event
|
||||
ChatFormatEvent formatEvent = new ChatFormatEvent(user, this, messageFormat, message);
|
||||
|
||||
Bukkit.getPluginManager().callEvent(formatEvent);
|
||||
|
||||
if (formatEvent.isCancelled()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Get formatted message
|
||||
TextComponent formattedMessage = (TextComponent)MiniMessage.get().parse(formatEvent.getFormat(),
|
||||
"br", "\n",
|
||||
"color", "<color:" + getColor().toString() + ">",
|
||||
"phase", Long.toString(System.currentTimeMillis() % 25),
|
||||
"server", simpleChat.getConfig().getString("server-name", "Server"),
|
||||
"message", formatEvent.getMessage());
|
||||
|
||||
// Call custom chat event
|
||||
ChatComponentEvent componentEvent = new ChatComponentEvent(user, this, formattedMessage, formatEvent.getMessage(), getAudience(user));
|
||||
|
||||
Bukkit.getPluginManager().callEvent(componentEvent);
|
||||
|
||||
// Send message as normal
|
||||
for (Player onlinePlayer : Bukkit.getOnlinePlayers()) {
|
||||
ChatUser chatUser = simpleChat.getUserService().wrap(onlinePlayer);
|
||||
|
||||
TextColor userColor = chatUser.getChannelSettings(this).getColor();
|
||||
|
||||
String prefix;
|
||||
|
||||
if (isNationChat()) {
|
||||
prefix = simpleChat.getConfig().getString("spy-prefix-nation");
|
||||
} else if (isTownChat()) {
|
||||
prefix = simpleChat.getConfig().getString("spy-prefix-town");
|
||||
} else if (isPartyChat()) {
|
||||
prefix = simpleChat.getConfig().getString("spy-prefix-mcmmo");
|
||||
} else {
|
||||
prefix = simpleChat.getConfig().getString("spy-prefix");
|
||||
}
|
||||
|
||||
prefix = processPlaceholders(user, prefix);
|
||||
|
||||
if (userColor == null) {
|
||||
if (isUserSpying(user, chatUser)) {
|
||||
prefix = prefix.replace("<color>", "<color:" + getColor() + ">");
|
||||
|
||||
chatUser.sendMessage(MiniMessage.get().parse(prefix).append(componentEvent.getComponent()));
|
||||
} else if (componentEvent.getRecipients().contains(chatUser)) {
|
||||
chatUser.sendMessage(componentEvent.getComponent());
|
||||
}
|
||||
} else {
|
||||
prefix = prefix.replace("<color>", "<color:" + chatUser.getChannelSettings(this).getColor().asHexString() + ">");
|
||||
String format = formatEvent.getFormat();
|
||||
|
||||
TextComponent newFormat = (TextComponent)MiniMessage.get().parse(format,
|
||||
"br", "\n",
|
||||
"color", "<color:" + userColor.toString() + ">",
|
||||
"phase", Long.toString(System.currentTimeMillis() % 25),
|
||||
"server", simpleChat.getConfig().getString("server-name", "Server"),
|
||||
"message", formatEvent.getMessage());
|
||||
|
||||
if (isUserSpying(user, chatUser)) {
|
||||
newFormat = (TextComponent)MiniMessage.get().parse(prefix, "br", "\n")
|
||||
.append(newFormat);
|
||||
} else if (!componentEvent.getRecipients().contains(chatUser)) {
|
||||
return;
|
||||
}
|
||||
|
||||
ChatComponentEvent newEvent = new ChatComponentEvent(user, this, newFormat, formatEvent.getMessage(), Collections.singletonList(chatUser));
|
||||
|
||||
Bukkit.getPluginManager().callEvent(newEvent);
|
||||
|
||||
chatUser.sendMessage(newEvent.getComponent());
|
||||
}
|
||||
}
|
||||
|
||||
// Log message to console
|
||||
String sm = user.isShadowMuted() ? "[SM] " : "";
|
||||
System.out.println(sm + LegacyComponentSerializer.legacy().serialize(componentEvent.getComponent()));
|
||||
|
||||
// Route message to bungee / discord (if message originates from this server)
|
||||
// Use instanceof and not isOnline, if this message originates from another then the instanceof will
|
||||
// fail, but isOnline may succeed if the player is online on both servers (somehow).
|
||||
if (user.isOnline() && fromBungee) {
|
||||
if (shouldForwardFormatting() && shouldBungee()) {
|
||||
sendMessageToBungee(user.asPlayer(), componentEvent.getComponent());
|
||||
} else if (shouldBungee()) {
|
||||
sendMessageToBungee(user.asPlayer(), message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isUserSpying(ChatUser sender, ChatUser target) {
|
||||
if (!canPlayerSee(sender, target, false)) {
|
||||
if (target.getChannelSettings(this).isSpying()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendComponent(ChatUser player, Component component) {
|
||||
for (ChatUser user : getAudience(player)) {
|
||||
user.sendMessage(component);
|
||||
}
|
||||
|
||||
System.out.println(LegacyComponentSerializer.legacy().serialize(component));
|
||||
}
|
||||
|
||||
public void sendMessageToBungee(Player player, Component component) {
|
||||
simpleChat.getPluginMessageManager().sendComponent(this, player, component);
|
||||
}
|
||||
|
||||
public void sendMessageToBungee(Player player, String message) {
|
||||
simpleChat.getPluginMessageManager().sendMessage(this, player, message);
|
||||
}
|
||||
|
||||
private String getFormat(ChatUser user) {
|
||||
String group = "default";
|
||||
|
||||
if (!firstMatchingGroup()) {
|
||||
if (user.isOnline()) {
|
||||
group = simpleChat.getPermission().getPrimaryGroup(user.asPlayer());
|
||||
} else {
|
||||
group = simpleChat.getPermission().getPrimaryGroup(null, user.asOfflinePlayer());
|
||||
}
|
||||
} else {
|
||||
String[] groups;
|
||||
|
||||
if (user.isOnline()) {
|
||||
groups = simpleChat.getPermission().getPlayerGroups(user.asPlayer());
|
||||
} else {
|
||||
groups = simpleChat.getPermission().getPlayerGroups(null, user.asOfflinePlayer());
|
||||
}
|
||||
|
||||
for (String entry : groups) {
|
||||
if (getFormats().containsKey(entry.toLowerCase())) {
|
||||
group = entry;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return getFormat(group);
|
||||
}
|
||||
|
||||
public boolean canPlayerSee(ChatUser sender, ChatUser target, boolean checkSpying) {
|
||||
Player targetPlayer = target.asPlayer();
|
||||
|
||||
if (checkSpying && targetPlayer.hasPermission("simplechat.spy." + getName())) {
|
||||
if (target.getChannelSettings(this).isSpying()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!targetPlayer.hasPermission("simplechat.channels." + getName() + ".see")) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (sender.isOnline()) {
|
||||
if (getDistance() == 0) {
|
||||
if (!targetPlayer.getWorld().equals(sender.asPlayer().getWorld())) {
|
||||
return false;
|
||||
}
|
||||
} else if (getDistance() > 0) {
|
||||
Location one = targetPlayer.getLocation();
|
||||
Location two = sender.asPlayer().getLocation();
|
||||
|
||||
if (!one.getWorld().equals(two.getWorld())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (one.distance(two) > getDistance()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (isIgnorable()) {
|
||||
if (target.isIgnoringUser(sender) && !targetPlayer.hasPermission("simplechat.ignore.exempt")) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (target.getChannelSettings(this).isIgnored()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public Map<String, String> getFormats() {
|
||||
HashMap<String, String> formats = new HashMap<>();
|
||||
|
||||
ConfigurationSection section = simpleChat.getConfig().getConfigurationSection("channels").getConfigurationSection(getKey());
|
||||
ConfigurationSection formatSection = section.getConfigurationSection("formats");
|
||||
|
||||
if (formatSection == null) {
|
||||
formatSection = simpleChat.getConfig().getConfigurationSection("default").getConfigurationSection("formats");
|
||||
}
|
||||
|
||||
for (String key : formatSection.getKeys(false)) {
|
||||
formats.put(key, formatSection.getString(key));
|
||||
}
|
||||
|
||||
return formats;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TextColor getColor() {
|
||||
return TextColor.fromHexString((String) getSetting("color"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFormat(String group) {
|
||||
return getFormats().getOrDefault(group, getFormats().getOrDefault("default", "<white><%player_displayname%<white>> <message>"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDefault() {
|
||||
return (Boolean) getSetting("default");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isIgnorable() {
|
||||
return (Boolean)getSetting("ignorable");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean shouldBungee() {
|
||||
return (Boolean) getSetting("should-bungee");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
String name = (String) getSetting("name");
|
||||
return name == null ? key : name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getDistance() {
|
||||
return (Integer) getSetting("distance");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSwitchMessage() {
|
||||
return (String) getSetting("switch-message");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSwitchOtherMessage() {
|
||||
return (String) getSetting("switch-other-message");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getToggleOffMessage() {
|
||||
return (String) getSetting("toggle-off-message");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getToggleOnMessage() {
|
||||
return (String) getSetting("toggle-on-message");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getToggleOtherOnMessage() {
|
||||
return (String) getSetting("toggle-other-on");
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getToggleOtherOffMessage() {
|
||||
return (String) getSetting("toggle-other-off");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean shouldForwardFormatting() {
|
||||
return (Boolean) getSetting("forward-format");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean filterEnabled() {
|
||||
return (Boolean) getSetting("filter-enabled");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean firstMatchingGroup() {
|
||||
return (Boolean) getSetting("first-matching-group");
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Pattern> getItemLinkPatterns() {
|
||||
ArrayList<Pattern> itemPatterns = new ArrayList<>();
|
||||
|
||||
for (String entry : simpleChat.getConfig().getStringList("item-link-placeholders")) {
|
||||
itemPatterns.add(Pattern.compile(Pattern.quote(entry)));
|
||||
}
|
||||
|
||||
return itemPatterns;
|
||||
}
|
||||
|
||||
private Object getSetting(String key) {
|
||||
ConfigurationSection section = simpleChat.getConfig().getConfigurationSection("channels").getConfigurationSection(getKey());
|
||||
|
||||
if (section.contains(key)) {
|
||||
return section.get(key);
|
||||
}
|
||||
|
||||
ConfigurationSection defaultSection = simpleChat.getConfig().getConfigurationSection("default");
|
||||
|
||||
if (defaultSection.contains(key)) {
|
||||
return defaultSection.get(key);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private String getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,110 +0,0 @@
|
||||
package net.draycia.simplechat.channels.impls;
|
||||
|
||||
import com.palmergames.bukkit.towny.TownyAPI;
|
||||
import com.palmergames.bukkit.towny.event.TownAddResidentEvent;
|
||||
import com.palmergames.bukkit.towny.event.TownRemoveResidentEvent;
|
||||
import com.palmergames.bukkit.towny.exceptions.NotRegisteredException;
|
||||
import com.palmergames.bukkit.towny.object.Resident;
|
||||
import net.draycia.simplechat.SimpleChat;
|
||||
import net.draycia.simplechat.storage.ChatUser;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
|
||||
public class TownChatChannel extends SimpleChatChannel implements Listener {
|
||||
|
||||
public TownChatChannel(String name, SimpleChat simpleChat) {
|
||||
super(name, simpleChat);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String processPlaceholders(ChatUser user, String input) {
|
||||
return input.replace("<town>", getTown(user));
|
||||
}
|
||||
|
||||
private String getTown(ChatUser user) {
|
||||
try {
|
||||
Resident resident = TownyAPI.getInstance().getDataSource().getResident(user.asPlayer().getName());
|
||||
|
||||
if (resident.hasTown()) {
|
||||
return resident.getTown().getFormattedName();
|
||||
}
|
||||
} catch (NotRegisteredException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canPlayerSee(ChatUser sender, ChatUser target, boolean checkSpying) {
|
||||
if (checkSpying && target.asPlayer().hasPermission("simplechat.spy." + getName())) {
|
||||
if (target.getChannelSettings(this).isSpying()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if (super.canPlayerSee(sender, target, false) && sender != null) {
|
||||
try {
|
||||
Resident resident = TownyAPI.getInstance().getDataSource().getResident(target.asPlayer().getName());
|
||||
|
||||
if (resident.hasTown()) {
|
||||
return resident.getTown().hasResident(sender.asPlayer().getName());
|
||||
}
|
||||
} catch (NotRegisteredException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canPlayerUse(ChatUser user) {
|
||||
if (super.canPlayerUse(user)) {
|
||||
try {
|
||||
return TownyAPI.getInstance().getDataSource().getResident(user.asPlayer().getName()).hasTown();
|
||||
} catch (NotRegisteredException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isInTown(ChatUser user) {
|
||||
try {
|
||||
return TownyAPI.getInstance().getDataSource().getResident(user.asPlayer().getName()).hasTown();
|
||||
} catch (NotRegisteredException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onResidentRemove(TownRemoveResidentEvent event) {
|
||||
String name = event.getResident().getName();
|
||||
ChatUser user = getSimpleChat().getUserService().wrap(Bukkit.getOfflinePlayer(name));
|
||||
|
||||
if (user.getSelectedChannel().isTownChat()) {
|
||||
user.clearSelectedChannel();
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerJoin(PlayerJoinEvent event) {
|
||||
ChatUser user = getSimpleChat().getUserService().wrap(event.getPlayer());
|
||||
|
||||
if (user.getSelectedChannel().isTownChat() && !isInTown(user)) {
|
||||
user.clearSelectedChannel();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isTownChat() {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,63 +0,0 @@
|
||||
package net.draycia.simplechat.commands;
|
||||
|
||||
import co.aikar.commands.BaseCommand;
|
||||
import co.aikar.commands.ConditionFailedException;
|
||||
import co.aikar.commands.annotation.*;
|
||||
import net.draycia.simplechat.SimpleChat;
|
||||
import net.draycia.simplechat.channels.ChatChannel;
|
||||
import net.draycia.simplechat.channels.impls.AllianceChatChannel;
|
||||
import net.draycia.simplechat.channels.impls.NationChatChannel;
|
||||
import net.draycia.simplechat.channels.impls.PartyChatChannel;
|
||||
import net.draycia.simplechat.channels.impls.TownChatChannel;
|
||||
import net.draycia.simplechat.storage.ChatUser;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
@CommandAlias("%channelName")
|
||||
@CommandPermission("simplechat.switch")
|
||||
public class AliasedChannelCommand extends BaseCommand {
|
||||
|
||||
private SimpleChat simpleChat;
|
||||
private ChatChannel chatChannel;
|
||||
|
||||
public AliasedChannelCommand(SimpleChat simpleChat, ChatChannel chatChannel) {
|
||||
this.simpleChat = simpleChat;
|
||||
this.chatChannel = chatChannel;
|
||||
}
|
||||
|
||||
@Default
|
||||
public void baseCommand(Player player, @Optional String[] args) {
|
||||
ChatUser user = simpleChat.getUserService().wrap(player);
|
||||
|
||||
if (!chatChannel.canPlayerUse(user)) {
|
||||
if (chatChannel.isTownChat() && !((TownChatChannel)chatChannel).isInTown(user)) {
|
||||
throw new ConditionFailedException(simpleChat.getConfig().getString("language.town-cannot-use"));
|
||||
} else if (chatChannel.isNationChat() && !((NationChatChannel)chatChannel).isInNation(user)) {
|
||||
throw new ConditionFailedException(simpleChat.getConfig().getString("language.nation-cannot-use"));
|
||||
} else if (chatChannel.isAllianceChat() && !((AllianceChatChannel)chatChannel).isInNation(user)) {
|
||||
throw new ConditionFailedException(simpleChat.getConfig().getString("alliance-cannot-use"));
|
||||
} else if (chatChannel.isPartyChat() && !((PartyChatChannel)chatChannel).isInParty(user)) {
|
||||
throw new ConditionFailedException(simpleChat.getConfig().getString("party-cannot-use"));
|
||||
} else {
|
||||
throw new ConditionFailedException(simpleChat.getConfig().getString("cannot-use-channel"));
|
||||
}
|
||||
}
|
||||
|
||||
if (args == null || args.length == 0) {
|
||||
user.setSelectedChannel(getChatChannel());
|
||||
|
||||
user.sendMessage(MiniMessage.get().parse(
|
||||
getChatChannel().getSwitchMessage(), "color", "<" + getChatChannel().getColor().toString() + ">",
|
||||
"channel", getChatChannel().getName()));
|
||||
} else {
|
||||
Bukkit.getScheduler().scheduleAsyncDelayedTask(simpleChat, () -> {
|
||||
getChatChannel().sendMessage(user, String.join(" ", args), false);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public ChatChannel getChatChannel() {
|
||||
return chatChannel;
|
||||
}
|
||||
}
|
||||
@@ -1,60 +0,0 @@
|
||||
package net.draycia.simplechat.commands;
|
||||
|
||||
import co.aikar.commands.BaseCommand;
|
||||
import co.aikar.commands.annotation.*;
|
||||
import net.draycia.simplechat.SimpleChat;
|
||||
import net.draycia.simplechat.channels.ChatChannel;
|
||||
import net.draycia.simplechat.storage.ChatUser;
|
||||
import net.draycia.simplechat.storage.UserChannelSettings;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
@CommandAlias("ch|channel|switch")
|
||||
@CommandPermission("simplechat.switch")
|
||||
public class ChannelCommand extends BaseCommand {
|
||||
|
||||
private SimpleChat simpleChat;
|
||||
|
||||
public ChannelCommand(SimpleChat simpleChat) {
|
||||
this.simpleChat = simpleChat;
|
||||
}
|
||||
|
||||
@Default
|
||||
@CommandCompletion("@chatchannel")
|
||||
public void baseCommand(Player player, @Conditions("canuse:true") ChatChannel channel, @Optional String[] args) {
|
||||
ChatUser user = simpleChat.getUserService().wrap(player);
|
||||
|
||||
if (args == null || args.length == 0) {
|
||||
user.setSelectedChannel(channel);
|
||||
|
||||
user.sendMessage(MiniMessage.get().parse(channel.getSwitchMessage(),
|
||||
"br", "\n",
|
||||
"color", "<" + channel.getColor().toString() + ">",
|
||||
"channel", channel.getName()));
|
||||
} else {
|
||||
Bukkit.getScheduler().scheduleAsyncDelayedTask(simpleChat, () -> {
|
||||
channel.sendMessage(user, String.join(" ", args), false);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@CommandPermission("simplechat.switch.others")
|
||||
@Subcommand("other")
|
||||
@CommandCompletion("@chatchannel @players")
|
||||
public void baseCommand(CommandSender sender, ChatChannel channel, ChatUser user) {
|
||||
user.setSelectedChannel(channel);
|
||||
|
||||
String message = channel.getSwitchMessage();
|
||||
String otherMessage = channel.getSwitchOtherMessage();
|
||||
|
||||
user.sendMessage(MiniMessage.get().parse(message, "br", "\n",
|
||||
"color", "<color:" + channel.getColor().toString() + ">", "channel", channel.getName()));
|
||||
|
||||
simpleChat.getAudiences().audience(sender).sendMessage(MiniMessage.get().parse(otherMessage, "br", "\n",
|
||||
"color", "<color:" + channel.getColor().toString() + ">", "channel", channel.getName(),
|
||||
"player", user.asOfflinePlayer().getName()));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
package net.draycia.simplechat.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 net.draycia.simplechat.SimpleChat;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
@CommandAlias("chatreload|creload")
|
||||
@CommandPermission("simplechat.reload")
|
||||
public class ChatReloadCommand extends BaseCommand {
|
||||
|
||||
private SimpleChat simpleChat;
|
||||
|
||||
public ChatReloadCommand(SimpleChat simpleChat) {
|
||||
this.simpleChat = simpleChat;
|
||||
}
|
||||
|
||||
@Default
|
||||
public void baseCommand(CommandSender sender) {
|
||||
simpleChat.reloadConfig();
|
||||
simpleChat.getChannelManager().reloadChannels();
|
||||
simpleChat.reloadPatterns();
|
||||
|
||||
Component message = MiniMessage.get().parse(simpleChat.getConfig().getString("language.reloaded"),
|
||||
"br", "\n");
|
||||
|
||||
simpleChat.getAudiences().audience(sender).sendMessage(message);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,55 +0,0 @@
|
||||
package net.draycia.simplechat.commands;
|
||||
|
||||
import co.aikar.commands.BaseCommand;
|
||||
import co.aikar.commands.CommandIssuer;
|
||||
import co.aikar.commands.annotation.CommandAlias;
|
||||
import co.aikar.commands.annotation.CommandPermission;
|
||||
import co.aikar.commands.annotation.Default;
|
||||
import net.draycia.simplechat.SimpleChat;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
@CommandAlias("clearchat|cc")
|
||||
@CommandPermission("simplechat.clearchat.clear")
|
||||
public class ClearChatCommand extends BaseCommand {
|
||||
|
||||
private SimpleChat simpleChat;
|
||||
|
||||
public ClearChatCommand(SimpleChat simpleChat) {
|
||||
this.simpleChat = simpleChat;
|
||||
}
|
||||
|
||||
@Default
|
||||
public void baseCommand(CommandIssuer issuer) {
|
||||
String format = simpleChat.getConfig().getString("clear-chat-message", "");
|
||||
Component component = MiniMessage.get().parse(format, "br", "\n");
|
||||
|
||||
for (Player player : Bukkit.getOnlinePlayers()) {
|
||||
if (player.hasPermission("simplechat.clearchat.exempt")) {
|
||||
continue;
|
||||
}
|
||||
|
||||
Audience audience = simpleChat.getAudiences().player(player);
|
||||
|
||||
for (int i = 0; i < simpleChat.getConfig().getInt("clear-chat-amount", 100); i++) {
|
||||
audience.sendMessage(component);
|
||||
}
|
||||
|
||||
String name = issuer.isPlayer() ? ((Player)issuer).getName() : "Console";
|
||||
|
||||
if (player.hasPermission("simplechat.clearchat.notify")) {
|
||||
String message = simpleChat.getConfig().getString("language.clear-notify");
|
||||
audience.sendMessage(MiniMessage.get().parse(message, "player", name));
|
||||
}
|
||||
|
||||
if (player.hasPermission("simplechat.clearchat.exempt")) {
|
||||
String message = simpleChat.getConfig().getString("language.clear-exempt");
|
||||
audience.sendMessage(MiniMessage.get().parse(message, "player", name));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
package net.draycia.simplechat.commands;
|
||||
|
||||
import co.aikar.commands.BaseCommand;
|
||||
import co.aikar.commands.annotation.CommandAlias;
|
||||
import co.aikar.commands.annotation.CommandCompletion;
|
||||
import co.aikar.commands.annotation.CommandPermission;
|
||||
import co.aikar.commands.annotation.Default;
|
||||
import net.draycia.simplechat.SimpleChat;
|
||||
import net.draycia.simplechat.storage.ChatUser;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
@CommandAlias("ignore")
|
||||
@CommandPermission("simplechat.ignore")
|
||||
public class IgnoreCommand extends BaseCommand {
|
||||
|
||||
private SimpleChat simpleChat;
|
||||
|
||||
public IgnoreCommand(SimpleChat simpleChat) {
|
||||
this.simpleChat = simpleChat;
|
||||
}
|
||||
|
||||
@Default
|
||||
@CommandCompletion("@players")
|
||||
public void baseCommand(Player player, ChatUser targetUser) {
|
||||
ChatUser user = simpleChat.getUserService().wrap(player);
|
||||
|
||||
String message;
|
||||
|
||||
if (user.isIgnoringUser(targetUser)) {
|
||||
user.setIgnoringUser(targetUser, false);
|
||||
message = simpleChat.getConfig().getString("language.not-ignoring-user");
|
||||
} else {
|
||||
user.setIgnoringUser(targetUser, true);
|
||||
message = simpleChat.getConfig().getString("language.ignoring-user");
|
||||
}
|
||||
|
||||
user.sendMessage(MiniMessage.get().parse(message, "br", "\n"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
package net.draycia.simplechat.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 me.clip.placeholderapi.PlaceholderAPI;
|
||||
import net.draycia.simplechat.SimpleChat;
|
||||
import net.draycia.simplechat.storage.ChatUser;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
@CommandAlias("me|rp")
|
||||
@CommandPermission("simplechat.me")
|
||||
public class MeCommand extends BaseCommand {
|
||||
|
||||
private SimpleChat simpleChat;
|
||||
|
||||
public MeCommand(SimpleChat simpleChat) {
|
||||
this.simpleChat = simpleChat;
|
||||
}
|
||||
|
||||
@Default
|
||||
public void baseCommand(Player player, String... args) {
|
||||
String message = String.join(" ", args).replace("</pre>", "");
|
||||
String format = PlaceholderAPI.setPlaceholders(player, simpleChat.getConfig().getString("language.me"));
|
||||
Component component = MiniMessage.get().parse(format, "br", "\n", "message", message);
|
||||
|
||||
ChatUser user = simpleChat.getUserService().wrap(player);
|
||||
|
||||
if (user.isShadowMuted()) {
|
||||
user.sendMessage(component);
|
||||
} else {
|
||||
for (Player onlinePlayer : Bukkit.getOnlinePlayers()) {
|
||||
if (simpleChat.getUserService().wrap(onlinePlayer).isIgnoringUser(user)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
simpleChat.getAudiences().player(onlinePlayer).sendMessage(component);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
package net.draycia.simplechat.commands;
|
||||
|
||||
import co.aikar.commands.BaseCommand;
|
||||
import co.aikar.commands.annotation.CommandAlias;
|
||||
import co.aikar.commands.annotation.CommandCompletion;
|
||||
import co.aikar.commands.annotation.CommandPermission;
|
||||
import co.aikar.commands.annotation.Default;
|
||||
import net.draycia.simplechat.SimpleChat;
|
||||
import net.draycia.simplechat.storage.ChatUser;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
@CommandAlias("msg|whisper|message|w")
|
||||
@CommandPermission("simplechat.message")
|
||||
public class MessageCommand extends BaseCommand {
|
||||
|
||||
private SimpleChat simpleChat;
|
||||
|
||||
public MessageCommand(SimpleChat simpleChat) {
|
||||
this.simpleChat = simpleChat;
|
||||
}
|
||||
|
||||
@Default
|
||||
@CommandCompletion("@players")
|
||||
public void baseCommand(Player player, ChatUser targetUser, String... args) {
|
||||
if (args.length == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
ChatUser sender = simpleChat.getUserService().wrap(player);
|
||||
targetUser.sendMessage(sender, String.join(" ", args));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
package net.draycia.simplechat.commands;
|
||||
|
||||
import co.aikar.commands.BaseCommand;
|
||||
import co.aikar.commands.annotation.CommandAlias;
|
||||
import co.aikar.commands.annotation.CommandCompletion;
|
||||
import co.aikar.commands.annotation.CommandPermission;
|
||||
import co.aikar.commands.annotation.Default;
|
||||
import net.draycia.simplechat.SimpleChat;
|
||||
import net.draycia.simplechat.storage.ChatUser;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
@CommandAlias("mute")
|
||||
@CommandPermission("simplechat.mute")
|
||||
public class MuteCommand extends BaseCommand {
|
||||
|
||||
private SimpleChat simpleChat;
|
||||
|
||||
public MuteCommand(SimpleChat simpleChat) {
|
||||
this.simpleChat = simpleChat;
|
||||
}
|
||||
|
||||
@Default
|
||||
@CommandCompletion("@players")
|
||||
public void baseCommand(CommandSender sender, ChatUser targetUser) {
|
||||
String format;
|
||||
|
||||
if (targetUser.isMuted()) {
|
||||
targetUser.setMuted(true);
|
||||
format = simpleChat.getConfig().getString("language.no-longer-muted");
|
||||
} else {
|
||||
targetUser.setMuted(false);
|
||||
format = simpleChat.getConfig().getString("language.is-now-muted");
|
||||
}
|
||||
|
||||
Component message = MiniMessage.get().parse(format, "br", "\n",
|
||||
"user", targetUser.asOfflinePlayer().getName());
|
||||
|
||||
simpleChat.getAudiences().audience(sender).sendMessage(message);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
package net.draycia.simplechat.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 net.draycia.simplechat.SimpleChat;
|
||||
import net.draycia.simplechat.storage.ChatUser;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
@CommandAlias("reply|r")
|
||||
@CommandPermission("simplechat.reply")
|
||||
public class ReplyCommand extends BaseCommand {
|
||||
|
||||
private SimpleChat simpleChat;
|
||||
|
||||
public ReplyCommand(SimpleChat simpleChat) {
|
||||
this.simpleChat = simpleChat;
|
||||
}
|
||||
|
||||
@Default
|
||||
public void baseCommand(Player player, String... args) {
|
||||
if (args.length == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
ChatUser user = simpleChat.getUserService().wrap(player);
|
||||
ChatUser targetUser = simpleChat.getUserService().wrap(user.getReplyTarget());
|
||||
|
||||
if (user.getReplyTarget() == null) {
|
||||
String message = simpleChat.getConfig().getString("language.no-reply-target");
|
||||
Component component = MiniMessage.get().parse(message, "br", "\n");
|
||||
user.sendMessage(component);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
targetUser.sendMessage(user, String.join(" ", args));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
package net.draycia.simplechat.commands;
|
||||
|
||||
import co.aikar.commands.BaseCommand;
|
||||
import co.aikar.commands.ConditionFailedException;
|
||||
import co.aikar.commands.annotation.*;
|
||||
import net.draycia.simplechat.SimpleChat;
|
||||
import net.draycia.simplechat.channels.ChatChannel;
|
||||
import net.draycia.simplechat.storage.ChatUser;
|
||||
import net.draycia.simplechat.storage.UserChannelSettings;
|
||||
import net.kyori.adventure.text.format.TextColor;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
@CommandAlias("setcolor")
|
||||
@CommandPermission("simplechat.setcolor")
|
||||
public class SetColorCommand extends BaseCommand {
|
||||
|
||||
private SimpleChat simpleChat;
|
||||
|
||||
public SetColorCommand(SimpleChat simpleChat) {
|
||||
this.simpleChat = simpleChat;
|
||||
}
|
||||
|
||||
@Default
|
||||
@CommandCompletion("@chatchannel")
|
||||
public void baseCommand(Player player, @Conditions("canuse:true") ChatChannel channel, String color) {
|
||||
// TODO: condition for ChatChannel existing
|
||||
if (channel == null || color == null) {
|
||||
throw new ConditionFailedException("Channel or Color not supplied!");
|
||||
}
|
||||
|
||||
ChatUser user = simpleChat.getUserService().wrap(player);
|
||||
|
||||
// TODO: move into conditions
|
||||
if (!player.hasPermission("simplechat.setcolor." + channel.getName())) {
|
||||
throw new ConditionFailedException("You can't set colors for this channel!");
|
||||
}
|
||||
|
||||
UserChannelSettings settings = user.getChannelSettings(channel);
|
||||
|
||||
settings.setColor(TextColor.fromHexString(color));
|
||||
|
||||
user.sendMessage(MiniMessage.get().parse(simpleChat.getConfig().getString("language.channel-color-set"),
|
||||
"br", "\n", "color", "<color:" + color + ">", "channel", channel.getName(), "hex", color));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
package net.draycia.simplechat.commands;
|
||||
|
||||
import co.aikar.commands.BaseCommand;
|
||||
import co.aikar.commands.annotation.CommandAlias;
|
||||
import co.aikar.commands.annotation.CommandCompletion;
|
||||
import co.aikar.commands.annotation.CommandPermission;
|
||||
import co.aikar.commands.annotation.Default;
|
||||
import net.draycia.simplechat.SimpleChat;
|
||||
import net.draycia.simplechat.storage.ChatUser;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
@CommandAlias("shadowmute|sm|smute")
|
||||
@CommandPermission("simplechat.shadowmute")
|
||||
public class ShadowMuteCommand extends BaseCommand {
|
||||
|
||||
private SimpleChat simpleChat;
|
||||
|
||||
public ShadowMuteCommand(SimpleChat simpleChat) {
|
||||
this.simpleChat = simpleChat;
|
||||
}
|
||||
|
||||
@Default
|
||||
@CommandCompletion("@players")
|
||||
public void baseCommand(CommandSender sender, ChatUser targetUser) {
|
||||
String format;
|
||||
|
||||
if (targetUser.isShadowMuted()) {
|
||||
targetUser.setShadowMuted(false);
|
||||
format = simpleChat.getConfig().getString("language.no-longer-shadow-muted");
|
||||
} else {
|
||||
targetUser.setShadowMuted(true);
|
||||
format = simpleChat.getConfig().getString("language.is-now-shadow-muted");
|
||||
}
|
||||
|
||||
Component message = MiniMessage.get().parse(format, "br", "\n",
|
||||
"user", targetUser.asOfflinePlayer().getName());
|
||||
|
||||
simpleChat.getAudiences().audience(sender).sendMessage(message);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,60 +0,0 @@
|
||||
package net.draycia.simplechat.commands;
|
||||
|
||||
import co.aikar.commands.BaseCommand;
|
||||
import co.aikar.commands.annotation.*;
|
||||
import net.draycia.simplechat.SimpleChat;
|
||||
import net.draycia.simplechat.channels.ChatChannel;
|
||||
import net.draycia.simplechat.storage.ChatUser;
|
||||
import net.draycia.simplechat.storage.UserChannelSettings;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
@CommandAlias("spych|spychannel|spy")
|
||||
@CommandPermission("simplechat.spy")
|
||||
public class SpyChannelCommand extends BaseCommand {
|
||||
|
||||
private SimpleChat simpleChat;
|
||||
|
||||
public SpyChannelCommand(SimpleChat simpleChat) {
|
||||
this.simpleChat = simpleChat;
|
||||
}
|
||||
|
||||
@Default
|
||||
@CommandCompletion("@channel")
|
||||
public void baseCommand(Player player, @Conditions("exists:true") ChatChannel channel) {
|
||||
ChatUser user = simpleChat.getUserService().wrap(player);
|
||||
|
||||
String message;
|
||||
|
||||
UserChannelSettings settings = user.getChannelSettings(channel);
|
||||
|
||||
if (settings.isSpying()) {
|
||||
settings.setSpying(false);
|
||||
message = simpleChat.getConfig().getString("language.spy-toggled-off");
|
||||
} else {
|
||||
settings.setSpying(true);
|
||||
message = simpleChat.getConfig().getString("language.spy-toggled-on");
|
||||
}
|
||||
|
||||
user.sendMessage(MiniMessage.get().parse(message, "br", "\n",
|
||||
"color", "<color:" + channel.getColor().toString() + ">", "channel", channel.getName()));
|
||||
}
|
||||
|
||||
@Subcommand("whispers")
|
||||
public void whispersCommand(Player player) {
|
||||
ChatUser user = simpleChat.getUserService().wrap(player);
|
||||
|
||||
String message;
|
||||
|
||||
if (user.isSpyingWhispers()) {
|
||||
user.setSpyingWhispers(false);
|
||||
message = simpleChat.getConfig().getString("language.spy-whispers-off");
|
||||
} else {
|
||||
user.setSpyingWhispers(true);
|
||||
message = simpleChat.getConfig().getString("language.spy-whispers-on");
|
||||
}
|
||||
|
||||
user.sendMessage(MiniMessage.get().parse(message, "br", "\n"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,70 +0,0 @@
|
||||
package net.draycia.simplechat.commands;
|
||||
|
||||
import co.aikar.commands.BaseCommand;
|
||||
import co.aikar.commands.annotation.*;
|
||||
import net.draycia.simplechat.SimpleChat;
|
||||
import net.draycia.simplechat.channels.ChatChannel;
|
||||
import net.draycia.simplechat.storage.ChatUser;
|
||||
import net.draycia.simplechat.storage.UserChannelSettings;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
@CommandAlias("tch|togglec|togglechannel|toggle")
|
||||
@CommandPermission("simplechat.toggle")
|
||||
public class ToggleCommand extends BaseCommand {
|
||||
|
||||
private SimpleChat simpleChat;
|
||||
|
||||
public ToggleCommand(SimpleChat simpleChat) {
|
||||
this.simpleChat = simpleChat;
|
||||
}
|
||||
|
||||
@Default
|
||||
@CommandCompletion("@chatchannel @players")
|
||||
public void baseCommand(Player player, @Conditions("canuse:true") ChatChannel channel) {
|
||||
ChatUser user = simpleChat.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(MiniMessage.get().parse(message, "br", "\n",
|
||||
"color", "<color:" + channel.getColor().toString() + ">", "channel", channel.getName()));
|
||||
}
|
||||
|
||||
@CommandPermission("simplechat.toggle.others")
|
||||
@Subcommand("other")
|
||||
@CommandCompletion("@chatchannel @players")
|
||||
public void baseCommand(CommandSender sender, @Conditions("canuse:true") ChatChannel channel, ChatUser user) {
|
||||
String message;
|
||||
String otherMessage;
|
||||
|
||||
UserChannelSettings settings = user.getChannelSettings(channel);
|
||||
|
||||
if (settings.isIgnored()) {
|
||||
settings.setIgnoring(false);
|
||||
message = channel.getToggleOffMessage();
|
||||
otherMessage = channel.getToggleOtherOffMessage();
|
||||
} else {
|
||||
settings.setIgnoring(true);
|
||||
message = channel.getToggleOnMessage();
|
||||
otherMessage = channel.getToggleOtherOnMessage();
|
||||
}
|
||||
|
||||
user.sendMessage(MiniMessage.get().parse(message, "br", "\n",
|
||||
"color", "<color:" + channel.getColor().toString() + ">", "channel", channel.getName()));
|
||||
|
||||
simpleChat.getAudiences().audience(sender).sendMessage(MiniMessage.get().parse(otherMessage,
|
||||
"br", "\n", "color", "<color:" + channel.getColor().toString() + ">",
|
||||
"channel", channel.getName(), "player", user.asOfflinePlayer().getName()));
|
||||
}
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
package net.draycia.simplechat.events;
|
||||
|
||||
import net.draycia.simplechat.channels.ChatChannel;
|
||||
import net.draycia.simplechat.storage.ChatUser;
|
||||
import org.bukkit.event.Event;
|
||||
import org.bukkit.event.HandlerList;
|
||||
|
||||
public class ChannelSwitchEvent extends Event {
|
||||
|
||||
/** Bukkit event stuff **/
|
||||
private static final HandlerList handlers = new HandlerList();
|
||||
|
||||
@Override
|
||||
public HandlerList getHandlers() {
|
||||
return handlers;
|
||||
}
|
||||
|
||||
public static HandlerList getHandlerList() {
|
||||
return handlers;
|
||||
}
|
||||
|
||||
/** Relevant stuff **/
|
||||
private ChatChannel channel;
|
||||
private ChatUser user;
|
||||
|
||||
public ChannelSwitchEvent(ChatChannel channel, ChatUser user) {
|
||||
this.channel = channel;
|
||||
this.user = user;
|
||||
}
|
||||
|
||||
public ChatUser getUser() {
|
||||
return user;
|
||||
}
|
||||
|
||||
public ChatChannel getChannel() {
|
||||
return channel;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,81 +0,0 @@
|
||||
package net.draycia.simplechat.events;
|
||||
|
||||
import net.draycia.simplechat.channels.ChatChannel;
|
||||
import net.draycia.simplechat.storage.ChatUser;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
import org.bukkit.event.Cancellable;
|
||||
import org.bukkit.event.Event;
|
||||
import org.bukkit.event.HandlerList;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class ChatComponentEvent extends Event implements Cancellable {
|
||||
|
||||
private static final HandlerList HANDLERS_LIST = new HandlerList();
|
||||
private boolean isCancelled = false;
|
||||
|
||||
private ChatUser user;
|
||||
private ChatChannel chatChannel;
|
||||
private TextComponent component;
|
||||
private String originalMessage;
|
||||
private List<ChatUser> recipients;
|
||||
|
||||
public ChatComponentEvent(ChatUser user, ChatChannel chatChannel, TextComponent component, String originalMessage, List<ChatUser> recipients) {
|
||||
super(true);
|
||||
|
||||
this.user = user;
|
||||
this.chatChannel = chatChannel;
|
||||
this.component = component;
|
||||
this.originalMessage = originalMessage;
|
||||
this.recipients = recipients;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCancelled() {
|
||||
return isCancelled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCancelled(boolean cancelled) {
|
||||
this.isCancelled = cancelled;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public HandlerList getHandlers() {
|
||||
return HANDLERS_LIST;
|
||||
}
|
||||
|
||||
public static HandlerList getHandlerList() {
|
||||
return HANDLERS_LIST;
|
||||
}
|
||||
|
||||
public ChatUser getUser() {
|
||||
return user;
|
||||
}
|
||||
|
||||
public ChatChannel getChatChannel() {
|
||||
return chatChannel;
|
||||
}
|
||||
|
||||
public void setChatChannel(ChatChannel chatChannel) {
|
||||
this.chatChannel = chatChannel;
|
||||
}
|
||||
|
||||
public TextComponent getComponent() {
|
||||
return component;
|
||||
}
|
||||
|
||||
public void setComponent(TextComponent component) {
|
||||
this.component = component;
|
||||
}
|
||||
|
||||
public List<ChatUser> getRecipients() {
|
||||
return recipients;
|
||||
}
|
||||
|
||||
public String getOriginalMessage() {
|
||||
return originalMessage;
|
||||
}
|
||||
}
|
||||
@@ -1,77 +0,0 @@
|
||||
package net.draycia.simplechat.events;
|
||||
|
||||
import net.draycia.simplechat.channels.ChatChannel;
|
||||
import net.draycia.simplechat.storage.ChatUser;
|
||||
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 ChatUser user;
|
||||
private ChatChannel chatChannel;
|
||||
private String format;
|
||||
private String message;
|
||||
|
||||
public ChatFormatEvent(ChatUser user, ChatChannel chatChannel, String format, String message) {
|
||||
super(true);
|
||||
|
||||
this.user = user;
|
||||
this.chatChannel = chatChannel;
|
||||
this.format = format;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCancelled() {
|
||||
return isCancelled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCancelled(boolean cancelled) {
|
||||
this.isCancelled = cancelled;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public HandlerList getHandlers() {
|
||||
return HANDLERS_LIST;
|
||||
}
|
||||
|
||||
public static HandlerList getHandlerList() {
|
||||
return HANDLERS_LIST;
|
||||
}
|
||||
|
||||
public ChatUser getUser() {
|
||||
return user;
|
||||
}
|
||||
|
||||
public ChatChannel getChatChannel() {
|
||||
return chatChannel;
|
||||
}
|
||||
|
||||
public void setChatChannel(ChatChannel chatChannel) {
|
||||
this.chatChannel = chatChannel;
|
||||
}
|
||||
|
||||
public String getFormat() {
|
||||
return format;
|
||||
}
|
||||
|
||||
public void setFormat(String format) {
|
||||
this.format = format;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
package net.draycia.simplechat.events;
|
||||
|
||||
import net.draycia.simplechat.storage.ChatUser;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.bukkit.event.Event;
|
||||
import org.bukkit.event.HandlerList;
|
||||
|
||||
public class PrivateMessageEvent extends Event {
|
||||
|
||||
/** Bukkit event stuff **/
|
||||
private static final HandlerList handlers = new HandlerList();
|
||||
|
||||
@Override
|
||||
public HandlerList getHandlers() {
|
||||
return handlers;
|
||||
}
|
||||
|
||||
public static HandlerList getHandlerList() {
|
||||
return handlers;
|
||||
}
|
||||
|
||||
/** Relevant stuff **/
|
||||
private final ChatUser sender;
|
||||
private final ChatUser target;
|
||||
private final Component senderComponent;
|
||||
private final Component targetComponent;
|
||||
private final String message;
|
||||
|
||||
public PrivateMessageEvent(ChatUser sender, ChatUser target, Component senderComponent, Component targetComponent, String message) {
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
package net.draycia.simplechat.listeners;
|
||||
|
||||
import net.draycia.simplechat.SimpleChat;
|
||||
import net.draycia.simplechat.storage.ChatUser;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.AsyncPlayerChatEvent;
|
||||
|
||||
public class BukkitChatListener implements Listener {
|
||||
|
||||
private SimpleChat simpleChat;
|
||||
|
||||
public BukkitChatListener(SimpleChat simpleChat) {
|
||||
this.simpleChat = simpleChat;
|
||||
}
|
||||
|
||||
// Chat messages
|
||||
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
|
||||
public void onPlayerchat(AsyncPlayerChatEvent event) {
|
||||
event.setCancelled(true);
|
||||
|
||||
ChatUser user = simpleChat.getUserService().wrap(event.getPlayer());
|
||||
|
||||
if (event.isAsynchronous()) {
|
||||
user.getSelectedChannel().sendMessage(user, event.getMessage(), false);
|
||||
} else {
|
||||
Bukkit.getScheduler().scheduleAsyncDelayedTask(simpleChat, () -> {
|
||||
user.getSelectedChannel().sendMessage(user, event.getMessage(), false);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
package net.draycia.simplechat.listeners;
|
||||
|
||||
import net.draycia.simplechat.SimpleChat;
|
||||
import net.draycia.simplechat.events.ChatFormatEvent;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
|
||||
public class CapsHandler implements Listener {
|
||||
|
||||
private SimpleChat simpleChat;
|
||||
|
||||
public CapsHandler(SimpleChat simpleChat) {
|
||||
this.simpleChat = simpleChat;
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.LOW)
|
||||
public void onMessage(ChatFormatEvent event) {
|
||||
if (!simpleChat.getConfig().getBoolean("caps-protection.enabled")) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!(event.getMessage().length() >= simpleChat.getConfig().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 >= simpleChat.getConfig().getDouble("caps-protection.percent-caps"))) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (simpleChat.getConfig().getBoolean("block-message")) {
|
||||
event.setCancelled(true);
|
||||
} else {
|
||||
event.setMessage(event.getMessage().toLowerCase());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
package net.draycia.simplechat.listeners;
|
||||
|
||||
import net.draycia.simplechat.SimpleChat;
|
||||
import net.draycia.simplechat.events.ChatComponentEvent;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
|
||||
public class EmptyChatHandler implements Listener {
|
||||
|
||||
private SimpleChat simpleChat;
|
||||
|
||||
public EmptyChatHandler(SimpleChat simpleChat) {
|
||||
this.simpleChat = simpleChat;
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.MONITOR)
|
||||
public void onComponent(ChatComponentEvent event) {
|
||||
if (event.getRecipients().size() == 1 && event.getUser().equals(event.getRecipients().get(0))) {
|
||||
String message = simpleChat.getConfig().getString("language.empty-channel");
|
||||
|
||||
if (message == null || message.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
event.getUser().sendMessage(MiniMessage.get().parse(message));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
package net.draycia.simplechat.listeners;
|
||||
|
||||
import net.draycia.simplechat.SimpleChat;
|
||||
import net.draycia.simplechat.channels.ChatChannel;
|
||||
import net.draycia.simplechat.events.ChatFormatEvent;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
|
||||
public class FilterHandler implements Listener {
|
||||
|
||||
private SimpleChat simpleChat;
|
||||
|
||||
public FilterHandler(SimpleChat simpleChat) {
|
||||
this.simpleChat = simpleChat;
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.LOWEST)
|
||||
public void onFilter(ChatFormatEvent event) {
|
||||
ChatChannel channel = event.getChatChannel();
|
||||
String filterText = simpleChat.getConfig().getString("filter-text", "****");
|
||||
|
||||
if (channel.filterEnabled() && simpleChat.getConfig().contains("filters")) {
|
||||
for (String entry : simpleChat.getConfig().getStringList("filters")) {
|
||||
event.setMessage(event.getMessage().replaceAll(entry, filterText));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
package net.draycia.simplechat.listeners;
|
||||
|
||||
import net.draycia.simplechat.SimpleChat;
|
||||
import net.draycia.simplechat.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 SimpleChat simpleChat;
|
||||
|
||||
public ItemLinkHandler(SimpleChat simpleChat) {
|
||||
this.simpleChat = simpleChat;
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.LOWEST)
|
||||
public void onItemLink(ChatComponentEvent event) {
|
||||
// Handle item linking placeholders
|
||||
if (event.getUser().isOnline()) {
|
||||
for (Pattern pattern : event.getChatChannel().getItemLinkPatterns()) {
|
||||
String patternContent = pattern.toString().replace("\\Q", "").replace("\\E", "");
|
||||
|
||||
if (event.getOriginalMessage().contains(patternContent)) {
|
||||
TextComponent component = event.getComponent().replace(pattern, (input) -> {
|
||||
return TextComponent.builder().append(simpleChat.getItemStackUtils().createComponent(event.getUser().asPlayer()));
|
||||
});
|
||||
|
||||
event.setComponent(component);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
package net.draycia.simplechat.listeners;
|
||||
|
||||
import net.draycia.simplechat.events.ChatFormatEvent;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
|
||||
public class LegacyFormatHandler implements Listener {
|
||||
|
||||
@EventHandler(priority = EventPriority.LOWEST)
|
||||
public void onLegacyFormat(ChatFormatEvent event) {
|
||||
Component component = LegacyComponentSerializer.legacy('&').deserialize(event.getFormat());
|
||||
event.setFormat(MiniMessage.get().serialize(component));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
package net.draycia.simplechat.listeners;
|
||||
|
||||
import net.draycia.simplechat.events.ChatFormatEvent;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
|
||||
public class MuteHandler implements Listener {
|
||||
|
||||
@EventHandler
|
||||
public void onMute(ChatFormatEvent event) {
|
||||
if (event.getUser().isMuted()) {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
package net.draycia.simplechat.listeners;
|
||||
|
||||
import net.draycia.simplechat.events.ChatFormatEvent;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
|
||||
public class OfflineNameHandler implements Listener {
|
||||
|
||||
@EventHandler(priority = EventPriority.HIGHEST)
|
||||
public void onOfflineMessage(ChatFormatEvent event) {
|
||||
// If the player isn't online (cross server message), use their normal name
|
||||
if (!event.getUser().isOnline()) {
|
||||
event.setFormat(event.getFormat().replace("%player_displayname%", "%player_name%"));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
package net.draycia.simplechat.listeners;
|
||||
|
||||
import net.draycia.simplechat.SimpleChat;
|
||||
import net.draycia.simplechat.events.ChatComponentEvent;
|
||||
import net.draycia.simplechat.storage.ChatUser;
|
||||
import net.kyori.adventure.key.Key;
|
||||
import net.kyori.adventure.sound.Sound;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
|
||||
public class PingHandler implements Listener {
|
||||
|
||||
private SimpleChat simpleChat;
|
||||
|
||||
public PingHandler(SimpleChat simpleChat) {
|
||||
this.simpleChat = simpleChat;
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.MONITOR)
|
||||
public void onPing(ChatComponentEvent event) {
|
||||
for (ChatUser user : event.getRecipients()) {
|
||||
if (user.getUUID().equals(event.getUser().getUUID())) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (event.getOriginalMessage().contains(user.asPlayer().getName())) {
|
||||
if (simpleChat.getConfig().getBoolean("pings.enabled")) {
|
||||
Key key = Key.of(simpleChat.getConfig().getString("pings.sound"));
|
||||
Sound.Source source = Sound.Source.valueOf(simpleChat.getConfig().getString("pings.source"));
|
||||
float volume = (float)simpleChat.getConfig().getDouble("pings.volume");
|
||||
float pitch = (float)simpleChat.getConfig().getDouble("pings.pitch");
|
||||
|
||||
user.playSound(Sound.of(key, source, volume, pitch));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
package net.draycia.simplechat.listeners;
|
||||
|
||||
import me.clip.placeholderapi.PlaceholderAPI;
|
||||
import net.draycia.simplechat.SimpleChat;
|
||||
import net.draycia.simplechat.events.ChatFormatEvent;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
|
||||
public class PlaceholderHandler implements Listener {
|
||||
|
||||
private SimpleChat simpleChat;
|
||||
|
||||
public PlaceholderHandler(SimpleChat simpleChat) {
|
||||
this.simpleChat = simpleChat;
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.LOWEST)
|
||||
public void onChatPlaceholder(ChatFormatEvent event) {
|
||||
ConfigurationSection replacements = simpleChat.getConfig().getConfigurationSection("replacements");
|
||||
|
||||
if (replacements != null) {
|
||||
for (String key : replacements.getKeys(false)) {
|
||||
event.setMessage(event.getMessage().replace(key, replacements.getString(key)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.LOWEST)
|
||||
public void onPapiPlaceholder(ChatFormatEvent event) {
|
||||
event.setFormat(PlaceholderAPI.setPlaceholders(event.getUser().asOfflinePlayer(), event.getFormat()));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
package net.draycia.simplechat.listeners;
|
||||
|
||||
import net.draycia.simplechat.events.ChatComponentEvent;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
|
||||
public class ShadowMuteHandler implements Listener {
|
||||
|
||||
@EventHandler(priority = EventPriority.LOWEST)
|
||||
public void onComponent(ChatComponentEvent event) {
|
||||
if (event.getUser().isShadowMuted()) {
|
||||
if (event.getRecipients().size() == 1 && event.getUser().equals(event.getRecipients().get(0))) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
event.getRecipients().clear();
|
||||
event.getRecipients().add(event.getUser());
|
||||
} catch (UnsupportedOperationException e) {}
|
||||
}
|
||||
|
||||
// TODO: figure out a way to add the console "[SM]" prefix for shadow muted players
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,116 +0,0 @@
|
||||
package net.draycia.simplechat.managers;
|
||||
|
||||
import co.aikar.commands.CommandManager;
|
||||
import net.draycia.simplechat.SimpleChat;
|
||||
import net.draycia.simplechat.channels.ChatChannel;
|
||||
import net.draycia.simplechat.channels.impls.*;
|
||||
import net.draycia.simplechat.commands.AliasedChannelCommand;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.event.Listener;
|
||||
|
||||
public class ChannelManager {
|
||||
|
||||
public ChannelManager(SimpleChat simpleChat) {
|
||||
boolean hasTownChat = false;
|
||||
boolean hasNationChat = false;
|
||||
boolean hasAllianceChat = false;
|
||||
boolean hasPartyChat = false;
|
||||
|
||||
for (String key : simpleChat.getConfig().getConfigurationSection("channels").getKeys(false)) {
|
||||
ChatChannel channel;
|
||||
|
||||
ConfigurationSection section = simpleChat.getConfig().getConfigurationSection("channels").getConfigurationSection(key);
|
||||
|
||||
String name = section.getString("name");
|
||||
|
||||
if (name != null && name.length() > 16) {
|
||||
simpleChat.getLogger().warning("Channel name [" + name + "] too long! Max length: 16.");
|
||||
simpleChat.getLogger().warning("Skipping channel, please check your settings!");
|
||||
continue;
|
||||
}
|
||||
|
||||
String type = section.getString("type");
|
||||
|
||||
if (type == null) {
|
||||
type = "normal";
|
||||
}
|
||||
|
||||
switch (type.toLowerCase()) {
|
||||
case "towny":
|
||||
channel = new TownChatChannel(key, simpleChat);
|
||||
hasTownChat = true;
|
||||
break;
|
||||
case "nation":
|
||||
channel = new NationChatChannel(key, simpleChat);
|
||||
hasNationChat = true;
|
||||
break;
|
||||
case "alliance":
|
||||
channel = new AllianceChatChannel(key, simpleChat);
|
||||
hasAllianceChat = true;
|
||||
break;
|
||||
case "party":
|
||||
channel = new PartyChatChannel(key, simpleChat);
|
||||
hasPartyChat = true;
|
||||
break;
|
||||
case "normal":
|
||||
channel = new SimpleChatChannel(key, simpleChat);
|
||||
break;
|
||||
default:
|
||||
simpleChat.getLogger().warning("Invalid channel type for channel [" + key + "]. Skipping channel.");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (channel.isTownChat() || channel.isNationChat() || channel.isAllianceChat()) {
|
||||
if (!Bukkit.getPluginManager().isPluginEnabled("Towny")) {
|
||||
simpleChat.getLogger().warning("Towny related channel with key [" + key + "] found, but Towny isn't installed. Skipping channel.");
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (channel.isPartyChat()) {
|
||||
if (!Bukkit.getPluginManager().isPluginEnabled("mcMMO")) {
|
||||
simpleChat.getLogger().warning("mcMMO related channel with key [" + key + "] found, but mcMMO isn't installed. Skipping channel.");
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
simpleChat.getChannels().add(channel);
|
||||
|
||||
CommandManager commandManager = simpleChat.getCommandManager().getCommandManager();
|
||||
|
||||
String commandName = section.contains("aliases") ? section.getString("aliases") : key;
|
||||
|
||||
commandManager.getCommandReplacements().addReplacement("channelName", commandName);
|
||||
commandManager.registerCommand(new AliasedChannelCommand(simpleChat, channel));
|
||||
|
||||
if (channel instanceof Listener) {
|
||||
Bukkit.getPluginManager().registerEvents((Listener)channel, simpleChat);
|
||||
}
|
||||
}
|
||||
|
||||
boolean townyInstalled = Bukkit.getPluginManager().isPluginEnabled("Towny");
|
||||
boolean mcmmoInstalled = Bukkit.getPluginManager().isPluginEnabled("mcMMO");
|
||||
|
||||
if (!hasTownChat && townyInstalled) {
|
||||
simpleChat.getLogger().info("Towny installed but no Town channel is setup!");
|
||||
}
|
||||
|
||||
if (!hasNationChat && townyInstalled) {
|
||||
simpleChat.getLogger().info("Towny installed but no Nation channel is setup!");
|
||||
}
|
||||
|
||||
if (!hasAllianceChat && townyInstalled) {
|
||||
simpleChat.getLogger().info("Towny installed but no Alliance channel is setup!");
|
||||
}
|
||||
|
||||
if (!hasPartyChat && mcmmoInstalled) {
|
||||
simpleChat.getLogger().info("mcMMO installed but no Party channel is setup!");
|
||||
}
|
||||
}
|
||||
|
||||
public void reloadChannels() {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,118 +0,0 @@
|
||||
package net.draycia.simplechat.managers;
|
||||
|
||||
import co.aikar.commands.*;
|
||||
import net.draycia.simplechat.SimpleChat;
|
||||
import net.draycia.simplechat.channels.ChatChannel;
|
||||
import net.draycia.simplechat.channels.impls.AllianceChatChannel;
|
||||
import net.draycia.simplechat.channels.impls.NationChatChannel;
|
||||
import net.draycia.simplechat.channels.impls.PartyChatChannel;
|
||||
import net.draycia.simplechat.channels.impls.TownChatChannel;
|
||||
import net.draycia.simplechat.commands.*;
|
||||
import net.draycia.simplechat.storage.ChatUser;
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class CommandManager {
|
||||
|
||||
private SimpleChat simpleChat;
|
||||
private BukkitCommandManager commandManager;
|
||||
|
||||
public CommandManager(SimpleChat simpleChat) {
|
||||
this.simpleChat = simpleChat;
|
||||
|
||||
commandManager = new BukkitCommandManager(simpleChat);
|
||||
|
||||
commandManager.enableUnstableAPI("help");
|
||||
|
||||
commandManager.getCommandCompletions().registerCompletion("chatchannel", (context) -> {
|
||||
ChatUser user = simpleChat.getUserService().wrap(context.getPlayer());
|
||||
|
||||
ArrayList<String> completions = new ArrayList<>();
|
||||
|
||||
for (ChatChannel chatChannel : simpleChat.getChannels()) {
|
||||
if (chatChannel.canPlayerUse(user)) {
|
||||
completions.add(chatChannel.getName());
|
||||
}
|
||||
}
|
||||
|
||||
return completions;
|
||||
});
|
||||
|
||||
commandManager.getCommandCompletions().registerCompletion("channel", (context) -> {
|
||||
ArrayList<String> completions = new ArrayList<>();
|
||||
|
||||
for (ChatChannel chatChannel : simpleChat.getChannels()) {
|
||||
completions.add(chatChannel.getName());
|
||||
}
|
||||
|
||||
return completions;
|
||||
});
|
||||
|
||||
commandManager.getCommandContexts().registerContext(ChatChannel.class, (context) -> {
|
||||
String name = context.popFirstArg();
|
||||
|
||||
for (ChatChannel chatChannel : simpleChat.getChannels()) {
|
||||
if (chatChannel.getName().equalsIgnoreCase(name)) {
|
||||
return chatChannel;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
});
|
||||
|
||||
commandManager.getCommandContexts().registerContext(ChatUser.class, (context) -> {
|
||||
return simpleChat.getUserService().wrap(Bukkit.getOfflinePlayer(context.popFirstArg()));
|
||||
});
|
||||
|
||||
commandManager.getCommandConditions().addCondition(ChatChannel.class,"canuse", (context, execution, value) -> {
|
||||
if (value == null) {
|
||||
throw new ConditionFailedException(simpleChat.getConfig().getString("language.cannot-use-channel"));
|
||||
}
|
||||
|
||||
ChatUser user = simpleChat.getUserService().wrap(context.getIssuer().getPlayer());
|
||||
|
||||
if (!value.canPlayerUse(user)) {
|
||||
if (value.isTownChat() && !((TownChatChannel)value).isInTown(user)) {
|
||||
throw new ConditionFailedException(simpleChat.getConfig().getString("language.town-cannot-use"));
|
||||
} else if (value.isNationChat() && !((NationChatChannel)value).isInNation(user)) {
|
||||
throw new ConditionFailedException(simpleChat.getConfig().getString("language.nation-cannot-use"));
|
||||
} else if (value.isAllianceChat() && !((AllianceChatChannel)value).isInNation(user)) {
|
||||
throw new ConditionFailedException(simpleChat.getConfig().getString("language.alliance-cannot-use"));
|
||||
} else if (value.isPartyChat() && !((PartyChatChannel)value).isInParty(user)) {
|
||||
throw new ConditionFailedException(simpleChat.getConfig().getString("language.party-cannot-use"));
|
||||
} else {
|
||||
throw new ConditionFailedException(simpleChat.getConfig().getString("language.cannot-use-channel"));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
commandManager.getCommandConditions().addCondition(ChatChannel.class,"exists", (context, execution, value) -> {
|
||||
if (value == null) {
|
||||
throw new ConditionFailedException(simpleChat.getConfig().getString("language.cannot-use-channel"));
|
||||
}
|
||||
});
|
||||
|
||||
setupCommands(commandManager);
|
||||
}
|
||||
|
||||
private void setupCommands(BukkitCommandManager manager) {
|
||||
manager.registerCommand(new ToggleCommand(simpleChat));
|
||||
manager.registerCommand(new ChannelCommand(simpleChat));
|
||||
manager.registerCommand(new IgnoreCommand(simpleChat));
|
||||
manager.registerCommand(new ClearChatCommand(simpleChat));
|
||||
manager.registerCommand(new ShadowMuteCommand(simpleChat));
|
||||
manager.registerCommand(new ChatReloadCommand(simpleChat));
|
||||
manager.registerCommand(new MeCommand(simpleChat));
|
||||
manager.registerCommand(new MessageCommand(simpleChat));
|
||||
manager.registerCommand(new ReplyCommand(simpleChat));
|
||||
manager.registerCommand(new MuteCommand(simpleChat));
|
||||
manager.registerCommand(new SetColorCommand(simpleChat));
|
||||
manager.registerCommand(new SpyChannelCommand(simpleChat));
|
||||
}
|
||||
|
||||
public co.aikar.commands.CommandManager getCommandManager() {
|
||||
return commandManager;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,167 +0,0 @@
|
||||
package net.draycia.simplechat.managers;
|
||||
|
||||
import com.google.common.io.ByteArrayDataInput;
|
||||
import com.google.common.io.ByteArrayDataOutput;
|
||||
import com.google.common.io.ByteStreams;
|
||||
import io.github.leonardosnt.bungeechannelapi.BungeeChannelApi;
|
||||
import net.draycia.simplechat.SimpleChat;
|
||||
import net.draycia.simplechat.channels.ChatChannel;
|
||||
import net.draycia.simplechat.storage.ChatUser;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.messaging.PluginMessageListener;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public class PluginMessageManager implements PluginMessageListener {
|
||||
|
||||
private SimpleChat simpleChat;
|
||||
private BungeeChannelApi api;
|
||||
|
||||
public PluginMessageManager(SimpleChat simpleChat) {
|
||||
this.simpleChat = simpleChat;
|
||||
|
||||
simpleChat.getServer().getMessenger().registerOutgoingPluginChannel(simpleChat, "BungeeCord");
|
||||
simpleChat.getServer().getMessenger().registerIncomingPluginChannel(simpleChat, "BungeeCord", this);
|
||||
|
||||
api = BungeeChannelApi.of(simpleChat);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPluginMessageReceived(String channel, @NotNull Player player, @NotNull byte[] message) {
|
||||
if (!channel.equals("BungeeCord")) {
|
||||
return;
|
||||
}
|
||||
|
||||
ByteArrayDataInput input = ByteStreams.newDataInput(message);
|
||||
|
||||
String subchannel = input.readUTF();
|
||||
short length = input.readShort();
|
||||
byte[] msgbytes = new byte[length];
|
||||
input.readFully(msgbytes);
|
||||
|
||||
ByteArrayDataInput msg = ByteStreams.newDataInput(msgbytes);
|
||||
|
||||
if (subchannel.equals("simplechat:message")) {
|
||||
onMessageReceived(msg);
|
||||
} else if (subchannel.equals("simplechat:component")) {
|
||||
onComponentReceived(msg);
|
||||
} else if (subchannel.equals("simplechat:whisper")) {
|
||||
onWhisperReceived(msg);
|
||||
}
|
||||
}
|
||||
|
||||
private void onMessageReceived(ByteArrayDataInput in) {
|
||||
String chatChannelName = in.readUTF();
|
||||
|
||||
ChatChannel chatChannel = simpleChat.getChannel(chatChannelName);
|
||||
|
||||
if (chatChannel == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
UUID playerUUID;
|
||||
|
||||
try {
|
||||
playerUUID = UUID.fromString(in.readUTF());
|
||||
} catch (IllegalArgumentException e) {
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
|
||||
ChatUser user = simpleChat.getUserService().wrap(playerUUID);
|
||||
|
||||
String chatMessage = in.readUTF();
|
||||
|
||||
Bukkit.getScheduler().scheduleAsyncDelayedTask(simpleChat, () -> {
|
||||
chatChannel.sendMessage(user, chatMessage, true);
|
||||
});
|
||||
}
|
||||
|
||||
public void onComponentReceived(ByteArrayDataInput in) {
|
||||
String chatChannelName = in.readUTF();
|
||||
|
||||
ChatChannel chatChannel = simpleChat.getChannel(chatChannelName);
|
||||
|
||||
if (chatChannel == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
UUID playerUUID;
|
||||
|
||||
try {
|
||||
playerUUID = UUID.fromString(in.readUTF());
|
||||
} catch (IllegalArgumentException e) {
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
|
||||
ChatUser user = simpleChat.getUserService().wrap(playerUUID);
|
||||
|
||||
String chatMessage = in.readUTF();
|
||||
|
||||
Bukkit.getScheduler().scheduleAsyncDelayedTask(simpleChat, () -> {
|
||||
chatChannel.sendComponent(user, MiniMessage.get().parse(chatMessage, "br", "\n"));
|
||||
});
|
||||
}
|
||||
|
||||
private void onWhisperReceived(ByteArrayDataInput in) {
|
||||
UUID targetUUID;
|
||||
|
||||
try {
|
||||
targetUUID = UUID.fromString(in.readUTF());
|
||||
} catch (IllegalArgumentException e) {
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
|
||||
String chatMessage = in.readUTF();
|
||||
|
||||
ChatUser target = simpleChat.getUserService().wrap(targetUUID);
|
||||
target.sendMessage(MiniMessage.get().parse(chatMessage, "br", "\n"));
|
||||
}
|
||||
|
||||
public void sendMessage(ChatChannel chatChannel, Player player, String message) {
|
||||
ByteArrayDataOutput msg = ByteStreams.newDataOutput();
|
||||
|
||||
msg.writeUTF(chatChannel.getName());
|
||||
msg.writeUTF(player.getUniqueId().toString());
|
||||
msg.writeUTF(message);
|
||||
|
||||
api.forward("ALL", "simplechat:message", msg.toByteArray());
|
||||
}
|
||||
|
||||
public void sendComponent(ChatChannel chatChannel, Player player, Component component) {
|
||||
ByteArrayDataOutput msg = ByteStreams.newDataOutput();
|
||||
|
||||
msg.writeUTF(chatChannel.getName());
|
||||
msg.writeUTF(player.getUniqueId().toString());
|
||||
msg.writeUTF(MiniMessage.get().serialize(component));
|
||||
|
||||
api.forward("ALL", "simplechat:component", msg.toByteArray());
|
||||
}
|
||||
|
||||
public void sendComponentToPlayer(ChatUser sender, ChatUser target, Component toComponent, Component fromComponent) {
|
||||
api.getPlayerList("ALL").whenComplete((result, error) -> {
|
||||
if (!result.contains(target.asOfflinePlayer().getName())) {
|
||||
return;
|
||||
}
|
||||
|
||||
sender.sendMessage(toComponent);
|
||||
|
||||
if (sender.isShadowMuted()) {
|
||||
return;
|
||||
}
|
||||
|
||||
ByteArrayDataOutput msg = ByteStreams.newDataOutput();
|
||||
|
||||
msg.writeUTF(target.getUUID().toString());
|
||||
msg.writeUTF(MiniMessage.get().serialize(fromComponent));
|
||||
|
||||
api.forward("ALL", "simplechat:whisper", msg.toByteArray());
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,87 +0,0 @@
|
||||
package net.draycia.simplechat.managers;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import io.lettuce.core.RedisClient;
|
||||
import io.lettuce.core.RedisURI;
|
||||
import io.lettuce.core.pubsub.RedisPubSubListener;
|
||||
import io.lettuce.core.pubsub.StatefulRedisPubSubConnection;
|
||||
import io.lettuce.core.pubsub.api.sync.RedisPubSubCommands;
|
||||
import net.draycia.simplechat.SimpleChat;
|
||||
import net.draycia.simplechat.storage.impl.SimpleChatUser;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.UUID;
|
||||
|
||||
public class RedisManager {
|
||||
|
||||
private SimpleChat simpleChat;
|
||||
|
||||
private RedisClient client;
|
||||
private StatefulRedisPubSubConnection<String, String> connection;
|
||||
private RedisPubSubCommands<String, String> sync;
|
||||
|
||||
private Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
||||
private Type userType = new TypeToken<SimpleChatUser>() {}.getType();
|
||||
|
||||
public RedisManager(SimpleChat simpleChat) {
|
||||
this.simpleChat = simpleChat;
|
||||
|
||||
String host = simpleChat.getConfig().getString("redis.host");
|
||||
int port = simpleChat.getConfig().getInt("redis.port");
|
||||
int database = simpleChat.getConfig().getInt("redis.database");
|
||||
|
||||
RedisURI uri = RedisURI.Builder.redis(host, port)
|
||||
.withDatabase(database)
|
||||
.build();
|
||||
|
||||
this.client = RedisClient.create(uri);
|
||||
this.connection = client.connectPubSub();
|
||||
this.sync = connection.sync();
|
||||
|
||||
connection.addListener(new RedisPubSubListener<String, String>() {
|
||||
@Override
|
||||
public void message(String channel, String message) {
|
||||
if (channel.equalsIgnoreCase("refreshuser")) {
|
||||
simpleChat.getUserService().refreshUser(UUID.fromString(message));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void message(String pattern, String channel, String message) { }
|
||||
|
||||
@Override
|
||||
public void subscribed(String channel, long count) { }
|
||||
|
||||
@Override
|
||||
public void psubscribed(String pattern, long count) { }
|
||||
|
||||
@Override
|
||||
public void unsubscribed(String channel, long count) { }
|
||||
|
||||
@Override
|
||||
public void punsubscribed(String pattern, long count) { }
|
||||
});
|
||||
|
||||
sync.subscribe("userdata");
|
||||
}
|
||||
|
||||
public void publishUser(SimpleChatUser user) {
|
||||
String data = gson.toJson(user, userType);
|
||||
|
||||
sync.set(user.getUUID().toString(), data);
|
||||
sync.publish("refreshuser", user.getUUID().toString());
|
||||
}
|
||||
|
||||
public SimpleChatUser getUser(UUID uuid) {
|
||||
String value = sync.get(uuid.toString());
|
||||
|
||||
if (value == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return gson.fromJson(value, userType);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
package net.draycia.simplechat.storage;
|
||||
|
||||
import net.draycia.simplechat.channels.ChatChannel;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public interface ChatUser extends Audience {
|
||||
|
||||
Player asPlayer();
|
||||
OfflinePlayer asOfflinePlayer();
|
||||
UUID getUUID();
|
||||
|
||||
boolean isOnline();
|
||||
|
||||
ChatChannel getSelectedChannel();
|
||||
void setSelectedChannel(ChatChannel channel);
|
||||
|
||||
void clearSelectedChannel();
|
||||
|
||||
UserChannelSettings getChannelSettings(ChatChannel channel);
|
||||
|
||||
void setSpyingWhispers(boolean spyingWhispers);
|
||||
boolean isSpyingWhispers();
|
||||
|
||||
void setMuted(boolean muted);
|
||||
boolean isMuted();
|
||||
|
||||
void setShadowMuted(boolean shadowMuted);
|
||||
boolean isShadowMuted();
|
||||
|
||||
@Nullable UUID getReplyTarget();
|
||||
void setReplyTarget(@Nullable UUID target);
|
||||
void setReplyTarget(@Nullable ChatUser user);
|
||||
|
||||
boolean isIgnoringUser(UUID uuid);
|
||||
boolean isIgnoringUser(ChatUser user);
|
||||
void setIgnoringUser(UUID uuid, boolean ignoring);
|
||||
void setIgnoringUser(ChatUser user, boolean ignoring);
|
||||
|
||||
void sendMessage(ChatUser sender, String message);
|
||||
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
package net.draycia.simplechat.storage;
|
||||
|
||||
import net.kyori.adventure.text.format.TextColor;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public interface UserChannelSettings {
|
||||
|
||||
boolean isSpying();
|
||||
void setSpying(boolean spying);
|
||||
|
||||
boolean isIgnored();
|
||||
void setIgnoring(boolean ignored);
|
||||
|
||||
@Nullable TextColor getColor();
|
||||
void setColor(@Nullable TextColor color);
|
||||
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
package net.draycia.simplechat.storage;
|
||||
|
||||
import org.bukkit.OfflinePlayer;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public abstract class UserService {
|
||||
|
||||
public abstract ChatUser wrap(OfflinePlayer player);
|
||||
public abstract ChatUser wrap(UUID uuid);
|
||||
|
||||
public abstract void refreshUser(UUID uuid);
|
||||
|
||||
public abstract void cleanUp();
|
||||
|
||||
}
|
||||
@@ -1,118 +0,0 @@
|
||||
package net.draycia.simplechat.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.simplechat.SimpleChat;
|
||||
import net.draycia.simplechat.storage.ChatUser;
|
||||
import net.draycia.simplechat.storage.UserService;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class JSONUserService extends UserService {
|
||||
|
||||
private final SimpleChat simpleChat;
|
||||
|
||||
private final LoadingCache<UUID, SimpleChatUser> userCache = CacheBuilder.newBuilder()
|
||||
.expireAfterWrite(10, TimeUnit.MINUTES)
|
||||
.removalListener(this::saveUser)
|
||||
.build(CacheLoader.from(this::loadUser));
|
||||
|
||||
private Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
||||
private Type userType = new TypeToken<SimpleChatUser>() {}.getType();
|
||||
|
||||
public JSONUserService(SimpleChat simpleChat) {
|
||||
this.simpleChat =simpleChat;
|
||||
|
||||
Bukkit.getScheduler().scheduleSyncRepeatingTask(simpleChat, userCache::cleanUp, 0L, 20 * 60 * 10);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChatUser wrap(OfflinePlayer player) {
|
||||
return wrap(player.getUniqueId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChatUser wrap(UUID uuid) {
|
||||
try {
|
||||
return userCache.get(uuid);
|
||||
} catch (ExecutionException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void refreshUser(UUID uuid) {
|
||||
userCache.invalidate(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cleanUp() {
|
||||
userCache.invalidateAll();
|
||||
userCache.cleanUp();
|
||||
}
|
||||
|
||||
private SimpleChatUser loadUser(UUID uuid) {
|
||||
if (simpleChat.getRedisManager() != null) {
|
||||
SimpleChatUser user = simpleChat.getRedisManager().getUser(uuid);
|
||||
|
||||
if (user != null) {
|
||||
return user;
|
||||
}
|
||||
}
|
||||
|
||||
File userFile = new File(simpleChat.getDataFolder(), "users/" + uuid.toString() + ".json");
|
||||
ensureFileExists(userFile);
|
||||
|
||||
try (JsonReader reader = gson.newJsonReader(new FileReader(userFile))) {
|
||||
SimpleChatUser user = gson.fromJson(reader, userType);
|
||||
|
||||
if (user != null) {
|
||||
return user;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return new SimpleChatUser(uuid);
|
||||
}
|
||||
|
||||
private void saveUser(RemovalNotification<UUID, SimpleChatUser> notification) {
|
||||
File userFile = new File(simpleChat.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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,197 +0,0 @@
|
||||
package net.draycia.simplechat.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.simplechat.SimpleChat;
|
||||
import net.draycia.simplechat.channels.ChatChannel;
|
||||
import net.draycia.simplechat.storage.ChatUser;
|
||||
import net.draycia.simplechat.storage.UserChannelSettings;
|
||||
import net.draycia.simplechat.storage.UserService;
|
||||
import net.kyori.adventure.text.format.TextColor;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class MySQLUserService extends UserService {
|
||||
|
||||
private final LoadingCache<UUID, SimpleChatUser> userCache = CacheBuilder.newBuilder()
|
||||
.expireAfterWrite(10, TimeUnit.MINUTES)
|
||||
.removalListener(this::saveUser)
|
||||
.build(CacheLoader.from(this::loadUser));
|
||||
|
||||
private SimpleChat simpleChat;
|
||||
private Database database;
|
||||
|
||||
private MySQLUserService() { }
|
||||
|
||||
public MySQLUserService(SimpleChat simpleChat) {
|
||||
this.simpleChat = simpleChat;
|
||||
|
||||
ConfigurationSection section = simpleChat.getConfig().getConfigurationSection("storage");
|
||||
|
||||
String username = section.getString("username");
|
||||
String password = section.getString("password");
|
||||
String dbname = section.getString("database");
|
||||
String hostandport = section.getString("hostname") + ":" + section.getString("port");
|
||||
|
||||
database = new HikariPooledDatabase(BukkitDB.getRecommendedOptions(simpleChat, 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)");
|
||||
|
||||
database.executeUpdate("CREATE TABLE IF NOT EXISTS sc_channel_settings (uuid CHAR(36), channel CHAR(16), spying BOOLEAN, ignored BOOLEAN, color TINYTEXT, PRIMARY KEY (uuid, channel))");
|
||||
|
||||
database.executeUpdate("CREATE TABLE IF NOT EXISTS sc_ignored_users (uuid CHAR(36), user CHAR(36), PRIMARY KEY (uuid, user))");
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
Bukkit.getScheduler().scheduleSyncRepeatingTask(simpleChat, userCache::cleanUp, 0L, 20 * 60 * 10);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChatUser wrap(OfflinePlayer player) {
|
||||
return wrap(player.getUniqueId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChatUser wrap(UUID uuid) {
|
||||
try {
|
||||
return userCache.get(uuid);
|
||||
} catch (ExecutionException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cleanUp() {
|
||||
userCache.invalidateAll();
|
||||
userCache.cleanUp();
|
||||
database.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void refreshUser(UUID uuid) {
|
||||
userCache.invalidate(uuid);
|
||||
}
|
||||
|
||||
private SimpleChatUser loadUser(UUID uuid) {
|
||||
if (simpleChat.getRedisManager() != null) {
|
||||
SimpleChatUser user = simpleChat.getRedisManager().getUser(uuid);
|
||||
|
||||
if (user != null) {
|
||||
return user;
|
||||
}
|
||||
}
|
||||
|
||||
SimpleChatUser user = new SimpleChatUser(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());
|
||||
|
||||
user.setSelectedChannel(simpleChat.getChannel(users.getString("channel")));
|
||||
user.setMuted(users.<Boolean>get("muted"));
|
||||
user.setShadowMuted(users.<Boolean>get("shadowmuted"));
|
||||
user.setSpyingWhispers(users.<Boolean>get("spyingwhispers"));
|
||||
|
||||
for (DbRow channelSetting : channelSettings) {
|
||||
ChatChannel chatChannel = simpleChat.getChannel(channelSetting.getString("channel"));
|
||||
UserChannelSettings settings = user.getChannelSettings(chatChannel);
|
||||
|
||||
settings.setSpying(channelSetting.<Boolean>get("spying"));
|
||||
settings.setIgnoring(channelSetting.<Boolean>get("ignored"));
|
||||
|
||||
String color = channelSetting.getString("color");
|
||||
|
||||
if (color != null) {
|
||||
settings.setColor(TextColor.fromHexString(color));
|
||||
}
|
||||
}
|
||||
|
||||
for (DbRow ignoredUser : ignoredUsers) {
|
||||
user.setIgnoringUser(UUID.fromString(ignoredUser.getString("user")), true);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
private void saveUser(RemovalNotification<UUID, SimpleChatUser> notification) {
|
||||
SimpleChatUser user = notification.getValue();
|
||||
|
||||
if (user == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
database.createTransaction(stm -> {
|
||||
// Save user general data
|
||||
String selectedName = null;
|
||||
|
||||
if (user.getSelectedChannel() != null) {
|
||||
selectedName = user.getSelectedChannel().getName();
|
||||
}
|
||||
|
||||
simpleChat.getLogger().info("Saving user data!");
|
||||
stm.executeUpdateQuery("INSERT INTO sc_users (uuid, channel, muted, shadowmuted, spyingwhispers) VALUES (?, ?, ?, ?, ?) " +
|
||||
"ON DUPLICATE KEY UPDATE channel = ?, muted = ?, shadowmuted = ?, spyingwhispers = ?",
|
||||
user.getUUID().toString(), selectedName, user.isMuted(), user.isShadowMuted(), user.isSpyingWhispers(),
|
||||
selectedName, user.isMuted(), user.isShadowMuted(), user.isSpyingWhispers());
|
||||
|
||||
simpleChat.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);
|
||||
}
|
||||
|
||||
simpleChat.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;
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,261 +0,0 @@
|
||||
package net.draycia.simplechat.storage.impl;
|
||||
|
||||
import net.draycia.simplechat.SimpleChat;
|
||||
import net.draycia.simplechat.channels.ChatChannel;
|
||||
import net.draycia.simplechat.events.ChannelSwitchEvent;
|
||||
import net.draycia.simplechat.events.PrivateMessageEvent;
|
||||
import net.draycia.simplechat.storage.ChatUser;
|
||||
import net.draycia.simplechat.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 net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
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 SimpleChatUser implements ChatUser, ForwardingAudience {
|
||||
|
||||
private transient SimpleChat simpleChat;
|
||||
|
||||
private UUID uuid;
|
||||
|
||||
private String selectedChannel = null;
|
||||
private Map<String, SimpleUserChannelSettings> channelSettings = new HashMap<>();
|
||||
private List<UUID> ignoredUsers = new ArrayList<>();
|
||||
private boolean muted = false;
|
||||
private boolean shadowMuted = false;
|
||||
private boolean spyingWhispers = false;
|
||||
|
||||
private transient UUID replyTarget = null;
|
||||
|
||||
public SimpleChatUser() {
|
||||
this.simpleChat = (SimpleChat)Bukkit.getPluginManager().getPlugin("SimpleChat");
|
||||
}
|
||||
|
||||
public SimpleChatUser(UUID uuid) {
|
||||
this.simpleChat = (SimpleChat)Bukkit.getPluginManager().getPlugin("SimpleChat");
|
||||
this.uuid = uuid;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull Iterable<? extends Audience> audiences() {
|
||||
return Collections.singleton(simpleChat.getAudiences().player(uuid));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Player asPlayer() {
|
||||
return Bukkit.getPlayer(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public OfflinePlayer asOfflinePlayer() {
|
||||
return Bukkit.getOfflinePlayer(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isOnline() {
|
||||
return asOfflinePlayer().isOnline();
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID getUUID() {
|
||||
return uuid;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChatChannel getSelectedChannel() {
|
||||
return selectedChannel == null ? simpleChat.getDefaultChannel() : simpleChat.getChannel(selectedChannel);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSelectedChannel(ChatChannel chatChannel) {
|
||||
this.selectedChannel = chatChannel.getName();
|
||||
syncToRedis();
|
||||
|
||||
Bukkit.getPluginManager().callEvent(new ChannelSwitchEvent(chatChannel, this));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearSelectedChannel() {
|
||||
setSelectedChannel(simpleChat.getDefaultChannel());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isIgnoringUser(UUID uuid) {
|
||||
return ignoredUsers.contains(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isIgnoringUser(ChatUser user) {
|
||||
return ignoredUsers.contains(user.getUUID());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setIgnoringUser(UUID uuid, boolean ignoring) {
|
||||
if (ignoring) {
|
||||
ignoredUsers.add(uuid);
|
||||
} else {
|
||||
ignoredUsers.remove(uuid);
|
||||
}
|
||||
|
||||
syncToRedis();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setIgnoringUser(ChatUser user, boolean ignoring) {
|
||||
setIgnoringUser(user.getUUID(), ignoring);
|
||||
}
|
||||
|
||||
public List<UUID> getIgnoredUsers() {
|
||||
return ignoredUsers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setMuted(boolean muted) {
|
||||
this.muted = muted;
|
||||
|
||||
syncToRedis();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isMuted() {
|
||||
return muted;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setShadowMuted(boolean shadowMuted) {
|
||||
this.shadowMuted = shadowMuted;
|
||||
|
||||
syncToRedis();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isShadowMuted() {
|
||||
return shadowMuted;
|
||||
}
|
||||
|
||||
@CheckForNull
|
||||
@Override
|
||||
public UUID getReplyTarget() {
|
||||
return replyTarget;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setReplyTarget(UUID target) {
|
||||
this.replyTarget = target;
|
||||
|
||||
syncToRedis();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setReplyTarget(ChatUser user) {
|
||||
setReplyTarget(user.getUUID());
|
||||
}
|
||||
|
||||
@Override
|
||||
public UserChannelSettings getChannelSettings(ChatChannel channel) {
|
||||
return channelSettings.computeIfAbsent(channel.getName(), (name) -> new SimpleUserChannelSettings());
|
||||
}
|
||||
|
||||
public Map<String, ? extends UserChannelSettings> getChannelSettings() {
|
||||
return channelSettings;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSpyingWhispers(boolean spyingWhispers) {
|
||||
this.spyingWhispers = spyingWhispers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSpyingWhispers() {
|
||||
return spyingWhispers;
|
||||
}
|
||||
|
||||
public void sendMessage(ChatUser sender, String message) {
|
||||
if (isIgnoringUser(sender) || sender.isIgnoringUser(this)) {
|
||||
return;
|
||||
}
|
||||
|
||||
String toPlayerFormat = simpleChat.getConfig().getString("language.message-to-other");
|
||||
String fromPlayerFormat = simpleChat.getConfig().getString("language.message-from-other");
|
||||
|
||||
String senderName = sender.asOfflinePlayer().getName();
|
||||
String senderOfflineName = senderName;
|
||||
|
||||
String targetName = this.asOfflinePlayer().getName();
|
||||
String targetOfflineName = targetName;
|
||||
|
||||
if (sender.isOnline()) {
|
||||
senderName = sender.asPlayer().getDisplayName();
|
||||
}
|
||||
|
||||
if (this.isOnline()) {
|
||||
targetName = this.asPlayer().getDisplayName();
|
||||
}
|
||||
|
||||
Component toPlayerComponent = MiniMessage.get().parse(toPlayerFormat, "br", "\n",
|
||||
"message", message,
|
||||
"targetname", targetOfflineName, "sendername", senderOfflineName,
|
||||
"target", targetName, "sender", senderName);
|
||||
|
||||
Component fromPlayerComponent = MiniMessage.get().parse(fromPlayerFormat, "br", "\n",
|
||||
"message", message,
|
||||
"targetname", targetOfflineName, "sendername", senderOfflineName,
|
||||
"target", targetName, "sender", senderName);
|
||||
|
||||
if (this.isOnline()) {
|
||||
if (sender.isOnline()) {
|
||||
sender.sendMessage(toPlayerComponent);
|
||||
|
||||
if (sender.isShadowMuted()) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.sendMessage(fromPlayerComponent);
|
||||
|
||||
sender.setReplyTarget(this);
|
||||
this.setReplyTarget(sender);
|
||||
|
||||
if (simpleChat.getConfig().getBoolean("pings.on-whisper")) {
|
||||
Key key = Key.of(simpleChat.getConfig().getString("pings.sound"));
|
||||
Sound.Source source = Sound.Source.valueOf(simpleChat.getConfig().getString("pings.source"));
|
||||
float volume = (float)simpleChat.getConfig().getDouble("pings.volume");
|
||||
float pitch = (float)simpleChat.getConfig().getDouble("pings.pitch");
|
||||
|
||||
this.playSound(Sound.of(key, source, volume, pitch));
|
||||
}
|
||||
}
|
||||
} else if (sender.isOnline()) {
|
||||
simpleChat.getPluginMessageManager().sendComponentToPlayer(sender, this, toPlayerComponent, fromPlayerComponent);
|
||||
}
|
||||
|
||||
for (Player player : Bukkit.getOnlinePlayers()) {
|
||||
ChatUser user = simpleChat.getUserService().wrap(player);
|
||||
|
||||
if (user.getUUID().equals(sender.getUUID()) || user.getUUID().equals(getUUID())) {
|
||||
continue;
|
||||
}
|
||||
|
||||
user.sendMessage(MiniMessage.get().parse(simpleChat.getConfig().getString("language.spy-whispers"), "br", "\n",
|
||||
"message", message,
|
||||
"targetname", targetOfflineName, "sendername", senderOfflineName,
|
||||
"target", targetName, "sender", senderName));
|
||||
}
|
||||
|
||||
Bukkit.getPluginManager().callEvent(new PrivateMessageEvent(sender, this, toPlayerComponent, fromPlayerComponent, message));
|
||||
}
|
||||
|
||||
private void syncToRedis() {
|
||||
if (simpleChat.getRedisManager() != null) {
|
||||
simpleChat.getRedisManager().publishUser(this);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
package net.draycia.simplechat.storage.impl;
|
||||
|
||||
import net.draycia.simplechat.storage.UserChannelSettings;
|
||||
import net.kyori.adventure.text.format.TextColor;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class SimpleUserChannelSettings implements UserChannelSettings {
|
||||
|
||||
private boolean spying;
|
||||
private boolean ignored;
|
||||
private String color;
|
||||
|
||||
@Override
|
||||
public boolean isSpying() {
|
||||
return this.spying;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSpying(boolean spying) {
|
||||
this.spying = spying;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isIgnored() {
|
||||
return ignored;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setIgnoring(boolean ignored) {
|
||||
this.ignored = ignored;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable TextColor getColor() {
|
||||
if (color == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return TextColor.fromHexString(color);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setColor(@Nullable TextColor color) {
|
||||
if (color == null) {
|
||||
this.color = null;
|
||||
return;
|
||||
}
|
||||
|
||||
this.color = color.asHexString();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,66 +0,0 @@
|
||||
/*
|
||||
Simple channel based chat plugin for Spigot
|
||||
Copyright (C) 2020 Alexander Söderberg
|
||||
|
||||
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.simplechat.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 Class<?> craftItemStackClass;
|
||||
private final Method asNMSCopyMethod;
|
||||
private final Class<?> itemStackClass;
|
||||
private final Method cMethod;
|
||||
|
||||
public ItemStackUtils() throws NoSuchMethodException, ClassNotFoundException {
|
||||
final String version = Bukkit.getServer().getClass().getPackage().getName()
|
||||
.substring("org.bukkit.craftbukkit.".length());
|
||||
|
||||
this.craftItemStackClass = Class.forName("org.bukkit.craftbukkit." + version + ".inventory.CraftItemStack");
|
||||
this.asNMSCopyMethod = this.craftItemStackClass.getMethod("asNMSCopy", ItemStack.class);
|
||||
this.itemStackClass = Class.forName("net.minecraft.server." + version + ".ItemStack");
|
||||
this.cMethod = this.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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user