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
14 changes: 13 additions & 1 deletion src/main/java/ch/njol/skript/lang/function/Function.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,19 @@ public boolean isSingle() {
for (int i = 0; i < parameters.length; i++) {
Parameter<?> parameter = parameters[i];
Object[] parameterValue = parameter.keyed ? convertToKeyed(parameterValues[i]) : parameterValues[i];
if (parameterValue == null) { // Go for default value

// see https://github.com/SkriptLang/Skript/pull/8135
if ((parameterValues[i] == null || parameterValues[i].length == 0)
&& parameter.keyed
&& parameter.def != null
) {
Object[] defaultValue = parameter.def.getArray(event);
if (defaultValue.length == 1) {
parameterValue = KeyedValue.zip(defaultValue, null);
} else {
parameterValue = defaultValue;
}
} else if (parameterValue == null) { // Go for default value
assert parameter.def != null; // Should've been parse error
Object[] defaultValue = parameter.def.getArray(event);
if (parameter.keyed && KeyProviderExpression.areKeysRecommended(parameter.def)) {
Expand Down
12 changes: 11 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 @@ -98,7 +98,17 @@ public static JavaFunction<?> registerFunction(JavaFunction<?> function) {
Skript.debug((signature.local ? "local " : "") + "function " + name + "(" + StringUtils.join(params, ", ") + ")"
+ (c != null ? " :: " + (signature.isSingle() ? c.getName().getSingular() : c.getName().getPlural()) : "") + ":");

Function<?> function = new ScriptFunction<>(signature, node);
Function<?> function;
try {
function = new ScriptFunction<>(signature, node);
} catch (SkriptAPIException ex) {
//noinspection ThrowableNotThrown
Skript.exception(ex, "Error while trying to load a function");

// avoid getting a "function is already registered" error when the function implementation is not known yet
Functions.unregisterFunction(signature);
return null;
}

if (namespace.getFunction(signature.name) == null) {
namespace.addFunction(function);
Expand Down
6 changes: 6 additions & 0 deletions src/test/skript/tests/syntaxes/structures/StructFunction.sk
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ test "function structure type hints":
type_hint_argument_test({_number})
assert the first element of the last parse logs is set # contains "The function 'type_hint_argument_test(boolean)' does not exist." (NEEDS FIXING)

local function list_argument_single_default(x: texts="hey") :: texts:
return {_x::*}

test "function list argument with a single default":
assert list_argument_single_default() = "hey"

local function literal_test(x: entity type):
stop
local function literal_test_projectile(x: entity):
Expand Down