diff --git a/src/fr/flifloo/StaffToggle/Commands/Staff.java b/src/fr/flifloo/StaffToggle/Commands/Staff.java index 08b6332..dbaa116 100644 --- a/src/fr/flifloo/StaffToggle/Commands/Staff.java +++ b/src/fr/flifloo/StaffToggle/Commands/Staff.java @@ -4,33 +4,48 @@ import org.bukkit.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; +import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.entity.Player; import fr.flifloo.StaffToggle.Main; public class Staff implements CommandExecutor { - Main plugin; + private Main plugin; + private FileConfiguration language; + private FileConfiguration staffConf; public Staff(Main plugin) { this.plugin = plugin; + language = plugin.language.config; + staffConf = plugin.staffConf.config; } @Override - public boolean onCommand(CommandSender player, Command cmd, String label, String[] args) { - if (!(player instanceof Player)) { - player.sendMessage(ChatColor.RED + "Command only for players !"); + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + String message; + if (!(sender instanceof Player)) { + sender.sendMessage(ChatColor.RED + "Command only for players !"); return false; } if (args.length == 0 || args[0].equalsIgnoreCase("help")) { - String message = plugin.language.config.getString("staff.help"); - player.sendMessage(ChatColor.translateAlternateColorCodes('&', message)); + message = language.getString("staff.help"); + sender.sendMessage(ChatColor.translateAlternateColorCodes('&', message)); } - else if (args[0].equalsIgnoreCase("modo")) { - player.sendMessage(ChatColor.GOLD + "Switch modo !"); + else if (args[0].equalsIgnoreCase("modo") || args[0].equalsIgnoreCase("admin")) { + Player player = (Player) sender; + if (!staffConf.isSet("players." + player.getUniqueId())) { + player.sendMessage("Not config yet !"); + staffConf.set("players." + player.getUniqueId(), "test"); + plugin.staffConf.save(); + } else { + player.sendMessage("Fond config !"); + } } - else if (args[0].equalsIgnoreCase("admin")) { - player.sendMessage(ChatColor.GOLD + "Switch admin !"); + else { + message = language.getString("unknown"); + sender.sendMessage(ChatColor.translateAlternateColorCodes('&', message)); + return false; } return true; } diff --git a/src/fr/flifloo/StaffToggle/Configuration/Language.java b/src/fr/flifloo/StaffToggle/Configuration/Language.java deleted file mode 100644 index b8d9f52..0000000 --- a/src/fr/flifloo/StaffToggle/Configuration/Language.java +++ /dev/null @@ -1,42 +0,0 @@ -package fr.flifloo.StaffToggle.Configuration; - -import fr.flifloo.StaffToggle.Main; -import org.bukkit.configuration.InvalidConfigurationException; -import org.bukkit.configuration.file.FileConfiguration; -import org.bukkit.configuration.file.YamlConfiguration; - -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.nio.file.Files; -import java.nio.file.StandardCopyOption; - -public class Language{ - public FileConfiguration config; - - public Language (Main main) { - String configLanguage = main.getConfig().getString("language"); - InputStream resourceLanguage = main.getResource("languages/" + configLanguage + ".yml"); - File configFile = new File(main.getDataFolder(), "language.yml"); - - if (!configFile.exists()) { - configFile.getParentFile().mkdirs(); - try { - Files.copy(resourceLanguage, configFile.toPath()); - } catch (IOException e) { - e.printStackTrace(); - } - } - - config = new YamlConfiguration(); - try { - config.load(configFile); - if (!configLanguage.equalsIgnoreCase(config.getString("language"))) { - Files.copy(resourceLanguage, configFile.toPath(), StandardCopyOption.REPLACE_EXISTING); - config.load(configFile); - } - } catch (IOException | InvalidConfigurationException e) { - e.printStackTrace(); - } - } -} diff --git a/src/fr/flifloo/StaffToggle/Configurations/Configuration.java b/src/fr/flifloo/StaffToggle/Configurations/Configuration.java new file mode 100644 index 0000000..7dc5d22 --- /dev/null +++ b/src/fr/flifloo/StaffToggle/Configurations/Configuration.java @@ -0,0 +1,51 @@ +package fr.flifloo.StaffToggle.Configurations; + +import fr.flifloo.StaffToggle.Main; +import org.bukkit.configuration.InvalidConfigurationException; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.configuration.file.YamlConfiguration; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.StandardCopyOption; + +public class Configuration{ + public String fileName; + public FileConfiguration config; + public File configFile; + InputStream resourceFile; + private Main plugin; + + public Configuration (Main main, String name, String resourcePath) { + this.plugin = main; + this.fileName = name; + resourceFile = this.plugin.getResource(resourcePath); + configFile = new File(this.plugin.getDataFolder(), fileName + ".yml"); + + if (!this.configFile.exists()) { + this.configFile.getParentFile().mkdirs(); + try { + Files.copy(resourceFile, configFile.toPath()); + } catch (IOException e) { + e.printStackTrace(); + } + } + + this.config = new YamlConfiguration(); + try { + this.config.load(this.configFile); + } catch (IOException | InvalidConfigurationException e) { + e.printStackTrace(); + } + } + + public void save() { + try { + this.config.save(this.configFile); + } catch (IOException e) { + this.plugin.getLogger().warning("Unable to save " + this.fileName); // shouldn't really happen, but save throws the exception + } + } +} diff --git a/src/fr/flifloo/StaffToggle/Configurations/Language.java b/src/fr/flifloo/StaffToggle/Configurations/Language.java new file mode 100644 index 0000000..2a7ae8b --- /dev/null +++ b/src/fr/flifloo/StaffToggle/Configurations/Language.java @@ -0,0 +1,26 @@ +package fr.flifloo.StaffToggle.Configurations; + +import fr.flifloo.StaffToggle.Main; +import org.bukkit.configuration.InvalidConfigurationException; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.StandardCopyOption; + +public class Language extends Configuration{ + + public Language (Main main) { + super(main, "language", "languages/" + main.getConfig().getString("language") + ".yml"); + + if (!main.getConfig().getString("language").equalsIgnoreCase(config.getString("language"))) { + try { + Files.copy(this.resourceFile, this.configFile.toPath(), StandardCopyOption.REPLACE_EXISTING); + config.load(configFile); + } catch (IOException | InvalidConfigurationException e) { + e.printStackTrace(); + } + } + + } +} diff --git a/src/fr/flifloo/StaffToggle/Configurations/StaffConf.java b/src/fr/flifloo/StaffToggle/Configurations/StaffConf.java new file mode 100644 index 0000000..50363ba --- /dev/null +++ b/src/fr/flifloo/StaffToggle/Configurations/StaffConf.java @@ -0,0 +1,9 @@ +package fr.flifloo.StaffToggle.Configurations; + +import fr.flifloo.StaffToggle.Main; + +public class StaffConf extends Configuration{ + public StaffConf(Main main) { + super(main, "staffConf", "staffConf.yml"); + } +} diff --git a/src/fr/flifloo/StaffToggle/Main.java b/src/fr/flifloo/StaffToggle/Main.java index 9d02dac..c5f58c9 100644 --- a/src/fr/flifloo/StaffToggle/Main.java +++ b/src/fr/flifloo/StaffToggle/Main.java @@ -1,20 +1,18 @@ package fr.flifloo.StaffToggle; -import fr.flifloo.StaffToggle.Commands.*; -import fr.flifloo.StaffToggle.Configuration.*; -import org.bukkit.configuration.file.FileConfiguration; +import fr.flifloo.StaffToggle.Commands.Staff; +import fr.flifloo.StaffToggle.Configurations.*; import org.bukkit.plugin.java.JavaPlugin; -import java.io.IOException; - public class Main extends JavaPlugin { public Language language; + public StaffConf staffConf; @Override public void onEnable() { saveDefaultConfig(); - getConfig().getString("language"); language = new Language(this); + staffConf = new StaffConf(this); registerCommands(); } diff --git a/src/languages/en.yml b/src/languages/en.yml index 7446d7b..f5ba280 100644 --- a/src/languages/en.yml +++ b/src/languages/en.yml @@ -1,6 +1,13 @@ language: en +unknon: "&cUnknown command !" staff: help: "&3=========={Staff help}==========\n &e/staff modo&9: Toggle moderation\n &e/staff admin&9: Toggle administration\n -&3==============================" \ No newline at end of file +&3==============================" + modo: + on: "&eModeration on" + off: "&6Moderation off" + admin: + on: "&eAdministration on" + off: "&6Administration off" \ No newline at end of file diff --git a/src/staffConf.yml b/src/staffConf.yml new file mode 100644 index 0000000..238c7ea --- /dev/null +++ b/src/staffConf.yml @@ -0,0 +1 @@ +players: \ No newline at end of file