From b820ff58d11acb06509d677a710c308f440d91d4 Mon Sep 17 00:00:00 2001 From: Nobody314159 <84757662+Nobody314159@users.noreply.github.com> Date: Sat, 11 May 2024 18:10:52 -0700 Subject: [PATCH] BREAKING: Update CommandAPI version to 9.4.1 (#56) --- build.gradle.kts | 4 +- .../bestiary/BestiaryCommand.java | 116 ++++++----- .../commands/LibraryOfSoulsCommand.java | 184 +++++++++--------- .../commands/SpawnerNBTCommand.java | 25 +-- 4 files changed, 166 insertions(+), 163 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index f79fa4e..538e543 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -46,11 +46,13 @@ repositories { maven { url = uri("https://repo.maven.apache.org/maven2/") } + + maven("https://repo.codemc.org/repository/maven-public/") } dependencies { compileOnly("io.papermc.paper:paper-api:1.18.2-R0.1-SNAPSHOT") - compileOnly("dev.jorel.CommandAPI:commandapi-core:8.7.0") + compileOnly("dev.jorel:commandapi-bukkit-core:9.4.1") compileOnly("com.playmonumenta:nbteditor:4.0") compileOnly("com.playmonumenta:redissync:3.0") compileOnly("com.google.code.gson:gson:2.8.5") diff --git a/src/main/java/com/playmonumenta/libraryofsouls/bestiary/BestiaryCommand.java b/src/main/java/com/playmonumenta/libraryofsouls/bestiary/BestiaryCommand.java index 7ba7cfb..66d0843 100644 --- a/src/main/java/com/playmonumenta/libraryofsouls/bestiary/BestiaryCommand.java +++ b/src/main/java/com/playmonumenta/libraryofsouls/bestiary/BestiaryCommand.java @@ -32,45 +32,46 @@ public class BestiaryCommand { public static void register() { final String command = "bestiary"; + EntitySelectorArgument.OnePlayer playerArg = new EntitySelectorArgument.OnePlayer("player"); + IntegerArgument amountArg = new IntegerArgument("amount"); + new CommandAPICommand(command) .withSubcommand(new CommandAPICommand("get") .withPermission(CommandPermission.fromString("los.bestiary.get")) - .withArguments(new EntitySelectorArgument.OnePlayer("player")) - .withArguments(new StringArgument("mobLabel").replaceSuggestions(LibraryOfSoulsCommand.LIST_MOBS_FUNCTION)) + .withArguments(playerArg) + .withArguments(LibraryOfSoulsCommand.mobLabelArg) .executes((sender, args) -> { int kills = 0; - Soul soul = LibraryOfSoulsCommand.getSoul((String)args[1]); + SoulEntry soul = LibraryOfSoulsCommand.getSoul(args.getByArgument(LibraryOfSoulsCommand.mobLabelArg)); + Player player = args.getByArgument(playerArg); try { - kills = BestiaryManager.getKillsForMob((Player)args[0], LibraryOfSoulsCommand.getSoul((String)args[1])); + kills = BestiaryManager.getKillsForMob(player, soul); } catch (Exception ex) { throw CommandAPI.failWithString(ex.getMessage()); } - if (sender instanceof Player) { - sender.sendMessage(MessageFormat.format("{0}{1} {2}has killed {3}{4} {5}{6}", - ChatColor.BLUE, ((Player)args[0]).getName(), - ChatColor.WHITE, - ChatColor.GREEN, kills, - ChatColor.WHITE, LegacyComponentSerializer.legacySection().serialize(soul.getDisplayName()))); - } + sender.sendMessage(Component.text().append(Component.text(player.getName(), NamedTextColor.BLUE)) + .append(Component.text(" has killed ")) + .append(Component.text(kills + " ", NamedTextColor.GREEN)) + .append(soul.getDisplayName())); return kills; })) .withSubcommand(new CommandAPICommand("set") .withPermission(CommandPermission.fromString("los.bestiary.set")) - .withArguments(new EntitySelectorArgument.OnePlayer("player")) - .withArguments(new StringArgument("mobLabel").replaceSuggestions(LibraryOfSoulsCommand.LIST_MOBS_FUNCTION)) - .withArguments(new IntegerArgument("amount")) + .withArguments(playerArg) + .withArguments(LibraryOfSoulsCommand.mobLabelArg) + .withArguments(amountArg) .executes((sender, args) -> { - BestiaryManager.setKillsForMob((Player)args[0], LibraryOfSoulsCommand.getSoul((String)args[1]), (Integer)args[2]); + BestiaryManager.setKillsForMob(args.getByArgument(playerArg), LibraryOfSoulsCommand.getSoul(args.getByArgument(LibraryOfSoulsCommand.mobLabelArg)), args.getByArgument(amountArg)); })) .withSubcommand(new CommandAPICommand("add") .withPermission(CommandPermission.fromString("los.bestiary.add")) - .withArguments(new EntitySelectorArgument.OnePlayer("player")) - .withArguments(new StringArgument("mobLabel").replaceSuggestions(LibraryOfSoulsCommand.LIST_MOBS_FUNCTION)) - .withArguments(new IntegerArgument("amount")) + .withArguments(playerArg) + .withArguments(LibraryOfSoulsCommand.mobLabelArg) + .withArguments(amountArg) .executes((sender, args) -> { int kills = 0; try { - kills = BestiaryManager.addKillsToMob((Player)args[0], LibraryOfSoulsCommand.getSoul((String)args[1]), (Integer)args[2]); + kills = BestiaryManager.addKillsToMob(args.getByArgument(playerArg), LibraryOfSoulsCommand.getSoul(args.getByArgument(LibraryOfSoulsCommand.mobLabelArg)), args.getByArgument(amountArg)); } catch (Exception ex) { throw CommandAPI.failWithString(ex.getMessage()); } @@ -82,28 +83,28 @@ public static void register() { Player player = LibraryOfSoulsCommand.getPlayer(sender); BestiaryArea bestiary = LibraryOfSouls.Config.getBestiary(); if (bestiary == null) { - player.sendMessage(ChatColor.RED + "Bestiary not loaded"); + player.sendMessage(Component.text("Bestiary not loaded", NamedTextColor.RED)); } else { bestiary.openBestiary(player, null, null, -1); } })) .withSubcommand(new CommandAPICommand("open") .withPermission(CommandPermission.fromString("los.bestiary.openother")) - .withArguments(new EntitySelectorArgument.OnePlayer("player")) + .withArguments(playerArg) .executes((sender, args) -> { - Player player = (Player)args[0]; + Player player = args.getByArgument(playerArg); BestiaryArea bestiary = LibraryOfSouls.Config.getBestiary(); if (bestiary == null) { - player.sendMessage(ChatColor.RED + "Bestiary not loaded"); + player.sendMessage(Component.text("Bestiary not loaded", NamedTextColor.RED)); } else { bestiary.openBestiary(player, null, null, -1); } })) .withSubcommand(new CommandAPICommand("info") .withPermission(CommandPermission.fromString("los.bestiary.info")) - .withArguments(new StringArgument("mobLabel").replaceSuggestions(LibraryOfSoulsCommand.LIST_MOBS_FUNCTION)) + .withArguments(LibraryOfSoulsCommand.mobLabelArg) .executesPlayer((sender, args) -> { - String name = (String)args[0]; + String name = args.getByArgument(LibraryOfSoulsCommand.mobLabelArg); SoulEntry soul = SoulsDatabase.getInstance().getSoul(name); if (soul == null) { throw CommandAPI.failWithString("Mob '" + name + "' not found"); @@ -114,9 +115,9 @@ public static void register() { })) .withSubcommand(new CommandAPICommand("deleteall") .withPermission(CommandPermission.fromString("los.bestiary.deleteall")) - .withArguments(new EntitySelectorArgument.OnePlayer("player")) + .withArguments(playerArg) .executes((sender, args) -> { - Player player = (Player)args[0]; + Player player = args.getByArgument(playerArg); BestiaryManager.deleteAll(player); })) .register(); @@ -125,34 +126,34 @@ public static void register() { public static void registerWriteAccessCommands() { final String command = "bestiary"; + TextArgument loreArg = new TextArgument("lore"); + TextArgument descriptionArg = new TextArgument("description"); + new CommandAPICommand(command) .withSubcommand(new CommandAPICommand("lore") .withPermission(CommandPermission.fromString("los.bestiary.lore")) - .withArguments(new StringArgument("mobLabel").replaceSuggestions(LibraryOfSoulsCommand.LIST_MOBS_FUNCTION)) - .withArguments(new TextArgument("lore")) + .withArguments(LibraryOfSoulsCommand.mobLabelArg) + .withArguments(loreArg) .executesPlayer((sender, args) -> { - String name = (String)args[0]; + String name = args.getByArgument(LibraryOfSoulsCommand.mobLabelArg); SoulEntry soul = SoulsDatabase.getInstance().getSoul(name); if (soul == null) { throw CommandAPI.failWithString("Mob '" + name + "' not found"); } else { - Component component = Component.text((String)args[1]); - List compList = new ArrayList(); - compList.add(component); - soul.setLore(compList, sender); + Component component = Component.text(args.getByArgument(loreArg)); + soul.setLore(List.of(component), sender); } }) .executesProxy((sender, args) -> { if (sender.getCallee() instanceof Player player) { - String name = (String)args[0]; + String name = args.getByArgument(LibraryOfSoulsCommand.mobLabelArg); SoulEntry soul = SoulsDatabase.getInstance().getSoul(name); if (soul == null) { throw CommandAPI.failWithString("Mob '" + name + "' not found"); + } else { + Component component = Component.text(args.getByArgument(loreArg)); + soul.setLore(List.of(component), player); } - Component component = Component.text((String)args[1]); - List compList = new ArrayList(); - compList.add(component); - soul.setLore(compList, player); } else { throw CommandAPI.failWithString("Callee must be instance of Player"); } @@ -160,9 +161,9 @@ public static void registerWriteAccessCommands() { .withSubcommand(new CommandAPICommand("lore") .withSubcommand(new CommandAPICommand("clear") .withPermission(CommandPermission.fromString("los.bestiary.lore")) - .withArguments(new StringArgument("mobLabel").replaceSuggestions(LibraryOfSoulsCommand.LIST_MOBS_FUNCTION)) + .withArguments(LibraryOfSoulsCommand.mobLabelArg) .executesPlayer((sender, args) -> { - String name = (String)args[0]; + String name = args.getByArgument(LibraryOfSoulsCommand.mobLabelArg); SoulEntry soul = SoulsDatabase.getInstance().getSoul(name); if (soul == null) { throw CommandAPI.failWithString("Mob '" + name + "' not found"); @@ -171,7 +172,7 @@ public static void registerWriteAccessCommands() { }))) .withSubcommand(new CommandAPICommand("lore") .withSubcommand(new CommandAPICommand("frommainhand") - .withArguments(new StringArgument("mobLabel").replaceSuggestions(LibraryOfSoulsCommand.LIST_MOBS_FUNCTION)) + .withArguments(LibraryOfSoulsCommand.mobLabelArg) .withPermission(CommandPermission.fromString("los.bestiary.lore")) .executesPlayer((sender, args) -> { ItemStack item = sender.getInventory().getItemInMainHand(); @@ -179,7 +180,7 @@ public static void registerWriteAccessCommands() { throw CommandAPI.failWithString("You need a valid item with lore text!"); } List lore = item.lore(); - String name = (String)args[0]; + String name = args.getByArgument(LibraryOfSoulsCommand.mobLabelArg); SoulEntry soul = SoulsDatabase.getInstance().getSoul(name); if (soul == null) { throw CommandAPI.failWithString("Mob '" + name + "' not found"); @@ -188,31 +189,28 @@ public static void registerWriteAccessCommands() { }))) .withSubcommand(new CommandAPICommand("description") .withPermission(CommandPermission.fromString("los.bestiary.description")) - .withArguments(new StringArgument("mobLabel").replaceSuggestions(LibraryOfSoulsCommand.LIST_MOBS_FUNCTION)) - .withArguments(new TextArgument("description")) + .withArguments(LibraryOfSoulsCommand.mobLabelArg) + .withArguments(descriptionArg) .executesPlayer((sender, args) -> { - String name = (String)args[0]; + String name = args.getByArgument(LibraryOfSoulsCommand.mobLabelArg); SoulEntry soul = SoulsDatabase.getInstance().getSoul(name); if (soul == null) { throw CommandAPI.failWithString("Mob '" + name + "' not found"); } else { - Component component = Component.text((String)args[1]); - List compList = new ArrayList<>(); - compList.add(component); - soul.setDescription(compList, sender); + Component component = Component.text(args.getByArgument(descriptionArg)); + soul.setDescription(List.of(component), sender); } }) .executesProxy((sender, args) -> { if (sender.getCallee() instanceof Player player) { - String name = (String)args[0]; + String name = args.getByArgument(LibraryOfSoulsCommand.mobLabelArg); SoulEntry soul = SoulsDatabase.getInstance().getSoul(name); if (soul == null) { throw CommandAPI.failWithString("Mob '" + name + "' not found"); + } else { + Component component = Component.text(args.getByArgument(descriptionArg)); + soul.setDescription(List.of(component), player); } - Component component = Component.text((String)args[1]); - List compList = new ArrayList<>(); - compList.add(component); - soul.setDescription(compList, player); } else { throw CommandAPI.failWithString("Callee must be instance of Player"); } @@ -220,9 +218,9 @@ public static void registerWriteAccessCommands() { .withSubcommand(new CommandAPICommand("description") .withSubcommand(new CommandAPICommand("clear") .withPermission(CommandPermission.fromString("los.bestiary.description")) - .withArguments(new StringArgument("mobLabel").replaceSuggestions(LibraryOfSoulsCommand.LIST_MOBS_FUNCTION)) + .withArguments(LibraryOfSoulsCommand.mobLabelArg) .executesPlayer((sender, args) -> { - String name = (String)args[0]; + String name = args.getByArgument(LibraryOfSoulsCommand.mobLabelArg); SoulEntry soul = SoulsDatabase.getInstance().getSoul(name); if (soul == null) { throw CommandAPI.failWithString("Mob '" + name + "' not found"); @@ -231,7 +229,7 @@ public static void registerWriteAccessCommands() { }))) .withSubcommand(new CommandAPICommand("description") .withSubcommand(new CommandAPICommand("frommainhand") - .withArguments(new StringArgument("mobLabel").replaceSuggestions(LibraryOfSoulsCommand.LIST_MOBS_FUNCTION)) + .withArguments(LibraryOfSoulsCommand.mobLabelArg) .withPermission(CommandPermission.fromString("los.bestiary.description")) .executesPlayer((sender, args) -> { ItemStack item = sender.getInventory().getItemInMainHand(); @@ -239,7 +237,7 @@ public static void registerWriteAccessCommands() { throw CommandAPI.failWithString("You need a valid item with lore text!"); } List lore = item.lore(); - String name = (String)args[0]; + String name = args.getByArgument(LibraryOfSoulsCommand.mobLabelArg); SoulEntry soul = SoulsDatabase.getInstance().getSoul(name); if (soul == null) { throw CommandAPI.failWithString("Mob '" + name + "' not found"); diff --git a/src/main/java/com/playmonumenta/libraryofsouls/commands/LibraryOfSoulsCommand.java b/src/main/java/com/playmonumenta/libraryofsouls/commands/LibraryOfSoulsCommand.java index d573fbd..f716b0b 100644 --- a/src/main/java/com/playmonumenta/libraryofsouls/commands/LibraryOfSoulsCommand.java +++ b/src/main/java/com/playmonumenta/libraryofsouls/commands/LibraryOfSoulsCommand.java @@ -14,8 +14,10 @@ import dev.jorel.commandapi.CommandAPI; import dev.jorel.commandapi.CommandAPICommand; import dev.jorel.commandapi.CommandPermission; +import dev.jorel.commandapi.arguments.Argument; import dev.jorel.commandapi.arguments.ArgumentSuggestions; import dev.jorel.commandapi.arguments.IntegerArgument; +import dev.jorel.commandapi.arguments.LiteralArgument; import dev.jorel.commandapi.arguments.LocationArgument; import dev.jorel.commandapi.arguments.MultiLiteralArgument; import dev.jorel.commandapi.arguments.ScoreHolderArgument; @@ -37,18 +39,30 @@ public class LibraryOfSoulsCommand { /* Several sub commands have this same tab completion */ - public static final ArgumentSuggestions LIST_MOBS_FUNCTION = ArgumentSuggestions.strings((info) -> SoulsDatabase.getInstance().listMobNames().stream().toArray(String[]::new)); - public static final ArgumentSuggestions LIST_SOUL_PARTIES_FUNCTION = ArgumentSuggestions.strings((info) -> SoulsDatabase.getInstance().listSoulPartyNames().stream().toArray(String[]::new)); - public static final ArgumentSuggestions LIST_SOUL_POOLS_FUNCTION = ArgumentSuggestions.strings((info) -> SoulsDatabase.getInstance().listSoulPoolNames().stream().toArray(String[]::new)); - public static final ArgumentSuggestions LIST_SOUL_GROUPS_FUNCTION = ArgumentSuggestions.strings((info) -> SoulsDatabase.getInstance().listSoulGroupNames().stream().toArray(String[]::new)); + public static final ArgumentSuggestions LIST_MOBS_FUNCTION = ArgumentSuggestions.strings((info) -> SoulsDatabase.getInstance().listMobNames().stream().toArray(String[]::new)); + public static final ArgumentSuggestions LIST_SOUL_PARTIES_FUNCTION = ArgumentSuggestions.strings((info) -> SoulsDatabase.getInstance().listSoulPartyNames().stream().toArray(String[]::new)); + public static final ArgumentSuggestions LIST_SOUL_POOLS_FUNCTION = ArgumentSuggestions.strings((info) -> SoulsDatabase.getInstance().listSoulPoolNames().stream().toArray(String[]::new)); + public static final ArgumentSuggestions LIST_SOUL_GROUPS_FUNCTION = ArgumentSuggestions.strings((info) -> SoulsDatabase.getInstance().listSoulGroupNames().stream().toArray(String[]::new)); private static final String COMMAND = "los"; private static final Pattern VALID_SOUL_GROUP_LABEL = Pattern.compile("[0-9A-Za-z_]+"); + public static final Argument mobLabelArg = new StringArgument("mobLabel").replaceSuggestions(LIST_MOBS_FUNCTION); + public static final Argument partyLabelArg = new ScoreHolderArgument.Single("partyLabel").replaceSuggestions(LIST_SOUL_PARTIES_FUNCTION); + public static final Argument poolLabelArg = new ScoreHolderArgument.Single("poolLabel").replaceSuggestions(LIST_SOUL_POOLS_FUNCTION); + public static final Argument groupLabelArg = new ScoreHolderArgument.Single("groupLabel").replaceSuggestions(LIST_SOUL_GROUPS_FUNCTION); + // No clue why these are scoreholder arguments, but not going to take the risk of changing them + public static void register() { + LocationArgument locationArg = new LocationArgument("location"); + LocationArgument pos1Arg = new LocationArgument("pos1"); + LocationArgument pos2Arg = new LocationArgument("pos2"); + Argument areaArg = new StringArgument("area").replaceSuggestions(ArgumentSuggestions.strings((info) -> SoulsDatabase.getInstance().listMobLocations().stream().toArray(String[]::new))); + Argument idArg = new StringArgument("id").replaceSuggestions(ArgumentSuggestions.strings((info) -> SoulsDatabase.getInstance().listMobTypes().stream().toArray(String[]::new))); + /* los open */ new CommandAPICommand(COMMAND) .withPermission(CommandPermission.fromString("los.open")) - .withArguments(new MultiLiteralArgument("open")) + .withArguments(new LiteralArgument("open")) .executes((sender, args) -> { Player player = getPlayer(sender); new SoulsInventory(player, SoulsDatabase.getInstance().getSouls(), "") @@ -59,24 +73,24 @@ public static void register() { /* los get */ new CommandAPICommand(COMMAND) .withPermission(CommandPermission.fromString("los.get")) - .withArguments(new MultiLiteralArgument("get")) - .withArguments(new StringArgument("mobLabel").replaceSuggestions(LIST_MOBS_FUNCTION)) + .withArguments(new LiteralArgument("get")) + .withArguments(mobLabelArg) .executes((sender, args) -> { PlayerInventory inv = getPlayer(sender).getInventory(); if (inv.firstEmpty() == -1) { throw CommandAPI.failWithString("Your inventory is full!"); } - inv.addItem(getSoul((String)args[1]).getBoS()); + inv.addItem(getSoul(args.getByArgument(mobLabelArg)).getBoS()); }) .register(); /* los party */ new CommandAPICommand(COMMAND) .withPermission(CommandPermission.fromString("los.party")) - .withArguments(new MultiLiteralArgument("party")) - .withArguments(new ScoreHolderArgument.Single("partyLabel").replaceSuggestions(LIST_SOUL_PARTIES_FUNCTION)) + .withArguments(new LiteralArgument("party")) + .withArguments(partyLabelArg) .executes((sender, args) -> { - String partyLabel = (String)args[1]; + String partyLabel = args.getByArgument(partyLabelArg); SoulsDatabase database = SoulsDatabase.getInstance(); sender.sendMessage(Component.text("Party counts:")); SoulPartyEntry party = database.getSoulParty(partyLabel); @@ -97,10 +111,10 @@ public static void register() { /* los pool */ new CommandAPICommand(COMMAND) .withPermission(CommandPermission.fromString("los.pool")) - .withArguments(new MultiLiteralArgument("pool")) - .withArguments(new ScoreHolderArgument.Single("poolLabel").replaceSuggestions(LIST_SOUL_POOLS_FUNCTION)) + .withArguments(new LiteralArgument("pool")) + .withArguments(poolLabelArg) .executes((sender, args) -> { - String poolLabel = (String)args[1]; + String poolLabel = args.getByArgument(poolLabelArg); SoulsDatabase database = SoulsDatabase.getInstance(); sender.sendMessage(Component.text("Pool weights:")); long totalWeight = 0; @@ -118,17 +132,17 @@ public static void register() { .clickEvent(ClickEvent.suggestCommand(entryCommand)) .hoverEvent(Component.text(entryCommand))); } - sender.sendMessage(Component.text("Total weight: " + Long.toString(totalWeight))); + sender.sendMessage(Component.text("Total weight: " + totalWeight)); }) .register(); /* los averagegroup */ new CommandAPICommand(COMMAND) .withPermission(CommandPermission.fromString("los.averagegroup")) - .withArguments(new MultiLiteralArgument("averagegroup")) - .withArguments(new ScoreHolderArgument.Single("groupLabel").replaceSuggestions(LIST_SOUL_GROUPS_FUNCTION)) + .withArguments(new LiteralArgument("averagegroup")) + .withArguments(groupLabelArg) .executes((sender, args) -> { - String groupLabel = (String)args[1]; + String groupLabel = args.getByArgument(groupLabelArg); sender.sendMessage(Component.text("Pool weights:")); for (Map.Entry entry : getSoulGroup(groupLabel).getAverageSouls().entrySet()) { Component name = entry.getKey().getName(); @@ -141,11 +155,11 @@ public static void register() { /* los history */ new CommandAPICommand(COMMAND) .withPermission(CommandPermission.fromString("los.history")) - .withArguments(new MultiLiteralArgument("history")) - .withArguments(new StringArgument("mobLabel").replaceSuggestions(LIST_MOBS_FUNCTION)) + .withArguments(new LiteralArgument("history")) + .withArguments(mobLabelArg) .executes((sender, args) -> { Player player = getPlayer(sender); - new SoulsInventory(player, getSoul((String)args[1]).getHistory(), "History") + new SoulsInventory(player, getSoul(args.getByArgument(mobLabelArg)).getHistory(), "History") .openInventory(player, LibraryOfSouls.getInstance()); }) .register(); @@ -153,33 +167,33 @@ public static void register() { /* los summon */ new CommandAPICommand(COMMAND) .withPermission(CommandPermission.fromString("los.summon")) - .withArguments(new MultiLiteralArgument("summon")) - .withArguments(new LocationArgument("location")) - .withArguments(new StringArgument("mobLabel").replaceSuggestions(LIST_MOBS_FUNCTION)) + .withArguments(new LiteralArgument("summon")) + .withArguments(locationArg) + .withArguments(mobLabelArg) .executes((sender, args) -> { - getSoul((String)args[2]).summon((Location)args[1]); + getSoul(args.getByArgument(mobLabelArg)).summon(args.getByArgument(locationArg)); }) .register(); /* los summongroup */ new CommandAPICommand(COMMAND) .withPermission(CommandPermission.fromString("los.summongroup")) - .withArguments(new MultiLiteralArgument("summongroup")) - .withArguments(new ScoreHolderArgument.Single("groupLabel").replaceSuggestions(LIST_SOUL_GROUPS_FUNCTION)) - .withArguments(new LocationArgument("pos1")) - .withArguments(new LocationArgument("pos2")) + .withArguments(new LiteralArgument("summongroup")) + .withArguments(groupLabelArg) + .withArguments(pos1Arg) + .withArguments(pos2Arg) .executes((sender, args) -> { - Location pos1 = (Location)args[2]; - Location pos2 = (Location)args[3]; + Location pos1 = args.getByArgument(pos1Arg); + Location pos2 = args.getByArgument(pos2Arg); BoundingBox bb = BoundingBox.of(pos1, pos2); - getSoulGroup((String)args[1]).summonGroup(new Random(), pos1.getWorld(), bb); + getSoulGroup(args.getByArgument(groupLabelArg)).summonGroup(new Random(), pos1.getWorld(), bb); }) .register(); /* los search */ new CommandAPICommand(COMMAND) .withPermission(CommandPermission.fromString("los.search")) - .withArguments(new MultiLiteralArgument("search")) + .withArguments(new LiteralArgument("search")) .executes((sender, args) -> { Player player = getPlayer(sender); List souls = SoulsDatabase.getInstance().getSoulsByLocation(null); @@ -194,11 +208,11 @@ public static void register() { /* los search */ new CommandAPICommand(COMMAND) .withPermission(CommandPermission.fromString("los.search")) - .withArguments(new MultiLiteralArgument("search")) - .withArguments(new StringArgument("area").replaceSuggestions(ArgumentSuggestions.strings((info) -> SoulsDatabase.getInstance().listMobLocations().stream().toArray(String[]::new)))) + .withArguments(new LiteralArgument("search")) + .withArguments(areaArg) .executes((sender, args) -> { Player player = getPlayer(sender); - String area = (String)args[1]; + String area = args.getByArgument(areaArg); List souls = SoulsDatabase.getInstance().getSoulsByLocation(area); if (souls == null) { throw CommandAPI.failWithString("Area '" + area + "' not found"); @@ -211,11 +225,11 @@ public static void register() { /* los searchtype */ new CommandAPICommand(COMMAND) .withPermission(CommandPermission.fromString("los.search")) - .withArguments(new MultiLiteralArgument("searchtype")) - .withArguments(new StringArgument("id").replaceSuggestions(ArgumentSuggestions.strings((info) -> SoulsDatabase.getInstance().listMobTypes().stream().toArray(String[]::new)))) + .withArguments(new LiteralArgument("searchtype")) + .withArguments(idArg) .executes((sender, args) -> { Player player = getPlayer(sender); - String id = (String)args[1]; + String id = args.getByArgument(idArg); List souls = SoulsDatabase.getInstance().getSoulsByType(id); if (souls == null) { throw CommandAPI.failWithString("Mob type '" + id + "' not found"); @@ -228,11 +242,11 @@ public static void register() { /* los spawner */ new CommandAPICommand(COMMAND) .withPermission(CommandPermission.fromString("los.spawner")) - .withArguments(new MultiLiteralArgument("spawner")) - .withArguments(new StringArgument("mobLabel").replaceSuggestions(LIST_MOBS_FUNCTION)) + .withArguments(new LiteralArgument("spawner")) + .withArguments(mobLabelArg) .executes((sender, args) -> { Player player = getPlayer(sender); - String name = (String)args[1]; + String name = args.getByArgument(mobLabelArg); Soul soul = SoulsDatabase.getInstance().getSoul(name); if (soul == null) { throw CommandAPI.failWithString("Soul '" + name + "' not found"); @@ -243,15 +257,15 @@ public static void register() { } public static void registerWriteAccessCommands() { + IntegerArgument countArg = new IntegerArgument("count", 0); + IntegerArgument weightArg = new IntegerArgument("weight", 0); + /* los autoupdate */ new CommandAPICommand(COMMAND) .withPermission(CommandPermission.fromString("los.autoupdate")) - .withArguments(new MultiLiteralArgument("autoupdate")) - .executes((sender, args) -> { - if (!(sender instanceof Player)) { - throw CommandAPI.failWithString("autoupdate must be run by a player"); - } - SoulsDatabase.getInstance().autoUpdate(sender, ((Player) sender).getLocation()); + .withArguments(new LiteralArgument("autoupdate")) + .executesPlayer((sender, args) -> { + SoulsDatabase.getInstance().autoUpdate(sender, sender.getLocation()); }) .register(); @@ -262,9 +276,6 @@ public static void registerWriteAccessCommands() { .executes((sender, args) -> { Player player = getPlayer(sender); BookOfSouls bos = getBos(player); - if (bos == null) { - throw CommandAPI.failWithString("You must be holding a Book of Souls"); - } SoulsDatabase.getInstance().add(player, bos); }) @@ -273,13 +284,10 @@ public static void registerWriteAccessCommands() { /* los update */ new CommandAPICommand(COMMAND) .withPermission(CommandPermission.fromString("los.update")) - .withArguments(new MultiLiteralArgument("update")) + .withArguments(new LiteralArgument("update")) .executes((sender, args) -> { Player player = getPlayer(sender); BookOfSouls bos = getBos(player); - if (bos == null) { - throw CommandAPI.failWithString("You must be holding a Book of Souls"); - } SoulsDatabase.getInstance().update(player, bos); }) @@ -288,21 +296,21 @@ public static void registerWriteAccessCommands() { /* los del */ new CommandAPICommand(COMMAND) .withPermission(CommandPermission.fromString("los.del")) - .withArguments(new MultiLiteralArgument("del")) - .withArguments(new StringArgument("mobLabel").replaceSuggestions(LIST_MOBS_FUNCTION)) + .withArguments(new LiteralArgument("del")) + .withArguments(mobLabelArg) .executes((sender, args) -> { - SoulsDatabase.getInstance().del(sender, (String)args[1]); + SoulsDatabase.getInstance().del(sender, args.getByArgument(mobLabelArg)); }) .register(); /* los addparty */ new CommandAPICommand(COMMAND) .withPermission(CommandPermission.fromString("los.addparty")) - .withArguments(new MultiLiteralArgument("addparty")) - .withArguments(new ScoreHolderArgument.Single("partyLabel").replaceSuggestions(ArgumentSuggestions.empty())) + .withArguments(new LiteralArgument("addparty")) + .withArguments(partyLabelArg.replaceSuggestions(ArgumentSuggestions.empty())) .executes((sender, args) -> { Player player = getPlayer(sender); - String partyLabel = (String)args[1]; + String partyLabel = args.getByArgument(partyLabelArg); String partyLabelNoPrefix = partyLabel; if (partyLabelNoPrefix.startsWith(LibraryOfSoulsAPI.SOUL_PARTY_PREFIX)) { partyLabelNoPrefix = partyLabelNoPrefix.substring(1); @@ -318,15 +326,15 @@ public static void registerWriteAccessCommands() { /* los updateparty */ new CommandAPICommand(COMMAND) .withPermission(CommandPermission.fromString("los.updateparty")) - .withArguments(new MultiLiteralArgument("updateparty")) - .withArguments(new ScoreHolderArgument.Single("partyLabel").replaceSuggestions(LIST_SOUL_PARTIES_FUNCTION)) - .withArguments(new ScoreHolderArgument.Single("groupLabel").replaceSuggestions(LIST_SOUL_GROUPS_FUNCTION)) - .withArguments(new IntegerArgument("count", 0)) + .withArguments(new LiteralArgument("updateparty")) + .withArguments(partyLabelArg) + .withArguments(groupLabelArg) + .withArguments(countArg) .executes((sender, args) -> { Player player = getPlayer(sender); - String partyLabel = (String)args[1]; - String entryLabel = (String)args[2]; - int count = (int)args[3]; + String partyLabel = args.getByArgument(partyLabelArg); + String entryLabel = args.getByArgument(groupLabelArg); + int count = args.getByArgument(countArg); SoulsDatabase.getInstance().updateParty(player, partyLabel, entryLabel, count); }) @@ -335,10 +343,10 @@ public static void registerWriteAccessCommands() { /* los delparty */ new CommandAPICommand(COMMAND) .withPermission(CommandPermission.fromString("los.delparty")) - .withArguments(new MultiLiteralArgument("delparty")) - .withArguments(new ScoreHolderArgument.Single("partyLabel").replaceSuggestions(LIST_SOUL_PARTIES_FUNCTION)) + .withArguments(new LiteralArgument("delparty")) + .withArguments(partyLabelArg) .executes((sender, args) -> { - String partyLabel = (String)args[1]; + String partyLabel = args.getByArgument(partyLabelArg); SoulsDatabase.getInstance().delParty(sender, partyLabel); }) .register(); @@ -346,11 +354,11 @@ public static void registerWriteAccessCommands() { /* los addpool */ new CommandAPICommand(COMMAND) .withPermission(CommandPermission.fromString("los.addpool")) - .withArguments(new MultiLiteralArgument("addpool")) - .withArguments(new ScoreHolderArgument.Single("poolLabel").replaceSuggestions(ArgumentSuggestions.empty())) + .withArguments(new LiteralArgument("addpool")) + .withArguments(poolLabelArg.replaceSuggestions(ArgumentSuggestions.empty())) .executes((sender, args) -> { Player player = getPlayer(sender); - String poolLabel = (String)args[1]; + String poolLabel = args.getByArgument(poolLabelArg); String poolLabelNoPrefix = poolLabel; if (poolLabelNoPrefix.startsWith(LibraryOfSoulsAPI.SOUL_POOL_PREFIX)) { poolLabelNoPrefix = poolLabelNoPrefix.substring(1); @@ -366,15 +374,15 @@ public static void registerWriteAccessCommands() { /* los updatepool */ new CommandAPICommand(COMMAND) .withPermission(CommandPermission.fromString("los.updatepool")) - .withArguments(new MultiLiteralArgument("updatepool")) - .withArguments(new ScoreHolderArgument.Single("poolLabel").replaceSuggestions(LIST_SOUL_POOLS_FUNCTION)) - .withArguments(new ScoreHolderArgument.Single("groupLabel").replaceSuggestions(LIST_SOUL_GROUPS_FUNCTION)) - .withArguments(new IntegerArgument("weight", 0)) + .withArguments(new LiteralArgument("updatepool")) + .withArguments(poolLabelArg) + .withArguments(groupLabelArg) + .withArguments(weightArg) .executes((sender, args) -> { Player player = getPlayer(sender); - String poolLabel = (String)args[1]; - String entryLabel = (String)args[2]; - int count = (int)args[3]; + String poolLabel = args.getByArgument(poolLabelArg); + String entryLabel = args.getByArgument(groupLabelArg); + int count = args.getByArgument(weightArg); SoulsDatabase.getInstance().updatePool(player, poolLabel, entryLabel, count); }) @@ -383,10 +391,10 @@ public static void registerWriteAccessCommands() { /* los delpool */ new CommandAPICommand(COMMAND) .withPermission(CommandPermission.fromString("los.delpool")) - .withArguments(new MultiLiteralArgument("delpool")) - .withArguments(new ScoreHolderArgument.Single("poolLabel").replaceSuggestions(LIST_SOUL_POOLS_FUNCTION)) + .withArguments(new LiteralArgument("delpool")) + .withArguments(poolLabelArg) .executes((sender, args) -> { - String poolLabel = (String)args[1]; + String poolLabel = args.getByArgument(poolLabelArg); SoulsDatabase.getInstance().delPool(sender, poolLabel); }) .register(); @@ -411,10 +419,10 @@ public static SoulGroup getSoulGroup(String name) throws WrapperCommandSyntaxExc } public static Player getPlayer(CommandSender sender) throws WrapperCommandSyntaxException { - if (sender instanceof Player) { - return (Player) sender; - } else if ((sender instanceof ProxiedCommandSender) && (((ProxiedCommandSender)sender).getCallee() instanceof Player)) { - return (Player) ((ProxiedCommandSender)sender).getCallee(); + if (sender instanceof Player player) { + return player; + } else if (sender instanceof ProxiedCommandSender proxiedCommandSender && proxiedCommandSender.getCallee() instanceof Player player) { + return player; } throw CommandAPI.failWithString("This command must be run by / as a player"); diff --git a/src/main/java/com/playmonumenta/libraryofsouls/commands/SpawnerNBTCommand.java b/src/main/java/com/playmonumenta/libraryofsouls/commands/SpawnerNBTCommand.java index bee962a..48dd435 100644 --- a/src/main/java/com/playmonumenta/libraryofsouls/commands/SpawnerNBTCommand.java +++ b/src/main/java/com/playmonumenta/libraryofsouls/commands/SpawnerNBTCommand.java @@ -7,7 +7,8 @@ import dev.jorel.commandapi.arguments.IntegerArgument; import dev.jorel.commandapi.arguments.MultiLiteralArgument; import dev.jorel.commandapi.exceptions.WrapperCommandSyntaxException; -import org.bukkit.ChatColor; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.NamedTextColor; import org.bukkit.Material; import org.bukkit.block.CreatureSpawner; import org.bukkit.entity.Player; @@ -16,28 +17,22 @@ public class SpawnerNBTCommand { - public static void registerType(String method) { + public static void register() { CommandPermission perms = CommandPermission.fromString("los.nbtheldspawner"); + MultiLiteralArgument methodArg = new MultiLiteralArgument("method", "MaxSpawnDelay", "MinSpawnDelay", "RequiredPlayerRange", "SpawnCount", "SpawnRange"); + IntegerArgument valueArg = new IntegerArgument("value"); new CommandAPICommand("nbtheldspawner") .withPermission(perms) - .withArguments(new MultiLiteralArgument(method)) - .withArguments(new IntegerArgument("value")) - .executes((sender, args) -> { - changeSpawnerNBT(method, (Integer)args[1], (Player)sender); + .withArguments(methodArg) + .withArguments(valueArg) + .executesPlayer((sender, args) -> { + changeSpawnerNBT(args.getByArgument(methodArg), args.getByArgument(valueArg), sender); }) .register(); } - public static void register() { - registerType("MaxSpawnDelay"); - registerType("MinSpawnDelay"); - registerType("RequiredPlayerRange"); - registerType("SpawnCount"); - registerType("SpawnRange"); - } - private static void changeSpawnerNBT(String method, int argument, Player player) throws WrapperCommandSyntaxException { ItemStack item = player.getInventory().getItemInMainHand(); if (!item.getType().equals(Material.SPAWNER)) { @@ -76,6 +71,6 @@ private static void changeSpawnerNBT(String method, int argument, Player player) SpawnerInventory.updateSpawnerItemDisplay(item, spawner); - player.sendMessage(ChatColor.GREEN + method + " set to " + Integer.toString(argument)); + player.sendMessage(Component.text(method + " set to " + argument, NamedTextColor.GREEN)); } }