Command handlers no longer need to handle null targets

This commit is contained in:
Melledy 2023-12-01 00:59:46 -08:00
parent 97dd287389
commit e816474764
18 changed files with 44 additions and 123 deletions

View File

@ -12,4 +12,6 @@ public @interface Command {
public String desc() default "";
public String permission() default "";
public boolean requireTarget() default false;
}

View File

@ -5,8 +5,12 @@ import emu.lunarcore.game.player.Player;
public interface CommandHandler {
public default Command getData() {
return this.getClass().getAnnotation(Command.class);
}
public default String getLabel() {
return this.getClass().getAnnotation(Command.class).label();
return getData().label();
}
public default void sendMessage(Player player, String message) {

View File

@ -108,19 +108,30 @@ public class CommandManager {
// Execute
if (handler != null) {
// Command annotation data
Command command = handler.getClass().getAnnotation(Command.class);
Command command = handler.getData();
// Check permission
if (this.checkPermission(sender, command)) {
// Check targeted permission
// Build command arguments
CommandArgs cmdArgs = new CommandArgs(sender, args);
// Check targeted permission
if (sender != cmdArgs.getTarget() && !this.checkTargetPermission(sender, command)) {
handler.sendMessage(sender, "You do not have permission to use this command on another player.");
return;
}
// Make sure our command has a target
if (command.requireTarget() && cmdArgs.getTarget() == null) {
handler.sendMessage(sender, "Error: Targeted player not found or offline");
return;
}
// Log
if (sender != null && LunarCore.getConfig().getLogOptions().commands) {
LunarCore.getLogger().info("[UID: " + sender.getUid() + "] " + sender.getName() + " used command: " + message);
}
// Run command
handler.execute(sender, cmdArgs);
} else {

View File

@ -11,17 +11,11 @@ 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 {cur | all | lineup} lv(level) p(ascension) r(eidolon) s(skill levels). Sets the current avatar's properties")
@Command(label = "avatar", aliases = {"a"}, requireTarget = true, 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
public void execute(Player sender, CommandArgs args) {
// Check target
if (args.getTarget() == null) {
this.sendMessage(sender, "Error: Targeted player not found or offline");
return;
}
// Temp avatar list
List<GameAvatar> changeList = new ArrayList<>();

View File

@ -10,17 +10,11 @@ import emu.lunarcore.game.enums.ItemMainType;
import emu.lunarcore.game.inventory.GameItem;
import emu.lunarcore.game.player.Player;
@Command(label = "clear", permission = "player.clear", desc = "/clear {relics | lightcones | materials | items}. Removes filtered items from the player inventory.")
@Command(label = "clear", permission = "player.clear", requireTarget = true, desc = "/clear {relics | lightcones | materials | items}. Removes filtered items from the player inventory.")
public class ClearCommand implements CommandHandler {
@Override
public void execute(Player sender, CommandArgs args) {
// Check target
if (args.getTarget() == null) {
this.sendMessage(sender, "Error: Targeted player not found or offline");
return;
}
List<GameItem> toRemove = new LinkedList<>();
String type = args.get(0).toLowerCase();

View File

@ -8,17 +8,11 @@ import emu.lunarcore.game.player.Player;
import emu.lunarcore.game.player.PlayerGender;
import emu.lunarcore.server.packet.send.PacketGetHeroBasicTypeInfoScRsp;
@Command(label = "gender", permission = "player.gender", desc = "/gender {male | female}. Sets the player gender.")
@Command(label = "gender", permission = "player.gender", requireTarget = true, desc = "/gender {male | female}. Sets the player gender.")
public class GenderCommand implements CommandHandler {
@Override
public void execute(Player sender, CommandArgs args) {
// Check target
if (args.getTarget() == null) {
this.sendMessage(sender, "Error: Targeted player not found or offline");
return;
}
// Set world level
Player target = args.getTarget();
PlayerGender playerGender = null;

View File

@ -15,17 +15,11 @@ import emu.lunarcore.game.enums.ItemSubType;
import emu.lunarcore.game.inventory.GameItem;
import emu.lunarcore.game.player.Player;
@Command(label = "giveall", aliases = {"ga"}, permission = "player.give", desc = "/giveall {materials | avatars | lightcones | relics}. Gives the targeted player items.")
@Command(label = "giveall", aliases = {"ga"}, permission = "player.give", requireTarget = true, desc = "/giveall {materials | avatars | lightcones | relics}. Gives the targeted player items.")
public class GiveAllCommand implements CommandHandler {
@Override
public void execute(Player sender, CommandArgs args) {
// Check target
if (args.getTarget() == null) {
this.sendMessage(sender, "Error: Targeted player not found or offline");
return;
}
Player target = args.getTarget();
String type = args.get(0).toLowerCase();

View File

@ -14,17 +14,11 @@ import emu.lunarcore.game.inventory.GameItem;
import emu.lunarcore.game.player.Player;
import emu.lunarcore.util.Utils;
@Command(label = "give", aliases = {"g"}, permission = "player.give", desc = "/give [item id] x[amount]. Gives the targetted player an item.")
@Command(label = "give", aliases = {"g"}, permission = "player.give", requireTarget = true, desc = "/give [item id] x[amount]. Gives the targetted player an item.")
public class GiveCommand implements CommandHandler {
@Override
public void execute(Player sender, CommandArgs args) {
// Check target
if (args.getTarget() == null) {
this.sendMessage(sender, "Error: Targeted player not found or offline");
return;
}
int itemId = Utils.parseSafeInt(args.get(0));
int amount = Math.max(args.getAmount(), 1);

View File

@ -6,17 +6,11 @@ import emu.lunarcore.command.CommandHandler;
import emu.lunarcore.game.player.lineup.PlayerLineup;
import emu.lunarcore.game.player.Player;
@Command(label = "heal", permission = "player.heal", desc = "/heal. Heals your avatars.")
@Command(label = "heal", permission = "player.heal", requireTarget = true, desc = "/heal. Heals your avatars.")
public class HealCommand implements CommandHandler {
@Override
public void execute(Player sender, CommandArgs args) {
// Check target
if (args.getTarget() == null) {
this.sendMessage(sender, "Error: Targeted player not found or offline");
return;
}
PlayerLineup lineup = args.getTarget().getCurrentLineup();
lineup.forEachAvatar(avatar -> {
avatar.setCurrentHp(lineup, 10000);

View File

@ -8,18 +8,13 @@ import emu.lunarcore.game.player.Player;
@Command(
label = "kick",
desc = "/kick @[player id]. Kicks a player from the server.",
requireTarget = true,
permission = "player.kick"
)
public final class KickCommand implements CommandHandler {
@Override
public void execute(Player sender, CommandArgs args) {
// Check target
if (args.getTarget() == null) {
this.sendMessage(sender, "Error: Targeted player not found or offline");
return;
}
// Kick player
args.getTarget().getSession().close();

View File

@ -6,41 +6,28 @@ import java.util.List;
import emu.lunarcore.command.Command;
import emu.lunarcore.command.CommandArgs;
import emu.lunarcore.command.CommandHandler;
import emu.lunarcore.data.GameData;
import emu.lunarcore.data.excel.ItemExcel;
import emu.lunarcore.game.inventory.GameItem;
import emu.lunarcore.game.mail.Mail;
import emu.lunarcore.game.player.Player;
@Command(label = "mail", aliases = {"m"}, permission = "player.mail", desc = "/mail [content]. Sends the targeted player a system mail.")
@Command(label = "mail", aliases = {"m"}, permission = "player.mail", requireTarget = true, desc = "/mail [content]. Sends the targeted player a system mail.")
public class MailCommand implements CommandHandler {
@Override
public void execute(Player sender, CommandArgs args) {
// Check target
if (args.getTarget() == null) {
this.sendMessage(sender, "Error: Targeted player not found or offline");
return;
}
// Get attachments
List<GameItem> attachments = new ArrayList<>();
var it = args.getList().iterator();
while (it.hasNext()) {
try {
String str = it.next();
if (args.getMap() != null) {
for (var entry : args.getMap().int2IntEntrySet()) {
if (entry.getIntValue() <= 0) continue;
if (str.contains(":")) {
String[] split = str.split(":");
int itemId = Integer.parseInt(split[0]);
int count = Integer.parseInt(split[1]);
attachments.add(new GameItem(itemId, count));
it.remove();
}
} catch (Exception e) {
ItemExcel itemExcel = GameData.getItemExcelMap().get(entry.getIntKey());
if (itemExcel == null) continue;
attachments.add(new GameItem(itemExcel, entry.getIntValue()));
}
}

View File

@ -5,17 +5,11 @@ import emu.lunarcore.command.CommandArgs;
import emu.lunarcore.command.CommandHandler;
import emu.lunarcore.game.player.Player;
@Command(label = "permission", aliases = {"perm"}, permission = "admin.permission", desc = "/permission {add | remove | clear} [permission]. Gives/removes a permission from the targeted player.")
@Command(label = "permission", aliases = {"perm"}, permission = "admin.permission", requireTarget = true, desc = "/permission {add | remove | clear} [permission]. Gives/removes a permission from the targeted player.")
public class PermissionCommand implements CommandHandler {
@Override
public void execute(Player sender, CommandArgs args) {
// Check target
if (args.getTarget() == null) {
this.sendMessage(sender, "Error: Targeted player not found or offline");
return;
}
String type = args.get(0).toLowerCase();
String permission = args.get(1).toLowerCase();

View File

@ -6,17 +6,11 @@ import emu.lunarcore.command.CommandArgs;
import emu.lunarcore.command.CommandHandler;
import emu.lunarcore.game.player.Player;
@Command(label = "refill", aliases = {"rf"}, permission = "player.refill", desc = "/refill - refill your skill points in open world.")
@Command(label = "refill", aliases = {"rf"}, permission = "player.refill", requireTarget = true, desc = "/refill - refill your skill points in open world.")
public class RefillMPCommand implements CommandHandler {
@Override
public void execute(Player sender, CommandArgs args) {
// Check target
if (args.getTarget() == null) {
this.sendMessage(sender, "Error: Targeted player not found or offline");
return;
}
sender.getCurrentLineup().addMp(GameConstants.MAX_MP);
this.sendMessage(sender, "Successfully refilled skill points for " + args.getTarget().getName());
}

View File

@ -6,17 +6,11 @@ import emu.lunarcore.command.CommandArgs;
import emu.lunarcore.command.CommandHandler;
import emu.lunarcore.game.player.Player;
@Command(label = "energy", permission = "player.energy", desc = "/energy. Refills all characters energy in current lineup.")
@Command(label = "energy", permission = "player.energy", requireTarget = true, desc = "/energy. Refills all characters energy in current lineup.")
public class RefillSPCommand implements CommandHandler {
@Override
public void execute(Player sender, CommandArgs args) {
// Check target
if (args.getTarget() == null) {
this.sendMessage(sender, "Error: Targeted player not found or offline");
return;
}
PlayerLineup lineup = args.getTarget().getCurrentLineup();
lineup.forEachAvatar(avatar -> {
avatar.setCurrentSp(lineup, 10000);

View File

@ -10,17 +10,11 @@ import emu.lunarcore.data.excel.MazePlaneExcel;
import emu.lunarcore.game.player.Player;
import emu.lunarcore.util.Utils;
@Command(label = "scene", aliases = {"sc"}, permission = "player.scene", desc = "/scene [scene id] [floor id]. Teleports the player to the specified scene.")
@Command(label = "scene", aliases = {"sc"}, permission = "player.scene", requireTarget = true, desc = "/scene [scene id] [floor id]. Teleports the player to the specified scene.")
public class SceneCommand implements CommandHandler {
@Override
public void execute(Player sender, CommandArgs args) {
// Check target
if (args.getTarget() == null) {
this.sendMessage(sender, "Error: Targeted player not found or offline");
return;
}
// Get arguments
int planeId = Utils.parseSafeInt(args.get(0));
int floorId = Utils.parseSafeInt(args.get(1));

View File

@ -6,17 +6,11 @@ import emu.lunarcore.command.CommandArgs;
import emu.lunarcore.command.CommandHandler;
import emu.lunarcore.game.player.Player;
@Command(label = "setlevel", aliases = {"level"}, permission = "player.setlevel", desc = "/setlevel [level] - Sets the targeted player's trailblazer level.")
@Command(label = "setlevel", aliases = {"level"}, permission = "player.setlevel", requireTarget = true, desc = "/setlevel [level] - Sets the targeted player's trailblazer level.")
public class SetLevelCommand implements CommandHandler {
@Override
public void execute(Player sender, CommandArgs args) {
// Check target
if (args.getTarget() == null) {
this.sendMessage(sender, "Error: Targeted player not found or offline");
return;
}
int targetLevel = Utils.parseSafeInt(args.get(0));
args.getTarget().setLevel(targetLevel);

View File

@ -16,17 +16,11 @@ import emu.lunarcore.game.scene.entity.EntityProp;
import emu.lunarcore.util.Position;
import emu.lunarcore.util.Utils;
@Command(label = "spawn", permission = "player.spawn", desc = "/spawn [monster/prop id] x[amount] s[stage id]. Spawns a monster or prop near the targeted player.")
@Command(label = "spawn", permission = "player.spawn", requireTarget = true, desc = "/spawn [monster/prop id] x[amount] s[stage id]. Spawns a monster or prop near the targeted player.")
public class SpawnCommand implements CommandHandler {
@Override
public void execute(Player sender, CommandArgs args) {
// Check target
if (args.getTarget() == null) {
this.sendMessage(sender, "Error: Targeted player not found or offline");
return;
}
Player target = args.getTarget();
if (target.getScene() == null) {

View File

@ -6,17 +6,11 @@ import emu.lunarcore.command.CommandHandler;
import emu.lunarcore.game.player.Player;
import emu.lunarcore.util.Utils;
@Command(label = "worldlevel", aliases = {"wl"}, permission = "player.worldlevel", desc = "/worldlevel [world level]. Sets the targeted player's equilibrium level.")
@Command(label = "worldlevel", aliases = {"wl"}, permission = "player.worldlevel", requireTarget = true, desc = "/worldlevel [world level]. Sets the targeted player's equilibrium level.")
public class WorldLevelCommand implements CommandHandler {
@Override
public void execute(Player sender, CommandArgs args) {
// Check target
if (args.getTarget() == null) {
this.sendMessage(sender, "Error: Targeted player not found or offline");
return;
}
// Set world level
int level = Utils.parseSafeInt(args.get(0));
level = Math.min(Math.max(level, 0), 6);