diff --git a/docs/scarpet/Full.md b/docs/scarpet/Full.md index 0c52ce0f..afcf91b9 100644 --- a/docs/scarpet/Full.md +++ b/docs/scarpet/Full.md @@ -383,7 +383,12 @@ TODO: add more information about l-value behaviour. ### `Matching Operator ~` This operator should be understood as 'matches', 'contains', 'is_in', or 'find me some stuff about something else. -For strings it matches the right operand as a regular expression to the left, returning the first match. +For strings it matches the right operand as a regular expression to the left, returning: + - `null` if there is no match + - matched phrase if no grouping is applied + - matched element if one group is applied + - list of matches if more than one grouping is applied + This can be used to extract information from unparsed nbt's in a more convoluted way (use `get(...)` for more appropriate way of doing it). For lists it checks if an element is in the list, and returns the index of that element, or `null` if no such element was found, especially that the use of `first(...)` @@ -397,7 +402,14 @@ any extra arguments.
 l(1,2,3) ~ 2  => 1
 l(1,2,3) ~ 4  => null
+
+'foobar' ~ 'baz'  => null
 'foobar' ~ '.b'  => 'ob'
+'foobar' ~ '(.)b'  => 'o'
+'foobar' ~ '((.)b)'  => ['ob', 'o']
+'foobar' ~ '((.)(b))'  => ['ob', 'o', 'b']
+'foobar' ~ '(?:(.)(?:b))'  => 'o'
+
 player('*') ~ 'gnembon'  // null unless player gnembon is logged in (better to use player('gnembon') instead
 p ~ 'sneaking' // if p is an entity returns whether p is sneaking
 
diff --git a/docs/scarpet/language/Operators.md b/docs/scarpet/language/Operators.md index c552a2fc..262f4cc2 100644 --- a/docs/scarpet/language/Operators.md +++ b/docs/scarpet/language/Operators.md @@ -59,7 +59,12 @@ TODO: add more information about l-value behaviour. ### `Matching Operator ~` This operator should be understood as 'matches', 'contains', 'is_in', or 'find me some stuff about something else. -For strings it matches the right operand as a regular expression to the left, returning the first match. +For strings it matches the right operand as a regular expression to the left, returning: + - `null` if there is no match + - matched phrase if no grouping is applied + - matched element if one group is applied + - list of matches if more than one grouping is applied + This can be used to extract information from unparsed nbt's in a more convoluted way (use `get(...)` for more appropriate way of doing it). For lists it checks if an element is in the list, and returns the index of that element, or `null` if no such element was found, especially that the use of `first(...)` @@ -73,7 +78,14 @@ any extra arguments.
 l(1,2,3) ~ 2  => 1
 l(1,2,3) ~ 4  => null
+
+'foobar' ~ 'baz'  => null
 'foobar' ~ '.b'  => 'ob'
+'foobar' ~ '(.)b'  => 'o'
+'foobar' ~ '((.)b)'  => ['ob', 'o']
+'foobar' ~ '((.)(b))'  => ['ob', 'o', 'b']
+'foobar' ~ '(?:(.)(?:b))'  => 'o'
+
 player('*') ~ 'gnembon'  // null unless player gnembon is logged in (better to use player('gnembon') instead
 p ~ 'sneaking' // if p is an entity returns whether p is sneaking
 
diff --git a/src/main/java/carpet/mixins/PlayerInventory_scarpetEventMixin.java b/src/main/java/carpet/mixins/PlayerInventory_scarpetEventMixin.java index ea60fa34..a0e6830a 100644 --- a/src/main/java/carpet/mixins/PlayerInventory_scarpetEventMixin.java +++ b/src/main/java/carpet/mixins/PlayerInventory_scarpetEventMixin.java @@ -29,7 +29,7 @@ public abstract class PlayerInventory_scarpetEventMixin Item item = stack.getItem(); int count = stack.getCount(); boolean res = playerInventory.insertStack(-1, stack); - if (res) + if (count != stack.getCount()) // res returns false for larger item adding to a almost full ineventory { ItemStack diffStack = new ItemStack(item, count - stack.getCount()); diffStack.setTag(stack.getTag()); diff --git a/src/main/java/carpet/script/value/Value.java b/src/main/java/carpet/script/value/Value.java index 2e355da6..33876084 100644 --- a/src/main/java/carpet/script/value/Value.java +++ b/src/main/java/carpet/script/value/Value.java @@ -5,6 +5,8 @@ import com.google.gson.JsonElement; import com.google.gson.JsonPrimitive; import net.minecraft.nbt.Tag; +import java.util.ArrayList; +import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -145,7 +147,16 @@ public abstract class Value implements Comparable, Cloneable { final Pattern p = Pattern.compile(value1.getString()); final Matcher m = p.matcher(this.getString()); - return m.find()?new StringValue(m.group()):Value.NULL; + if (!m.find()) return Value.NULL; + int gc = m.groupCount(); + if (gc == 0) return new StringValue(m.group()); + if (gc == 1) return new StringValue(m.group(1)); + List groups = new ArrayList<>(gc); + for (int i = 1; i <= gc; i++) + { + groups.add(new StringValue(m.group(i))); + } + return ListValue.wrap(groups); } public int length() {