Add optional arguments all and lineup to /avatar

This commit is contained in:
Melledy
2023-11-30 06:53:20 -08:00
parent 994382bf50
commit 940bae0217
2 changed files with 56 additions and 14 deletions

View File

@@ -1,5 +1,9 @@
package emu.lunarcore.command.commands;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import emu.lunarcore.command.Command;
import emu.lunarcore.command.CommandArgs;
import emu.lunarcore.command.CommandHandler;
@@ -7,7 +11,7 @@ import emu.lunarcore.game.avatar.GameAvatar;
import emu.lunarcore.game.player.Player;
import emu.lunarcore.server.packet.send.PacketPlayerSyncScNotify;
@Command(label = "avatar", aliases = {"a"}, permission = "player.avatar", desc = "/avatar lv(level) p(ascension) r(eidolon) s(skill levels). Sets the current avatar's properties")
@Command(label = "avatar", aliases = {"a"}, permission = "player.avatar", desc = "/avatar {cur | all | lineup} lv(level) p(ascension) r(eidolon) s(skill levels). Sets the current avatar's properties")
public class AvatarCommand implements CommandHandler {
@Override
@@ -18,25 +22,45 @@ public class AvatarCommand implements CommandHandler {
return;
}
// Get current leader avatar
GameAvatar avatar = args.getTarget().getCurrentLeaderAvatar();
if (avatar == null) {
// No leader!
return;
// Temp avatar list
List<GameAvatar> changeList = new ArrayList<>();
// Handle optional arguments
switch (args.get(0).toLowerCase()) {
case "all":
args.getTarget().getAvatars().forEach(changeList::add);
break;
case "lineup":
args.getTarget().getCurrentLineup().forEachAvatar(changeList::add);
break;
case "cur":
default:
changeList.add(args.getTarget().getCurrentLeaderAvatar());
break;
}
// Change properties
// Try to set properties of avatars
Iterator<GameAvatar> it = changeList.iterator();
while (it.hasNext()) {
GameAvatar avatar = it.next();
if (args.setProperties(avatar)) {
// Save avatar
avatar.save();
// Send packet
args.getTarget().sendPacket(new PacketPlayerSyncScNotify(avatar));
// Send message
sender.sendMessage("Set avatar properties successfully");
} else {
sender.sendMessage("No avatar properties to change");
// Remove from list if nothing was changed
it.remove();
}
}
if (changeList.size() > 0) {
// Send packet
args.getTarget().sendPacket(new PacketPlayerSyncScNotify(changeList.toArray(GameAvatar[]::new)));
// Send message
this.sendMessage(sender, "Set avatar(s) properties successfully");
} else {
this.sendMessage(sender, "No avatar properties to change");
}
}

View File

@@ -71,6 +71,24 @@ public class PacketPlayerSyncScNotify extends BasePacket {
this.setData(data);
}
public PacketPlayerSyncScNotify(GameAvatar... avatars) { // Ugly workaround
this();
var data = PlayerSyncScNotify.newInstance();
for (var avatar : avatars) {
// Sync avatar
data.getMutableAvatarSync().addAvatarList(avatar.toProto());
// Also update hero basic type info if were updating the main character
if (avatar.getHeroPath() != null) {
data.getMutableBasicTypeInfoList().add(avatar.getHeroPath().toProto());
}
}
this.setData(data);
}
public PacketPlayerSyncScNotify(Collection<GameItem> items) {
this();