config reload command

This commit is contained in:
2023-06-24 22:22:47 +02:00
parent 0e9984bbb7
commit 4b386923e6
5 changed files with 97 additions and 6 deletions
@@ -17,18 +17,25 @@ public class NJNConfig {
private final HashMap<String, DisplayContent> displayContents = new HashMap<>(); private final HashMap<String, DisplayContent> displayContents = new HashMap<>();
public final String noPermissionMessage, configReloadedMessage;
protected NJNConfig(NotJustNameplates plugin) { protected NJNConfig(NotJustNameplates plugin) {
this.plugin = plugin; this.plugin = plugin;
plugin.saveDefaultConfig(); plugin.saveDefaultConfig();
plugin.reloadConfig(); plugin.reloadConfig();
this.config = plugin.getConfig();
reload();
}
public void reload() {
displayContents.clear();
this.config = plugin.getConfig(); 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", "<red>You do not have permission to use this.");
configReloadedMessage = messagesSection.getString("config-reloaded", "<aqua>NJN Config reloaded.");
ConfigurationSection namePlatesSection = config.getConfigurationSection("nameplates"); ConfigurationSection namePlatesSection = config.getConfigurationSection("nameplates");
if (namePlatesSection == null) { if (namePlatesSection == null) {
plugin.getLogger().severe("No nameplates section found in config! Correct your config and reload."); 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) { public DisplayContent getDisplayContent(String name) {
return displayContents.get(name); return displayContents.get(name);
} }
@@ -1,5 +1,6 @@
package me.youhavetrouble.notjustnameplates; package me.youhavetrouble.notjustnameplates;
import me.youhavetrouble.notjustnameplates.commands.MainCommand;
import me.youhavetrouble.notjustnameplates.nameplates.TeamManagementListener; import me.youhavetrouble.notjustnameplates.nameplates.TeamManagementListener;
import org.bukkit.permissions.PermissionDefault; import org.bukkit.permissions.PermissionDefault;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
@@ -11,14 +12,22 @@ public final class NotJustNameplates extends JavaPlugin {
private static NJNConfig config; private static NJNConfig config;
private static long time = Long.MIN_VALUE; private static long time = Long.MIN_VALUE;
private TeamManagementListener teamManagementListener = null;
@Override @Override
public void onEnable() { public void onEnable() {
instance = this; instance = this;
config = new NJNConfig(this); config = new NJNConfig(this);
DefaultPermissions.registerPermission("notjustnameplates.seeown", "Allows a player to see their own nameplate", PermissionDefault.FALSE); 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, () -> { getServer().getScheduler().runTaskTimerAsynchronously(this, () -> {
time++; time++;
if (config == null) return; if (config == null) return;
@@ -31,6 +40,12 @@ public final class NotJustNameplates extends JavaPlugin {
}, 1, 1); }, 1, 1);
} }
public void reloadPluginConfig() {
config = new NJNConfig(this);
teamManagementListener.reloadTeams();
}
public static NotJustNameplates getInstance() { public static NotJustNameplates getInstance() {
return instance; return instance;
} }
@@ -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<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) {
List<String> 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<>());
}
}
@@ -59,6 +59,18 @@ public class TeamManagementListener implements Listener {
nameplate.remove(); 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) { private void addPlayerToTeam(@NotNull Player player, @NotNull Player target) {
CraftPlayer craftPlayer = (CraftPlayer) target; CraftPlayer craftPlayer = (CraftPlayer) target;
ClientboundSetPlayerTeamPacket teamCreatePacket = ClientboundSetPlayerTeamPacket ClientboundSetPlayerTeamPacket teamCreatePacket = ClientboundSetPlayerTeamPacket
+4
View File
@@ -21,3 +21,7 @@ nameplates:
# "fixed" - no rotation # "fixed" - no rotation
billboard: "horizontal" billboard: "horizontal"
messages:
config-reloaded: "<aqua>NJN Config reloaded."
no-permission: "<red>You do not have permission to use this."