nbt contd

This commit is contained in:
gnembon committed 2019-07-19 17:47:08 -03:00
1 parent 2b4be7d0b8
commit eee23e4ba6
4 files changed
+60 -39

No files matched your search

@@ -8,11 +8,13 @@ import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.block.entity.HopperBlockEntity;
import net.minecraft.command.arguments.ItemStackArgument;
import net.minecraft.command.arguments.ItemStringReader;
import net.minecraft.command.arguments.NbtPathArgumentType;
import net.minecraft.entity.Entity;
import net.minecraft.entity.passive.VillagerEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.AbstractNumberTag;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.StringNbtReader;
import net.minecraft.nbt.Tag;
@@ -22,13 +24,14 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import static java.lang.Math.abs;
public class NBTSerializableValue extends Value
{
private String nbtString = null;
private CompoundTag nbtTag = null;
private Tag nbtTag = null;
private Supplier<CompoundTag> nbtSupplier = null;
public NBTSerializableValue(ItemStack stack)
@@ -51,7 +54,7 @@ public class NBTSerializableValue extends Value
};
}
public NBTSerializableValue(CompoundTag tag)
public NBTSerializableValue(Tag tag)
{
nbtTag = tag;
}
@@ -155,7 +158,17 @@ public class NBTSerializableValue extends Value
return slot;
}
public CompoundTag getTag()
public static Value decodeTag(Tag t)
{
if (t instanceof CompoundTag)
return new NBTSerializableValue(t);
if (t instanceof AbstractNumberTag)
return new NumericValue(((AbstractNumberTag) t).getDouble());
// more can be done here
return new StringValue(t.asString());
}
public Tag getTag()
{
if (nbtTag == null)
nbtTag = nbtSupplier.get();
@@ -184,6 +197,18 @@ public class NBTSerializableValue extends Value
return true;
}
public CompoundTag getCompoundTag()
{
try
{
return (CompoundTag) getTag();
}
catch (ClassCastException e)
{
throw new InternalExpressionException(getString()+" is not a valid compound tag");
}
}
public static class InventoryLocator
{
public Object owner;
@@ -202,35 +227,32 @@ public class NBTSerializableValue extends Value
@Override
public Value getElementAt(Value value)
{
String path = value.getString();
String pathString = value.getString();
//return Value.NULL;
NbtPathArgumentType.NbtPath path;
try
{
path = NbtPathArgumentType.nbtPath().method_9362(new StringReader(pathString));
}
catch (CommandSyntaxException exc)
{
throw new InternalExpressionException("Incorrect nbt path: "+pathString);
}
String res = null;
try
{
List<Tag> tags = path.get(getTag());
if (tags.size()==0)
return Value.NULL;
if (tags.size()==1)
return new NBTSerializableValue(tags.get(0));
return ListValue.wrap(tags.stream().map(NBTSerializableValue::new).collect(Collectors.toList()));
}
catch (CommandSyntaxException ignored) { }
return Value.NULL;
/*
CompoundTag nbttagcompound = e.toTag((new CompoundTag()));
if (a==null)
return new NBTSerializableValue(nbttagcompound);//StringValue(nbttagcompound.toString());
NbtPathArgumentType.NbtPath path;
try
{
path = NbtPathArgumentType.nbtPath().method_9362(new StringReader(a.getString()));
}
catch (CommandSyntaxException exc)
{
throw new InternalExpressionException("Incorrect path: "+a.getString());
}
String res = null;
try
{
List<Tag> tags = path.get(nbttagcompound);
if (tags.size()==0)
return Value.NULL;
if (tags.size()==1)
return new NBTSerializableValue(tags.get(0));
return ListValue.wrap(tags.stream().map(NBTSerializableValue::new).collect(Collectors.toList()));
}
catch (CommandSyntaxException ignored) { }
return Value.NULL;
*/
/*int numitems = items.size();
long range = abs(index)/numitems;
index += (range+2)*numitems;