diff --git a/src/main/java/ch/njol/skript/classes/data/DefaultFunctions.java b/src/main/java/ch/njol/skript/classes/data/DefaultFunctions.java index e2c5392dd39..d028845071b 100644 --- a/src/main/java/ch/njol/skript/classes/data/DefaultFunctions.java +++ b/src/main/java/ch/njol/skript/classes/data/DefaultFunctions.java @@ -25,6 +25,7 @@ import ch.njol.skript.lang.function.Parameter; import ch.njol.skript.lang.function.SimpleJavaFunction; import ch.njol.skript.lang.util.SimpleLiteral; +import ch.njol.skript.registrations.Classes; import ch.njol.skript.registrations.DefaultClasses; import ch.njol.skript.util.Color; import ch.njol.skript.util.ColorRGB; @@ -568,6 +569,24 @@ public Boolean[] executeSimple(Object[][] params) { }).description("Returns true if the input is NaN (not a number).") .examples("isNaN(0) # false", "isNaN(0/0) # true", "isNaN(sqrt(-1)) # true") .since("2.8.0"); + + Functions.registerFunction(new SimpleJavaFunction("concat", new Parameter[] { + new Parameter<>("texts", DefaultClasses.OBJECT, false, null) + }, DefaultClasses.STRING, true) { + @Override + public String[] executeSimple(Object[][] params) { + StringBuilder builder = new StringBuilder(); + for (Object object : params[0]) { + builder.append(Classes.toString(object)); + } + return new String[] {builder.toString()}; + } + }).description("Joins the provided texts (and other things) into a single text.") + .examples( + "concat(\"hello \", \"there\") # hello there", + "concat(\"foo \", 100, \" bar\") # foo 100 bar" + ).since("INSERT VERSION"); + } } diff --git a/src/main/java/ch/njol/skript/classes/data/DefaultOperations.java b/src/main/java/ch/njol/skript/classes/data/DefaultOperations.java index e6397a1e767..e3f9965c1c4 100644 --- a/src/main/java/ch/njol/skript/classes/data/DefaultOperations.java +++ b/src/main/java/ch/njol/skript/classes/data/DefaultOperations.java @@ -115,6 +115,9 @@ public class DefaultOperations { Arithmetics.registerOperation(Operator.SUBTRACTION, Date.class, Timespan.class, Date::minus); Arithmetics.registerDifference(Date.class, Timespan.class, Date::difference); + // String - String + Arithmetics.registerOperation(Operator.ADDITION, String.class, String.class, String::concat); + } } diff --git a/src/test/skript/tests/misc/string concatenation.sk b/src/test/skript/tests/misc/string concatenation.sk new file mode 100644 index 00000000000..251fd5566af --- /dev/null +++ b/src/test/skript/tests/misc/string concatenation.sk @@ -0,0 +1,29 @@ + +test "string concatenation": + + # string + string + assert "hello " + "there" is "hello there" with "string + string concat failed" + assert "foo" + "bar" is "foobar" with "string + string concat failed" + assert "foo" + " " + "bar" is "foo bar" with "string + string + string concat failed" + assert "hello" + " " + "there" is "hello there" with "string + string + string concat failed" + + # ? + string + set {_var} to "hello" + set {_var} to {_var} + " there" + assert {_var} is "hello there" with "var + string concat failed" + + # ? + string + set {_var1} to "hello " + set {_var2} to "there" + assert {_var1} + {_var2} is "hello there" with "var + var concat failed" + + # variable-string + string + set {_var} to "hello" + assert "%{_var}% " + "there" is "hello there" with "var-string + string concat failed" + + # string + number = + # parser prevents us adding "foo" + 1 or comparing "foo" + 1 with a string + # we test the edge case where somebody slips past us! + set {_var} to 1 + set {_var} to "foo" + {_var} + assert {_var} doesn't exist with "string + number concat succeeded" diff --git a/src/test/skript/tests/syntaxes/functions/concat.sk b/src/test/skript/tests/syntaxes/functions/concat.sk new file mode 100644 index 00000000000..4b57aab8547 --- /dev/null +++ b/src/test/skript/tests/syntaxes/functions/concat.sk @@ -0,0 +1,31 @@ + +test "concat() function": + + # string + string + assert concat("hello ", "there") is "hello there" with "string + string concat() failed" + assert concat("foo", "bar") is "foobar" with "string + string concat() failed" + assert concat("foo", " ", "bar") is "foo bar" with "string + string + string concat() failed" + assert concat("hello", " ", "there") is "hello there" with "string + string + string concat() failed" + assert concat("a", "b", "c", "d", "e") is "abcde" with "5 strings concat() failed" + + # ? + string + set {_var} to "hello" + set {_var} to concat({_var}, " there") + assert {_var} is "hello there" with "var + string concat() failed" + + # ? + string + set {_var1} to "hello " + set {_var2} to "there" + assert concat({_var1}, {_var2}) is "hello there" with "var + var concat() failed" + + # variable-string + string + set {_var} to "hello" + assert concat("%{_var}% ", "there") is "hello there" with "var-string + string concat() failed" + + # string + non-string + # unlike the maths expression we CAN concat objects here! + set {_var} to 1 + set {_var} to concat("foo", {_var}) + assert {_var} is "foo1" with "string + number concat() failed" + assert concat("a", 1, "b", 2) is "a1b2" with "strings + numbers concat() failed" + assert concat("my nice new ", stone sword) is "my nice new stone sword" with "string + item concat() failed"