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
|
@ -6,6 +6,7 @@ import java.lang.reflect.InvocationTargetException;
|
|||
import me.unurled.sacredrealms.sr.SR;
|
||||
import me.unurled.sacredrealms.sr.commands.admin.AttributeCommand;
|
||||
import me.unurled.sacredrealms.sr.commands.admin.ClientBuildCommand;
|
||||
import me.unurled.sacredrealms.sr.commands.admin.LevelCommand;
|
||||
import me.unurled.sacredrealms.sr.commands.player.ResetAdventureCommand;
|
||||
import me.unurled.sacredrealms.sr.managers.Manager;
|
||||
import org.bukkit.command.PluginCommand;
|
||||
|
@ -36,6 +37,7 @@ public class CommandManager extends Manager {
|
|||
|
||||
registerCommand("attributes", AttributeCommand.class);
|
||||
registerCommand("clientbuild", ClientBuildCommand.class);
|
||||
registerCommand("level", LevelCommand.class);
|
||||
|
||||
registerCommand("resetadventure", ResetAdventureCommand.class);
|
||||
}
|
||||
|
|
|
@ -182,6 +182,8 @@ public class AttributeCommand implements TabExecutor {
|
|||
"Set " + attribute.getName() + " to " + value + " for " + target.getName());
|
||||
player.setItemAttributes(attribute, target.getInventory().getItemInMainHand(), value);
|
||||
syncSRToPlayer(player, target);
|
||||
PlayerManager pm = PlayerManager.getInstance(PlayerManager.class);
|
||||
pm.savePlayer(player);
|
||||
} catch (NumberFormatException e) {
|
||||
sender.sendMessage("Invalid value.");
|
||||
}
|
||||
|
@ -202,6 +204,8 @@ public class AttributeCommand implements TabExecutor {
|
|||
player.removeItemAttributes(attribute, target.getInventory().getItemInMainHand());
|
||||
syncSRToPlayer(player, target);
|
||||
sender.sendMessage("Removed " + attribute.getName() + " for " + target.getName());
|
||||
PlayerManager pm = PlayerManager.getInstance(PlayerManager.class);
|
||||
pm.savePlayer(player);
|
||||
} else {
|
||||
sender.sendMessage("Usage: /attribute <set|get|remove> <player> <attribute> [value]");
|
||||
}
|
||||
|
|
|
@ -0,0 +1,82 @@
|
|||
package me.unurled.sacredrealms.sr.commands.admin;
|
||||
|
||||
import java.util.List;
|
||||
import me.unurled.sacredrealms.sr.components.player.PlayerManager;
|
||||
import me.unurled.sacredrealms.sr.components.player.SRPlayer;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.TabExecutor;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class LevelCommand implements TabExecutor {
|
||||
|
||||
/**
|
||||
* Executes the given command, returning its success. <br>
|
||||
* If false is returned, then the "usage" plugin.yml entry for this command (if defined) will be
|
||||
* sent to the player.
|
||||
*
|
||||
* @param sender Source of the command
|
||||
* @param command Command which was executed
|
||||
* @param label Alias of the command which was used
|
||||
* @param args Passed command arguments
|
||||
* @return true if a valid command, otherwise false
|
||||
*/
|
||||
@Override
|
||||
public boolean onCommand(
|
||||
@NotNull CommandSender sender,
|
||||
@NotNull Command command,
|
||||
@NotNull String label,
|
||||
@NotNull String[] args) {
|
||||
if (!sender.hasPermission("sr.level")) {
|
||||
sender.sendMessage("You do not have permission to use this command.");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (args.length != 2) {
|
||||
sender.sendMessage("Usage: /level <player> <level>");
|
||||
return true;
|
||||
}
|
||||
|
||||
Player target = sender.getServer().getPlayer(args[0]);
|
||||
|
||||
if (target == null) {
|
||||
sender.sendMessage("Player not found.");
|
||||
return true;
|
||||
}
|
||||
|
||||
PlayerManager pm = PlayerManager.getInstance(PlayerManager.class);
|
||||
SRPlayer player = pm.getPlayer(target.getUniqueId());
|
||||
|
||||
if (player != null) {
|
||||
player.setLevel(Integer.parseInt(args[1]));
|
||||
// save player data
|
||||
pm.savePlayer(player);
|
||||
} else {
|
||||
sender.sendMessage("Player not found.");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Requests a list of possible completions for a command argument.
|
||||
*
|
||||
* @param sender Source of the command. For players tab-completing a command inside of a command
|
||||
* block, this will be the player, not the command block.
|
||||
* @param command Command which was executed
|
||||
* @param label Alias of the command which was used
|
||||
* @param args The arguments passed to the command, including final partial argument to be
|
||||
* completed
|
||||
* @return A List of possible completions for the final argument, or null to default to the
|
||||
* command executor
|
||||
*/
|
||||
@Override
|
||||
public @Nullable List<String> onTabComplete(
|
||||
@NotNull CommandSender sender,
|
||||
@NotNull Command command,
|
||||
@NotNull String label,
|
||||
@NotNull String[] args) {
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,7 +18,11 @@ permissions:
|
|||
default: op
|
||||
description: When the player has permission for the command /attributes set
|
||||
sr.clientbuild:
|
||||
default: op
|
||||
description: When the player has permission for the command /clientbuild
|
||||
sr.level:
|
||||
default: op
|
||||
description: When the player has permission for the command /level
|
||||
sr.resetadventure:
|
||||
default: op
|
||||
description: When the player has permission for the command /resetadventure
|
||||
|
@ -28,5 +32,7 @@ commands:
|
|||
description: Set the attributes of the player.
|
||||
clientbuild:
|
||||
description: Set the client build of the player.
|
||||
level:
|
||||
description: Set the level of the player.
|
||||
resetadventure:
|
||||
description: Reset the adventure of the player
|
Loading…
Add table
Add a link
Reference in a new issue