Remake set method for auto save in configurations, simplify staff command and add save and restore of inventory

This commit is contained in:
Ethanell 2019-08-26 00:10:40 +02:00
parent 0647d1d717
commit fb217cf7fe
2 changed files with 32 additions and 17 deletions

View file

@ -8,6 +8,10 @@ import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import fr.flifloo.StaffToggle.Main;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import java.util.List;
public class Staff implements CommandExecutor {
private Main plugin;
@ -27,26 +31,32 @@ public class Staff implements CommandExecutor {
sender.sendMessage(ChatColor.RED + "Command only for players !");
return false;
}
Player player = (Player) sender;
PlayerInventory playerIvt = player.getInventory();
String playerConf = "players." + player.getUniqueId();
String state = playerConf + ".state";
String armor = playerConf + ".inventory" + ".armor";
String content = playerConf + ".inventory" + "content";
if (args.length == 0 || args[0].equalsIgnoreCase("help")) {
message = language.getString("staff.help");
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', message));
if (!staffConf.isSet(state)) {
plugin.staffConf.set(state, false);
}
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 {
message = language.getString("unknown");
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', message));
return false;
if (!staffConf.getBoolean(state)){
plugin.staffConf.set(state, true);
plugin.staffConf.set(armor, playerIvt.getArmorContents());
plugin.staffConf.set(content, playerIvt.getContents());
playerIvt.clear();
player.sendMessage("On");
} else {
plugin.staffConf.set(state, false);
playerIvt.clear();
ItemStack[] backupIvt = (ItemStack[]) staffConf.get(armor);
playerIvt.setArmorContents(backupIvt);
backupIvt = (ItemStack[]) staffConf.get(content);
playerIvt.setContents(backupIvt);
player.sendMessage("Off");
}
return true;
}
}

View file

@ -48,4 +48,9 @@ public class Configuration{
this.plugin.getLogger().warning("Unable to save " + this.fileName); // shouldn't really happen, but save throws the exception
}
}
public void set(String path, Object value) {
this.config.set(path, value);
this.save();
}
}