Skip to content

Commit bf89638

Browse files
Merge branch 'dev/patch' into dev/feature
2 parents e7af55f + 4f7a984 commit bf89638

File tree

10 files changed

+581
-43
lines changed

10 files changed

+581
-43
lines changed

src/main/java/ch/njol/skript/effects/EffKill.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ protected void execute(Event event) {
5252
if (entity instanceof Damageable damageable) {
5353
if (SUPPORTS_DAMAGE_SOURCE) {
5454
EntityDamageEvent.DamageCause cause = EntityDamageEvent.DamageCause.KILL;
55-
HealthUtils.damage(damageable, 100 + damageable.getHealth(), DamageUtils.getDamageSourceFromCause(cause));
55+
HealthUtils.damage(damageable, Double.POSITIVE_INFINITY, DamageUtils.getDamageSourceFromCause(cause));
5656
} else {
5757
HealthUtils.setHealth(damageable, 0);
5858
HealthUtils.damage(damageable, 1);

src/main/java/ch/njol/skript/expressions/ExprAmount.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import ch.njol.skript.lang.ExpressionList;
1212
import ch.njol.skript.lang.ExpressionType;
1313
import ch.njol.skript.lang.SkriptParser.ParseResult;
14+
import ch.njol.skript.lang.Variable;
1415
import ch.njol.skript.lang.util.SimpleExpression;
1516
import ch.njol.skript.lang.util.common.AnyAmount;
1617
import ch.njol.skript.util.LiteralUtils;
@@ -42,6 +43,7 @@ public class ExprAmount extends SimpleExpression<Number> {
4243
@SuppressWarnings("null")
4344
private ExpressionList<?> exprs;
4445
private @Nullable Expression<AnyAmount> any;
46+
private @Nullable Variable<?> list;
4547

4648
@Override
4749
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
@@ -54,7 +56,6 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye
5456
this.exprs = exprs[0] instanceof ExpressionList<?> exprList
5557
? exprList
5658
: new ExpressionList<>(new Expression<?>[]{ exprs[0] }, Object.class, false);
57-
5859
this.exprs = (ExpressionList<?>) LiteralUtils.defendExpression(this.exprs);
5960
if (!LiteralUtils.canInitSafely(this.exprs)) {
6061
return false;
@@ -65,13 +66,20 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye
6566
return false;
6667
}
6768

69+
if (exprs[0] instanceof Variable<?> variable)
70+
this.list = variable;
71+
6872
return true;
6973
}
7074

7175
@Override
7276
protected Number[] get(Event event) {
7377
if (any != null)
7478
return new Number[] {any.getOptionalSingle(event).orElse(() -> 0).amount()};
79+
80+
if (list != null)
81+
return new Long[]{(long) list.size(event)};
82+
7583
return new Long[]{(long) exprs.getArray(event).length};
7684
}
7785

src/main/java/ch/njol/skript/lang/Variable.java

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import ch.njol.skript.structures.StructVariables.DefaultVariables;
1515
import ch.njol.skript.util.StringMode;
1616
import ch.njol.skript.util.Utils;
17+
import com.google.common.base.Preconditions;
18+
import org.skriptlang.skript.util.IndexTrackingTreeMap;
1719
import ch.njol.skript.variables.Variables;
1820
import ch.njol.util.Kleenean;
1921
import ch.njol.util.Pair;
@@ -446,7 +448,7 @@ private T[] getConvertedArray(Event event) {
446448
}
447449

448450
private void set(Event event, @Nullable Object value) {
449-
Variables.setVariable("" + name.toString(event), value, event, local);
451+
Variables.setVariable(name.toString(event), value, event, local);
450452
}
451453

452454
private void setIndex(Event event, String index, @Nullable Object value) {
@@ -456,6 +458,33 @@ private void setIndex(Event event, String index, @Nullable Object value) {
456458
Variables.setVariable(name.substring(0, name.length() - 1) + index, value, event, local);
457459
}
458460

461+
public int size(Event event) {
462+
Preconditions.checkState(list, "Cannot get the size of a single variable");
463+
Map<?, ?> map = (Map<?, ?>) getRaw(event);
464+
if (map == null)
465+
return 0;
466+
467+
int size = map.size();
468+
if (map.containsKey(null)) // if we're trying to get the size of {_list::*}, exclude {_list} from being counted
469+
size--;
470+
471+
if (!(map instanceof IndexTrackingTreeMap<?> indexTrackingMap)) {
472+
for (Object value : map.values()) {
473+
if (value instanceof Map<?, ?> sublist && !sublist.containsKey(null))
474+
size--;
475+
}
476+
return size;
477+
}
478+
479+
Collection<String> sublistIndices = indexTrackingMap.mapIndices();
480+
for (String sublistIndex : sublistIndices) {
481+
if (!((Map<?, ?>) map.get(sublistIndex)).containsKey(null))
482+
size--;
483+
}
484+
485+
return size;
486+
}
487+
459488
@Override
460489
public Class<?> @Nullable [] acceptChange(ChangeMode mode) {
461490
if (!list && mode == ChangeMode.SET)
@@ -494,22 +523,6 @@ public void change(Event event, Object @NotNull [] delta, ChangeMode mode, @NotN
494523
public void change(Event event, Object @Nullable [] delta, ChangeMode mode) throws UnsupportedOperationException {
495524
switch (mode) {
496525
case DELETE:
497-
if (list) {
498-
ArrayList<String> toDelete = new ArrayList<>();
499-
Map<String, Object> map = (Map<String, Object>) getRaw(event);
500-
if (map == null)
501-
return;
502-
for (Entry<String, Object> entry : map.entrySet()) {
503-
if (entry.getKey() != null){
504-
toDelete.add(entry.getKey());
505-
}
506-
}
507-
for (String index : toDelete) {
508-
assert index != null;
509-
setIndex(event, index, null);
510-
}
511-
}
512-
513526
set(event, null);
514527
break;
515528
case SET:
@@ -594,6 +607,13 @@ public void change(Event event, Object @Nullable [] delta, ChangeMode mode) thro
594607
}
595608
} else {
596609
assert mode == ChangeMode.ADD;
610+
if (map instanceof IndexTrackingTreeMap<Object> indexTrackingMap) {
611+
for (Object value : delta) {
612+
int index = indexTrackingMap.nextOpenIndex();
613+
setIndex(event, String.valueOf(index), value);
614+
}
615+
return;
616+
}
597617
int i = 1;
598618
for (Object value : delta) {
599619
if (map != null)

src/main/java/ch/njol/skript/variables/VariablesMap.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import ch.njol.skript.lang.Variable;
44
import ch.njol.util.StringUtils;
55
import org.jetbrains.annotations.Nullable;
6+
import org.skriptlang.skript.util.IndexTrackingTreeMap;
67

78
import java.util.Comparator;
89
import java.util.HashMap;
@@ -251,7 +252,7 @@ void setVariable(String name, @Nullable Object value) {
251252
break;
252253
} else if (value != null) {
253254
// Create child node, add it to parent and continue iteration
254-
childNode = new TreeMap<>(VARIABLE_NAME_COMPARATOR);
255+
childNode = new IndexTrackingTreeMap<>(VARIABLE_NAME_COMPARATOR);
255256

256257
parent.put(childNodeName, childNode);
257258
parent = (TreeMap<String, Object>) childNode;
@@ -304,7 +305,7 @@ void setVariable(String name, @Nullable Object value) {
304305
break;
305306
} else if (value != null) {
306307
// Need to continue iteration, create new child node and put old value in it
307-
TreeMap<String, Object> newChildNodeMap = new TreeMap<>(VARIABLE_NAME_COMPARATOR);
308+
TreeMap<String, Object> newChildNodeMap = new IndexTrackingTreeMap<>(VARIABLE_NAME_COMPARATOR);
308309
newChildNodeMap.put(null, childNode);
309310

310311
// Add new child node to parent
@@ -369,7 +370,7 @@ public VariablesMap copy() {
369370
*/
370371
@SuppressWarnings("unchecked")
371372
private static TreeMap<String, Object> copyTreeMap(TreeMap<String, Object> original) {
372-
TreeMap<String, Object> copy = new TreeMap<>(VARIABLE_NAME_COMPARATOR);
373+
TreeMap<String, Object> copy = new IndexTrackingTreeMap<>(VARIABLE_NAME_COMPARATOR);
373374

374375
for (Entry<String, Object> child : original.entrySet()) {
375376
String key = child.getKey();

src/main/java/org/skriptlang/skript/common/properties/elements/expressions/PropExprAmount.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import ch.njol.skript.lang.Expression;
77
import ch.njol.skript.lang.ExpressionList;
88
import ch.njol.skript.lang.SkriptParser.ParseResult;
9+
import ch.njol.skript.lang.Variable;
910
import ch.njol.skript.util.LiteralUtils;
1011
import ch.njol.util.Kleenean;
1112
import org.bukkit.event.Event;
@@ -38,6 +39,7 @@ public static void register(SyntaxRegistry registry) {
3839
}
3940

4041
private ExpressionList<?> exprs;
42+
private @Nullable Variable<?> list;
4143
private boolean useProperties;
4244

4345
@Override
@@ -46,13 +48,14 @@ public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean is
4648
// amounts of x, y -> property
4749
// amount of x, y -> list length
4850
useProperties = parseResult.hasTag("s") || expressions[0].isSingle();
49-
if (useProperties) {
51+
if (useProperties)
5052
return super.init(expressions, matchedPattern, isDelayed, parseResult);
51-
} else {
52-
// if exprlist or varlist, count elements
53-
this.exprs = asExprList(expressions[0]);
54-
return LiteralUtils.canInitSafely(this.exprs);
55-
}
53+
54+
// if exprlist or varlist, count elements
55+
this.exprs = asExprList(expressions[0]);
56+
if (expressions[0] instanceof Variable<?> variable)
57+
this.list = variable;
58+
return LiteralUtils.canInitSafely(this.exprs);
5659
}
5760

5861
/**
@@ -76,6 +79,10 @@ public static ExpressionList<?> asExprList(Expression<?> expr) {
7679
protected Object @Nullable [] get(Event event) {
7780
if (useProperties)
7881
return super.get(event);
82+
83+
if (list != null)
84+
return new Long[]{(long) list.size(event)};
85+
7986
return new Long[]{(long) exprs.getArray(event).length};
8087
}
8188

src/main/java/org/skriptlang/skript/common/properties/elements/expressions/PropExprNumber.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import ch.njol.skript.lang.Expression;
77
import ch.njol.skript.lang.ExpressionList;
88
import ch.njol.skript.lang.SkriptParser.ParseResult;
9+
import ch.njol.skript.lang.Variable;
910
import ch.njol.skript.util.LiteralUtils;
1011
import ch.njol.util.Kleenean;
1112
import org.bukkit.event.Event;
@@ -35,27 +36,33 @@ public static void register(SyntaxRegistry registry) {
3536
}
3637

3738
private ExpressionList<?> exprs;
39+
private @Nullable Variable<?> list;
3840
private boolean useProperties;
3941

4042
@Override
4143
public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
42-
// size[s] of x -> property
43-
// sizes of x, y -> property
44-
// size of x, y -> list length
44+
// number[s] of x -> property
45+
// numbers of x, y -> property
46+
// number of x, y -> list length
4547
useProperties = parseResult.hasTag("s") || expressions[0].isSingle();
46-
if (useProperties) {
48+
if (useProperties)
4749
return super.init(expressions, matchedPattern, isDelayed, parseResult);
48-
} else {
49-
// if exprlist or varlist, count elements
50-
this.exprs = PropExprAmount.asExprList(expressions[0]);
51-
return LiteralUtils.canInitSafely(this.exprs);
52-
}
50+
51+
// if exprlist or varlist, count elements
52+
this.exprs = PropExprAmount.asExprList(expressions[0]);
53+
if (expressions[0] instanceof Variable<?> variable)
54+
this.list = variable;
55+
return LiteralUtils.canInitSafely(this.exprs);
5356
}
5457

5558
@Override
5659
protected Object @Nullable [] get(Event event) {
5760
if (useProperties)
5861
return super.get(event);
62+
63+
if (list != null)
64+
return new Long[]{(long) list.size(event)};
65+
5966
return new Long[]{(long) exprs.getArray(event).length};
6067
}
6168

src/main/java/org/skriptlang/skript/common/properties/elements/expressions/PropExprSize.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import ch.njol.skript.lang.Expression;
77
import ch.njol.skript.lang.ExpressionList;
88
import ch.njol.skript.lang.SkriptParser.ParseResult;
9+
import ch.njol.skript.lang.Variable;
910
import ch.njol.skript.util.LiteralUtils;
1011
import ch.njol.util.Kleenean;
1112
import org.bukkit.event.Event;
@@ -35,6 +36,7 @@ public static void register(SyntaxRegistry registry) {
3536
}
3637

3738
private ExpressionList<?> exprs;
39+
private @Nullable Variable<?> list;
3840
private boolean useProperties;
3941

4042
@Override
@@ -43,19 +45,24 @@ public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean is
4345
// sizes of x, y -> property
4446
// size of x, y -> list length
4547
useProperties = parseResult.hasTag("s") || expressions[0].isSingle();
46-
if (useProperties) {
48+
if (useProperties)
4749
return super.init(expressions, matchedPattern, isDelayed, parseResult);
48-
} else {
49-
// if exprlist or varlist, count elements
50-
this.exprs = PropExprAmount.asExprList(expressions[0]);
51-
return LiteralUtils.canInitSafely(this.exprs);
52-
}
50+
51+
// if exprlist or varlist, count elements
52+
this.exprs = PropExprAmount.asExprList(expressions[0]);
53+
if (expressions[0] instanceof Variable<?> variable)
54+
this.list = variable;
55+
return LiteralUtils.canInitSafely(this.exprs);
5356
}
5457

5558
@Override
5659
protected Object @Nullable [] get(Event event) {
5760
if (useProperties)
5861
return super.get(event);
62+
63+
if (list != null)
64+
return new Long[]{(long) list.size(event)};
65+
5966
return new Long[]{(long) exprs.getArray(event).length};
6067
}
6168

src/main/java/org/skriptlang/skript/registration/SyntaxInfoImpl.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ private static Priority estimatePriority(Collection<String> patterns) {
3434
return SyntaxInfo.PATTERN_MATCHES_EVERYTHING;
3535
}
3636
priority = SyntaxInfo.COMBINED;
37+
} else if (chars[i] == '<') {
38+
if (i > 0 && chars[i - 1] == '\\') { // skip escaped angle brackets
39+
continue;
40+
}
41+
// regular expression string
42+
return SyntaxInfo.PATTERN_MATCHES_EVERYTHING;
3743
}
3844
}
3945
}

0 commit comments

Comments
 (0)