Load all chat users on startup
Load all parties on startup
This commit is contained in:
parent
9957158758
commit
571f02b6df
|
|
@ -1,6 +1,7 @@
|
|||
package com.alttd.chat.database;
|
||||
|
||||
import com.alttd.chat.managers.ChatUserManager;
|
||||
import com.alttd.chat.managers.PartyManager;
|
||||
import com.alttd.chat.objects.ChatUser;
|
||||
import com.alttd.chat.objects.Mail;
|
||||
import com.alttd.chat.objects.Party;
|
||||
|
|
@ -133,8 +134,7 @@ public class Queries {
|
|||
|
||||
//Party
|
||||
|
||||
public static void getParties() {
|
||||
HashMap<Integer, Party> parties = new HashMap<>(); //TODO Replace with a proper way/location to store this in
|
||||
public static void loadParties() {
|
||||
String query = "SELECT * FROM parties";
|
||||
|
||||
try {
|
||||
|
|
@ -149,14 +149,12 @@ public class Queries {
|
|||
String partyName = resultSet.getString("party_name");
|
||||
String password = resultSet.getString("password");
|
||||
|
||||
parties.put(id, new Party(id, ownerUuid, partyName, password));
|
||||
PartyManager.addParty(new Party(id, ownerUuid, partyName, password));
|
||||
}
|
||||
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
getChatUsers(parties); //TODO This parameter should be temporary, it should access the actual list of parties normally
|
||||
}
|
||||
|
||||
public static Party addParty(UUID partyOwner, String partyName, String password) {
|
||||
|
|
@ -292,7 +290,6 @@ public class Queries {
|
|||
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;
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import com.alttd.chat.objects.ChatUser;
|
|||
import com.alttd.chat.objects.Mail;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
|
@ -16,6 +17,7 @@ public final class ChatUserManager {
|
|||
|
||||
public static void initialize() {
|
||||
chatUsers = new ArrayList<>();
|
||||
loadUsers();
|
||||
}
|
||||
|
||||
public static void addUser(ChatUser user) {
|
||||
|
|
@ -32,11 +34,7 @@ public final class ChatUserManager {
|
|||
return user;
|
||||
}
|
||||
}
|
||||
ChatUser user = Queries.loadChatUser(uuid);
|
||||
if(user == null) user = new ChatUser(uuid, -1, false, false);
|
||||
Queries.saveUser(user);
|
||||
chatUsers.add(user);
|
||||
return user;
|
||||
return Queries.loadChatUser(uuid);
|
||||
}
|
||||
|
||||
public List<Mail> getUnReadMail(ChatUser user) {
|
||||
|
|
@ -44,4 +42,12 @@ public final class ChatUserManager {
|
|||
.filter(Mail::isUnRead)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public static void loadUsers() {
|
||||
Queries.loadChatUsers();
|
||||
}
|
||||
|
||||
protected static List<ChatUser> getChatUsers() {
|
||||
return Collections.unmodifiableList(chatUsers);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
47
api/src/main/java/com/alttd/chat/managers/PartyManager.java
Normal file
47
api/src/main/java/com/alttd/chat/managers/PartyManager.java
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
package com.alttd.chat.managers;
|
||||
|
||||
import com.alttd.chat.database.Queries;
|
||||
import com.alttd.chat.objects.ChatUser;
|
||||
import com.alttd.chat.objects.Party;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.UUID;
|
||||
|
||||
public class PartyManager {
|
||||
|
||||
private static ArrayList<Party> parties;
|
||||
|
||||
public static void initialize() {
|
||||
parties = new ArrayList<>();
|
||||
loadParties();
|
||||
}
|
||||
|
||||
public static void addParty(Party party) {
|
||||
parties.add(party);
|
||||
}
|
||||
|
||||
public static void removeParty(Party party) {
|
||||
parties.remove(party);
|
||||
}
|
||||
|
||||
public static Party getParty(int id) {
|
||||
for(Party party : parties) {
|
||||
if(id == party.getPartyId()) {
|
||||
return party;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Party getParty(UUID uuid) {
|
||||
return getParty(ChatUserManager.getChatUser(uuid).getPartyId());
|
||||
}
|
||||
|
||||
public static void loadParties() {
|
||||
Queries.loadParties();
|
||||
for (ChatUser chatUser : ChatUserManager.getChatUsers()) {
|
||||
Party party = getParty(chatUser.getPartyId());
|
||||
if (party != null) party.addUser(chatUser);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -29,6 +29,8 @@ public class PlayerListener implements Listener {
|
|||
ChatUser chatUser = new ChatUser(uuid, -1, false, false);
|
||||
ChatUserManager.addUser(chatUser);
|
||||
Queries.saveUser(chatUser);
|
||||
|
||||
//TODO load player on other servers with plugin message?
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user