level command and refactoring to save and load player
This commit is contained in:
parent
cde1c48b7a
commit
0af15fbd5a
6 changed files with 167 additions and 40 deletions
|
@ -30,6 +30,7 @@ public class PlayerManager extends Manager {
|
|||
|
||||
public static final String PLAYER_KEY = "sr.players.";
|
||||
private final HashMap<UUID, SRPlayer> players;
|
||||
private DataHandler dh;
|
||||
|
||||
public PlayerManager() {
|
||||
super();
|
||||
|
@ -39,21 +40,18 @@ public class PlayerManager extends Manager {
|
|||
/** Save the data */
|
||||
@Override
|
||||
public void saveData() {
|
||||
DataManager dm = DataManager.getInstance(DataManager.class);
|
||||
if (dm == null) {
|
||||
error("DataManager is null, Can't save player data.");
|
||||
return;
|
||||
if (dh == null) {
|
||||
DataManager dm = DataManager.getInstance(DataManager.class);
|
||||
if (dm == null) {
|
||||
error("DataManager is null, Can't save player data.");
|
||||
return;
|
||||
}
|
||||
dh = dm.getDataHandler();
|
||||
}
|
||||
DataHandler dh = dm.getDataHandler();
|
||||
|
||||
// save all players
|
||||
Gson gson =
|
||||
new GsonBuilder()
|
||||
.excludeFieldsWithoutExposeAnnotation()
|
||||
.registerTypeAdapter(Inventory.class, new InventorySerializer())
|
||||
.registerTypeAdapter(PotionEffect.class, new PotionEffectSerializer())
|
||||
.create();
|
||||
for (SRPlayer player : players.values()) {
|
||||
dh.set(PLAYER_KEY + player.getUuid(), gson.toJson(player));
|
||||
savePlayer(player);
|
||||
}
|
||||
|
||||
players.clear();
|
||||
|
@ -61,7 +59,12 @@ public class PlayerManager extends Manager {
|
|||
|
||||
@Nullable
|
||||
public SRPlayer getPlayer(UUID uuid) {
|
||||
return players.get(uuid);
|
||||
if (players.containsKey(uuid)) {
|
||||
return players.get(uuid);
|
||||
}
|
||||
// load player data and returns it;
|
||||
|
||||
return loadPlayer(uuid);
|
||||
}
|
||||
|
||||
public void addPlayer(@NotNull SRPlayer player) {
|
||||
|
@ -76,26 +79,31 @@ public class PlayerManager extends Manager {
|
|||
return players.containsKey(uuid);
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerJoin(PlayerJoinEvent e) {
|
||||
DataManager dm = DataManager.getInstance(DataManager.class);
|
||||
if (dm == null) {
|
||||
error("DataManager is null, Can't load player " + e.getPlayer().getName() + "'s data" + ".");
|
||||
return;
|
||||
public void savePlayer(SRPlayer player) {
|
||||
if (dh == null) {
|
||||
DataManager dm = DataManager.getInstance(DataManager.class);
|
||||
if (dm == null) {
|
||||
error("DataManager is null, Can't save player data.");
|
||||
return;
|
||||
}
|
||||
dh = dm.getDataHandler();
|
||||
}
|
||||
DataHandler dh = dm.getDataHandler();
|
||||
// save all players
|
||||
Gson gson =
|
||||
new GsonBuilder()
|
||||
.registerTypeAdapter(Inventory.class, new InventoryDeserializer())
|
||||
.registerTypeAdapter(PotionEffect.class, new PotionEffectDeserializer())
|
||||
.excludeFieldsWithoutExposeAnnotation()
|
||||
.registerTypeAdapter(Inventory.class, new InventorySerializer())
|
||||
.registerTypeAdapter(PotionEffect.class, new PotionEffectSerializer())
|
||||
.create();
|
||||
String json = dh.get(PLAYER_KEY + e.getPlayer().getUniqueId());
|
||||
SRPlayer srPlayer = gson.fromJson(json, SRPlayer.class);
|
||||
if (srPlayer == null) {
|
||||
srPlayer = new SRPlayer(e.getPlayer());
|
||||
}
|
||||
dh.set(PLAYER_KEY + player.getUuid(), gson.toJson(player));
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerJoin(@NotNull PlayerJoinEvent e) {
|
||||
SRPlayer srPlayer = loadPlayer(e.getPlayer().getUniqueId());
|
||||
if (srPlayer == null) {
|
||||
return;
|
||||
}
|
||||
addPlayer(srPlayer);
|
||||
|
||||
e.getPlayer().getInventory().clear();
|
||||
|
@ -106,6 +114,31 @@ public class PlayerManager extends Manager {
|
|||
e.getPlayer().updateInventory();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private SRPlayer loadPlayer(UUID p) {
|
||||
if (dh == null) {
|
||||
DataManager dm = DataManager.getInstance(DataManager.class);
|
||||
if (dm == null) {
|
||||
error("DataManager is null, Can't save player data.");
|
||||
return null;
|
||||
}
|
||||
dh = dm.getDataHandler();
|
||||
}
|
||||
|
||||
Gson gson =
|
||||
new GsonBuilder()
|
||||
.registerTypeAdapter(Inventory.class, new InventoryDeserializer())
|
||||
.registerTypeAdapter(PotionEffect.class, new PotionEffectDeserializer())
|
||||
.excludeFieldsWithoutExposeAnnotation()
|
||||
.create();
|
||||
String json = dh.get(PLAYER_KEY + p);
|
||||
SRPlayer srPlayer = gson.fromJson(json, SRPlayer.class);
|
||||
if (srPlayer == null) {
|
||||
srPlayer = new SRPlayer(p);
|
||||
}
|
||||
return srPlayer;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerQuit(@NotNull PlayerQuitEvent e) {
|
||||
SRPlayer player = getPlayer(e.getPlayer().getUniqueId());
|
||||
|
@ -113,19 +146,7 @@ public class PlayerManager extends Manager {
|
|||
return;
|
||||
}
|
||||
// save for player
|
||||
DataManager dm = DataManager.getInstance(DataManager.class);
|
||||
if (dm == null) {
|
||||
error("DataManager is null, Can't save player " + e.getPlayer().getName() + "'s data" + ".");
|
||||
return;
|
||||
}
|
||||
DataHandler dh = dm.getDataHandler();
|
||||
Gson gson =
|
||||
new GsonBuilder()
|
||||
.excludeFieldsWithoutExposeAnnotation()
|
||||
.registerTypeAdapter(Inventory.class, new InventorySerializer())
|
||||
.registerTypeAdapter(PotionEffect.class, new PotionEffectSerializer())
|
||||
.create();
|
||||
dh.set("sr.players." + e.getPlayer().getUniqueId(), gson.toJson(player));
|
||||
savePlayer(player);
|
||||
|
||||
removePlayer(e.getPlayer().getUniqueId());
|
||||
}
|
||||
|
|
|
@ -29,6 +29,14 @@ public class SRPlayer {
|
|||
@Expose private List<PotionEffect> potionEffects;
|
||||
@Expose private Inventory inventory;
|
||||
|
||||
public SRPlayer(@NotNull UUID uuid) {
|
||||
this.uuid = uuid;
|
||||
Player player = Bukkit.getPlayer(uuid);
|
||||
this.inventory = (player != null) ? player.getInventory() : null;
|
||||
this.itemAttributes = new HashMap<>();
|
||||
this.potionEffects = new ArrayList<>();
|
||||
}
|
||||
|
||||
public SRPlayer(@NotNull Player player) {
|
||||
this.uuid = player.getUniqueId();
|
||||
this.inventory = player.getInventory();
|
||||
|
@ -214,4 +222,8 @@ public class SRPlayer {
|
|||
public int getLevel() {
|
||||
return level;
|
||||
}
|
||||
|
||||
public void setLevel(int i) {
|
||||
level = i;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue