From 4b386923e614726456e45d6469836f7894fbdc75 Mon Sep 17 00:00:00 2001 From: youhavetrouble Date: Sat, 24 Jun 2023 22:22:47 +0200 Subject: [PATCH] config reload command --- .../notjustnameplates/NJNConfig.java | 18 +++++-- .../notjustnameplates/NotJustNameplates.java | 17 +++++- .../commands/MainCommand.java | 52 +++++++++++++++++++ .../nameplates/TeamManagementListener.java | 12 +++++ src/main/resources/config.yml | 4 ++ 5 files changed, 97 insertions(+), 6 deletions(-) create mode 100644 src/main/java/me/youhavetrouble/notjustnameplates/commands/MainCommand.java diff --git a/src/main/java/me/youhavetrouble/notjustnameplates/NJNConfig.java b/src/main/java/me/youhavetrouble/notjustnameplates/NJNConfig.java index 356e102..a8ffc7e 100644 --- a/src/main/java/me/youhavetrouble/notjustnameplates/NJNConfig.java +++ b/src/main/java/me/youhavetrouble/notjustnameplates/NJNConfig.java @@ -17,18 +17,25 @@ public class NJNConfig { private final HashMap displayContents = new HashMap<>(); + public final String noPermissionMessage, configReloadedMessage; + protected NJNConfig(NotJustNameplates plugin) { this.plugin = plugin; plugin.saveDefaultConfig(); plugin.reloadConfig(); - this.config = plugin.getConfig(); - reload(); - } - public void reload() { - displayContents.clear(); this.config = plugin.getConfig(); + ConfigurationSection messagesSection = config.getConfigurationSection("messages"); + if (messagesSection == null) { + messagesSection = config.createSection("messages"); + plugin.getLogger().severe("No messages section found in config! Correct your config and reload."); + } + + noPermissionMessage = messagesSection.getString("no-permission", "You do not have permission to use this."); + configReloadedMessage = messagesSection.getString("config-reloaded", "NJN Config reloaded."); + + ConfigurationSection namePlatesSection = config.getConfigurationSection("nameplates"); if (namePlatesSection == null) { plugin.getLogger().severe("No nameplates section found in config! Correct your config and reload."); @@ -44,6 +51,7 @@ public class NJNConfig { } } + public DisplayContent getDisplayContent(String name) { return displayContents.get(name); } diff --git a/src/main/java/me/youhavetrouble/notjustnameplates/NotJustNameplates.java b/src/main/java/me/youhavetrouble/notjustnameplates/NotJustNameplates.java index dc675d8..c2c583e 100644 --- a/src/main/java/me/youhavetrouble/notjustnameplates/NotJustNameplates.java +++ b/src/main/java/me/youhavetrouble/notjustnameplates/NotJustNameplates.java @@ -1,5 +1,6 @@ package me.youhavetrouble.notjustnameplates; +import me.youhavetrouble.notjustnameplates.commands.MainCommand; import me.youhavetrouble.notjustnameplates.nameplates.TeamManagementListener; import org.bukkit.permissions.PermissionDefault; import org.bukkit.plugin.java.JavaPlugin; @@ -11,14 +12,22 @@ public final class NotJustNameplates extends JavaPlugin { private static NJNConfig config; private static long time = Long.MIN_VALUE; + private TeamManagementListener teamManagementListener = null; + @Override public void onEnable() { instance = this; config = new NJNConfig(this); DefaultPermissions.registerPermission("notjustnameplates.seeown", "Allows a player to see their own nameplate", PermissionDefault.FALSE); + DefaultPermissions.registerPermission("notjustnameplates.command", "Allows a player to use the /njn command", PermissionDefault.TRUE); + + this.teamManagementListener = new TeamManagementListener(this); + + getServer().getPluginManager().registerEvents(this.teamManagementListener, this); + + new MainCommand(this); - getServer().getPluginManager().registerEvents(new TeamManagementListener(this), this); getServer().getScheduler().runTaskTimerAsynchronously(this, () -> { time++; if (config == null) return; @@ -31,6 +40,12 @@ public final class NotJustNameplates extends JavaPlugin { }, 1, 1); } + public void reloadPluginConfig() { + config = new NJNConfig(this); + teamManagementListener.reloadTeams(); + + } + public static NotJustNameplates getInstance() { return instance; } diff --git a/src/main/java/me/youhavetrouble/notjustnameplates/commands/MainCommand.java b/src/main/java/me/youhavetrouble/notjustnameplates/commands/MainCommand.java new file mode 100644 index 0000000..767f577 --- /dev/null +++ b/src/main/java/me/youhavetrouble/notjustnameplates/commands/MainCommand.java @@ -0,0 +1,52 @@ +package me.youhavetrouble.notjustnameplates.commands; + + +import me.youhavetrouble.notjustnameplates.NotJustNameplates; +import net.kyori.adventure.text.Component; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.util.StringUtil; +import org.jetbrains.annotations.NotNull; + +import java.util.ArrayList; +import java.util.List; + +public class MainCommand extends Command { + + public MainCommand(NotJustNameplates plugin) { + super("njn"); + setPermission("notjustnameplates.command"); + plugin.getServer().getCommandMap().register("njn", this); + } + + @Override + public boolean execute(@NotNull CommandSender sender, @NotNull String commandLabel, @NotNull String[] args) { + + if (args.length == 0) { + sender.sendMessage(Component.text("NotJustNameplates " + NotJustNameplates.getInstance().getPluginMeta().getVersion())); + return true; + } + + if (args[0].equalsIgnoreCase("reload")) { + if (!sender.hasPermission("notjustnameplates.command.reload")) { + sender.sendMessage(Component.text("You do not have permission to use this command")); + return true; + } + NotJustNameplates.getInstance().reloadPluginConfig(); + sender.sendMessage(Component.text("Reloaded config")); + return true; + } + + return false; + } + + @Override + public @NotNull List tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) { + List completions = new ArrayList<>(); + if (args.length == 0) return completions; + if (args.length == 1 && sender.hasPermission("notjustnameplates.command.reload")) { + completions.add("reload"); + } + return StringUtil.copyPartialMatches(args[args.length - 1], completions, new ArrayList<>()); + } +} diff --git a/src/main/java/me/youhavetrouble/notjustnameplates/nameplates/TeamManagementListener.java b/src/main/java/me/youhavetrouble/notjustnameplates/nameplates/TeamManagementListener.java index 9b7d165..5103b48 100644 --- a/src/main/java/me/youhavetrouble/notjustnameplates/nameplates/TeamManagementListener.java +++ b/src/main/java/me/youhavetrouble/notjustnameplates/nameplates/TeamManagementListener.java @@ -59,6 +59,18 @@ public class TeamManagementListener implements Listener { nameplate.remove(); } + public void reloadTeams() { + this.players.values().forEach(Nameplate::remove); + this.players.clear(); + for (Player player : Bukkit.getOnlinePlayers()) { + DisplayContent displayContent = NotJustNameplates.getPluginConfig().getDisplayContent("default"); + players.put(player.getName(), new Nameplate(player.getUniqueId(), displayContent != null ? displayContent : new DisplayContent())); + } + for (Player player : Bukkit.getOnlinePlayers()) { + sendTeamMembers(player); + } + } + private void addPlayerToTeam(@NotNull Player player, @NotNull Player target) { CraftPlayer craftPlayer = (CraftPlayer) target; ClientboundSetPlayerTeamPacket teamCreatePacket = ClientboundSetPlayerTeamPacket diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 7f64320..5d0728e 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -21,3 +21,7 @@ nameplates: # "fixed" - no rotation billboard: "horizontal" +messages: + config-reloaded: "NJN Config reloaded." + no-permission: "You do not have permission to use this." +