forked from kaboomserver/extras
Optimize user prefix loading/saving
This commit is contained in:
parent
67220510b1
commit
6936749213
3 changed files with 39 additions and 18 deletions
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 + " <prefix|off>");
|
||||
} 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)));
|
||||
|
|
|
@ -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");
|
||||
|
|
Loading…
Reference in a new issue