Skip to content

Commit

Permalink
feat: Config reloading
Browse files Browse the repository at this point in the history
Adds /pk reload to reload the config
  • Loading branch information
Allymonies committed Jun 6, 2021
1 parent 5902547 commit e839b54
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 7 deletions.
12 changes: 10 additions & 2 deletions src/main/java/com/omnipico/pluralkitmc/CommandPK.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@

public class CommandPK implements CommandExecutor, TabCompleter {
PluralKitData data;
JavaPlugin plugin;
PluralKitMC plugin;

public CommandPK(PluralKitData data, JavaPlugin plugin) {
public CommandPK(PluralKitData data, PluralKitMC plugin) {
this.data = data;
this.plugin = plugin;
}
Expand Down Expand Up @@ -198,6 +198,13 @@ public void run() {
} else {
player.spigot().sendMessage( new ComponentBuilder().append(ChatUtils.pluginTag).append(" Could not find any members :(").color(ChatColor.RED).create());
}
} else if (args[0].toLowerCase().equals("reload")) {
if (player.hasPermission("pluralkitmc.reload") || player.hasPermission("pluralkitmc.*") || player.hasPermission("*")) {
plugin.reloadConfigData();
player.spigot().sendMessage( new ComponentBuilder().append(ChatUtils.pluginTag).append(" Config reloaded.").color(ChatColor.GREEN).create());
} else {
player.spigot().sendMessage( new ComponentBuilder("You do not have permission for this command.").color(ChatColor.RED).create());
}
} else if (args[0].toLowerCase().equals("switch") || args[0].toLowerCase().equals("sw")) {
if (args.length >= 2) {
List<String> newFronters = new ArrayList<>();
Expand Down Expand Up @@ -244,6 +251,7 @@ public List<String> onTabComplete(CommandSender sender, Command command, String
subCommands.add("member");
subCommands.add("random");
subCommands.add("switch");
subCommands.add("reload");
if (args.length == 1) {
return subCommands;
} else {
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/omnipico/pluralkitmc/PluralKitData.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ public PluralKitData(FileConfiguration config, JavaPlugin plugin) {
cacheUpdateFrequency = config.getLong("cache_update_frequency");
}

public void setConfig(FileConfiguration config) {
this.config = config;
}

String getSystemId(UUID uuid) {
if (userCache.containsKey(uuid)) {
return userCache.get(uuid).systemId;
Expand Down
15 changes: 13 additions & 2 deletions src/main/java/com/omnipico/pluralkitmc/PluralKitMC.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,34 @@

public class PluralKitMC extends JavaPlugin {
Chat chat;
PluralKitData data;
ProxyListener proxyListener;
@Override
public void onEnable() {
this.saveDefaultConfig();
if(Bukkit.getServer().getPluginManager().getPlugin("Vault") != null){
chat = getServer().getServicesManager().load(Chat.class);
}
FileConfiguration config = this.getConfig();
PluralKitData data = new PluralKitData(config, this);
this.data = new PluralKitData(config, this);
//Fired when the server enables the plugin
CommandPK commandPK = new CommandPK(data, this);
this.getCommand("pk").setExecutor(commandPK);
this.getCommand("pk").setTabCompleter(commandPK);
getServer().getPluginManager().registerEvents(new ProxyListener(data, config, chat), this);
proxyListener = new ProxyListener(data, config, chat);
getServer().getPluginManager().registerEvents(proxyListener, this);
}

@Override
public void onDisable() {
//Fired when the server stops and disables all plugins
}

public void reloadConfigData() {
this.reloadConfig();
FileConfiguration config = this.getConfig();
proxyListener.setConfig(config);
data.setConfig(config);
}

}
8 changes: 6 additions & 2 deletions src/main/java/com/omnipico/pluralkitmc/ProxyListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,19 @@ public class ProxyListener implements Listener {

public ProxyListener(PluralKitData data, FileConfiguration config, Chat chat) {
this.data = data;
this.config = config;
this.chat = chat;

setConfig(config);
}

public void setConfig(FileConfiguration config) {
this.config = config;
defaultNameColor = ChatUtils.replaceColor(config.getString("default_name_color","&b"));
if (config.contains("message_format")) {
format = ChatUtils.replaceColor(Objects.requireNonNull(config.getString("message_format")).replace("%message%","%2$s"));
} else {
format = ChatColor.WHITE.toString() + "[PK] " + ChatColor.AQUA.toString() + "%member% " + ChatColor.DARK_GRAY + "> " + ChatColor.WHITE + "%2$s";
}

}

@EventHandler(priority = EventPriority.HIGHEST) // Listening for the event.
Expand Down
6 changes: 5 additions & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: PluralKitMC
version: 0.4.6
version: 0.4.7
author: Allymonies
main: com.omnipico.pluralkitmc.PluralKitMC
api-version: 1.16
Expand All @@ -20,10 +20,14 @@ permissions:
pluralkitmc.link:
default: true
description: Allows linking one's pluralkit token to Minecraft (bypassing privacy settings)
pluralkitmc.reload:
default: false
description: Allows reloading the PluralKitMC config
pluralkitmc.*:
default: op
description: Allows access to every pluralkitmc command.
children:
pluralkitmc.pk: true
pluralkitmc.update: true
pluralkitmc.link: true
pluralkitmc.reload: true

0 comments on commit e839b54

Please sign in to comment.