Add /lineup command

This commit is contained in:
Melledy
2023-12-02 04:28:00 -08:00
parent f9dd1dabc7
commit 9c200f8142
3 changed files with 71 additions and 1 deletions

View File

@@ -0,0 +1,64 @@
package emu.lunarcore.command.commands;
import java.util.ArrayList;
import java.util.List;
import emu.lunarcore.command.Command;
import emu.lunarcore.command.CommandArgs;
import emu.lunarcore.command.CommandHandler;
import emu.lunarcore.game.avatar.GameAvatar;
import emu.lunarcore.game.player.Player;
import emu.lunarcore.game.player.lineup.PlayerLineup;
import emu.lunarcore.util.Utils;
@Command(label = "lineup", permission = "player.lineup", requireTarget = true, desc = "/lineup [avatar ids]. USE AT YOUR OWN RISK. Sets your current lineup with the specified avatar ids.")
public class LineupCommand implements CommandHandler {
@Override
public void execute(Player sender, CommandArgs args) {
// Get target player
Player target = args.getTarget();
// Do not set lineup while the target player is in a battle
if (target.isInBattle()) {
this.sendMessage(sender, "Error: The targeted player is in a battle");
return;
}
// Temp avatar list
List<Integer> avatars = new ArrayList<>();
// Validate avatars in temp list
for (String arg : args.getList()) {
// Make sure the avatar actually exist
GameAvatar avatar = target.getAvatarById(Utils.parseSafeInt(arg));
if (avatar == null) continue;
avatars.add(avatar.getAvatarId());
// Soft cap check
if (avatars.size() >= 64) {
break;
}
}
// Replace cleanly
if (avatars.size() > 0) {
// Only replace lineup if we have avatars to replace with
// The client wont like it if we have a lineup with 0 avatars.
PlayerLineup lineup = target.getCurrentLineup();
lineup.getAvatars().clear();
lineup.getAvatars().addAll(avatars);
lineup.save();
// Send client packets to sync lineup
lineup.refreshLineup();
target.getScene().syncLineup();
this.sendMessage(sender, "Set the lineup of " + target.getName() + " successfully");
} else {
this.sendMessage(sender, "No avatars could be added");
}
}
}

View File

@@ -47,7 +47,7 @@ public class GameAvatar implements GameEntity {
private transient Player owner; private transient Player owner;
private transient AvatarExcel excel; private transient AvatarExcel excel;
private int avatarId; // Id of avatar private int avatarId; // Id of avatar in the excels
private AvatarData data; private AvatarData data;
@Setter private int level; @Setter private int level;
@Setter private int exp; @Setter private int exp;

View File

@@ -83,6 +83,9 @@ public class Utils {
return Math.max(Math.min((int) ((level - 11) / 10D), 6), 0); return Math.max(Math.min((int) ((level - 11) / 10D), 6), 0);
} }
/**
* Parses the string argument as a signed decimal integer. Returns a 0 if the string argument is not an integer.
*/
public static int parseSafeInt(String s) { public static int parseSafeInt(String s) {
if (s == null) { if (s == null) {
return 0; return 0;
@@ -99,6 +102,9 @@ public class Utils {
return i; return i;
} }
/**
* Parses the string argument as a signed decimal long. Returns a 0 if the string argument is not a long.
*/
public static long parseSafeLong(String s) { public static long parseSafeLong(String s) {
if (s == null) { if (s == null) {
return 0; return 0;