Skip to content

Commit

Permalink
feat: complete command, reload on changes with command and fixes many…
Browse files Browse the repository at this point in the history
… bugs
  • Loading branch information
ezTxmMC committed Jul 27, 2024
1 parent 1ddef15 commit 44c18e0
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public final class LuckPrefix extends JavaPlugin {
private PlayerManager playerManager;
private GroupManager groupManager;
private GroupListener groupListener;
private UpdateChecker updateChecker;

@Override
public void onEnable() {
Expand All @@ -52,6 +53,7 @@ public void onEnable() {
groupListener.createGroup();
groupListener.deleteGroup();
groupManager.loadGroups();
updateChecker = new UpdateChecker(getDescription().getVersion());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,25 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, @N
return false;
}
if (args.length < 1) {
adventurePlayer.sendMessage(new TextUtil(LuckPrefix.getInstance().getPrefix() + "<#ff3333>This command needs arguments to work.").miniMessage());
adventurePlayer.sendMessage(new TextUtil("""
<dark_gray><st>------------</st><#77ef77>LuckPrefix<dark_gray><st>------------</st>
<dark_gray>» <gray>/luckprefix group <name> prefix - Shows the current prefix
<dark_gray>» <gray>/luckprefix group <name> prefix set <string> - Set the current prefix
<dark_gray>» <gray>/luckprefix group <name> suffix - Shows the current suffix
<dark_gray>» <gray>/luckprefix group <name> suffix set <string> - Set the current suffix
<dark_gray>» <gray>/luckprefix group <name> tabformat - Shows the current tabformat
<dark_gray>» <gray>/luckprefix group <name> tabformat set <string> - Set the current tabformat
<dark_gray>» <gray>/luckprefix group <name> chatformat - Shows the current chatformat
<dark_gray>» <gray>/luckprefix group <name> chatformat set <string> - Set the current chatformat
<dark_gray>» <gray>/luckprefix group <name> sortid - Shows the current sortid
<dark_gray>» <gray>/luckprefix group <name> sortid set <string> - Set the current sortid
<dark_gray>» <gray>/luckprefix group <name> namecolor - Shows the current namecolor
<dark_gray>» <gray>/luckprefix group <name> namecolor set <string> - Set the current namecolor
<dark_gray><st>------------</st><#77ef77>LuckPrefix<dark_gray><st>------------</st>""").miniMessage());
return false;
}
switch (args[0]) {
case "group" -> {
ConfigManager groupsFile = LuckPrefix.getInstance().getGroupsFile();
FileConfiguration groupsConfig = LuckPrefix.getInstance().getGroupsFile().getConfiguration();
LuckPerms luckPerms = LuckPrefix.getInstance().getLuckPerms();
Group group = luckPerms.getGroupManager().getGroup(args[1]);
if (args.length < 3) {
adventurePlayer.sendMessage(new TextUtil("""
<dark_gray><st>------------</st><#77ef77>LuckPrefix<dark_gray><st>------------</st>
Expand All @@ -69,6 +79,10 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, @N
<dark_gray><st>------------</st><#77ef77>LuckPrefix<dark_gray><st>------------</st>""").miniMessage());
return false;
}
ConfigManager groupsFile = LuckPrefix.getInstance().getGroupsFile();
FileConfiguration groupsConfig = LuckPrefix.getInstance().getGroupsFile().getConfiguration();
LuckPerms luckPerms = LuckPrefix.getInstance().getLuckPerms();
Group group = luckPerms.getGroupManager().getGroup(args[1]);
if (group == null) {
adventurePlayer.sendMessage(new TextUtil(LuckPrefix.getInstance().getPrefix() + "<#ff3333>This group doesn't exist.").miniMessage());
return false;
Expand All @@ -83,7 +97,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, @N
builder.append(" ").append(args[i]);
}
groupsConfig.set(group.getName().toLowerCase() + ".Prefix", builder.toString());
groupsFile.saveConfiguration();
groupsFile.reloadConfig();
return true;
}
String prefix = groupsConfig.getString(group.getName().toLowerCase() + ".Prefix");
Expand All @@ -98,7 +112,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, @N
builder.append(" ").append(args[i]);
}
groupsConfig.set(group.getName().toLowerCase() + ".Suffix", builder.toString());
groupsFile.saveConfiguration();
groupsFile.reloadConfig();
return true;
}
String prefix = groupsConfig.getString(group.getName().toLowerCase() + ".Suffix");
Expand All @@ -113,7 +127,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, @N
builder.append(" ").append(args[i]);
}
groupsConfig.set(group.getName().toLowerCase() + ".Chatformat", builder.toString());
groupsFile.saveConfiguration();
groupsFile.reloadConfig();
return true;
}
String prefix = groupsConfig.getString(group.getName().toLowerCase() + ".Chatformat");
Expand All @@ -128,7 +142,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, @N
builder.append(" ").append(args[i]);
}
groupsConfig.set(group.getName().toLowerCase() + ".Tabformat", builder.toString());
groupsFile.saveConfiguration();
groupsFile.reloadConfig();
return true;
}
String prefix = groupsConfig.getString(group.getName().toLowerCase() + ".Tabformat");
Expand All @@ -141,7 +155,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, @N
try {
int sortID = Integer.parseInt(args[4]);
groupsConfig.set(group.getName().toLowerCase() + ".SortID", sortID);
groupsFile.saveConfiguration();
groupsFile.reloadConfig();
return true;
} catch (NumberFormatException e) {
adventurePlayer.sendMessage(new TextUtil(LuckPrefix.getInstance().getPrefix() + "<#ff3333>This isn't a number.").miniMessage());
Expand All @@ -162,7 +176,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, @N
return false;
}
groupsConfig.set(group.getName().toLowerCase() + ".NameColor", color.name().toLowerCase());
groupsFile.saveConfiguration();
groupsFile.reloadConfig();
return true;
} catch (EnumConstantNotPresentException e) {
adventurePlayer.sendMessage(new TextUtil(LuckPrefix.getInstance().getPrefix() + "<#ff3333>This isn't a number.").miniMessage());
Expand All @@ -181,8 +195,8 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, @N
case "reloadconfig" -> {
adventurePlayer.sendMessage(new TextUtil(LuckPrefix.getInstance().getPrefix() + "Reloading configurations...").miniMessage());
LuckPrefix.getInstance().getConfig().load(new File("plugins/LuckPrefix/config.yml"));
LuckPrefix.getInstance().getDatabaseFile().getConfiguration().load(new File("plugins/LuckPrefix/database.yml"));
LuckPrefix.getInstance().getDatabaseFile().getConfiguration().load(new File("plugins/LuckPrefix/groups.yml"));
LuckPrefix.getInstance().getDatabaseFile().reloadConfig();
LuckPrefix.getInstance().getGroupsFile().reloadConfig();
GroupManager groupManager = LuckPrefix.getInstance().getGroupManager();
for (Player onlinePlayer : Bukkit.getOnlinePlayers()) {
onlinePlayer.getScoreboard().getTeams().forEach(Team::unregister);
Expand Down Expand Up @@ -230,38 +244,72 @@ public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Comman
if (args.length == 4) {
if (args[0].equalsIgnoreCase("group")) {
if (args[2].equalsIgnoreCase("prefix")) {
List<String> arguments = new ArrayList<>(List.of("<text>"));
List<String> arguments = new ArrayList<>(List.of("set"));
arguments.removeIf(argument -> !argument.startsWith(args[3]));
return arguments;
}
if (args[2].equalsIgnoreCase("suffix")) {
List<String> arguments = new ArrayList<>(List.of("<text>"));
List<String> arguments = new ArrayList<>(List.of("set"));
arguments.removeIf(argument -> !argument.startsWith(args[3]));
return arguments;
}
if (args[2].equalsIgnoreCase("tabformat")) {
List<String> arguments = new ArrayList<>(List.of("<text>"));
List<String> arguments = new ArrayList<>(List.of("set"));
arguments.removeIf(argument -> !argument.startsWith(args[3]));
return arguments;
}
if (args[2].equalsIgnoreCase("chatformat")) {
List<String> arguments = new ArrayList<>(List.of("<text>"));
List<String> arguments = new ArrayList<>(List.of("set"));
arguments.removeIf(argument -> !argument.startsWith(args[3]));
return arguments;
}
if (args[2].equalsIgnoreCase("sortID")) {
List<String> arguments = new ArrayList<>(List.of("<number>"));
List<String> arguments = new ArrayList<>(List.of("set"));
arguments.removeIf(argument -> !argument.startsWith(args[3]));
return arguments;
}
if (args[2].equalsIgnoreCase("color")) {
List<String> arguments = new ArrayList<>(List.of("set"));
arguments.removeIf(argument -> !argument.startsWith(args[3]));
return arguments;
}
}
}
if (args.length == 5) {
if (args[0].equalsIgnoreCase("group")) {
if (args[2].equalsIgnoreCase("prefix")) {
List<String> arguments = new ArrayList<>(List.of("<text>"));
arguments.removeIf(argument -> !argument.startsWith(args[4]));
return arguments;
}
if (args[2].equalsIgnoreCase("suffix")) {
List<String> arguments = new ArrayList<>(List.of("<text>"));
arguments.removeIf(argument -> !argument.startsWith(args[4]));
return arguments;
}
if (args[2].equalsIgnoreCase("tabformat")) {
List<String> arguments = new ArrayList<>(List.of("<text>"));
arguments.removeIf(argument -> !argument.startsWith(args[4]));
return arguments;
}
if (args[2].equalsIgnoreCase("chatformat")) {
List<String> arguments = new ArrayList<>(List.of("<text>"));
arguments.removeIf(argument -> !argument.startsWith(args[4]));
return arguments;
}
if (args[2].equalsIgnoreCase("sortID")) {
List<String> arguments = new ArrayList<>(List.of("<number>"));
arguments.removeIf(argument -> !argument.startsWith(args[4]));
return arguments;
}
if (args[2].equalsIgnoreCase("color")) {
List<String> colors = new ArrayList<>();
for (ChatColor color : ChatColor.values()) {
if (color.isColor() && !color.isFormat()) {
colors.add(color.name().toUpperCase());
}
}
colors.removeIf(argument -> !argument.startsWith(args[3]));
colors.removeIf(argument -> !argument.startsWith(args[4]));
return colors;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import de.eztxm.luckprefix.util.PlayerManager;
import de.eztxm.luckprefix.util.TextUtil;
import de.eztxm.luckprefix.util.UpdateChecker;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import net.luckperms.api.LuckPerms;
import net.luckperms.api.LuckPermsProvider;
import net.luckperms.api.model.user.User;
Expand All @@ -31,17 +30,13 @@ public void onJoin(PlayerJoinEvent event) {
FileConfiguration config = LuckPrefix.getInstance().getConfig();
playerManager.initializePlayer(player.getUniqueId(), group);
groupManager.setupGroups(player);
BukkitTask bukkitTask = Bukkit.getScheduler().runTaskTimerAsynchronously(LuckPrefix.getInstance(), () -> {
groupManager.setGroups(player);
}, 1, config.getLong("UpdateTime") * 20);
BukkitTask bukkitTask = Bukkit.getScheduler().runTaskTimerAsynchronously(LuckPrefix.getInstance(), () -> groupManager.setGroups(player), 1, config.getLong("UpdateTime") * 20);
playerManager.addJoinScheduler(player.getUniqueId(), bukkitTask);
playerManager.addUserGroup(player.getUniqueId(), group);
UpdateChecker checker = new UpdateChecker(LuckPrefix.getInstance().getDescription().getVersion());
UpdateChecker checker = LuckPrefix.getInstance().getUpdateChecker();
if (!checker.latestVersion()) {
player.sendMessage(ChatColor.translateAlternateColorCodes('&',
LegacyComponentSerializer.legacyAmpersand().serialize(new TextUtil(LuckPrefix.getInstance().getPrefix() +
"There is a new update available: <u><click:open_url:https://modrinth.com/plugin/luckprefix>" + checker.getLatestVersion()).miniMessage())
));
player.sendMessage(ChatColor.translateAlternateColorCodes('&', new TextUtil(LuckPrefix.getInstance().getPrefix() +
"There is a new update available: <u><click:open_url:https://modrinth.com/plugin/luckprefix>" + checker.getLatestVersion()).legacyMiniMessage()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ public ConfigManager(String fileName) {
this.configuration = YamlConfiguration.loadConfiguration(file);
}

@SneakyThrows
public void reloadConfig() {
saveConfiguration();
this.configuration.load(this.file);
}

public void addDefault(String path, Object value) {
this.configuration.addDefault(path, value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,33 @@ public void setGroups(Player player) {
Team team = scoreboard.getTeam(this.groupID.get(playerManager.getUserGroups().get(onlinePlayer.getUniqueId()))
+ playerManager.getUserGroups().get(onlinePlayer.getUniqueId()));
if (team == null) {
scoreboard.getTeam(this.groupID.get("default") + "default").addEntry(player.getName());
Team defaultRole = scoreboard.getTeam(this.groupID.get("default") + "default");
if (defaultRole == null) {
scoreboard.getTeams().forEach(Team::unregister);
for (Group loadedGroup : LuckPrefix.getInstance().getLuckPerms().getGroupManager().getLoadedGroups()) {
createGroup(loadedGroup.getName());
}
setupGroups(player);
defaultRole = scoreboard.getTeam(this.groupID.get("default") + "default");
if (defaultRole == null) {
continue;
}
defaultRole.addEntry(onlinePlayer.getName());
LuckPrefix.getInstance().getPlayerManager().setPlayerListName(player.getUniqueId());
return;
}
defaultRole.addEntry(onlinePlayer.getName());
return;
}
Team currentTeam = scoreboard.getEntryTeam(onlinePlayer.getName());
if (currentTeam == null) {
team.addEntry(onlinePlayer.getName());
return;
}
if (team.getEntries().contains(player.getName())) {
if (currentTeam != team) {
currentTeam.removeEntry(onlinePlayer.getName());
}
if (team.getEntries().contains(onlinePlayer.getName())) {
return;
}
team.addEntry(onlinePlayer.getName());
Expand Down

0 comments on commit 44c18e0

Please sign in to comment.