Fix Groups

This commit is contained in:
RayzsYT committed 2024-05-21 20:11:07 +02:00
1 parent 155997dc21
commit 851dcee347
6 files changed
+68 -31

No files matched your search

@@ -14,11 +14,11 @@ public class BlacklistCreator {
}
public static GroupBlacklist createGroupBlacklist(String group) {
return createGroupBlacklist(group, null);
return new GroupBlacklist(group, ".commands");
}
public static GroupBlacklist createGroupBlacklist(String group, String server) {
return new GroupBlacklist(group, (server != null ? ".servers." + server : ".commands"));
return new GroupBlacklist(group, (server != null ? ".servers." + server : "") + ".commands");
}
public static boolean exist(String server) {
@@ -6,7 +6,7 @@ import de.rayzs.pat.api.storage.storages.BlacklistStorage;
import java.io.Serializable;
import java.util.List;
public class GroupBlacklist extends BlacklistStorage implements Serializable {
public class GroupBlacklist extends BlacklistStorage {
public GroupBlacklist(String group, String navigatePath) { super("groups." + group + navigatePath); }
@@ -21,9 +21,7 @@ public class GroupBlacklist extends BlacklistStorage implements Serializable {
}
@Override
public BlacklistStorage remove(String command) {
return super.remove(command);
}
public BlacklistStorage remove(String command) { return super.remove(command); }
@Override
public BlacklistStorage clear() {
@@ -57,7 +57,7 @@ public class BlacklistStorage extends StorageTemplate implements Serializable {
}
public BlacklistStorage add(String command) {
getConfig().reload();
//getConfig().reload();
command = command.toLowerCase();
if(!commands.contains(command))
commands.add(command);
@@ -65,14 +65,14 @@ public class BlacklistStorage extends StorageTemplate implements Serializable {
}
public BlacklistStorage remove(String command) {
getConfig().reload();
//getConfig().reload();
command = command.toLowerCase();
commands.remove(command);
return this;
}
public BlacklistStorage clear() {
getConfig().reload();
//getConfig().reload();
commands.clear();
return this;
}
@@ -1,5 +1,6 @@
package de.rayzs.pat.utils.group;
import de.rayzs.pat.api.storage.Storage;
import de.rayzs.pat.api.storage.blacklist.impl.GroupBlacklist;
import de.rayzs.pat.utils.configuration.ConfigurationBuilder;
import de.rayzs.pat.api.storage.blacklist.BlacklistCreator;
@@ -10,7 +11,6 @@ import java.util.*;
public class Group implements Serializable {
private final HashMap<String, GroupBlacklist> serverGroupBlacklist = new HashMap<>();
private final GroupBlacklist generalGroupBlacklist;
private final String groupName;
@@ -53,10 +53,6 @@ public class Group implements Serializable {
generalGroupBlacklist.clear().save();
}
public Set<String> getServerNames() {
return serverGroupBlacklist.keySet();
}
public void clear(String server) {
GroupBlacklist serverGroupBlacklist = BlacklistCreator.createGroupBlacklist(groupName, server);
serverGroupBlacklist.clear().save();
@@ -75,7 +71,7 @@ public class Group implements Serializable {
}
public boolean contains(String command, String server) {
return generalGroupBlacklist.isListed(command) || getOrCreateGroupBlacklist(server).isListed(command);
return generalGroupBlacklist.isListed(command) || GroupManager.getOrCreateGroupList(groupName, server).getOrCreateGroupBlacklist(server).isListed(command);
}
public List<String> getCommands() {
@@ -84,29 +80,17 @@ public class Group implements Serializable {
public List<String> getCommands(String server) {
List<String> commands = new ArrayList<>(generalGroupBlacklist.getCommands());
commands.addAll(getOrCreateGroupBlacklist(server).getCommands());
GroupServer groupServer = GroupManager.getOrCreateGroupList(getGroupName(), server);
commands.addAll(groupServer.getOrCreateGroupBlacklist(server).getCommands());
return commands;
}
public GroupBlacklist getOrCreateGroupBlacklist(String server) {
GroupBlacklist groupBlacklist = null;
if(!serverGroupBlacklist.containsKey(server)) {
groupBlacklist = BlacklistCreator.createGroupBlacklist(groupName, server);
groupBlacklist.load();
serverGroupBlacklist.put(server, groupBlacklist);
}
return serverGroupBlacklist.getOrDefault(server, groupBlacklist);
}
public void deleteGroup() {
deleteGroup(null);
}
public void deleteGroup(String server) {
GroupBlacklist groupBlacklist = server == null ? generalGroupBlacklist : serverGroupBlacklist.get(server);
if(groupBlacklist == null) return;
groupBlacklist.getConfig().setAndSave("groups." + groupName + (server != null ? "." + server : ""), null);
Storage.Files.STORAGE.setAndSave("groups." + groupName + (server != null ? "." + server : ""), null);
}
public ConfigurationBuilder getConfig() {
@@ -1,12 +1,15 @@
package de.rayzs.pat.utils.group;
import de.rayzs.pat.api.storage.Storage;
import de.rayzs.pat.api.storage.blacklist.BlacklistCreator;
import de.rayzs.pat.api.storage.blacklist.impl.GroupBlacklist;
import de.rayzs.pat.utils.*;
import java.util.*;
public class GroupManager {
private static final List<Group> GROUPS = new ArrayList<>();
private static final HashMap<String, GroupServer> SERVER_GROUP_BLACKLIST = new HashMap<>();
public static void initialize() {
if(Reflection.isProxyServer()) Storage.Files.STORAGE.getKeys("groups", true).forEach(GroupManager::registerGroup);
@@ -106,7 +109,10 @@ public class GroupManager {
public static List<Group> getGroupsByServer(String server) {
List<Group> result = new ArrayList<>();
GROUPS.stream().filter(group -> Storage.isServer(server, group.getServerNames())).forEach(result::add);
GROUPS.stream().filter(group -> SERVER_GROUP_BLACKLIST.containsKey(group.getGroupName())).forEach(group -> {
GroupServer groupServer = SERVER_GROUP_BLACKLIST.get(group.getGroupName());
groupServer.getAllServers().stream().filter(currentServer -> Storage.isServer(server, currentServer)).forEach(currentServer -> result.add(getGroupByName(groupServer.getGroupName())));
});
return result;
}
@@ -134,6 +140,17 @@ public class GroupManager {
return result;
}
public static GroupServer getOrCreateGroupList(String groupName, String server) {
GroupServer groupServer = null;
if(!SERVER_GROUP_BLACKLIST.containsKey(server)) {
System.out.println("New groupserver: " + groupName);
groupServer = new GroupServer(groupName);
SERVER_GROUP_BLACKLIST.put(server, groupServer);
} else System.out.println("Already existing groupserver: " + groupName);
return SERVER_GROUP_BLACKLIST.getOrDefault(server, groupServer);
}
public static TinyGroup convertToTinyGroup(String groupName, List<String> commands) {
return new TinyGroup(groupName, commands);
}
@@ -0,0 +1,38 @@
package de.rayzs.pat.utils.group;
import de.rayzs.pat.api.storage.blacklist.BlacklistCreator;
import de.rayzs.pat.api.storage.blacklist.impl.GroupBlacklist;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class GroupServer {
private final HashMap<String, GroupBlacklist> groupServerBlacklist = new HashMap<>();
private final String groupName;
public GroupServer(String groupName) {
this.groupName = groupName;
}
public String getGroupName() {
return groupName;
}
public List<String> getAllServers() {
return new ArrayList<>(groupServerBlacklist.keySet());
}
public GroupBlacklist getOrCreateGroupBlacklist(String server) {
GroupBlacklist groupBlacklist = null;
if(!groupServerBlacklist.containsKey(server)) {
System.out.println("New group: " + groupName);
groupBlacklist = BlacklistCreator.createGroupBlacklist(groupName, server);
groupBlacklist.load();
groupServerBlacklist.put(server, groupBlacklist);
} else System.out.println("Already existing group: " + groupName);
return groupServerBlacklist.getOrDefault(server, groupBlacklist);
}
}