Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/main/java/ch/njol/skript/expressions/ExprAmount.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import ch.njol.skript.lang.ExpressionList;
import ch.njol.skript.lang.ExpressionType;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.lang.Variable;
import ch.njol.skript.lang.util.SimpleExpression;
import ch.njol.skript.lang.util.common.AnyAmount;
import ch.njol.skript.util.LiteralUtils;
Expand Down Expand Up @@ -42,6 +43,7 @@ public class ExprAmount extends SimpleExpression<Number> {
@SuppressWarnings("null")
private ExpressionList<?> exprs;
private @Nullable Expression<AnyAmount> any;
private @Nullable Variable<?> list;

@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
Expand All @@ -54,7 +56,6 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye
this.exprs = exprs[0] instanceof ExpressionList<?> exprList
? exprList
: new ExpressionList<>(new Expression<?>[]{ exprs[0] }, Object.class, false);

this.exprs = (ExpressionList<?>) LiteralUtils.defendExpression(this.exprs);
if (!LiteralUtils.canInitSafely(this.exprs)) {
return false;
Expand All @@ -65,13 +66,20 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye
return false;
}

if (exprs[0] instanceof Variable<?> variable)
this.list = variable;

return true;
}

@Override
protected Number[] get(Event event) {
if (any != null)
return new Number[] {any.getOptionalSingle(event).orElse(() -> 0).amount()};

if (list != null)
return new Long[]{(long) list.size(event)};

return new Long[]{(long) exprs.getArray(event).length};
}

Expand Down
54 changes: 37 additions & 17 deletions src/main/java/ch/njol/skript/lang/Variable.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import ch.njol.skript.structures.StructVariables.DefaultVariables;
import ch.njol.skript.util.StringMode;
import ch.njol.skript.util.Utils;
import com.google.common.base.Preconditions;
import org.skriptlang.skript.util.IndexTrackingTreeMap;
import ch.njol.skript.variables.Variables;
import ch.njol.util.Kleenean;
import ch.njol.util.Pair;
Expand Down Expand Up @@ -446,7 +448,7 @@ private T[] getConvertedArray(Event event) {
}

private void set(Event event, @Nullable Object value) {
Variables.setVariable("" + name.toString(event), value, event, local);
Variables.setVariable(name.toString(event), value, event, local);
}

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

public int size(Event event) {
Preconditions.checkState(list, "Cannot get the size of a single variable");
Map<?, ?> map = (Map<?, ?>) getRaw(event);
if (map == null)
return 0;

int size = map.size();
if (map.containsKey(null)) // if we're trying to get the size of {_list::*}, exclude {_list} from being counted
size--;

if (!(map instanceof IndexTrackingTreeMap<?> indexTrackingMap)) {
for (Object value : map.values()) {
if (value instanceof Map<?, ?> sublist && !sublist.containsKey(null))
size--;
}
return size;
}

Collection<String> sublistIndices = indexTrackingMap.mapIndices();
for (String sublistIndex : sublistIndices) {
if (!((Map<?, ?>) map.get(sublistIndex)).containsKey(null))
size--;
}

return size;
}

@Override
public Class<?> @Nullable [] acceptChange(ChangeMode mode) {
if (!list && mode == ChangeMode.SET)
Expand Down Expand Up @@ -494,22 +523,6 @@ public void change(Event event, Object @NotNull [] delta, ChangeMode mode, @NotN
public void change(Event event, Object @Nullable [] delta, ChangeMode mode) throws UnsupportedOperationException {
switch (mode) {
case DELETE:
if (list) {
ArrayList<String> toDelete = new ArrayList<>();
Map<String, Object> map = (Map<String, Object>) getRaw(event);
if (map == null)
return;
for (Entry<String, Object> entry : map.entrySet()) {
if (entry.getKey() != null){
toDelete.add(entry.getKey());
}
}
for (String index : toDelete) {
assert index != null;
setIndex(event, index, null);
}
}

set(event, null);
break;
case SET:
Expand Down Expand Up @@ -594,6 +607,13 @@ public void change(Event event, Object @Nullable [] delta, ChangeMode mode) thro
}
} else {
assert mode == ChangeMode.ADD;
if (map instanceof IndexTrackingTreeMap<Object> indexTrackingMap) {
for (Object value : delta) {
int index = indexTrackingMap.nextOpenIndex();
setIndex(event, String.valueOf(index), value);
}
return;
}
int i = 1;
for (Object value : delta) {
if (map != null)
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/ch/njol/skript/variables/VariablesMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import ch.njol.skript.lang.Variable;
import ch.njol.util.StringUtils;
import org.jetbrains.annotations.Nullable;
import org.skriptlang.skript.util.IndexTrackingTreeMap;

import java.util.Comparator;
import java.util.HashMap;
Expand Down Expand Up @@ -216,7 +217,7 @@ void setVariable(String name, @Nullable Object value) {
break;
} else if (value != null) {
// Create child node, add it to parent and continue iteration
childNode = new TreeMap<>(VARIABLE_NAME_COMPARATOR);
childNode = new IndexTrackingTreeMap<>(VARIABLE_NAME_COMPARATOR);

parent.put(childNodeName, childNode);
parent = (TreeMap<String, Object>) childNode;
Expand Down Expand Up @@ -269,7 +270,7 @@ void setVariable(String name, @Nullable Object value) {
break;
} else if (value != null) {
// Need to continue iteration, create new child node and put old value in it
TreeMap<String, Object> newChildNodeMap = new TreeMap<>(VARIABLE_NAME_COMPARATOR);
TreeMap<String, Object> newChildNodeMap = new IndexTrackingTreeMap<>(VARIABLE_NAME_COMPARATOR);
newChildNodeMap.put(null, childNode);

// Add new child node to parent
Expand Down Expand Up @@ -334,7 +335,7 @@ public VariablesMap copy() {
*/
@SuppressWarnings("unchecked")
private static TreeMap<String, Object> copyTreeMap(TreeMap<String, Object> original) {
TreeMap<String, Object> copy = new TreeMap<>(VARIABLE_NAME_COMPARATOR);
TreeMap<String, Object> copy = new IndexTrackingTreeMap<>(VARIABLE_NAME_COMPARATOR);

for (Entry<String, Object> child : original.entrySet()) {
String key = child.getKey();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.ExpressionList;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.lang.Variable;
import ch.njol.skript.util.LiteralUtils;
import ch.njol.util.Kleenean;
import org.bukkit.event.Event;
Expand Down Expand Up @@ -40,6 +41,7 @@ public static void register(SyntaxRegistry registry, Origin origin) {
}

private ExpressionList<?> exprs;
private @Nullable Variable<?> list;
private boolean useProperties;

@Override
Expand All @@ -48,13 +50,14 @@ public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean is
// amounts of x, y -> property
// amount of x, y -> list length
useProperties = parseResult.hasTag("s") || expressions[0].isSingle();
if (useProperties) {
if (useProperties)
return super.init(expressions, matchedPattern, isDelayed, parseResult);
} else {
// if exprlist or varlist, count elements
this.exprs = asExprList(expressions[0]);
return LiteralUtils.canInitSafely(this.exprs);
}

// if exprlist or varlist, count elements
this.exprs = asExprList(expressions[0]);
if (expressions[0] instanceof Variable<?> variable)
this.list = variable;
return LiteralUtils.canInitSafely(this.exprs);
}

/**
Expand All @@ -78,6 +81,10 @@ public static ExpressionList<?> asExprList(Expression<?> expr) {
protected Object @Nullable [] get(Event event) {
if (useProperties)
return super.get(event);

if (list != null)
return new Long[]{(long) list.size(event)};

return new Long[]{(long) exprs.getArray(event).length};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.ExpressionList;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.lang.Variable;
import ch.njol.skript.util.LiteralUtils;
import ch.njol.util.Kleenean;
import org.bukkit.event.Event;
Expand Down Expand Up @@ -37,27 +38,33 @@ public static void register(SyntaxRegistry registry, Origin origin) {
}

private ExpressionList<?> exprs;
private @Nullable Variable<?> list;
private boolean useProperties;

@Override
public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
// size[s] of x -> property
// sizes of x, y -> property
// size of x, y -> list length
// number[s] of x -> property
// numbers of x, y -> property
// number of x, y -> list length
useProperties = parseResult.hasTag("s") || expressions[0].isSingle();
if (useProperties) {
if (useProperties)
return super.init(expressions, matchedPattern, isDelayed, parseResult);
} else {
// if exprlist or varlist, count elements
this.exprs = PropExprAmount.asExprList(expressions[0]);
return LiteralUtils.canInitSafely(this.exprs);
}

// if exprlist or varlist, count elements
this.exprs = PropExprAmount.asExprList(expressions[0]);
if (expressions[0] instanceof Variable<?> variable)
this.list = variable;
return LiteralUtils.canInitSafely(this.exprs);
}

@Override
protected Object @Nullable [] get(Event event) {
if (useProperties)
return super.get(event);

if (list != null)
return new Long[]{(long) list.size(event)};

return new Long[]{(long) exprs.getArray(event).length};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.ExpressionList;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.lang.Variable;
import ch.njol.skript.util.LiteralUtils;
import ch.njol.util.Kleenean;
import org.bukkit.event.Event;
Expand Down Expand Up @@ -37,6 +38,7 @@ public static void register(SyntaxRegistry registry, Origin origin) {
}

private ExpressionList<?> exprs;
private @Nullable Variable<?> list;
private boolean useProperties;

@Override
Expand All @@ -45,19 +47,24 @@ public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean is
// sizes of x, y -> property
// size of x, y -> list length
useProperties = parseResult.hasTag("s") || expressions[0].isSingle();
if (useProperties) {
if (useProperties)
return super.init(expressions, matchedPattern, isDelayed, parseResult);
} else {
// if exprlist or varlist, count elements
this.exprs = PropExprAmount.asExprList(expressions[0]);
return LiteralUtils.canInitSafely(this.exprs);
}

// if exprlist or varlist, count elements
this.exprs = PropExprAmount.asExprList(expressions[0]);
if (expressions[0] instanceof Variable<?> variable)
this.list = variable;
return LiteralUtils.canInitSafely(this.exprs);
}

@Override
protected Object @Nullable [] get(Event event) {
if (useProperties)
return super.get(event);

if (list != null)
return new Long[]{(long) list.size(event)};

return new Long[]{(long) exprs.getArray(event).length};
}

Expand Down
Loading