Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

let encode_nbt has a target type. and add a function for NBT type probing #1838

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions src/main/java/carpet/script/api/Auxiliary.java
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,50 @@ else if (!interactable && targetBlock == null)

expression.addUnaryFunction("nbt", NBTSerializableValue::fromValue);

expression.addFunction("nbt_type", lv -> {
if (lv.isEmpty()) {
throw new InternalExpressionException("'nbt_type' requires parameters");
}

Value resulttTag;

if (lv.size() == 1) {
resulttTag = lv.get(0);
} else {
Value source = lv.get(0);
if (!(source instanceof NBTSerializableValue)){
source = new NBTSerializableValue(source.getString());
}
resulttTag=((NBTSerializableValue)source).get_nocast(lv.get(1));
}

if(resulttTag instanceof NBTSerializableValue nbttag){
return new StringValue(nbttag.getTag().getType().getPrettyName().substring(4).toLowerCase());
}
if (resulttTag instanceof ListValue listOfNbt) {
return ListValue.wrap(listOfNbt.getItems().stream().map(nbttag->new StringValue(((NBTSerializableValue)nbttag).getTag().getType().getPrettyName().substring(4).toLowerCase())));
}
if(resulttTag.isNull()){
return resulttTag;
}
throw new InternalExpressionException("'nbt_type' requires parameter of NBT Value.");
});

expression.addFunction("get_as_nbt", lv -> {
if (lv.size()<2) {
throw new InternalExpressionException("'get_as_nbt' requires 2 parameters");
}

Value source = lv.get(0);
if (!(source instanceof NBTSerializableValue)){
source = new NBTSerializableValue(source.getString());
}
Value resulttTag=((NBTSerializableValue)source).get_nocast(lv.get(1));

return resulttTag;

});

expression.addUnaryFunction("escape_nbt", v -> new StringValue(StringTag.quoteAndEscape(v.getString())));

expression.addUnaryFunction("parse_nbt", v -> {
Expand Down Expand Up @@ -480,6 +524,40 @@ else if (!interactable && targetBlock == null)
throw new InternalExpressionException("'encode_nbt' requires 1 or 2 parameters");
}
Value v = lv.get(0);
if(argSize > 1 && !(lv.get(1) instanceof BooleanValue)){
Tag res = null;
try {
res = switch (lv.get(1).getString().toLowerCase()) {
case "byte":
yield net.minecraft.nbt.ByteTag.valueOf((byte)NumericValue.asNumber(v).getLong());
case "short":
yield net.minecraft.nbt.ShortTag.valueOf((short)NumericValue.asNumber(v).getLong());
case "int":
yield net.minecraft.nbt.IntTag.valueOf((int)NumericValue.asNumber(v).getLong());
case "long":
yield net.minecraft.nbt.LongTag.valueOf(NumericValue.asNumber(v).getLong());
case "string":
yield net.minecraft.nbt.StringTag.valueOf(v.getString());
case "byte_array":
yield new net.minecraft.nbt.ByteArrayTag(((carpet.script.value.AbstractListValue)v).unpack().stream().map(x->(byte)NumericValue.asNumber(x).getLong()).toList());
case "int_array":
yield new net.minecraft.nbt.IntArrayTag(((carpet.script.value.AbstractListValue)v).unpack().stream().map(x->(int)NumericValue.asNumber(x).getLong()).toList());
case "long_array":
yield new net.minecraft.nbt.LongArrayTag(((carpet.script.value.AbstractListValue)v).unpack().stream().map(x->NumericValue.asNumber(x).getLong()).toList());
case "float":
yield net.minecraft.nbt.FloatTag.valueOf((float)NumericValue.asNumber(v).getDouble());
case "double":
yield net.minecraft.nbt.DoubleTag.valueOf(NumericValue.asNumber(v).getDouble());
default:
yield null;
};
} catch (ClassCastException e) {
// fall back to the old method?
}
if (res != null) {
return NBTSerializableValue.of(res);
}
}
boolean force = (argSize > 1) && lv.get(1).getBoolean();
Tag tag;
try
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/carpet/script/value/NBTSerializableValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,29 @@ public Value get(Value value)
return Value.NULL;
}

public Value get_nocast(Value value)
{
String valString = value.getString();
NbtPathArgument.NbtPath path = cachePath(valString);
try
{
List<Tag> tags = path.get(getTag());
if (tags.isEmpty())
{
return Value.NULL;
}
if (tags.size() == 1 && !valString.endsWith("[]"))
{
return NBTSerializableValue.of(tags.get(0));
}
return ListValue.wrap(tags.stream().filter(x->x!=null).map(NBTSerializableValue::new));
}
catch (CommandSyntaxException ignored)
{
}
return Value.NULL;
}

@Override
public boolean has(Value where)
{
Expand Down
Loading