Commit 2154ceea authored by Ethan's avatar Ethan 💬
Browse files

language manager

parent c5064e8d
......@@ -2,6 +2,7 @@ package com.ehhthan.mmoactionbar;
import com.ehhthan.mmoactionbar.command.MMOActionBarCommand;
import com.ehhthan.mmoactionbar.manager.ConfigManager;
import com.ehhthan.mmoactionbar.manager.LanguageManager;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import org.bstats.bukkit.Metrics;
import org.bukkit.Bukkit;
......@@ -15,6 +16,7 @@ public final class MMOActionBar extends JavaPlugin {
private static MMOActionBar plugin;
private ConfigManager configManager;
private LanguageManager languageManager;
private BukkitAudiences audience;
public static MMOActionBar getInstance() {
......@@ -42,8 +44,10 @@ public final class MMOActionBar extends JavaPlugin {
getLogger().log(Level.INFO, "PlaceholderAPI support detected.");
}
this.audience = BukkitAudiences.create(plugin);
this.configManager = new ConfigManager();
this.languageManager = new LanguageManager();
this.audience = BukkitAudiences.create(plugin);
}
public void reload() {
......@@ -51,11 +55,11 @@ public final class MMOActionBar extends JavaPlugin {
}
public void reload(@Nullable CommandSender sender) {
configManager.reload();
languageManager.reload();
reloadConfig();
if (sender != null)
configManager.sendMessage(sender, "plugin-reloaded", true);
languageManager.sendMessage(sender, "plugin-reloaded", true);
}
public ConfigManager getConfigManager() {
......@@ -65,4 +69,8 @@ public final class MMOActionBar extends JavaPlugin {
public BukkitAudiences getAudience() {
return audience;
}
public LanguageManager getLanguage() {
return languageManager;
}
}
......@@ -16,14 +16,14 @@ public class MMOActionBarCommand implements CommandExecutor, TabCompleter {
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) {
if (args.length == 0) {
MMOActionBar.getInstance().getConfigManager().sendMessage(sender, "unknown-command", true);
MMOActionBar.getInstance().getLanguage().sendMessage(sender, "unknown-command", true);
return false;
}
if (args[0].equals("reload")) {
MMOActionBar.getInstance().reload(sender);
return true;
}
MMOActionBar.getInstance().getConfigManager().sendMessage(sender, "unknown-command", true);
MMOActionBar.getInstance().getLanguage().sendMessage(sender, "unknown-command", true);
return false;
}
......
package com.ehhthan.mmoactionbar.manager;
import com.ehhthan.mmoactionbar.MMOActionBar;
import com.ehhthan.mmoactionbar.file.ConfigFile;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.bukkit.command.CommandSender;
import java.io.File;
import java.io.IOException;
......@@ -13,49 +8,12 @@ import java.nio.file.Files;
import java.util.logging.Level;
public class ConfigManager {
// cached config files
private ConfigFile language;
public ConfigManager() {
mkdir("language");
// load files with default configuration
for (DefaultFile file : DefaultFile.values())
if (file.isAutomatic())
file.checkFile();
reload();
}
public void reload() {
language = new ConfigFile("/language", "language");
}
public String getMessage(String path) {
return getMessage(path, false);
}
public String getMessage(String path, boolean hasPrefix) {
return LegacyComponentSerializer.legacySection().serialize(getComponentMessage(path, hasPrefix));
}
public Component getComponentMessage(String path) {
return getComponentMessage(path, false);
}
public Component getComponentMessage(String path, boolean hasPrefix) {
String prefix = (hasPrefix) ? language.getConfig().getString("prefix", "") : "";
String found = language.getConfig().getString(path);
return MiniMessage.get().parse(found == null ? "<MNF:" + path + ">" : prefix + found);
}
public void sendMessage(CommandSender sender, String path) {
sendMessage(sender, path, false);
}
public void sendMessage(CommandSender sender, String path, boolean hasPrefix) {
MMOActionBar.getInstance().getAudience().sender(sender).sendMessage(
getComponentMessage(path, hasPrefix));
}
private void mkdir(String path) {
......
package com.ehhthan.mmoactionbar.manager;
import com.ehhthan.mmoactionbar.MMOActionBar;
import com.ehhthan.mmoactionbar.file.ConfigFile;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.Nullable;
import java.util.Map;
public class LanguageManager {
private ConfigFile language;
public LanguageManager() {
reload();
}
public void reload() {
language = new ConfigFile("/language", "language");
}
@Deprecated
public String getMessage(String path) {
return getMessage(path, false);
}
@Deprecated
public String getMessage(String path, boolean hasPrefix) {
return LegacyComponentSerializer.legacySection().serialize(getComponentMessage(path, hasPrefix, null));
}
public Component getComponentMessage(String path) {
return getComponentMessage(path, false, null);
}
public Component getComponentMessage(String path, boolean hasPrefix) {
return getComponentMessage(path, hasPrefix, null);
}
//Map<String, String> placeholders
public Component getComponentMessage(String path, boolean hasPrefix, @Nullable Map<String, String> placeholders) {
String prefix = (hasPrefix) ? language.getConfig().getString("prefix", "") : "";
String found = language.getConfig().getString(path);
String input = found == null ? "<MNF:" + path + ">" : prefix + found;
if (placeholders != null)
return MiniMessage.get().parse(input, placeholders);
else
return MiniMessage.get().parse(input);
}
public void sendMessage(CommandSender sender, String path) {
sendMessage(sender, path, false, null);
}
public void sendMessage(CommandSender sender, String path, boolean hasPrefix) {
sendMessage(sender, path, hasPrefix, null);
}
public void sendMessage(CommandSender sender, String path, boolean hasPrefix, @Nullable Map<String, String> placeholders) {
MMOActionBar.getInstance().getAudience().sender(sender).sendMessage(
getComponentMessage(path, hasPrefix, placeholders));
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment