Minestom/src/main/java/net/minestom/server/command/builder/arguments/minecraft/ArgumentNbtCompoundTag.java

51 lines
1.4 KiB
Java
Raw Normal View History

package net.minestom.server.command.builder.arguments.minecraft;
import net.minestom.server.command.builder.arguments.Argument;
2020-10-24 16:58:27 +02:00
import org.jetbrains.annotations.NotNull;
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;
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\"}"}}
*/
public class ArgumentNbtCompoundTag extends Argument<NBTCompound> {
public static final int INVALID_NBT = 1;
public ArgumentNbtCompoundTag(String id) {
super(id, true);
}
@Override
2020-10-24 16:58:27 +02:00
public int getCorrectionResult(@NotNull String value) {
try {
NBT nbt = new SNBTParser(new StringReader(value)).parse();
return nbt instanceof NBTCompound ? SUCCESS : INVALID_NBT;
} catch (NBTException e) {
return INVALID_NBT;
}
}
2020-10-24 16:58:27 +02:00
@NotNull
@Override
2020-10-24 16:58:27 +02:00
public NBTCompound parse(@NotNull String value) {
try {
NBT nbt = new SNBTParser(new StringReader(value)).parse();
return (NBTCompound) nbt;
} catch (NBTException e) {
return null;
}
}
@Override
2020-10-24 16:58:27 +02:00
public int getConditionResult(@NotNull NBTCompound value) {
return SUCCESS;
}
}