Finished party command, and sub commands

This commit is contained in:
2021-08-08 11:36:06 +02:00
parent 0932bf8d51
commit 2cb843b68c
18 changed files with 462 additions and 142 deletions
@@ -1,6 +1,6 @@
package com.alttd.chat.config;
import com.alttd.chat.objects.Channel;
import com.alttd.chat.objects.channels.CustomChannel;
import com.google.common.base.Throwables;
import com.google.common.collect.Lists;
import com.google.common.reflect.TypeToken;
@@ -68,7 +68,6 @@ public final class Config {
e.printStackTrace();
}
}
public static void readConfig(Class<?> clazz, Object instance) {
for (Method method : clazz.getDeclaredMethods()) {
if (Modifier.isPrivate(method.getModifiers())) {
@@ -156,6 +155,7 @@ public final class Config {
return config.getNode(splitPath(path));
}
/** ONLY EDIT ANYTHING BELOW THIS LINE **/
public static List<String> PREFIXGROUPS = new ArrayList<>();
public static List<String> STAFFGROUPS = new ArrayList<>();
@@ -202,6 +202,11 @@ public final class Config {
GCCOOLDOWN = getInt("commands.globalchat.cooldown", GCCOOLDOWN);
}
public static String PARTY_FORMAT = "<dark_aqua>(<gray><sender></gray> <hover:show_text:on <server>> → Party</hover>) <message>";
private static void party() {
PARTY_FORMAT = getString("party.format", PARTY_FORMAT);
}
// TODO prefixes need hovers, this hasn't been setup yet!
public static String CHATFORMAT = "<white><light_purple><prefixall> <gray><hover:show_text:Click to message <sendername>><click:suggest_command:/msg <sendername> ><sender></hover>: <white><message>";
private static void Chat() {
@@ -249,7 +254,7 @@ public final class Config {
for (ConfigurationNode configurationNode : node.getChildrenMap().values()) {
String channelName = Objects.requireNonNull(configurationNode.getKey()).toString();
String key = "chat-channels." + channelName + ".";
new Channel(channelName,
new CustomChannel(channelName,
getString(key + "format", ""),
getList(key + "servers", Collections.EMPTY_LIST),
getBoolean(key + "proxy", false));
@@ -5,12 +5,16 @@ import com.alttd.chat.managers.PartyManager;
import com.alttd.chat.objects.ChatUser;
import com.alttd.chat.objects.Mail;
import com.alttd.chat.objects.Party;
import com.alttd.chat.util.ALogger;
import net.kyori.adventure.text.minimessage.MiniMessage;
import java.awt.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
import java.util.List;
public class Queries {
@@ -155,6 +159,41 @@ public class Queries {
} catch (SQLException e) {
e.printStackTrace();
}
loadPartyUsers();
}
private static void loadPartyUsers() {
String query = "SELECT chat_users.party_id, chat_users.uuid, nicknames.nickname, utility_users.Username " +
"FROM chat_users " +
"LEFT OUTER JOIN nicknames ON chat_users.UUID = nicknames.uuid " +
"LEFT OUTER JOIN utility_users ON chat_users.uuid = utility_users.UUID " +
"WHERE party_id != -1";
try {
Connection connection = DatabaseConnection.getConnection();
ResultSet resultSet = connection.prepareStatement(query).executeQuery();
while (resultSet.next()) {
int id = resultSet.getInt("party_id");
UUID uuid = UUID.fromString(resultSet.getString("uuid"));
String displayName = resultSet.getString("nickname");
if (displayName == null || displayName.isEmpty()) {
displayName = resultSet.getString("Username");
}
Party party = PartyManager.getParty(id);
if (party == null) {
ALogger.warn("Unable to retrieve party: " + id);
continue;
}
party.putUser(uuid, displayName);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static Party addParty(UUID partyOwner, String partyName, String password) {
@@ -228,14 +267,15 @@ public class Queries {
}
}
public static void removeParty(int id) {
String query = "DELETE FROM parties WHERE id = ?";
public static void addPartyUser(ChatUser user) {
String query = "UPDATE chat_users SET party_id = ? WHERE uuid = ?";
try {
Connection connection = DatabaseConnection.getConnection();
PreparedStatement statement = connection.prepareStatement(query);
statement.setInt(1, id);
statement.setInt(1, user.getPartyId());
statement.setString(2, user.getUuid().toString());
statement.execute();
} catch (SQLException e) {
@@ -243,46 +283,45 @@ public class Queries {
}
}
public static void removePartyUser(UUID uuid) {
String query = "UPDATE chat_users SET party_id = -1 WHERE uuid = ?";
try {
Connection connection = DatabaseConnection.getConnection();
PreparedStatement statement = connection.prepareStatement(query);
statement.setString(1, uuid.toString());
statement.execute();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void removeParty(int id) {
String deleteParty = "DELETE FROM parties WHERE id = ?";
String updateUsers = "UPDATE chat_users SET party_id = -1 WHERE party_id = ?";
try {
Connection connection = DatabaseConnection.getConnection();
PreparedStatement statement = connection.prepareStatement(deleteParty);
statement.setInt(1, id);
statement.execute();
statement = connection.prepareStatement(updateUsers);
statement.setInt(1, id);
statement.execute();
} catch (SQLException e) {
e.printStackTrace();
}
}
//-----------------------------------------
private static void getChatUsers(HashMap<Integer, Party> parties) { //TODO Get parties from cache somewhere
String query = "SELECT * FROM chat_users WHERE party_id != -1";
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 toggle_Gc = resultSet.getInt("toggled_gc") == 1;
if (partyId == 0) {
continue;
}
Party party = parties.get(partyId);
if (party == null) {
//TODO log this properly
System.out.println("INCORRECT LOGGING: party was empty, the party id stored in the database with user " + uuid + " was invalid.");
continue;
}
party.addUser(new ChatUser(uuid, partyId, toggled_chat, toggle_Gc));
//TODO maybe add to the cache as well?
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void loadChatUsers() { //TODO Get parties from cache somewhere
String query = "SELECT * FROM chat_users";
String query = "SELECT * FROM chat_users WHERE party_id > -1";
try {
Connection connection = DatabaseConnection.getConnection();
@@ -327,25 +366,6 @@ public class Queries {
return user;
}
public static void addUser(ChatUser user) {
String query = "INSERT INTO chat_users (uuid, party_id, toggled_chat, toggled_gc) VALUES (?, ?, ?, ?)";
try {
Connection connection = DatabaseConnection.getConnection();
PreparedStatement statement = connection.prepareStatement(query);
statement.setString(1, user.getUuid().toString());
statement.setInt(2, user.getPartyId());
statement.setInt(3, user.toggledPartyChat() ? 1 : 0);
statement.setInt(4, 0);
// statement.setInt(5, user.isGcOn() ? 1 : 0);
statement.execute();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void setPartyChatState(boolean toggledChat, UUID uuid) {
setBitWhereId("UPDATE chat_users set toggled_chat = ? WHERE uuid = ?", toggledChat, uuid);
}
@@ -368,21 +388,6 @@ public class Queries {
}
}
public static void removeUser(UUID uuid) {
String query = "DELETE FROM chat_users WHERE uuid = ?";
try {
Connection connection = DatabaseConnection.getConnection();
PreparedStatement statement = connection.prepareStatement(query);
statement.setString(1, uuid.toString());
statement.execute();
} catch (SQLException e) {
e.printStackTrace();
}
}
//-----------------------------------------
public static List<Mail> getMails(UUID uuid) {
@@ -8,7 +8,6 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
public final class ChatUserManager {
@@ -17,7 +16,6 @@ public final class ChatUserManager {
public static void initialize() {
chatUsers = new ArrayList<>();
loadUsers();
}
public static void addUser(ChatUser user) {
@@ -43,10 +41,6 @@ public final class ChatUserManager {
.collect(Collectors.toList());
}
public static void loadUsers() {
Queries.loadChatUsers();
}
protected static List<ChatUser> getChatUsers() {
return Collections.unmodifiableList(chatUsers);
}
@@ -3,6 +3,7 @@ package com.alttd.chat.managers;
import com.alttd.chat.database.Queries;
import com.alttd.chat.objects.ChatUser;
import com.alttd.chat.objects.Party;
import net.kyori.adventure.text.Component;
import java.util.ArrayList;
import java.util.UUID;
@@ -25,6 +26,7 @@ public class PartyManager {
}
public static Party getParty(int id) {
if (id < 0) return null;
for(Party party : parties) {
if(id == party.getPartyId()) {
return party;
@@ -33,6 +35,15 @@ public class PartyManager {
return null;
}
public static Party getParty(String partyName) {
for(Party party : parties) {
if(party.getPartyName().equalsIgnoreCase(partyName)) {
return party;
}
}
return null;
}
public static Party getParty(UUID uuid) {
return getParty(ChatUserManager.getChatUser(uuid).getPartyId());
}
@@ -10,7 +10,7 @@ import java.util.UUID;
public class ChatUser {
private final UUID uuid; // player uuid
private final int partyId; // the party they are in
private int partyId; // the party they are in
private boolean toggledPartyChat; // should chat messages instantly go to party chat when added, idk if this should be saved
private String name; // the nickname, doesn't need to be saved with the chatuser object, could be saved but we can get it from the nicknamesview
private Component displayName; // the nickname, doesn't need to be saved with the chatuser object, could be saved but we can get it from the nicknamesview
@@ -57,6 +57,10 @@ public class ChatUser {
return partyId;
}
public void setPartyId(int partyId) {
this.partyId = partyId;
}
public boolean toggledPartyChat() {
return toggledPartyChat;
}
@@ -1,9 +1,12 @@
package com.alttd.chat.objects;
import com.alttd.chat.database.Queries;
import com.alttd.chat.managers.ChatUserManager;
import com.alttd.chat.managers.PartyManager;
import net.kyori.adventure.text.serializer.plain.PlainComponentSerializer;
import java.util.HashMap;
import java.util.UUID;
import java.util.*;
import java.util.List;
public class Party {
@@ -11,7 +14,7 @@ public class Party {
private UUID ownerUuid;
private String partyName;
private String partyPassword;
private HashMap<UUID, ChatUser> partyUsers; //TODO might need to be a map?
private final HashMap<UUID, String> partyUsers; //TODO might need to be a map?
public Party(int partyId, UUID ownerUuid, String partyName, String partyPassword) {
this.partyId = partyId;
@@ -21,18 +24,23 @@ public class Party {
partyUsers = new HashMap<>();
}
public void addUser(HashMap<UUID, ChatUser> partyUsers) {
this.partyUsers.putAll(partyUsers);
public void putUser(UUID uuid, String displayName) {
this.partyUsers.put(uuid, displayName);
}
public void addUser(ChatUser partyUser) {
this.partyUsers.put(partyUser.getUuid(), partyUser);
Queries.addUser(partyUser);
this.partyUsers.put(partyUser.getUuid(), PlainComponentSerializer.plain().serialize(partyUser.getDisplayName()));
partyUser.setPartyId(getPartyId());
Queries.addPartyUser(partyUser);
}
public void removeUser(UUID uuid) {
removeUser(ChatUserManager.getChatUser(uuid));
}
public void removeUser(ChatUser partyUser) {
partyUsers.remove(partyUser.getUuid());
Queries.removeUser(partyUser.getUuid());
Queries.removePartyUser(partyUser.getUuid());
}
public int getPartyId() {
@@ -43,6 +51,12 @@ public class Party {
return ownerUuid;
}
public UUID newOwner() {
UUID uuid = partyUsers.keySet().iterator().next();
setOwnerUuid(uuid);
return uuid;
}
public void setOwnerUuid(UUID ownerUuid) {
this.ownerUuid = ownerUuid;
Queries.setPartyOwner(ownerUuid, partyId); //TODO: Async pls
@@ -70,11 +84,20 @@ public class Party {
return !partyPassword.isEmpty();
}
public HashMap<UUID, ChatUser> getPartyUsers() {
public HashMap<UUID, String> getPartyUsers() {
return partyUsers;
}
public void setPartyUsers(HashMap<UUID, ChatUser> partyUsers) {
this.partyUsers = partyUsers;
public void delete() {
Queries.removeParty(partyId);
PartyManager.removeParty(this);
}
public List<UUID> getPartyUsersUuid() {
return new ArrayList<>(partyUsers.keySet());
}
public String getUserDisplayName(UUID uuid) {
return partyUsers.get(uuid);
}
}
@@ -1,20 +1,20 @@
package com.alttd.chat.objects;
package com.alttd.chat.objects.channels;
import java.util.*;
import java.util.Collection;
import java.util.HashMap;
public abstract class Channel {
public class Channel {
public static HashMap<String, Channel> channels = new HashMap<>();
private String permission;
private String channelName;
private String format;
private List<String> servers;
private boolean proxy;
protected String permission;
protected String channelName;
protected String format;
protected boolean proxy;
public Channel(String channelName, String format, List<String> servers, boolean proxy) {
public Channel(String channelName, String format, boolean proxy) {
this.permission = "chat.channel." + channelName.toLowerCase();
this.channelName = channelName;
this.format = format;
this.servers = servers;
this.proxy = proxy;
channels.put(channelName.toLowerCase(), this);
}
@@ -35,10 +35,6 @@ public class Channel {
return format;
}
public List<String> getServers() {
return servers;
}
public boolean isProxy() {
return proxy;
}
@@ -0,0 +1,20 @@
package com.alttd.chat.objects.channels;
import java.util.*;
public class CustomChannel extends Channel {
private final List<String> servers;
public CustomChannel(String channelName, String format, List<String> servers, boolean proxy) {
super(channelName, format, proxy);
this.permission = "chat.channel." + channelName.toLowerCase();
this.channelName = channelName;
this.format = format;
this.servers = servers;
this.proxy = proxy;
channels.put(channelName.toLowerCase(), this);
}
public List<String> getServers() {
return servers;
}
}
@@ -0,0 +1,7 @@
package com.alttd.chat.objects.channels;
public class DefaultChannel extends Channel{
public DefaultChannel(String channelName, String format, boolean proxy) {
super(channelName, format, proxy);
}
}