level command and refactoring to save and load player

This commit is contained in:
unurled 2024-03-14 17:21:12 +01:00
parent cde1c48b7a
commit 0af15fbd5a
Signed by: unurled
GPG key ID: FDBC9CBE1F82423F
6 changed files with 167 additions and 40 deletions

View file

@ -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);
}

View file

@ -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]");
}

View file

@ -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;
}
}