Skip to content
This repository has been archived by the owner on Jan 20, 2024. It is now read-only.

Commit

Permalink
FConfiguration: rework getting the list of groups
Browse files Browse the repository at this point in the history
  • Loading branch information
TheFaser committed Nov 5, 2023
1 parent 6ec42f0 commit 8f05eec
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 28 deletions.
32 changes: 14 additions & 18 deletions src/main/java/net/flectone/chat/model/file/FConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,19 @@ public void load() {

@NotNull
public List<String> getCustomList(@Nullable Player player, @NotNull String string) {
String[] playerGroups = IntegrationsModule.getGroups(player);
if (playerGroups == null) playerGroups = new String[]{"default"};
ArrayList<String> playerGroups = IntegrationsModule.getGroups(player);
if (playerGroups == null) playerGroups = new ArrayList<>(List.of("default"));
else if (!playerGroups.contains("default")) playerGroups.add("default");

ArrayList<String> arrayList = new ArrayList<>();
for (String group : playerGroups) {
ConfigurationSection configurationSection = this.getConfigurationSection(group + string);
if (configurationSection == null) {
configurationSection = this.getConfigurationSection(group + "." + string);
if (configurationSection == null) continue;
}
ConfigurationSection configurationSection = this.getConfigurationSection(group + "." + string);
if (configurationSection == null) continue;

arrayList.addAll(configurationSection.getKeys(false));
}

return arrayList.isEmpty()
? this.getStringList("default." + string)
: arrayList;
return arrayList;
}

@NotNull
Expand All @@ -80,13 +76,13 @@ public String getString(@NotNull String string) {

@NotNull
public String getVaultString(@Nullable CommandSender sender, @NotNull String string) {
String[] playerGroups = null;
List<String> playerGroups = null;

if (sender instanceof Player player) {
playerGroups = IntegrationsModule.getGroups(player);
}

if (playerGroups == null) playerGroups = new String[]{"default"};
if (playerGroups == null) playerGroups = List.of("default");

String vaultString = "";

Expand All @@ -104,13 +100,13 @@ public String getVaultString(@Nullable CommandSender sender, @NotNull String str
}

public boolean getVaultBoolean(@Nullable CommandSender sender, @NotNull String string) {
String[] playerGroups = null;
List<String> playerGroups = null;

if (sender instanceof Player player) {
playerGroups = IntegrationsModule.getGroups(player);
}

if (playerGroups == null) playerGroups = new String[]{"default"};
if (playerGroups == null) playerGroups = List.of("default");

String vaultString = "";

Expand All @@ -128,13 +124,13 @@ public boolean getVaultBoolean(@Nullable CommandSender sender, @NotNull String s
}

public int getVaultInt(@NotNull CommandSender sender, @NotNull String string) {
String[] playerGroups = null;
List<String> playerGroups = null;

if (sender instanceof Player player) {
playerGroups = IntegrationsModule.getGroups(player);
}

if (playerGroups == null) playerGroups = new String[]{"default"};
if (playerGroups == null) playerGroups = List.of("default");

String vaultString = "";

Expand All @@ -152,13 +148,13 @@ public int getVaultInt(@NotNull CommandSender sender, @NotNull String string) {
}

public List<String> getVaultStringList(@NotNull CommandSender sender, @NotNull String string) {
String[] playerGroups = null;
List<String> playerGroups = null;

if (sender instanceof Player player) {
playerGroups = IntegrationsModule.getGroups(player);
}

if (playerGroups == null) playerGroups = new String[]{"default"};
if (playerGroups == null) playerGroups = List.of("default");

ArrayList<String> arrayList = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command
}

if (args[0].equalsIgnoreCase("default")) {
List<String> colorsKey = config.getCustomList(cmdSettings.getSender(), ".color.list");
List<String> colorsKey = config.getCustomList(cmdSettings.getSender(), "color.list");

for (String colorKey : colorsKey) {
colors.put(colorKey, config.getVaultString(cmdSettings.getSender(), "color.list." + colorKey));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;

public class FLuckPerms implements FIntegration {

public FLuckPerms() {
Expand Down Expand Up @@ -67,16 +69,14 @@ public String getSuffix(@NotNull Player player) {
return user.getCachedData().getMetaData().getSuffix();
}

@NotNull
public String[] getGroups(@NotNull Player player) {
@Nullable
public ArrayList<String> getGroups(@NotNull Player player) {
User user = provider.getUserManager().getUser(player.getUniqueId());
if (user == null) return null;

return user.getInheritedGroups(user.getQueryOptions())
return new ArrayList<>(user.getInheritedGroups(user.getQueryOptions())
.stream()
.map(Group::getName)
.toList()
.toArray(new String[]{});

.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

import static net.flectone.chat.manager.FileManager.integrations;
Expand Down Expand Up @@ -71,12 +73,16 @@ public static boolean isVanished(@NotNull Player player) {
}

@Nullable
public static String[] getGroups(@Nullable Player player) {
public static ArrayList<String> getGroups(@Nullable Player player) {
if (player == null) return null;
FIntegration luckPerms = get("LuckPerms");
if (luckPerms != null) return ((FLuckPerms) luckPerms).getGroups(player);
FIntegration vault = get("Vault");
if (vault != null) return new String[]{((FVault) vault).getPrimaryGroup(player)};
if (vault != null) {
String vaultGroup = ((FVault) vault).getPrimaryGroup(player);
if (vaultGroup == null) return null;
return new ArrayList<>(List.of(vaultGroup));
}
return null;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/flectone/chat/util/MessageUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static String formatAll(@Nullable Player sender, @Nullable Player recipie
if (colors == null) {
colors = new HashMap<>();

List<String> colorsKey = config.getCustomList(recipient, ".color.list");
List<String> colorsKey = config.getCustomList(recipient, "color.list");

for (String colorKey : colorsKey) {
colors.put(colorKey, config.getVaultString(recipient, "color.list." + colorKey));
Expand Down

0 comments on commit 8f05eec

Please sign in to comment.