Skip to content
Open
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: 10 additions & 0 deletions src/main/java/ch/njol/skript/classes/data/DefaultFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,16 @@ public Date[] executeSimple(Object[][] params) {
args.<Number>get("z").doubleValue()
)));

Functions.register(DefaultFunction.builder(skript, "vector", Vector.class)
.description("Creates a new vector with all components equal to the given value. Equivalent to vector(n, n, n).")
.examples("vector(5) # same as vector(5, 5, 5)")
.since("INSERT VERSION")
.parameter("n", Number.class)
.build(args -> {
double value = args.<Number>get("n").doubleValue();
return new Vector(value, value, value);
}));

Functions.registerFunction(new SimpleJavaFunction<Long>("calcExperience", new Parameter[] {
new Parameter<>("level", DefaultClasses.LONG, true, null)
}, DefaultClasses.LONG, true) {
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/ch/njol/skript/lang/function/Functions.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ public static DefaultFunction<?> register(DefaultFunction<?> function) {
if (!name.matches(functionNamePattern))
throw new SkriptAPIException("Invalid function name '%s'".formatted(name));

javaNamespace.addSignature((Signature<?>) function.signature());
if (javaNamespace.getSignature(name) == null) {
javaNamespace.addSignature((Signature<?>) function.signature());
}
javaNamespace.addFunction((Function<?>) function);
globalFunctions.put(function.name(), javaNamespace);

Expand Down
13 changes: 13 additions & 0 deletions src/test/skript/tests/syntaxes/expressions/ExprVectorFromXYZ.sk
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,16 @@ test "vector from xyz":
set {_random} to a random vector
set {_vector} to a new vector from x component of {_random}, y component of {_random}, z component of {_random}
assert {_vector} is {_random} with "random vector to created component equality failed (expected %{_random}%, got %{_vector}%)"

test "vector single argument":
assert vector(5) is vector(5, 5, 5) with "single-argument vector(5) failed"
assert vector(0) is vector(0, 0, 0) with "single-argument vector(0) failed"
assert vector(-10) is vector(-10, -10, -10) with "single-argument vector(-10) failed"
assert vector(3.14) is vector(3.14, 3.14, 3.14) with "single-argument vector(3.14) failed"
loop 60 times:
set {_n} to a random number between -100 and 100
set {_vector} to vector({_n})
assert {_vector} is vector({_n}, {_n}, {_n}) with "randomly-created single-argument vector equality failed (expected %vector({_n}, {_n}, {_n})%, got %{_vector}%)"
assert the x component of {_vector} is {_n} with "single-argument vector x component failed"
assert the y component of {_vector} is {_n} with "single-argument vector y component failed"
assert the z component of {_vector} is {_n} with "single-argument vector z component failed"