Start of ChatUserManager
This commit is contained in:
parent
66f7e46add
commit
abf5b4d2bb
|
|
@ -2,6 +2,7 @@ package com.alttd.chat;
|
|||
|
||||
import com.alttd.chat.config.Config;
|
||||
import com.alttd.chat.database.DatabaseConnection;
|
||||
import com.alttd.chat.managers.ChatUserManager;
|
||||
import net.luckperms.api.LuckPerms;
|
||||
import net.luckperms.api.LuckPermsProvider;
|
||||
import net.luckperms.api.model.group.Group;
|
||||
|
|
@ -16,7 +17,7 @@ public class ChatImplementation implements ChatAPI{
|
|||
private static ChatAPI instance;
|
||||
|
||||
private LuckPerms luckPerms;
|
||||
private DatabaseConnection databaseConnection;
|
||||
private DatabaseConnection databaseConnection; // todo this isn't needed can be removed
|
||||
|
||||
public ChatImplementation() {
|
||||
instance = this;
|
||||
|
|
@ -24,6 +25,8 @@ public class ChatImplementation implements ChatAPI{
|
|||
|
||||
luckPerms = getLuckPerms();
|
||||
databaseConnection = getDataBase();
|
||||
|
||||
ChatUserManager.initialize(); // loads all the users from the db and adds them.
|
||||
}
|
||||
|
||||
public static ChatAPI get() {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
package com.alttd.chat.database;
|
||||
|
||||
import com.alttd.chat.managers.ChatUserManager;
|
||||
import com.alttd.chat.objects.Party;
|
||||
import com.alttd.chat.objects.ChatUser;
|
||||
import com.alttd.chat.util.ALogger;
|
||||
|
|
@ -249,7 +250,7 @@ public class Queries {
|
|||
//-----------------------------------------
|
||||
|
||||
//Chat Users
|
||||
|
||||
//@teri what's this?
|
||||
private static void getChatUsers(HashMap<Integer, Party> parties) { //TODO Get parties from cache somewhere
|
||||
String query = "SELECT * FROM chat_users";
|
||||
|
||||
|
|
@ -287,6 +288,30 @@ public class Queries {
|
|||
}
|
||||
}
|
||||
|
||||
public static void loadChatUsers() { //TODO Get parties from cache somewhere
|
||||
String query = "SELECT * FROM chat_users";
|
||||
|
||||
try {
|
||||
Connection connection = DatabaseConnection.getConnection();
|
||||
|
||||
ResultSet resultSet = connection.prepareStatement(query).executeQuery();
|
||||
|
||||
while (resultSet.next()) {
|
||||
|
||||
UUID uuid = UUID.fromString(resultSet.getString("uuid"));
|
||||
int partyId = resultSet.getInt("party_id");
|
||||
boolean toggled_chat = resultSet.getInt("toggled_chat") == 1;
|
||||
boolean force_tp = resultSet.getInt("force_tp") == 1;
|
||||
boolean toggle_Gc = resultSet.getInt("toggled_gc") == 1;
|
||||
// could do a constructor for chatuser to accept the record?
|
||||
ChatUserManager.addUser(new ChatUser(uuid, partyId, toggled_chat, force_tp, toggle_Gc));
|
||||
}
|
||||
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static void addUser(ChatUser user) {
|
||||
String query = "INSERT INTO chat_users (uuid, party_id, toggled_chat, force_tp, toggled_gc) VALUES (?, ?, ?, ?, ?)";
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,31 @@
|
|||
package com.alttd.chat.managers;
|
||||
|
||||
import com.alttd.chat.database.Queries;
|
||||
import com.alttd.chat.objects.ChatUser;
|
||||
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CopyOnWriteArraySet;
|
||||
|
||||
public final class ChatUserManager {
|
||||
|
||||
private static CopyOnWriteArraySet<ChatUser> chatUsers;// not sure on this, could cause errors later on
|
||||
|
||||
public static void initialize() {
|
||||
chatUsers = new CopyOnWriteArraySet<>();
|
||||
Queries.loadChatUsers();
|
||||
}
|
||||
|
||||
public static void addUser(ChatUser user) {
|
||||
if(getChatUser(user.getUuid()) != null)
|
||||
chatUsers.add(user);
|
||||
}
|
||||
|
||||
public static ChatUser getChatUser(UUID uuid) {
|
||||
for(ChatUser user : chatUsers) {
|
||||
if(user.getUuid() == uuid) {
|
||||
return user;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
@ -15,6 +15,7 @@ public class ChatUser {
|
|||
private String staffPrefix;
|
||||
private String prefixAll;
|
||||
private boolean toggleGc;
|
||||
private UUID replytarget;
|
||||
|
||||
public ChatUser(UUID uuid, int partyId, boolean toggled_chat, boolean force_tp, boolean toggle_Gc) {
|
||||
this.uuid = uuid;
|
||||
|
|
@ -33,6 +34,7 @@ public class ChatUser {
|
|||
prefixAll = prefix + staffPrefix; //TODO test what this does cus I barely understand lp api
|
||||
// a boolean is lighter then a permission check, it's what I'd suggest doing here
|
||||
toggleGc = toggle_Gc;//Utility.checkPermission(uuid, "chat.gc"); //TODO put the actual permission here, I don't know what it is...
|
||||
replytarget = null;
|
||||
}
|
||||
|
||||
public UUID getUuid() {
|
||||
|
|
@ -100,4 +102,12 @@ public class ChatUser {
|
|||
public boolean isGcOn() {
|
||||
return toggleGc;
|
||||
}
|
||||
|
||||
public UUID getReplytarget() {
|
||||
return replytarget;
|
||||
}
|
||||
|
||||
public void setReplytarget(UUID replytarget) {
|
||||
this.replytarget = replytarget;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,11 +11,12 @@ public class GlobalChat implements CommandExecutor {
|
|||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
if(!(sender instanceof Player)) {
|
||||
if(!(sender instanceof Player)) { // must be a player
|
||||
return true;
|
||||
}
|
||||
Player player = (Player) sender;
|
||||
String message = StringUtils.join(args, " ", 0, args.length);
|
||||
ChatPlugin.getInstance().getChatHandler().globalChat(sender, message);
|
||||
ChatPlugin.getInstance().getChatHandler().globalChat(player, message);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2,6 +2,8 @@ package com.alttd.chat.handler;
|
|||
|
||||
import com.alttd.chat.ChatPlugin;
|
||||
import com.alttd.chat.config.Config;
|
||||
import com.alttd.chat.managers.ChatUserManager;
|
||||
import com.alttd.chat.objects.ChatUser;
|
||||
import com.google.common.io.ByteArrayDataOutput;
|
||||
import com.google.common.io.ByteStreams;
|
||||
import net.kyori.adventure.text.Component;
|
||||
|
|
@ -24,16 +26,22 @@ public class ChatHandler {
|
|||
plugin = ChatPlugin.getInstance();
|
||||
}
|
||||
|
||||
public void globalChat(CommandSender source, String message) {
|
||||
public void globalChat(Player player, String message) {
|
||||
ChatUser user = ChatUserManager.getChatUser(player.getUniqueId());
|
||||
if(user == null) return;
|
||||
if(!user.isGcOn()) {
|
||||
player.sendMessage();// GC IS OFF INFORM THEM ABOUT THIS and cancel
|
||||
return;
|
||||
}
|
||||
// Check if the player has global chat enabled, if not warn them
|
||||
String senderName, prefix = "";
|
||||
|
||||
Player sender = (Player) source;
|
||||
senderName = sender.getDisplayName(); // TODO this can be a component
|
||||
prefix = plugin.getChatAPI().getPrefix(sender.getUniqueId());
|
||||
senderName = player.getDisplayName(); // TODO this can be a component
|
||||
// can also be cached in the chatuser object?
|
||||
prefix = plugin.getChatAPI().getPrefix(player.getUniqueId());
|
||||
|
||||
MiniMessage miniMessage = MiniMessage.get();
|
||||
if(!source.hasPermission("chat.format"))
|
||||
if(!player.hasPermission("chat.format"))
|
||||
message = miniMessage.stripTokens(message);
|
||||
if(message.contains("[i]"))
|
||||
message = message.replace("[i]", "<[i]>");
|
||||
|
|
@ -52,9 +60,10 @@ public class ChatHandler {
|
|||
ByteArrayDataOutput out = ByteStreams.newDataOutput();
|
||||
out.writeUTF("globalchat");
|
||||
out.writeUTF(miniMessage.serialize(component));
|
||||
sender.sendPluginMessage(plugin, Config.MESSAGECHANNEL, out.toByteArray());
|
||||
player.sendPluginMessage(plugin, Config.MESSAGECHANNEL, out.toByteArray());
|
||||
}
|
||||
|
||||
// Start - move these to util
|
||||
public Component itemComponent(ItemStack item) {
|
||||
Component component = Component.text("[i]");
|
||||
if(item.getType().equals(Material.AIR))
|
||||
|
|
@ -91,4 +100,5 @@ public class ChatHandler {
|
|||
|
||||
return sb.toString();
|
||||
}
|
||||
// end - move these to util
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
package com.alttd.chat;
|
||||
|
||||
import com.alttd.chat.commands.GlobalAdminChat;
|
||||
import com.alttd.chat.commands.GlobalChatToggle;
|
||||
import com.alttd.chat.config.Config;
|
||||
import com.alttd.chat.handlers.ChatHandler;
|
||||
import com.alttd.chat.handlers.ServerHandler;
|
||||
|
|
@ -84,7 +83,6 @@ public class VelocityChat {
|
|||
|
||||
public void loadCommands() {
|
||||
new GlobalAdminChat(server);
|
||||
new GlobalChatToggle(server);
|
||||
// all (proxy)commands go here
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,51 +0,0 @@
|
|||
package com.alttd.chat.commands;
|
||||
|
||||
import com.alttd.chat.VelocityChat;
|
||||
import com.alttd.chat.config.Config;
|
||||
import com.mojang.brigadier.arguments.StringArgumentType;
|
||||
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
|
||||
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
|
||||
import com.mojang.brigadier.tree.LiteralCommandNode;
|
||||
import com.velocitypowered.api.command.BrigadierCommand;
|
||||
import com.velocitypowered.api.command.CommandMeta;
|
||||
import com.velocitypowered.api.command.CommandSource;
|
||||
import com.velocitypowered.api.proxy.Player;
|
||||
import com.velocitypowered.api.proxy.ProxyServer;
|
||||
import net.luckperms.api.LuckPerms;
|
||||
import net.luckperms.api.node.Node;
|
||||
|
||||
public class GlobalChatToggle {
|
||||
|
||||
public GlobalChatToggle(ProxyServer proxyServer) {
|
||||
LiteralCommandNode<CommandSource> command = LiteralArgumentBuilder
|
||||
.<CommandSource>literal("toggleglobalchat")
|
||||
.requires(ctx -> ctx instanceof Player)
|
||||
.requires(ctx -> ctx.hasPermission("command.proxy.globalchat"))// TODO permission system? load permissions from config?
|
||||
.then(RequiredArgumentBuilder
|
||||
.<CommandSource, String>argument("message", StringArgumentType.greedyString())
|
||||
.executes(context -> {
|
||||
LuckPerms luckPerms = VelocityChat.getPlugin().API().getLuckPerms();
|
||||
Player player = (Player) context;
|
||||
luckPerms.getUserManager().modifyUser(player.getUniqueId(), user -> {
|
||||
if(player.hasPermission(Config.GCPERMISSION)) { //TODO THIS MUST BE A CONSTANT FROM CONFIG?
|
||||
user.data().add(Node.builder(Config.GCPERMISSION).build());
|
||||
} else {
|
||||
user.data().remove(Node.builder(Config.GCPERMISSION).build());
|
||||
}
|
||||
});
|
||||
return 1;
|
||||
})
|
||||
)
|
||||
.executes(context -> 0)
|
||||
.build();
|
||||
|
||||
BrigadierCommand brigadierCommand = new BrigadierCommand(command);
|
||||
|
||||
CommandMeta.Builder metaBuilder = proxyServer.getCommandManager().metaBuilder(brigadierCommand);
|
||||
metaBuilder.aliases("togglegc");
|
||||
|
||||
CommandMeta meta = metaBuilder.build();
|
||||
|
||||
proxyServer.getCommandManager().register(meta, brigadierCommand);
|
||||
}
|
||||
}
|
||||
|
|
@ -12,37 +12,6 @@ import java.util.*;
|
|||
|
||||
public class ChatHandler {
|
||||
|
||||
private List<ChatUser> chatUsers;
|
||||
|
||||
public ChatHandler() {
|
||||
chatUsers = new ArrayList<>();
|
||||
}
|
||||
|
||||
public void addPlayer(ChatUser chatuser) {
|
||||
chatUsers.add(chatuser);
|
||||
}
|
||||
|
||||
public void removePlayer(ChatUser chatUser) {
|
||||
if(chatUser != null)
|
||||
chatUsers.remove(chatUser);
|
||||
}
|
||||
|
||||
public void removePlayer(UUID uuid) {
|
||||
removePlayer(getChatUser(uuid));
|
||||
}
|
||||
|
||||
public ChatUser getChatUser(UUID uuid) {
|
||||
for(ChatUser p: chatUsers) {
|
||||
if(p.getUuid() == uuid)
|
||||
return p;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public List<ChatUser> getChatPlayers() {
|
||||
return Collections.unmodifiableList(chatUsers);
|
||||
}
|
||||
|
||||
public void privateMessage(PrivateMessageEvent event) {
|
||||
String senderName;
|
||||
String receiverName;
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user