auto commit

This commit is contained in:
destro174
2021-07-27 18:46:58 +02:00
parent 04ac327365
commit 0891e252f9
25 changed files with 362 additions and 70 deletions
@@ -32,7 +32,7 @@ public final class Config {
public static File CONFIGPATH;
public static void init() { // todo setup share for the config
CONFIGPATH = new File(System.getProperty("user.home")+File.separator+"ChatPlugin");
CONFIGPATH = new File(System.getProperty("user.home") + File.separator + "share" + File.separator + "ChatPlugin");
CONFIG_FILE = new File(CONFIGPATH, "config.yml");;
configLoader = YAMLConfigurationLoader.builder()
.setFile(CONFIG_FILE)
@@ -175,8 +175,8 @@ public final class Config {
public static List<String> MESSAGECOMMANDALIASES = new ArrayList<>();
public static List<String> REPLYCOMMANDALIASES = new ArrayList<>();
public static String MESSAGESENDER = "<hover:show_text:Click to reply><click:suggest_command:/msg <receiver> ><light_purple>(Me -> <gray><receiver></gray>) <message>";
public static String MESSAGERECIEVER = "<hover:show_text:Click to reply><click:suggest_command:/msg <sender> ><light_purple>(<gray><sender></gray> on <server> -> Me) <message>";
public static String MESSAGESENDER = "<hover:show_text:Click to reply><click:suggest_command:/msg <receivername> ><light_purple>(Me -> <gray><receiver></gray>)</hover> <message>";
public static String MESSAGERECIEVER = "<hover:show_text:Click to reply><click:suggest_command:/msg <sendername> ><light_purple>(<gray><sender></gray> on <server> -> Me)</hover> <message>";
private static void messageCommand() {
MESSAGECOMMANDALIASES.clear();
REPLYCOMMANDALIASES.clear();
@@ -185,7 +185,7 @@ public final class Config {
MESSAGESENDER = getString("commands.message.sender-message", MESSAGESENDER);
MESSAGERECIEVER = getString("commands.message.reciever-message", MESSAGERECIEVER);
}
///broadcast <white><light_purple><prefix></light_purple> <gray>Momlly</gray> <hover:show_text:on Atoll><yellow>to Global</yellow></hover><gray>: We Love <gold>Teri</gold> and <light_purple>Kappa</light_purple></gray></white>
public static String GCFORMAT = "<white><light_purple><prefix></light_purple> <gray><sender></gray> <hover:show_text:on <server>><yellow>to Global</yellow></hover><gray>: <message>";
public static String GCPERMISSION = "proxy.globalchat";
public static List<String> GCALIAS = new ArrayList<>();
@@ -193,7 +193,7 @@ public final class Config {
public static String GCONCOOLDOWN = "You have to wait <cooldown> seconds before using this feature again."; // todo mini message formatting
public static int GCCOOLDOWN = 30;
private static void globalChat() {
MESSAGERECIEVER = getString("commands.globalchat.format", MESSAGERECIEVER);
GCFORMAT = getString("commands.globalchat.format", GCFORMAT);
GCPERMISSION = getString("commands.globalchat.view-chat-permission", GCPERMISSION);
GCALIAS.clear();
GCALIAS = getList("commands.globalchat.alias", Lists.newArrayList("gc", "global"));
@@ -202,13 +202,13 @@ public final class Config {
}
// TODO prefixes need hovers, this hasn't been setup yet!
public static String CHATFORMAT = "<white><light_purple><prefixall> <gray><sender>: <message>";
public static String CHATFORMAT = "<white><light_purple><prefixall> <gray><sender>: <white><message>";
private static void Chat() {
CHATFORMAT = getString("chat.format", CHATFORMAT);
}
public static List<String> GACECOMMANDALIASES = new ArrayList<>();
public static String GACFORMAT = "<hover:show_text:Click to reply><click:suggest_command:/acg ><yellow>(<sender> on <server> -> Team) <message>";
public static String GACFORMAT = "<hover:show_text:Click to reply><click:suggest_command:/acg ><yellow>(<sender> on <server> -> Team)</hover> <message>";
private static void globalAdminChat() {
GACECOMMANDALIASES = getList("commands.globaladminchat.aliases", Lists.newArrayList("acg"));
GACFORMAT = getString("commands.globaladminchat.format", GACFORMAT);
@@ -2,6 +2,7 @@ package com.alttd.chat.database;
import com.alttd.chat.managers.ChatUserManager;
import com.alttd.chat.objects.ChatUser;
import com.alttd.chat.objects.Mail;
import com.alttd.chat.objects.Party;
import java.sql.Connection;
@@ -19,6 +20,7 @@ public class Queries {
tables.add("CREATE TABLE IF NOT EXISTS ignored_users (`uuid` VARCHAR(36) NOT NULL, `ignored_uuid` VARCHAR(36) NOT NULL, PRIMARY KEY (`uuid`, `ignored_uuid`))");
tables.add("CREATE TABLE IF NOT EXISTS parties (`id` INT NOT NULL AUTO_INCREMENT, `owner_uuid` VARCHAR(36) NOT NULL, `party_name` VARCHAR(36) NOT NULL, `password` VARCHAR(36), PRIMARY KEY (`id`))");
tables.add("CREATE TABLE IF NOT EXISTS chat_users (`uuid` VARCHAR(36) NOT NULL, `party_id` INT NOT NULL, `toggled_chat` BIT(1) DEFAULT b'0', `toggled_gc` BIT(1) DEFAULT b'0', PRIMARY KEY (`uuid`))");
tables.add("CREATE TABLE IF NOT EXISTS mails (`id` INT NOT NULL AUTO_INCREMENT, `uuid` VARCHAR(36) NOT NULL, `from` VARCHAR(36) NOT NULL, `message` VARCHAR(256) NOT NULL, `sendtime` BIGINT default 0, `readtime` BIGINT default 0, PRIMARY KEY (`id`))");
try {
Connection connection = DatabaseConnection.getConnection();
@@ -295,8 +297,7 @@ public class Queries {
int partyId = resultSet.getInt("party_id");
boolean toggled_chat = resultSet.getInt("toggled_chat") == 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, toggle_Gc));
ChatUserManager.addUser(new ChatUser(uuid, partyId, toggled_chat, toggle_Gc));
}
} catch (SQLException e) {
@@ -304,6 +305,31 @@ public class Queries {
}
}
public static ChatUser loadChatUser(UUID uuid) { //TODO Get parties from cache somewhere
String query = "SELECT * FROM chat_users WHERE uuid = ?";
ChatUser user = null;
try {
Connection connection = DatabaseConnection.getConnection();
PreparedStatement statement = connection.prepareStatement(query);
statement.setString(1, uuid.toString());
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
int partyId = resultSet.getInt("party_id");
boolean toggled_chat = resultSet.getInt("toggled_chat") == 1;
boolean toggle_Gc = resultSet.getInt("toggled_gc") == 1;
user = new ChatUser(uuid, partyId, toggled_chat, toggle_Gc);
}
} catch (SQLException e) {
e.printStackTrace();
}
return user;
}
public static void addUser(ChatUser user) {
String query = "INSERT INTO chat_users (uuid, party_id, toggled_chat, toggled_gc) VALUES (?, ?, ?, ?)";
@@ -326,6 +352,10 @@ public class Queries {
setBitWhereId("UPDATE chat_users set toggled_chat = ? WHERE uuid = ?", toggledChat, uuid);
}
public static void setGlobalChatState(boolean globalChat, UUID uuid) {
setBitWhereId("UPDATE chat_users set toggled_gc = ? WHERE uuid = ?", globalChat, uuid);
}
private static void setBitWhereId(String query, boolean bool, UUID uuid) {
try {
Connection connection = DatabaseConnection.getConnection();
@@ -356,4 +386,48 @@ public class Queries {
}
//-----------------------------------------
public static LinkedList<Mail> getMails(UUID uuid) {
LinkedList<Mail> mails = new LinkedList<>();
String query = "SELECT * FROM mails where uuid = ?";
try {
Connection connection = DatabaseConnection.getConnection();
PreparedStatement statement = connection.prepareStatement(query);
statement.setString(1, uuid.toString());
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
UUID fromUUID = UUID.fromString(resultSet.getString("from"));
String message = resultSet.getString("message");
long sendTime = resultSet.getLong("sendtime");
long readTime = resultSet.getLong("readtime");
mails.add(new Mail(uuid, fromUUID, sendTime, readTime, message));
}
} catch (SQLException e) {
e.printStackTrace();
}
return mails;
}
public static void saveUser(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, user.isGcOn() ? 1 : 0);
statement.execute();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
@@ -1,5 +1,6 @@
package com.alttd.chat.managers;
import com.alttd.chat.database.Queries;
import com.alttd.chat.objects.ChatUser;
import com.alttd.chat.objects.Mail;
@@ -14,12 +15,14 @@ public final class ChatUserManager {
public static void initialize() {
chatUsers = new ArrayList<>();
//Queries.loadChatUsers(); // todo fix sql
}
public static void addUser(ChatUser user) {
if(getChatUser(user.getUuid()) == null)
chatUsers.add(user);
chatUsers.add(user);
}
public static void removeUser(ChatUser user) {
chatUsers.remove(user);
}
public static ChatUser getChatUser(UUID uuid) {
@@ -28,9 +31,11 @@ public final class ChatUserManager {
return user;
}
}
ChatUser user = new ChatUser(uuid, -1, false, false);
ChatUser user = Queries.loadChatUser(uuid);
if(user == null) user = new ChatUser(uuid, -1, false, false);
Queries.saveUser(user);
chatUsers.add(user);
return user; // create a new user?
return user;
}
public List<Mail> getUnReadMail(ChatUser user) {
@@ -13,9 +13,9 @@ public class ChatUser {
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
private Component prefix; // doesn't need saving, we get this from luckperms
private Component staffPrefix; // doesn't need saving, we get this from luckperms
private Component prefixAll; // doesn't need saving, we get this from luckperms
// private Component prefix; // doesn't need saving, we get this from luckperms
// private Component staffPrefix; // doesn't need saving, we get this from luckperms
// private Component prefixAll; // doesn't need saving, we get this from luckperms
private boolean toggleGc; // should be saved, this toggles if the player can see and use global chat
private String replyTarget; // reply target for use in /msg i don't mind setting this to null on login, feedback?
private long gcCooldown; // the time when they last used gc, is used for the cooldown, i wouldn't save this, but setting this to the login time means they can't use gc for 30 seconds after logging in
@@ -35,15 +35,15 @@ public class ChatUser {
}
setDisplayName(name);
prefix = Utility.getPrefix(uuid, true);
staffPrefix = Utility.getStaffPrefix(uuid);
prefixAll = Utility.getPrefix(uuid, false);
// prefix = Utility.getPrefix(uuid, true); // TODO we need to update this, so cache and update when needed or always request it?
// staffPrefix = Utility.getStaffPrefix(uuid);
//
// prefixAll = Utility.getPrefix(uuid, false);
this.toggleGc = toggleGc;
replyTarget = null;
gcCooldown = System.currentTimeMillis(); // players can't use gc for 30 seconds after logging in if we use this?
mails = new LinkedList<>(); // todo load mails
mails = Queries.getMails(uuid);
ignoredPlayers = Queries.getIgnoredUsers(uuid);
ignoredBy = new LinkedList<>(); // todo load ignoredPlayers
}
@@ -74,15 +74,18 @@ public class ChatUser {
}
public Component getPrefix() {
return prefix;
//return prefix;
return Utility.getPrefix(uuid, true); // No longer cache this data
}
public Component getStaffPrefix() {
return staffPrefix;
//return staffPrefix;
return Utility.getStaffPrefix(uuid);
}
public Component getPrefixAll() {
return prefixAll;
//return prefixAll;
return Utility.getPrefix(uuid, false);
}
public void toggleGc() {
@@ -117,6 +120,10 @@ public class ChatUser {
ignoredPlayers.add(uuid);
}
public void removeIgnoredPlayers(UUID uuid) {
ignoredPlayers.remove(uuid);
}
public LinkedList<UUID> getIgnoredBy() {
return ignoredBy;
}
@@ -6,20 +6,26 @@ public class Mail {
private final UUID uuid; // the player
private final UUID sender; // the sender
private boolean read;
private final long sendTime; // any other option for this? does the db store recordcreation and edit time?
private long readTime; // any other option for this?
private final String message; // do we want staff to edit this after being send but being unread?
public Mail(UUID player, UUID sender, Boolean read, long sendTime, long readTime, String message) {
public Mail(UUID player, UUID sender, long sendTime, long readTime, String message) {
this.uuid = player;
this.sender = sender;
this.read = read;
this.sendTime = sendTime;
this.readTime = readTime;
this.message = message;
}
public Mail(UUID player, UUID sender, String message) {
this.uuid = player;
this.sender = sender;
this.sendTime = System.nanoTime();
this.readTime = System.nanoTime();
this.message = message;
}
public UUID getUuid() {
return uuid;
}
@@ -29,11 +35,7 @@ public class Mail {
}
public boolean isUnRead() {
return read;
}
public void setRead(boolean read) {
this.read = read;
return getSendTime() != getReadTime();
}
public long getSendTime() {
@@ -72,7 +72,7 @@ public class Utility {
prefix.append(user.getCachedData().getMetaData().getPrefix());
}
return LegacyComponentSerializer.builder().character('&').hexColors().build().deserialize(prefix.toString());
return applyColor(prefix.toString());
}
public static Component getStaffPrefix(UUID uuid) {
@@ -85,7 +85,7 @@ public class Utility {
if(group != null)
prefix.append(group.getCachedData().getMetaData().getPrefix());
}
return LegacyComponentSerializer.builder().character('&').hexColors().build().deserialize(prefix.toString());
return applyColor(prefix.toString());
}
public static String getDisplayName(UUID uuid) {