2020-10-17 08:18:52 +02:00
|
|
|
package net.minestom.server.command.builder.arguments.minecraft;
|
|
|
|
|
2021-02-11 02:51:42 +01:00
|
|
|
import net.minestom.server.command.builder.NodeMaker;
|
2020-10-17 08:18:52 +02:00
|
|
|
import net.minestom.server.command.builder.arguments.Argument;
|
2021-01-08 03:07:37 +01:00
|
|
|
import net.minestom.server.command.builder.exception.ArgumentSyntaxException;
|
2021-02-09 00:25:10 +01:00
|
|
|
import net.minestom.server.network.packet.server.play.DeclareCommandsPacket;
|
2020-10-24 16:58:27 +02:00
|
|
|
import org.jetbrains.annotations.NotNull;
|
2020-10-17 08:18:52 +02:00
|
|
|
import org.jglrxavpok.hephaistos.nbt.NBT;
|
|
|
|
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
|
|
|
|
import org.jglrxavpok.hephaistos.nbt.NBTException;
|
2021-12-13 16:41:30 +01:00
|
|
|
import org.jglrxavpok.hephaistos.parser.SNBTParser;
|
2020-10-17 08:18:52 +02:00
|
|
|
|
|
|
|
import java.io.StringReader;
|
|
|
|
|
2020-10-17 11:29:05 +02:00
|
|
|
/**
|
|
|
|
* Argument used to retrieve a {@link NBTCompound} if you need key-value data.
|
|
|
|
* <p>
|
|
|
|
* Example: {display:{Name:"{\"text\":\"Sword of Power\"}"}}
|
|
|
|
*/
|
2020-10-17 08:18:52 +02:00
|
|
|
public class ArgumentNbtCompoundTag extends Argument<NBTCompound> {
|
|
|
|
|
|
|
|
public static final int INVALID_NBT = 1;
|
|
|
|
|
|
|
|
public ArgumentNbtCompoundTag(String id) {
|
|
|
|
super(id, true);
|
|
|
|
}
|
|
|
|
|
2020-10-24 16:58:27 +02:00
|
|
|
@NotNull
|
2020-10-17 08:18:52 +02:00
|
|
|
@Override
|
2021-01-08 03:07:37 +01:00
|
|
|
public NBTCompound parse(@NotNull String input) throws ArgumentSyntaxException {
|
2020-10-17 08:18:52 +02:00
|
|
|
try {
|
2021-01-08 03:07:37 +01:00
|
|
|
NBT nbt = new SNBTParser(new StringReader(input)).parse();
|
|
|
|
|
|
|
|
if (!(nbt instanceof NBTCompound))
|
|
|
|
throw new ArgumentSyntaxException("NBTCompound is invalid", input, INVALID_NBT);
|
|
|
|
|
2020-10-17 08:18:52 +02:00
|
|
|
return (NBTCompound) nbt;
|
|
|
|
} catch (NBTException e) {
|
2021-01-08 03:07:37 +01:00
|
|
|
throw new ArgumentSyntaxException("NBTCompound is invalid", input, INVALID_NBT);
|
2020-10-17 08:18:52 +02:00
|
|
|
}
|
|
|
|
}
|
2021-02-09 00:25:10 +01:00
|
|
|
|
|
|
|
@Override
|
2021-02-11 02:51:42 +01:00
|
|
|
public void processNodes(@NotNull NodeMaker nodeMaker, boolean executable) {
|
2021-02-10 23:09:38 +01:00
|
|
|
DeclareCommandsPacket.Node argumentNode = simpleArgumentNode(this, executable, false, false);
|
2021-02-09 00:25:10 +01:00
|
|
|
argumentNode.parser = "minecraft:nbt_compound_tag";
|
2021-02-11 02:51:42 +01:00
|
|
|
|
2021-02-11 05:08:49 +01:00
|
|
|
nodeMaker.addNodes(new DeclareCommandsPacket.Node[]{argumentNode});
|
2021-02-09 00:25:10 +01:00
|
|
|
}
|
2021-04-25 10:42:05 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return String.format("NbtCompound<%s>", getId());
|
|
|
|
}
|
2020-10-17 08:18:52 +02:00
|
|
|
}
|