diff --git a/gradle.properties b/gradle.properties index 669cf5c69..d4178c6c0 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,3 +1,3 @@ asmVersion=8.0.1 mixinVersion=0.8 -hephaistos_version=v1.1.2 \ No newline at end of file +hephaistos_version=v1.1.4 \ No newline at end of file diff --git a/src/main/java/net/minestom/server/command/CommandManager.java b/src/main/java/net/minestom/server/command/CommandManager.java index 6a5abd485..edadba28e 100644 --- a/src/main/java/net/minestom/server/command/CommandManager.java +++ b/src/main/java/net/minestom/server/command/CommandManager.java @@ -515,6 +515,12 @@ public final class CommandManager { } else if (argument instanceof ArgumentItemStack) { DeclareCommandsPacket.Node argumentNode = simpleArgumentNode(nodes, argument, executable, false); argumentNode.parser = "minecraft:item_stack"; + } else if (argument instanceof ArgumentNbtCompoundTag) { + DeclareCommandsPacket.Node argumentNode = simpleArgumentNode(nodes, argument, executable, false); + argumentNode.parser = "minecraft:nbt_compound_tag"; + } else if (argument instanceof ArgumentNbtTag) { + DeclareCommandsPacket.Node argumentNode = simpleArgumentNode(nodes, argument, executable, false); + argumentNode.parser = "minecraft:nbt_tag"; } return nodes; diff --git a/src/main/java/net/minestom/server/command/builder/Arguments.java b/src/main/java/net/minestom/server/command/builder/Arguments.java index e643b2312..499987fd9 100644 --- a/src/main/java/net/minestom/server/command/builder/Arguments.java +++ b/src/main/java/net/minestom/server/command/builder/Arguments.java @@ -10,6 +10,8 @@ import net.minestom.server.potion.PotionEffect; import net.minestom.server.utils.math.FloatRange; import net.minestom.server.utils.math.IntRange; import net.minestom.server.utils.time.UpdateOption; +import org.jglrxavpok.hephaistos.nbt.NBT; +import org.jglrxavpok.hephaistos.nbt.NBTCompound; import java.util.HashMap; import java.util.List; @@ -94,6 +96,14 @@ public class Arguments { return (ItemStack) getObject(id); } + public NBTCompound getNbtCompound(String id) { + return (NBTCompound) getObject(id); + } + + public NBT getNBT(String id) { + return (NBT) getObject(id); + } + public Object getObject(String id) { return args.getOrDefault(id, null); } diff --git a/src/main/java/net/minestom/server/command/builder/CommandDispatcher.java b/src/main/java/net/minestom/server/command/builder/CommandDispatcher.java index a0a189555..46b58c8e0 100644 --- a/src/main/java/net/minestom/server/command/builder/CommandDispatcher.java +++ b/src/main/java/net/minestom/server/command/builder/CommandDispatcher.java @@ -193,6 +193,7 @@ public class CommandDispatcher { if (conditionResult == Argument.SUCCESS) { executorArgs.setArg(argument.getId(), parsedValue); } else { + // Condition of an argument not correct, use the argument callback result.callback = argument.getCallback(); result.value = argValue; result.error = conditionResult; @@ -208,14 +209,15 @@ public class CommandDispatcher { if (!syntaxesSuggestions.isEmpty()) { final int max = syntaxesSuggestions.firstKey(); // number of correct arguments - // Get the data of the closest syntax - final CommandSuggestionHolder suggestionHolder = syntaxesSuggestions.get(max); - final CommandSyntax syntax = suggestionHolder.syntax; - final String argValue = suggestionHolder.argValue; - final int correctionResult = suggestionHolder.correctionResult; - final int argIndex = suggestionHolder.argIndex; + // Check if at least 1 argument of the syntax is correct + if (max > 0) { + // Get the data of the closest syntax + final CommandSuggestionHolder suggestionHolder = syntaxesSuggestions.get(max); + final CommandSyntax syntax = suggestionHolder.syntax; + final String argValue = suggestionHolder.argValue; + final int correctionResult = suggestionHolder.correctionResult; + final int argIndex = suggestionHolder.argIndex; - if (argValue.length() > 0) { // Found the closest syntax with at least 1 correct argument Argument argument = syntax.getArguments()[argIndex]; result.callback = argument.getCallback(); diff --git a/src/main/java/net/minestom/server/command/builder/arguments/ArgumentType.java b/src/main/java/net/minestom/server/command/builder/arguments/ArgumentType.java index 6f63da487..f08d81fe5 100644 --- a/src/main/java/net/minestom/server/command/builder/arguments/ArgumentType.java +++ b/src/main/java/net/minestom/server/command/builder/arguments/ArgumentType.java @@ -98,4 +98,12 @@ public class ArgumentType { return new ArgumentItemStack(id); } + public static ArgumentNbtCompoundTag NbtCompound(String id) { + return new ArgumentNbtCompoundTag(id); + } + + public static ArgumentNbtTag NBT(String id) { + return new ArgumentNbtTag(id); + } + } diff --git a/src/main/java/net/minestom/server/command/builder/arguments/minecraft/ArgumentItemStack.java b/src/main/java/net/minestom/server/command/builder/arguments/minecraft/ArgumentItemStack.java index 076aaba7f..f9e1b5760 100644 --- a/src/main/java/net/minestom/server/command/builder/arguments/minecraft/ArgumentItemStack.java +++ b/src/main/java/net/minestom/server/command/builder/arguments/minecraft/ArgumentItemStack.java @@ -7,6 +7,7 @@ import net.minestom.server.registry.Registries; import net.minestom.server.utils.NBTUtils; import org.jglrxavpok.hephaistos.nbt.NBT; import org.jglrxavpok.hephaistos.nbt.NBTCompound; +import org.jglrxavpok.hephaistos.nbt.NBTException; import org.jglrxavpok.hephaistos.nbt.SNBTParser; import java.io.StringReader; @@ -37,7 +38,7 @@ public class ArgumentItemStack extends Argument { try { NBT nbt = new SNBTParser(new StringReader(sNBT)).parse(); return nbt instanceof NBTCompound ? SUCCESS : INVALID_NBT; - } catch (Exception e) { + } catch (NBTException e) { return INVALID_NBT; } } @@ -59,7 +60,12 @@ public class ArgumentItemStack extends Argument { final String sNBT = value.substring(nbtIndex).replace("\\\"", "\""); - final NBTCompound compound = (NBTCompound) new SNBTParser(new StringReader(sNBT)).parse(); + NBTCompound compound = null; + try { + compound = (NBTCompound) new SNBTParser(new StringReader(sNBT)).parse(); + } catch (NBTException e) { + e.printStackTrace(); + } NBTUtils.loadDataIntoItem(itemStack, compound); diff --git a/src/main/java/net/minestom/server/command/builder/arguments/minecraft/ArgumentNbtCompoundTag.java b/src/main/java/net/minestom/server/command/builder/arguments/minecraft/ArgumentNbtCompoundTag.java new file mode 100644 index 000000000..d05afbba2 --- /dev/null +++ b/src/main/java/net/minestom/server/command/builder/arguments/minecraft/ArgumentNbtCompoundTag.java @@ -0,0 +1,43 @@ +package net.minestom.server.command.builder.arguments.minecraft; + +import net.minestom.server.command.builder.arguments.Argument; +import org.jglrxavpok.hephaistos.nbt.NBT; +import org.jglrxavpok.hephaistos.nbt.NBTCompound; +import org.jglrxavpok.hephaistos.nbt.NBTException; +import org.jglrxavpok.hephaistos.nbt.SNBTParser; + +import java.io.StringReader; + +public class ArgumentNbtCompoundTag extends Argument { + + public static final int INVALID_NBT = 1; + + public ArgumentNbtCompoundTag(String id) { + super(id, true); + } + + @Override + public int getCorrectionResult(String value) { + try { + NBT nbt = new SNBTParser(new StringReader(value)).parse(); + return nbt instanceof NBTCompound ? SUCCESS : INVALID_NBT; + } catch (NBTException e) { + return INVALID_NBT; + } + } + + @Override + public NBTCompound parse(String value) { + try { + NBT nbt = new SNBTParser(new StringReader(value)).parse(); + return (NBTCompound) nbt; + } catch (NBTException e) { + return null; + } + } + + @Override + public int getConditionResult(NBTCompound value) { + return SUCCESS; + } +} diff --git a/src/main/java/net/minestom/server/command/builder/arguments/minecraft/ArgumentNbtTag.java b/src/main/java/net/minestom/server/command/builder/arguments/minecraft/ArgumentNbtTag.java new file mode 100644 index 000000000..900d819f0 --- /dev/null +++ b/src/main/java/net/minestom/server/command/builder/arguments/minecraft/ArgumentNbtTag.java @@ -0,0 +1,42 @@ +package net.minestom.server.command.builder.arguments.minecraft; + +import net.minestom.server.command.builder.arguments.Argument; +import org.jglrxavpok.hephaistos.nbt.NBT; +import org.jglrxavpok.hephaistos.nbt.NBTException; +import org.jglrxavpok.hephaistos.nbt.SNBTParser; + +import java.io.StringReader; + +public class ArgumentNbtTag extends Argument { + + public static final int INVALID_NBT = 1; + + public ArgumentNbtTag(String id) { + super(id, true); + } + + @Override + public int getCorrectionResult(String value) { + try { + NBT nbt = new SNBTParser(new StringReader(value)).parse(); + return nbt != null ? SUCCESS : INVALID_NBT; + } catch (NBTException e) { + return INVALID_NBT; + } + } + + @Override + public NBT parse(String value) { + try { + NBT nbt = new SNBTParser(new StringReader(value)).parse(); + return nbt; + } catch (NBTException e) { + return null; + } + } + + @Override + public int getConditionResult(NBT value) { + return SUCCESS; + } +}