Merge branch 'master' into 1.17
This commit is contained in:
commit
95bac227a0
13 files changed
+177
-16
No files matched your search
@@ -36,12 +36,14 @@ import carpet.script.value.Value;
|
||||
import carpet.script.value.ValueConversions;
|
||||
import carpet.utils.Messenger;
|
||||
import net.minecraft.block.BlockState;
|
||||
import net.minecraft.command.DataCommandStorage;
|
||||
import net.minecraft.entity.Entity;
|
||||
import net.minecraft.entity.EntityType;
|
||||
import net.minecraft.entity.EquipmentSlot;
|
||||
import net.minecraft.entity.decoration.ArmorStandEntity;
|
||||
import net.minecraft.entity.player.PlayerEntity;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.nbt.NbtHelper;
|
||||
import net.minecraft.nbt.StringTag;
|
||||
import net.minecraft.nbt.Tag;
|
||||
@@ -86,6 +88,7 @@ import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static carpet.script.value.NBTSerializableValue.nameFromRegistryId;
|
||||
import static java.lang.Math.max;
|
||||
import static java.lang.Math.min;
|
||||
|
||||
@@ -1022,6 +1025,29 @@ public class Auxiliary {
|
||||
Value ret = new NumericValue(counts);
|
||||
return (c_, t_) -> ret;
|
||||
});
|
||||
|
||||
// nbt_storage()
|
||||
// nbt_storage(key)
|
||||
// nbt_storage(key, nbt)
|
||||
expression.addLazyFunction("nbt_storage", -1, (c, t, lv) -> {
|
||||
if (lv.size() > 2) throw new InternalExpressionException("'nbt_storage' requires 0, 1 or 2 arguments.");
|
||||
CarpetContext cc = (CarpetContext) c;
|
||||
DataCommandStorage storage = cc.s.getMinecraftServer().getDataCommandStorage();
|
||||
if (lv.size() == 0) {
|
||||
Value ret = ListValue.wrap(storage.getIds().map(i -> new StringValue(nameFromRegistryId(i))).collect(Collectors.toList()));
|
||||
return (_c, _t) -> ret;
|
||||
}
|
||||
String key = lv.get(0).evalValue(c).getString();
|
||||
CompoundTag old_nbt = storage.get(new Identifier(key));
|
||||
if (lv.size() == 2) {
|
||||
Value nbt = lv.get(1).evalValue(c);
|
||||
NBTSerializableValue new_nbt = (nbt instanceof NBTSerializableValue) ? (NBTSerializableValue) nbt
|
||||
: NBTSerializableValue.parseString(nbt.getString(), true);
|
||||
storage.set(new Identifier(key), new_nbt.getCompoundTag());
|
||||
}
|
||||
if (old_nbt == null) return LazyValue.NULL;
|
||||
return (_c, _t) -> new NBTSerializableValue(old_nbt);
|
||||
});
|
||||
}
|
||||
|
||||
private static <T> Stat<T> getStat(StatType<T> type, Identifier id)
|
||||
|
||||
@@ -14,10 +14,8 @@ import carpet.script.value.StringValue;
|
||||
import carpet.script.value.Value;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.regex.PatternSyntaxException;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class DataStructures {
|
||||
@@ -54,30 +52,23 @@ public class DataStructures {
|
||||
|
||||
expression.addFunction("split", (lv) ->
|
||||
{
|
||||
String delimiter;
|
||||
String hwat;
|
||||
Value delimiter;
|
||||
Value hwat;
|
||||
if (lv.size() == 1)
|
||||
{
|
||||
hwat = lv.get(0).getString();
|
||||
delimiter = "";
|
||||
hwat = lv.get(0);
|
||||
delimiter = StringValue.EMPTY;
|
||||
}
|
||||
else if (lv.size() == 2)
|
||||
{
|
||||
delimiter = lv.get(0).getString();
|
||||
hwat = lv.get(1).getString();
|
||||
delimiter = lv.get(0);
|
||||
hwat = lv.get(1);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new InternalExpressionException("'split' takes 1 or 2 arguments");
|
||||
}
|
||||
try
|
||||
{
|
||||
return ListValue.wrap(Arrays.stream(hwat.split(delimiter)).map(StringValue::new).collect(Collectors.toList()));
|
||||
}
|
||||
catch (PatternSyntaxException pse)
|
||||
{
|
||||
throw new InternalExpressionException("Incorrect pattern for 'split': "+pse.getMessage());
|
||||
}
|
||||
return hwat.split(delimiter);
|
||||
});
|
||||
|
||||
expression.addFunction("slice", (lv) ->
|
||||
|
||||
@@ -347,6 +347,24 @@ public class ListValue extends AbstractListValue implements ContainerValueInterf
|
||||
return new ListValue((Collection<? extends Value>) getItems().subList(from, to));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Value split(Value delimiter) {
|
||||
ListValue result = new ListValue();
|
||||
int startIndex = 0;
|
||||
int index = 0;
|
||||
for (Value val : this.items)
|
||||
{
|
||||
index++;
|
||||
if (val.equals(delimiter))
|
||||
{
|
||||
result.items.add(ListValue.wrap(this.items.subList(startIndex, index-1)));
|
||||
startIndex = index;
|
||||
}
|
||||
}
|
||||
result.items.add(ListValue.wrap(this.items.subList(startIndex, length())));
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double readDoubleNumber()
|
||||
{
|
||||
|
||||
@@ -203,6 +203,11 @@ public class MapValue extends AbstractListValue implements ContainerValueInterfa
|
||||
throw new InternalExpressionException("Cannot slice a map value");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Value split(Value delimiter) {
|
||||
throw new InternalExpressionException("Cannot split a map value");
|
||||
}
|
||||
|
||||
@Override
|
||||
public double readDoubleNumber()
|
||||
{
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package carpet.script.value;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonNull;
|
||||
import net.minecraft.nbt.StringTag;
|
||||
@@ -44,6 +46,11 @@ public class NullValue extends NumericValue // TODO check nonsingleton code
|
||||
return o instanceof NullValue ? 0 : -1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Value in(Value value) {
|
||||
return Value.NULL;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTypeString()
|
||||
{
|
||||
@@ -63,6 +70,11 @@ public class NullValue extends NumericValue // TODO check nonsingleton code
|
||||
return StringTag.of("null");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Value split(Value delimiter) {
|
||||
return ListValue.wrap(new ArrayList<Value>());
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonElement toJson()
|
||||
{
|
||||
|
||||
@@ -6,10 +6,12 @@ import com.google.gson.JsonPrimitive;
|
||||
import net.minecraft.nbt.Tag;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.regex.PatternSyntaxException;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public abstract class Value implements Comparable<Value>, Cloneable
|
||||
{
|
||||
@@ -188,6 +190,19 @@ public abstract class Value implements Comparable<Value>, Cloneable
|
||||
if (from > to) return StringValue.EMPTY;
|
||||
return new StringValue(value.substring(from, to));
|
||||
}
|
||||
|
||||
public Value split(Value delimiter)
|
||||
{
|
||||
try
|
||||
{
|
||||
return ListValue.wrap(Arrays.stream(getString().split(delimiter.getString())).map(StringValue::new).collect(Collectors.toList()));
|
||||
}
|
||||
catch (PatternSyntaxException pse)
|
||||
{
|
||||
throw new InternalExpressionException("Incorrect pattern for 'split': "+pse.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public double readDoubleNumber()
|
||||
{
|
||||
String s = getString();
|
||||
|
||||
Reference in new issue
Block a user