nbt tags are mutable, assuming the wrapper owns them.

This commit is contained in:
gnembon committed 2020-06-18 16:57:16 -03:00
1 parent 639283fce3
commit 56bd79aa69
1 file changed
+101 -44
@@ -33,6 +33,7 @@ import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Box; import net.minecraft.util.math.Box;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@@ -48,6 +49,7 @@ public class NBTSerializableValue extends Value implements ContainerValueInterfa
private String nbtString = null; private String nbtString = null;
private Tag nbtTag = null; private Tag nbtTag = null;
private Supplier<Tag> nbtSupplier = null; private Supplier<Tag> nbtSupplier = null;
private boolean owned = false;
private NBTSerializableValue() {} private NBTSerializableValue() {}
@@ -64,11 +66,18 @@ public class NBTSerializableValue extends Value implements ContainerValueInterfa
throw new InternalExpressionException("Incorrect NBT data: "+nbtString); throw new InternalExpressionException("Incorrect NBT data: "+nbtString);
} }
}; };
owned = true;
} }
public NBTSerializableValue(Tag tag) public NBTSerializableValue(Tag tag)
{ {
nbtTag = tag; nbtTag = tag;
owned = true;
}
public NBTSerializableValue(Supplier<Tag> tagSupplier)
{
nbtSupplier = tagSupplier;
} }
public static Value fromStack(ItemStack stack) public static Value fromStack(ItemStack stack)
@@ -115,6 +124,7 @@ public class NBTSerializableValue extends Value implements ContainerValueInterfa
NBTSerializableValue copy = new NBTSerializableValue(nbtTag); NBTSerializableValue copy = new NBTSerializableValue(nbtTag);
copy.nbtSupplier = this.nbtSupplier; copy.nbtSupplier = this.nbtSupplier;
copy.nbtString = this.nbtString; copy.nbtString = this.nbtString;
copy.owned = this.owned;
return copy; return copy;
} }
@@ -122,9 +132,8 @@ public class NBTSerializableValue extends Value implements ContainerValueInterfa
public Value deepcopy() public Value deepcopy()
{ {
NBTSerializableValue copy = (NBTSerializableValue) clone(); NBTSerializableValue copy = (NBTSerializableValue) clone();
// same fields except when tag is set - need to copy it. copy.owned = false;
if (copy.nbtTag != null) ensureOwnership();
copy.nbtTag = copy.getTag().copy();
return copy; return copy;
} }
@@ -290,7 +299,53 @@ public class NBTSerializableValue extends Value implements ContainerValueInterfa
private static Value decodeTag(Tag t) private static Value decodeTag(Tag t)
{ {
if (t instanceof CompoundTag) if (t instanceof CompoundTag)
return new NBTSerializableValue(t); 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 Value toValue()
{
return decodeTagDeep(this.getTag());
}
public static Value fromValue(Value v)
{
if (v instanceof NBTSerializableValue)
return v;
if (v instanceof NullValue)
return Value.NULL;
Value ret = NBTSerializableValue.parseString(v.getString());
if (ret == null)
return Value.NULL;
return ret;
}
private static Value decodeTagDeep(Tag t)
{
if (t instanceof CompoundTag)
{
Map<Value, Value> pairs = new HashMap<>();
CompoundTag ctag = (CompoundTag)t;
for (String key: ctag.getKeys())
{
pairs.put(new StringValue(key), decodeTagDeep(ctag.get(key)));
}
return MapValue.wrap(pairs);
}
if (t instanceof AbstractListTag)
{
List<Value> elems = new ArrayList<>();
AbstractListTag<? extends Tag> ltag = (AbstractListTag<? extends Tag>)t;
for (Tag elem: ltag)
{
elems.add(decodeTagDeep(elem));
}
return ListValue.wrap(elems);
}
if (t instanceof AbstractNumberTag) if (t instanceof AbstractNumberTag)
return new NumericValue(((AbstractNumberTag) t).getDouble()); return new NumericValue(((AbstractNumberTag) t).getDouble());
// more can be done here // more can be done here
@@ -304,13 +359,6 @@ public class NBTSerializableValue extends Value implements ContainerValueInterfa
return nbtTag; return nbtTag;
} }
private void replaceTag(Tag newTag)
{
nbtTag = newTag;
nbtString = null;
nbtSupplier = null; // just to be sure
}
@Override @Override
public boolean equals(final Object o) public boolean equals(final Object o)
{ {
@@ -346,6 +394,7 @@ public class NBTSerializableValue extends Value implements ContainerValueInterfa
{ {
try try
{ {
ensureOwnership();
return (CompoundTag) getTag(); return (CompoundTag) getTag();
} }
catch (ClassCastException e) catch (ClassCastException e)
@@ -364,12 +413,12 @@ public class NBTSerializableValue extends Value implements ContainerValueInterfa
public boolean put(Value where, Value value, Value conditions) public boolean put(Value where, Value value, Value conditions)
{ {
/// WIP /// WIP
ensureOwnership();
NbtPathArgumentType.NbtPath path = cachePath(where.getString()); NbtPathArgumentType.NbtPath path = cachePath(where.getString());
Tag tagToInsert = value instanceof NBTSerializableValue ? Tag tagToInsert = value instanceof NBTSerializableValue ?
((NBTSerializableValue) value).getTag() : ((NBTSerializableValue) value).getTag() :
new NBTSerializableValue(value.getString()).getTag(); new NBTSerializableValue(value.getString()).getTag();
Tag modifiedTag; boolean modifiedTag;
if (conditions instanceof NumericValue) if (conditions instanceof NumericValue)
{ {
modifiedTag = modify_insert((int)((NumericValue) conditions).getLong(), path, tagToInsert); modifiedTag = modify_insert((int)((NumericValue) conditions).getLong(), path, tagToInsert);
@@ -390,22 +439,17 @@ public class NBTSerializableValue extends Value implements ContainerValueInterfa
return false; return false;
} }
} }
if (modifiedTag != null) return modifiedTag;
{
replaceTag(modifiedTag);
return true;
}
return false;
} }
private Tag modify_insert(int index, NbtPathArgumentType.NbtPath nbtPath, Tag newElement) private boolean modify_insert(int index, NbtPathArgumentType.NbtPath nbtPath, Tag newElement)
{ {
return modify_insert(index, nbtPath, newElement, this.getTag().copy()); return modify_insert(index, nbtPath, newElement, this.getTag());
} }
private Tag modify_insert(int index, NbtPathArgumentType.NbtPath nbtPath, Tag newElement, Tag currentTag) private boolean modify_insert(int index, NbtPathArgumentType.NbtPath nbtPath, Tag newElement, Tag currentTag)
{ {
Collection<Tag> targets; Collection<Tag> targets;
try try
@@ -414,7 +458,7 @@ public class NBTSerializableValue extends Value implements ContainerValueInterfa
} }
catch (CommandSyntaxException e) catch (CommandSyntaxException e)
{ {
return null; return false;
} }
boolean modified = false; boolean modified = false;
@@ -428,27 +472,27 @@ public class NBTSerializableValue extends Value implements ContainerValueInterfa
{ {
AbstractListTag<?> targetList = (AbstractListTag) target; AbstractListTag<?> targetList = (AbstractListTag) target;
if (!targetList.addTag(index < 0 ? targetList.size() + index + 1 : index, newElement.copy())) if (!targetList.addTag(index < 0 ? targetList.size() + index + 1 : index, newElement.copy()))
return null; return false;
modified = true; modified = true;
} }
catch (IndexOutOfBoundsException ignored) catch (IndexOutOfBoundsException ignored)
{ {
} }
} }
return modified?currentTag:null; return modified;
} }
private Tag modify_merge(NbtPathArgumentType.NbtPath nbtPath, Tag replacement) //nbtPathArgumentType$NbtPath_1, list_1) private boolean modify_merge(NbtPathArgumentType.NbtPath nbtPath, Tag replacement) //nbtPathArgumentType$NbtPath_1, list_1)
{ {
if (!(replacement instanceof CompoundTag)) if (!(replacement instanceof CompoundTag))
{ {
return getTag(); return false;
} }
Tag originalTag = getTag().copy(); Tag ownTag = getTag();
try try
{ {
for (Tag target : nbtPath.getOrInit(originalTag, CompoundTag::new)) for (Tag target : nbtPath.getOrInit(ownTag, CompoundTag::new))
{ {
if (!(target instanceof CompoundTag)) if (!(target instanceof CompoundTag))
{ {
@@ -457,23 +501,26 @@ public class NBTSerializableValue extends Value implements ContainerValueInterfa
((CompoundTag) target).copyFrom((CompoundTag) replacement); ((CompoundTag) target).copyFrom((CompoundTag) replacement);
} }
} }
catch (CommandSyntaxException ignored) { } catch (CommandSyntaxException ignored)
return originalTag; {
return false;
}
return true;
} }
private Tag modify_replace(NbtPathArgumentType.NbtPath nbtPath, Tag replacement) //nbtPathArgumentType$NbtPath_1, list_1) private boolean modify_replace(NbtPathArgumentType.NbtPath nbtPath, Tag replacement) //nbtPathArgumentType$NbtPath_1, list_1)
{ {
Tag originalTag = getTag().copy(); Tag tag = getTag();
String pathText = nbtPath.toString(); String pathText = nbtPath.toString();
if (pathText.endsWith("]")) // workaround for array replacement or item in the array replacement if (pathText.endsWith("]")) // workaround for array replacement or item in the array replacement
{ {
if (nbtPath.remove(originalTag)==0) if (nbtPath.remove(tag)==0)
return null; return false;
Pattern pattern = Pattern.compile("\\[[^\\[]*]$"); Pattern pattern = Pattern.compile("\\[[^\\[]*]$");
Matcher matcher = pattern.matcher(pathText); Matcher matcher = pattern.matcher(pathText);
if (!matcher.find()) // malformed path if (!matcher.find()) // malformed path
{ {
return null; return false;
} }
String arrAccess = matcher.group(); String arrAccess = matcher.group();
int pos; int pos;
@@ -487,21 +534,21 @@ public class NBTSerializableValue extends Value implements ContainerValueInterfa
} }
catch (NumberFormatException e) catch (NumberFormatException e)
{ {
return null; return false;
} }
} }
NbtPathArgumentType.NbtPath newPath = cachePath(pathText.substring(0, pathText.length()-arrAccess.length())); NbtPathArgumentType.NbtPath newPath = cachePath(pathText.substring(0, pathText.length()-arrAccess.length()));
return modify_insert(pos,newPath,replacement, originalTag); return modify_insert(pos,newPath,replacement, tag);
} }
try try
{ {
nbtPath.put(originalTag, () -> replacement); nbtPath.put(tag, () -> replacement);
} }
catch (CommandSyntaxException e) catch (CommandSyntaxException e)
{ {
return null; return false;
} }
return originalTag; return true;
} }
@Override @Override
@@ -527,13 +574,23 @@ public class NBTSerializableValue extends Value implements ContainerValueInterfa
return cachePath(where.getString()).count(getTag()) > 0; return cachePath(where.getString()).count(getTag()) > 0;
} }
private void ensureOwnership()
{
if (!owned)
{
nbtTag = getTag().copy();
nbtString = null;
nbtSupplier = null; // just to be sure
owned = true;
}
}
@Override @Override
public boolean delete(Value where) public boolean delete(Value where)
{ {
NbtPathArgumentType.NbtPath path = cachePath(where.getString()); NbtPathArgumentType.NbtPath path = cachePath(where.getString());
Tag tag = getTag().copy(); ensureOwnership();
int removed = path.remove(tag); int removed = path.remove(getTag());
if (removed > 0) replaceTag(tag);
return removed > 0; return removed > 0;
} }