added copy and fixed cleaning of semicolons

This commit is contained in:
gnembon committed 2019-11-04 00:35:32 -04:00
1 parent e2c614c6de
commit efb87c8497
7 files changed
+86 -17

No files matched your search

@@ -58,6 +58,7 @@ import java.util.stream.Collectors;
import static carpet.script.value.NBTSerializableValue.nameFromRegistryId;
// TODO: decide whether copy(entity) should duplicate entity in the world.
public class EntityValue extends Value
{
private Entity entity;
@@ -43,6 +43,15 @@ public class ListValue extends AbstractListValue implements ContainerValueInterf
{
return new ListValue(items);
}
@Override
public Value deepcopy()
{
List<Value> copyItems = new ArrayList<>(items.size());
for (Value entry: items) copyItems.add(entry.deepcopy());
return new ListValue(copyItems);
}
public ListValue(Collection<? extends Value> list)
{
items = new ArrayList<>();
@@ -63,6 +63,15 @@ public class MapValue extends AbstractListValue implements ContainerValueInterfa
{
return new MapValue(map);
}
@Override
public Value deepcopy()
{
Map<Value, Value> copyMap = new HashMap<>();
map.forEach((key, value) -> copyMap.put(key.deepcopy(), value.deepcopy()));
return new MapValue(copyMap);
}
public MapValue(Map<Value,Value> other)
{
map = new HashMap<>(other);
@@ -95,11 +95,26 @@ public class NBTSerializableValue extends Value implements ContainerValueInterfa
return value;
}
@Override
public Value clone()
{
return new NBTSerializableValue(getTag().copy());
// sets only nbttag, even if emtpy;
NBTSerializableValue copy = new NBTSerializableValue(nbtTag);
copy.nbtSupplier = this.nbtSupplier;
copy.nbtString = this.nbtString;
return copy;
}
@Override
public Value deepcopy()
{
NBTSerializableValue copy = (NBTSerializableValue) clone();
// same fields except when tag is set - need to copy it.
if (copy.nbtTag != null)
copy.nbtTag = copy.getTag().copy();
return copy;
}
public static InventoryLocator locateInventory(CarpetContext c, List<LazyValue> params, int offset)
{
@@ -189,4 +189,17 @@ public abstract class Value implements Comparable<Value>, Cloneable
if (stringVal.isEmpty()) return 0;
return ("s"+stringVal).hashCode();
}
public Value deepcopy()
{
try
{
return (Value)this.clone();
}
catch (CloneNotSupportedException e)
{
// should never happen
throw new InternalExpressionException("Cannot make a copy of value: "+this);
}
};
}