Backup and restore of player working

This commit is contained in:
Ethanell 2020-04-07 20:03:25 +02:00
parent fb217cf7fe
commit 18919c4a20
5 changed files with 82 additions and 25 deletions

13
.gitignore vendored Normal file
View file

@ -0,0 +1,13 @@
# BuildTools
BuildTools
# Extra Jars
jars
# Test Server
TestServer
# JetBrains stuff
.idea
out
StaffToggle.iml

View file

@ -1,17 +1,20 @@
package fr.flifloo.StaffToggle.Commands;
import fr.flifloo.StaffToggle.Configurations.PlayerConfig;
import org.bukkit.ChatColor;
import org.bukkit.GameMode;
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.Item;
import org.bukkit.entity.Player;
import fr.flifloo.StaffToggle.Main;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import java.util.List;
import java.util.*;
public class Staff implements CommandExecutor {
private Main plugin;
@ -26,37 +29,59 @@ public class Staff implements CommandExecutor {
@Override
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;
}
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";
String state = "players." + player.getUniqueId() + ".state";
if (!staffConf.isSet(state)) {
plugin.staffConf.set(state, 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");
staffConf.set(state, false);
}
if (!staffConf.getBoolean(state)){
staffConf.set(state, true);
save(player, "normal");
restore(player, "staff");
player.sendMessage("Staff mod enabled !");
} else {
staffConf.set(state, false);
save(player, "staff");
restore(player, "normal");
player.sendMessage("Staff mod disabled !");
}
return true;
}
private void save(Player player, String mode) {
PlayerInventory playerInventory = player.getInventory();
String playerConf = "players." + player.getUniqueId() + "." + mode;
staffConf.set(playerConf + PlayerConfig.INVENTORY.getValue(), playerInventory.getContents());
staffConf.set(playerConf + PlayerConfig.ARMOR.getValue(), playerInventory.getArmorContents());
staffConf.set(playerConf + PlayerConfig.LEVEL.getValue(), player.getLevel());
staffConf.set(playerConf + PlayerConfig.EXP.getValue(), player.getExp());
staffConf.set(playerConf + PlayerConfig.GAMEMODE.getValue(), player.getGameMode());
}
private void restore(Player player, String mode) {
String playerConf = "players." + player.getUniqueId() + "." + mode;
ItemStack[] inventory = (ItemStack[]) staffConf.get(playerConf + PlayerConfig.INVENTORY.getValue());
ItemStack[] armor = (ItemStack[]) staffConf.get(playerConf + PlayerConfig.ARMOR.getValue());
int level = staffConf.getInt(playerConf + PlayerConfig.LEVEL.getValue());
Object exp = staffConf.get(playerConf + PlayerConfig.EXP.getValue());
GameMode gameMode = (GameMode) staffConf.get(playerConf + PlayerConfig.GAMEMODE.getValue());
PlayerInventory playerInventory = player.getInventory();
if (inventory != null)
playerInventory.setContents(inventory);
if (armor != null)
playerInventory.setArmorContents(armor);
if (gameMode == null)
gameMode = GameMode.SURVIVAL;
if (exp != null)
player.setExp((float) exp);
player.setLevel(level);
player.setGameMode(gameMode);
}
}

View file

@ -0,0 +1,19 @@
package fr.flifloo.StaffToggle.Configurations;
public enum PlayerConfig {
INVENTORY(".inv"),
ARMOR(".armor"),
LEVEL(".level"),
EXP(".exp"),
GAMEMODE(".gameMode");
String value;
PlayerConfig(String value) {
this.value = value;
}
public String getValue() {
return this.value;
}
}

View file

@ -18,7 +18,7 @@ public class Main extends JavaPlugin {
@Override
public void onDisable() {
staffConf.save();
}
public void registerCommands() {