diff --git a/pom.xml b/pom.xml index 365624f..4ba4eda 100644 --- a/pom.xml +++ b/pom.xml @@ -6,13 +6,13 @@ com.artillexstudios AxTrade - 1.4.0 + 1.5.0 jar AxTrade - 1.8 + 17 UTF-8 @@ -23,8 +23,8 @@ maven-compiler-plugin 3.8.1 - 12 - 12 + 17 + 17 -parameters @@ -112,7 +112,7 @@ com.artillexstudios.axapi axapi - 1.4.236 + 1.4.242 compile all @@ -131,7 +131,7 @@ me.clip placeholderapi - 2.11.5 + 2.11.6 provided @@ -143,7 +143,7 @@ net.kyori adventure-platform-bukkit - 4.3.2 + 4.3.3 compile diff --git a/src/main/java/com/artillexstudios/axtrade/commands/Commands.java b/src/main/java/com/artillexstudios/axtrade/commands/Commands.java index 1b7c686..9ba10e4 100644 --- a/src/main/java/com/artillexstudios/axtrade/commands/Commands.java +++ b/src/main/java/com/artillexstudios/axtrade/commands/Commands.java @@ -1,26 +1,36 @@ package com.artillexstudios.axtrade.commands; +import com.artillexstudios.axapi.nms.NMSHandlers; +import com.artillexstudios.axapi.utils.FastFieldAccessor; import com.artillexstudios.axapi.utils.StringUtils; import com.artillexstudios.axtrade.AxTrade; import com.artillexstudios.axtrade.hooks.HookManager; import com.artillexstudios.axtrade.lang.LanguageManager; import com.artillexstudios.axtrade.request.Requests; import com.artillexstudios.axtrade.trade.Trades; +import com.artillexstudios.axtrade.utils.CommandMessages; import com.artillexstudios.axtrade.utils.NumberUtils; import com.artillexstudios.axtrade.utils.SoundUtils; import org.bukkit.Bukkit; +import org.bukkit.OfflinePlayer; +import org.bukkit.Warning; import org.bukkit.command.CommandSender; +import org.bukkit.entity.HumanEntity; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; import revxrsal.commands.annotation.DefaultFor; import revxrsal.commands.annotation.Optional; import revxrsal.commands.annotation.Subcommand; +import revxrsal.commands.bukkit.BukkitCommandActor; import revxrsal.commands.bukkit.BukkitCommandHandler; import revxrsal.commands.bukkit.annotation.CommandPermission; +import revxrsal.commands.bukkit.exception.InvalidPlayerException; import revxrsal.commands.orphan.OrphanCommand; import revxrsal.commands.orphan.Orphans; +import java.util.Locale; import java.util.Map; +import java.util.stream.Collectors; import static com.artillexstudios.axtrade.AxTrade.CONFIG; import static com.artillexstudios.axtrade.AxTrade.GUIS; @@ -127,8 +137,32 @@ public class Commands implements OrphanCommand { Trades.addTrade(sender, other); } + private static BukkitCommandHandler handler = null; public static void registerCommand() { - final BukkitCommandHandler handler = BukkitCommandHandler.create(AxTrade.getInstance()); + if (handler == null) { + Warning.WarningState prevState = Bukkit.getWarningState(); + FastFieldAccessor accessor = FastFieldAccessor.forClassField(Bukkit.getServer().getClass().getPackage().getName() + ".CraftServer", "warningState"); + accessor.set(Bukkit.getServer(), Warning.WarningState.OFF); + handler = BukkitCommandHandler.create(AxTrade.getInstance()); + accessor.set(Bukkit.getServer(), prevState); + + handler.registerValueResolver(0, OfflinePlayer.class, context -> { + String value = context.pop(); + if (value.equalsIgnoreCase("self") || value.equalsIgnoreCase("me")) + return ((BukkitCommandActor) context.actor()).requirePlayer(); + OfflinePlayer player = NMSHandlers.getNmsHandler().getCachedOfflinePlayer(value); + if (player == null && !(player = Bukkit.getOfflinePlayer(value)).hasPlayedBefore()) + throw new InvalidPlayerException(context.parameter(), value); + return player; + }); + + handler.getAutoCompleter().registerParameterSuggestions(OfflinePlayer.class, (args, sender, command) -> { + return Bukkit.getOnlinePlayers().stream().map(HumanEntity::getName).collect(Collectors.toSet()); + }); + + handler.getTranslator().add(new CommandMessages()); + handler.setLocale(new Locale("en", "US")); + } handler.unregisterAllCommands(); handler.register(Orphans.path(CONFIG.getStringList("command-aliases").toArray(String[]::new)).handler(new Commands())); handler.registerBrigadier(); diff --git a/src/main/java/com/artillexstudios/axtrade/trade/Trade.java b/src/main/java/com/artillexstudios/axtrade/trade/Trade.java index d4618a7..88fa2bd 100644 --- a/src/main/java/com/artillexstudios/axtrade/trade/Trade.java +++ b/src/main/java/com/artillexstudios/axtrade/trade/Trade.java @@ -1,6 +1,7 @@ package com.artillexstudios.axtrade.trade; import com.artillexstudios.axapi.scheduler.Scheduler; +import com.artillexstudios.axapi.utils.ContainerUtils; import com.artillexstudios.axtrade.hooks.currency.CurrencyHook; import com.artillexstudios.axtrade.utils.HistoryUtils; import com.artillexstudios.axtrade.utils.NumberUtils; @@ -108,7 +109,7 @@ public class Trade { List player1Items = new ArrayList<>(); player1.getTradeGui().getItems().forEach(itemStack -> { if (itemStack == null) return; - player2.getPlayer().getInventory().addItem(itemStack); + ContainerUtils.INSTANCE.addOrDrop(player2.getPlayer().getInventory(), List.of(itemStack), player2.getPlayer().getLocation()); final String itemName = Utils.getFormattedItemName(itemStack); int itemAm = itemStack.getAmount(); player1Items.add(itemAm + "x " + itemName); @@ -119,7 +120,7 @@ public class Trade { List player2Items = new ArrayList<>(); player2.getTradeGui().getItems().forEach(itemStack -> { if (itemStack == null) return; - player1.getPlayer().getInventory().addItem(itemStack); + ContainerUtils.INSTANCE.addOrDrop(player1.getPlayer().getInventory(), List.of(itemStack), player1.getPlayer().getLocation()); final String itemName = Utils.getFormattedItemName(itemStack); int itemAm = itemStack.getAmount(); player2Items.add(itemAm + "x " + itemName); diff --git a/src/main/java/com/artillexstudios/axtrade/utils/CommandMessages.java b/src/main/java/com/artillexstudios/axtrade/utils/CommandMessages.java new file mode 100644 index 0000000..97d7cdc --- /dev/null +++ b/src/main/java/com/artillexstudios/axtrade/utils/CommandMessages.java @@ -0,0 +1,73 @@ +package com.artillexstudios.axtrade.utils; + +import com.artillexstudios.axapi.utils.StringUtils; +import revxrsal.commands.locales.LocaleReader; + +import java.util.Locale; + +import static com.artillexstudios.axtrade.AxTrade.CONFIG; +import static com.artillexstudios.axtrade.AxTrade.LANG; + +public class CommandMessages implements LocaleReader { + @Override + public boolean containsKey(String s) { + return true; + } + + @Override + public String get(String s) { + String res; + switch (s) { + case "invalid-enum", "invalid-number", "invalid-uuid", "invalid-url", "invalid-boolean": { + res = LANG.getString("commands.invalid-value") + .replace("%value%", "{0}"); + break; + } + case "missing-argument": { + res = LANG.getString("commands.missing-argument") + .replace("%value%", "{0}"); + break; + } + case "no-permission": { + res = LANG.getString("commands.no-permission"); + break; + } + case "number-not-in-range": { + res = LANG.getString("commands.out-of-range") + .replace("%number%", "{0}") + .replace("%min%", "{1}") + .replace("%max%", "{2}"); + break; + } + case "must-be-player": { + res = LANG.getString("commands.player-only"); + break; + } + case "must-be-console": { + res = LANG.getString("commands.console-only"); + break; + } + case "invalid-player": { + res = LANG.getString("commands.invalid-player") + .replace("%player%", "{0}"); + break; + } + case "invalid-selector": { + res = LANG.getString("commands.invalid-selector"); + break; + } + default: { + res = LANG.getString("commands.invalid-command"); + break; + } + } + return StringUtils.formatToString(CONFIG.getString("prefix", "") + res); + } + + private final Locale locale = new Locale("en", "US"); + + @Override + public Locale getLocale() { + return locale; + } +} diff --git a/src/main/resources/lang.yml b/src/main/resources/lang.yml index d7b9721..42618f0 100644 --- a/src/main/resources/lang.yml +++ b/src/main/resources/lang.yml @@ -87,5 +87,15 @@ placeholders: ready: "Ready" waiting: "Waiting" +commands: + invalid-value: "&#FF0000Invalid parameter: &#BB0000%value%" + invalid-command: "&#FF0000Invalid command or subcommand!" + missing-argument: "&#FF0000Missing argument! You must specify a value for &#BB0000%value%&#FF0000." + no-permission: "&#FF0000You don't have permission to access this command!" + out-of-range: "&#FF0000The &#BB0000%number% &#FF0000must be between &#BB0000%min% &#FF0000and &#BB0000%max%&#FF0000!" + player-only: "&#FF0000You must be a player to use this command!" + invalid-player: "&#FF0000The player &#BB0000%player% &#FF0000can not be found!" + invalid-selector: "&#FF0000You can not use this selector in this command!" + # do not change this -version: 2 \ No newline at end of file +version: 3 \ No newline at end of file