Rework of configurations file and first test of storage info

This commit is contained in:
Ethanell 2019-08-24 11:24:13 +02:00
parent b2220db516
commit 0647d1d717
8 changed files with 124 additions and 59 deletions

View file

@ -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;
}

View file

@ -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();
}
}
}

View file

@ -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
}
}
}

View file

@ -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();
}
}
}
}

View file

@ -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");
}
}

View file

@ -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();
}

View file

@ -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=============================="
modo:
on: "&eModeration on"
off: "&6Moderation off"
admin:
on: "&eAdministration on"
off: "&6Administration off"

1
src/staffConf.yml Normal file
View file

@ -0,0 +1 @@
players: