This commit is contained in:
BenceX100 2024-06-17 20:34:24 +02:00
parent 23860bc3c3
commit 6a74451f7c
5 changed files with 129 additions and 11 deletions

14
pom.xml
View File

@ -6,13 +6,13 @@
<groupId>com.artillexstudios</groupId>
<artifactId>AxTrade</artifactId>
<version>1.4.0</version>
<version>1.5.0</version>
<packaging>jar</packaging>
<name>AxTrade</name>
<properties>
<java.version>1.8</java.version>
<java.version>17</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
@ -23,8 +23,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>12</source>
<target>12</target>
<source>17</source>
<target>17</target>
<compilerArgs>
<arg>-parameters</arg>
</compilerArgs>
@ -112,7 +112,7 @@
<dependency>
<groupId>com.artillexstudios.axapi</groupId>
<artifactId>axapi</artifactId>
<version>1.4.236</version>
<version>1.4.242</version>
<scope>compile</scope>
<classifier>all</classifier>
</dependency>
@ -131,7 +131,7 @@
<dependency>
<groupId>me.clip</groupId>
<artifactId>placeholderapi</artifactId>
<version>2.11.5</version>
<version>2.11.6</version>
<scope>provided</scope>
</dependency>
<dependency>
@ -143,7 +143,7 @@
<dependency>
<groupId>net.kyori</groupId>
<artifactId>adventure-platform-bukkit</artifactId>
<version>4.3.2</version>
<version>4.3.3</version>
<scope>compile</scope>
</dependency>
<dependency>

View File

@ -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();

View File

@ -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<String> 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<String> 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);

View File

@ -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;
}
}

View File

@ -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
version: 3