From 6936749213aa1ab5c420b5bcffe19eb8c1272308 Mon Sep 17 00:00:00 2001 From: kaboom <58372747+kaboombot@users.noreply.github.com> Date: Fri, 20 May 2022 06:57:17 +0300 Subject: [PATCH] Optimize user prefix loading/saving --- src/main/java/pw/kaboom/extras/Main.java | 18 +++++++++++++++ .../kaboom/extras/commands/CommandPrefix.java | 16 +++++++------ .../extras/modules/player/PlayerChat.java | 23 ++++++++++--------- 3 files changed, 39 insertions(+), 18 deletions(-) diff --git a/src/main/java/pw/kaboom/extras/Main.java b/src/main/java/pw/kaboom/extras/Main.java index 869cdd1..f8083e9 100644 --- a/src/main/java/pw/kaboom/extras/Main.java +++ b/src/main/java/pw/kaboom/extras/Main.java @@ -1,10 +1,13 @@ package pw.kaboom.extras; +import java.io.File; import java.util.Collections; import org.bukkit.WorldCreator; import org.bukkit.WorldType; import org.bukkit.block.BlockFace; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.plugin.java.JavaPlugin; import pw.kaboom.extras.commands.CommandBroadcastVanilla; @@ -41,6 +44,9 @@ import pw.kaboom.extras.modules.server.ServerTabComplete; import pw.kaboom.extras.modules.server.ServerTick; public final class Main extends JavaPlugin { + private File prefixConfigFile; + private FileConfiguration prefixConfig; + @Override public void onLoad() { /* Fill lists */ @@ -60,6 +66,10 @@ public final class Main extends JavaPlugin { @Override public void onEnable() { + /* Prefixes */ + prefixConfigFile = new File(this.getDataFolder(), "prefixes.yml"); + prefixConfig = YamlConfiguration.loadConfiguration(prefixConfigFile); + /* Commands */ this.getCommand("broadcastvanilla").setExecutor(new CommandBroadcastVanilla()); this.getCommand("clearchat").setExecutor(new CommandClearChat()); @@ -107,4 +117,12 @@ public final class Main extends JavaPlugin { new WorldCreator("world_flatlands").generateStructures(false).type(WorldType.FLAT) ); } + + public File getPrefixConfigFile() { + return this.prefixConfigFile; + } + + public FileConfiguration getPrefixConfig() { + return this.prefixConfig; + } } diff --git a/src/main/java/pw/kaboom/extras/commands/CommandPrefix.java b/src/main/java/pw/kaboom/extras/commands/CommandPrefix.java index ceb1017..851df47 100644 --- a/src/main/java/pw/kaboom/extras/commands/CommandPrefix.java +++ b/src/main/java/pw/kaboom/extras/commands/CommandPrefix.java @@ -19,26 +19,28 @@ import org.bukkit.plugin.java.JavaPlugin; import pw.kaboom.extras.Main; public final class CommandPrefix implements CommandExecutor { + private static final File PREFIX_CONFIG_FILE = JavaPlugin + .getPlugin(Main.class).getPrefixConfigFile(); + private static final FileConfiguration PREFIX_CONFIG = JavaPlugin + .getPlugin(Main.class).getPrefixConfig(); + public boolean onCommand(final CommandSender sender, final Command cmd, final String label, final String[] args) { if (sender instanceof ConsoleCommandSender) { sender.sendMessage("Command has to be run by a player"); } else { final Player player = (Player) sender; - final File configFile = new File(JavaPlugin.getPlugin(Main.class).getDataFolder(), - "prefixes.yml"); - final FileConfiguration prefixConfig = YamlConfiguration.loadConfiguration(configFile); try { if (args.length == 0) { player.sendMessage(ChatColor.RED + "Usage: /" + label + " "); } else if ("off".equalsIgnoreCase(args[0])) { - prefixConfig.set(player.getUniqueId().toString(), null); - prefixConfig.save(configFile); + PREFIX_CONFIG.set(player.getUniqueId().toString(), null); + PREFIX_CONFIG.save(PREFIX_CONFIG_FILE); player.sendMessage("You no longer have a tag"); } else { - prefixConfig.set(player.getUniqueId().toString(), String.join(" ", args)); - prefixConfig.save(configFile); + PREFIX_CONFIG.set(player.getUniqueId().toString(), String.join(" ", args)); + PREFIX_CONFIG.save(PREFIX_CONFIG_FILE); player.sendMessage("You now have the tag: " + ChatColor.translateAlternateColorCodes( '&', String.join(" ", args))); diff --git a/src/main/java/pw/kaboom/extras/modules/player/PlayerChat.java b/src/main/java/pw/kaboom/extras/modules/player/PlayerChat.java index 52f554e..771fe98 100644 --- a/src/main/java/pw/kaboom/extras/modules/player/PlayerChat.java +++ b/src/main/java/pw/kaboom/extras/modules/player/PlayerChat.java @@ -20,6 +20,13 @@ import org.bukkit.plugin.java.JavaPlugin; import pw.kaboom.extras.Main; public final class PlayerChat implements Listener { + private static final FileConfiguration CONFIG = JavaPlugin.getPlugin(Main.class).getConfig(); + private static final FileConfiguration PREFIX_CONFIG = JavaPlugin + .getPlugin(Main.class).getPrefixConfig(); + + private static final String OP_TAG = CONFIG.getString("opTag"); + private static final String DEOP_TAG = CONFIG.getString("deOpTag"); + @EventHandler void onAsyncPlayerChat(final AsyncPlayerChatEvent event) { final Player player = event.getPlayer(); @@ -40,21 +47,15 @@ public final class PlayerChat implements Listener { return; } - final File configFile = new File(JavaPlugin.getPlugin(Main.class).getDataFolder(), - "prefixes.yml"); - final FileConfiguration prefixConfig = YamlConfiguration.loadConfiguration(configFile); - final String prefix; final String name = player.getDisplayName().toString(); + String prefix = PREFIX_CONFIG.getString(player.getUniqueId().toString()); - if (prefixConfig.getString(player.getUniqueId().toString()) != null) { - prefix = ChatColor.translateAlternateColorCodes( - '&', - prefixConfig.getString(player.getUniqueId().toString()) + " " + ChatColor.RESET - ); + if (prefix != null) { + prefix = ChatColor.translateAlternateColorCodes('&', prefix + " " + ChatColor.RESET); } else if (event.getPlayer().isOp()) { - prefix = JavaPlugin.getPlugin(Main.class).getConfig().getString("opTag"); + prefix = OP_TAG; } else { - prefix = JavaPlugin.getPlugin(Main.class).getConfig().getString("deOpTag"); + prefix = DEOP_TAG; } event.setFormat(prefix + name + ChatColor.RESET + ": " + ChatColor.RESET + "%2$s");