mirror of
https://github.com/KAMKEEL/CustomNPC-Plus.git
synced 2026-11-12 23:37:32 +00:00
Profile Rename Command + List Command
This commit is contained in:
1 parent
2ebe4ed9c5
commit
3cac4e764f
4 files changed
+145
-19
No files matched your search
@@ -26,6 +26,7 @@ public class CommandProfile extends CommandBase {
|
||||
registerCommand(new CommandProfileRemove());
|
||||
registerCommand(new CommandProfileAdmin());
|
||||
registerCommand(new CommandProfileList());
|
||||
registerCommand(new CommandProfileRename());
|
||||
}
|
||||
|
||||
public void registerCommand(CommandProfileBase command){
|
||||
|
||||
@@ -190,18 +190,72 @@ public class CommandProfileAdmin extends CommandProfileBase {
|
||||
sendError(sender, "Profile not found for player: " + targetPlayer);
|
||||
return;
|
||||
}
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("Profile Slots for ").append(targetPlayer).append(":\n");
|
||||
sendMessage(sender, "Profile Slots for " + targetPlayer + ":");
|
||||
for (Map.Entry<Integer, Slot> entry : targetProfile.slots.entrySet()) {
|
||||
int id = entry.getKey();
|
||||
String name = entry.getValue().getName();
|
||||
if (id == targetProfile.currentID) {
|
||||
sb.append(" * "); // active slot
|
||||
} else {
|
||||
sb.append(" - ");
|
||||
}
|
||||
sb.append("Slot ").append(id).append(": ").append(name).append("\n");
|
||||
String prefix = (id == targetProfile.currentID) ? "* " : "- ";
|
||||
sendMessage(sender, prefix + "Slot " + id + ": " + name);
|
||||
}
|
||||
sendMessage(sender, sb.toString());
|
||||
}
|
||||
|
||||
@SubCommand(
|
||||
desc = "Rename a specified player's slot.",
|
||||
usage = "<targetPlayer> <slotId> <newName>"
|
||||
)
|
||||
public void rename(ICommandSender sender, String[] args) throws CommandException {
|
||||
if (args.length < 3) {
|
||||
sendError(sender, "Usage: /profile admin rename <targetPlayer> <slotId> <newName>");
|
||||
return;
|
||||
}
|
||||
String targetPlayer = args[0];
|
||||
int slotId;
|
||||
try {
|
||||
slotId = Integer.parseInt(args[1]);
|
||||
} catch (NumberFormatException ex) {
|
||||
sendError(sender, "Slot ID must be a number.");
|
||||
return;
|
||||
}
|
||||
// Combine remaining arguments for the new name.
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 2; i < args.length; i++) {
|
||||
if (i > 2) sb.append(" ");
|
||||
sb.append(args[i]);
|
||||
}
|
||||
String newName = sb.toString().trim();
|
||||
// Cap name at 20 characters.
|
||||
if (newName.length() > 20) {
|
||||
newName = newName.substring(0, 20);
|
||||
}
|
||||
// Validate that name contains only letters and spaces.
|
||||
if (!newName.matches("[A-Za-z ]+")) {
|
||||
sendError(sender, "Invalid name. Only alphabetic characters and spaces are allowed.");
|
||||
return;
|
||||
}
|
||||
Profile targetProfile = ProfileController.getProfile(targetPlayer);
|
||||
if (targetProfile == null) {
|
||||
sendError(sender, "Profile not found for player: " + targetPlayer);
|
||||
return;
|
||||
}
|
||||
if (targetProfile.locked) {
|
||||
sendError(sender, "Profile for " + targetPlayer + " is locked. Please try again later.");
|
||||
return;
|
||||
}
|
||||
if (!targetProfile.slots.containsKey(slotId)) {
|
||||
sendError(sender, "Slot " + slotId + " not found in " + targetPlayer + "'s profile.");
|
||||
return;
|
||||
}
|
||||
// Rename the slot.
|
||||
Slot targetSlot = targetProfile.slots.get(slotId);
|
||||
targetSlot.setName(newName);
|
||||
// Save the updated profile.
|
||||
if (targetProfile.player != null) {
|
||||
ProfileController.save(targetProfile.player, targetProfile);
|
||||
} else {
|
||||
UUID uuid = ProfileController.getUUIDFromUsername(targetPlayer);
|
||||
if (uuid != null)
|
||||
ProfileController.saveOffline(targetProfile, uuid);
|
||||
}
|
||||
sendResult(sender, "Successfully renamed slot %s for %s to '%s'.", slotId, targetPlayer, newName);
|
||||
}
|
||||
}
|
||||
@@ -23,7 +23,7 @@ public class CommandProfileList extends CommandProfileBase {
|
||||
|
||||
@Override
|
||||
public String getUsage() {
|
||||
return ""; // No extra parameters
|
||||
return ""; // No extra parameters.
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -38,18 +38,12 @@ public class CommandProfileList extends CommandProfileBase {
|
||||
sendError(sender, "Profile not found.");
|
||||
return;
|
||||
}
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("Your Profile Slots:\n");
|
||||
sendMessage(sender, "Your Profile Slots:");
|
||||
for (Map.Entry<Integer, Slot> entry : profile.slots.entrySet()) {
|
||||
int id = entry.getKey();
|
||||
String name = entry.getValue().getName();
|
||||
if (id == profile.currentID) {
|
||||
sb.append(" * "); // mark active slot
|
||||
} else {
|
||||
sb.append(" - ");
|
||||
}
|
||||
sb.append("Slot ").append(id).append(": ").append(name).append("\n");
|
||||
String prefix = (id == profile.currentID) ? "* " : "- ";
|
||||
sendMessage(sender, prefix + "Slot " + id + ": " + name);
|
||||
}
|
||||
sendMessage(sender, sb.toString());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package kamkeel.npcs.command.profile;
|
||||
|
||||
import kamkeel.npcs.controllers.ProfileController;
|
||||
import kamkeel.npcs.controllers.data.Profile;
|
||||
import kamkeel.npcs.controllers.data.Slot;
|
||||
import net.minecraft.command.CommandException;
|
||||
import net.minecraft.command.ICommandSender;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
|
||||
public class CommandProfileRename extends CommandProfileBase {
|
||||
|
||||
@Override
|
||||
public String getCommandName() {
|
||||
return "rename";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return "Rename one of your profile slots. Only letters and spaces are allowed (max 20 characters).";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUsage() {
|
||||
return "<slotId> <newName>";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void processCommand(ICommandSender sender, String[] args) throws CommandException {
|
||||
if (!(sender instanceof EntityPlayer)) {
|
||||
sendError(sender, "This command can only be used by a player.");
|
||||
return;
|
||||
}
|
||||
if (args.length < 2) {
|
||||
sendError(sender, "Usage: " + getUsage());
|
||||
return;
|
||||
}
|
||||
int slotId;
|
||||
try {
|
||||
slotId = Integer.parseInt(args[0]);
|
||||
} catch (NumberFormatException e) {
|
||||
sendError(sender, "Slot ID must be a number.");
|
||||
return;
|
||||
}
|
||||
// Combine remaining arguments into one name string.
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 1; i < args.length; i++) {
|
||||
if (i > 1) sb.append(" ");
|
||||
sb.append(args[i]);
|
||||
}
|
||||
String newName = sb.toString().trim();
|
||||
// Cap at 20 characters.
|
||||
if (newName.length() > 20) {
|
||||
newName = newName.substring(0, 20);
|
||||
}
|
||||
// Validate that the new name contains only letters and spaces.
|
||||
if (!newName.matches("[A-Za-z ]+")) {
|
||||
sendError(sender, "Invalid name. Only alphabetic characters and spaces are allowed.");
|
||||
return;
|
||||
}
|
||||
|
||||
EntityPlayer player = (EntityPlayer) sender;
|
||||
Profile profile = ProfileController.getProfile(player);
|
||||
if (profile == null) {
|
||||
sendError(sender, "Profile not found.");
|
||||
return;
|
||||
}
|
||||
if (!profile.slots.containsKey(slotId)) {
|
||||
sendError(sender, "Slot " + slotId + " not found in your profile.");
|
||||
return;
|
||||
}
|
||||
Slot slot = profile.slots.get(slotId);
|
||||
slot.setName(newName);
|
||||
// Save the profile after renaming.
|
||||
ProfileController.save(player, profile);
|
||||
sendResult(sender, "Successfully renamed slot %s to '%s'.", slotId, newName);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user