Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Indices of X in List #7323

Open
wants to merge 29 commits into
base: dev/feature
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
84c9279
fix test
Burbulinis Dec 29, 2024
5506397
Merge branch 'dev/feature' into feature/indicesofx
Burbulinis Dec 29, 2024
8c60253
change test
Burbulinis Dec 29, 2024
d1a6642
Clean up the example
Burbulinis Dec 29, 2024
b48c447
Changes
Burbulinis Dec 29, 2024
970a809
Tidy up error message
Burbulinis Dec 29, 2024
7e16be8
Fix bug
Burbulinis Dec 29, 2024
09de482
Tiny error
Burbulinis Dec 29, 2024
5928603
Fix null return values
Burbulinis Dec 29, 2024
b2a5735
Allow non-variable lists and add position syntax
Burbulinis Dec 29, 2024
98ba05c
Fix indentation in test
Burbulinis Dec 29, 2024
7dc918f
Changes
Burbulinis Dec 29, 2024
e6ee8cc
Fix bug
Burbulinis Dec 29, 2024
234c8c4
ok.
Burbulinis Dec 29, 2024
dcb21ba
Clean up the class
Burbulinis Dec 30, 2024
ad15364
Changes
Burbulinis Dec 30, 2024
edb0024
Merge branch 'dev/feature' into feature/indicesofx
Moderocky Dec 30, 2024
8f8abea
Return Integer or String when value is null
Burbulinis Dec 30, 2024
6926715
Merge remote-tracking branch 'fork/feature/indicesofx' into feature/i…
Burbulinis Dec 30, 2024
66713d0
fix
Burbulinis Dec 30, 2024
c137e3d
Changes
Burbulinis Dec 31, 2024
968c108
Merge ExprIndicesOfX with ExprIndicesOf (renamed the class for clarity)
Burbulinis Dec 31, 2024
4d962b9
Clean up
Burbulinis Dec 31, 2024
690f55c
Fix pattern
Burbulinis Dec 31, 2024
e9c5ceb
Merge branch 'dev/feature' into feature/indicesofx
Efnilite Jan 1, 2025
062fc0b
Fix example
Burbulinis Jan 1, 2025
6680cd2
Merge remote-tracking branch 'fork/feature/indicesofx' into feature/i…
Burbulinis Jan 1, 2025
af134f9
Changes
Burbulinis Jan 1, 2025
3182e9b
Clear up the description
Burbulinis Jan 2, 2025
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
141 changes: 141 additions & 0 deletions src/main/java/ch/njol/skript/expressions/ExprIndicesOfX.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package ch.njol.skript.expressions;

import ch.njol.skript.Skript;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.Since;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.ExpressionType;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.lang.SyntaxStringBuilder;
import ch.njol.skript.lang.Variable;
import ch.njol.skript.lang.util.SimpleExpression;
import ch.njol.skript.util.LiteralUtils;
import ch.njol.util.Kleenean;
import org.bukkit.event.Event;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Name("Indices of X in List")
@Description("Returns all the indices of a list where their value is X.")
Burbulinis marked this conversation as resolved.
Show resolved Hide resolved
@Examples({
"set {_list::*} to 1, 2, 3, 1, 2, 3",
"set {_indices::*} to the indices of the value 1 in {_list::*}",
"# {_indices::*} is now 1, 4",
"",
"set {_indices::*} to the indices of the value 2 in {_list::*}",
"# {_indices::*} is now 2, 5",
"",
"set {_otherlist::burb} to 100",
"set {_otherlist::burp} to 100",
"set {_indices::*} to the first index of the value 100 in {_otherlist::*}",
"# {_indices::*} is now burb",
"set {_indices::*} to the last index of the value 100 in {_otherlist::*}",
"# {_indices::*} is now burp"
})
@Since("INSERT VERSION")
public class ExprIndicesOfX extends SimpleExpression<String> {
Burbulinis marked this conversation as resolved.
Show resolved Hide resolved

static {
Skript.registerExpression(ExprIndicesOfX.class, String.class, ExpressionType.COMBINED,
"[the] [1:first|2:last] (indices|index[es]) of [[the] value] %object% in %objects%"
Burbulinis marked this conversation as resolved.
Show resolved Hide resolved
);
}

private IndexType type;
private Expression<?> value;
private Variable<?> list;

@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
if (!(exprs[1] instanceof Variable<?> var) || !var.isList()) {
Skript.error("'" + exprs[1].toString(null, false) +
"' can only ever have one value at most, thus the 'indices of x in ...' expression is useless.");
Burbulinis marked this conversation as resolved.
Show resolved Hide resolved
Burbulinis marked this conversation as resolved.
Show resolved Hide resolved
return false;
}

list = var;
type = IndexType.get(parseResult.mark);
value = LiteralUtils.defendExpression(exprs[0]);

return LiteralUtils.canInitSafely(value);
}

@Override
protected String @Nullable [] get(Event event) {
Object value = this.value.getSingle(event);
if (value == null)
return null;

//noinspection unchecked
Map<String, Object> variable = (Map<String, Object>) list.getRaw(event);
if (variable == null)
return null;

List<String> indices = new ArrayList<>();

for (Map.Entry<String, Object> entry : variable.entrySet()) {
Object entryValue = entry.getValue();
if (entryValue instanceof Map<?, ?> map)
entryValue = map.get(null);
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved

if (entryValue.equals(value))
indices.add(entry.getKey());
}

if (indices.isEmpty())
return null;

if (type == IndexType.FIRST)
return new String[]{indices.get(0)};
else if (type == IndexType.LAST)
return new String[]{indices.get(indices.size() - 1)};
return indices.toArray(new String[0]);
}

@Override
public boolean isSingle() {
return type == IndexType.FIRST || type == IndexType.LAST;
}

@Override
public Class<? extends String> getReturnType() {
return String.class;
}

@Override
public String toString(@Nullable Event event, boolean debug) {
SyntaxStringBuilder builder = new SyntaxStringBuilder(event, debug);

builder.append(type.toString());
if (type != IndexType.ALL)
builder.append("indices");
else
builder.append("index");
builder.append("of value", value, "in", list);

return builder.toString();
}

private enum IndexType {
FIRST, LAST, ALL;

public static IndexType get(int mark) {
return switch (mark) {
case 1 -> FIRST;
case 2 -> LAST;
default -> ALL;
};
}
Burbulinis marked this conversation as resolved.
Show resolved Hide resolved

@Override
public String toString() {
return name().toLowerCase();
}
Burbulinis marked this conversation as resolved.
Show resolved Hide resolved
}

}
19 changes: 19 additions & 0 deletions src/test/skript/tests/syntaxes/expressions/ExprIndicesOfX.sk
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
test "indices of x":
set {_list::*} to 1, 2, 3, 1, 2, 3
set {_indices::*} to the indices of the value 1 in {_list::*}
assert {_indices::*} is "1", "4" with "indices of 1 failed"

set {_indices::*} to the first index of the value 3 in {_list::*}
assert {_indices::*} is "3" with "first index of 3 failed"

set {_indices::*} to the last index of the value 3 in {_list::*}
assert {_indices::*} is "6" with "last index of 3 failed"

set {_otherlist::burb} to test-location
set {_otherlist::_DJ8U3f;} to test-location
set {_otherlist::;'w20} to test-location
set {_otherList::breh} to 2
set {_otherList::quatro} to 4

set {_indices::*} to the indices of the value test-location in {_otherlist::*}
assert {_indices::*} is ";'w20", "_dj8u3f;", "burb" with "indices of test-location with symbols failed"
Loading