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