|
33 | 33 | import io.github.syst3ms.skriptparser.registration.tags.Tag; |
34 | 34 | import io.github.syst3ms.skriptparser.registration.tags.TagInfo; |
35 | 35 | import io.github.syst3ms.skriptparser.registration.tags.TagManager; |
| 36 | +import io.github.syst3ms.skriptparser.structures.functions.FunctionParameter; |
| 37 | +import io.github.syst3ms.skriptparser.structures.functions.Functions; |
| 38 | +import io.github.syst3ms.skriptparser.structures.functions.JavaFunction; |
36 | 39 | import io.github.syst3ms.skriptparser.types.Type; |
37 | 40 | import io.github.syst3ms.skriptparser.types.TypeManager; |
38 | 41 | import io.github.syst3ms.skriptparser.types.changers.Arithmetic; |
|
46 | 49 | import org.jetbrains.annotations.NotNull; |
47 | 50 | import org.jetbrains.annotations.Nullable; |
48 | 51 |
|
| 52 | +import java.lang.reflect.Array; |
49 | 53 | import java.util.ArrayList; |
50 | 54 | import java.util.Collections; |
51 | 55 | import java.util.HashMap; |
@@ -80,6 +84,7 @@ public class SkriptRegistration { |
80 | 84 | private final List<TagInfo<? extends Tag>> tags = new ArrayList<>(); |
81 | 85 | private final List<SkriptEventInfo<?>> events = new ArrayList<>(); |
82 | 86 | private final List<StructureInfo<?>> structures = new ArrayList<>(); |
| 87 | + private final List<FunctionRegistrar<?>> functions = new ArrayList<>(); |
83 | 88 | private final SkriptAddon registerer; |
84 | 89 | private final SkriptLogger logger; |
85 | 90 | protected boolean newTypes; |
@@ -172,6 +177,13 @@ public List<StructureInfo<?>> getStructures() { |
172 | 177 | return this.structures; |
173 | 178 | } |
174 | 179 |
|
| 180 | + /** |
| 181 | + * @return All currently registered functions |
| 182 | + */ |
| 183 | + public List<FunctionRegistrar<?>> getFunctions() { |
| 184 | + return this.functions; |
| 185 | + } |
| 186 | + |
175 | 187 | /** |
176 | 188 | * @return all currently registered types |
177 | 189 | */ |
@@ -765,6 +777,9 @@ public List<LogEntry> register(boolean ignoreLogs) { |
765 | 777 | Converters.registerConverters(this); |
766 | 778 | Converters.createMissingConverters(); |
767 | 779 | finishConsumers.forEach(consumer -> consumer.accept(registerer)); |
| 780 | + for (FunctionRegistrar<?> function : getFunctions()) { |
| 781 | + Functions.registerFunction(this, function.javaFunction); |
| 782 | + } |
768 | 783 | if (ignoreLogs) { |
769 | 784 | logger.clearLogs(); |
770 | 785 | return new ArrayList<>(); |
@@ -1048,6 +1063,105 @@ public void register() { |
1048 | 1063 | } |
1049 | 1064 | } |
1050 | 1065 |
|
| 1066 | + public class FunctionRegistrar<T> implements Registrar { |
| 1067 | + |
| 1068 | + private final Documentation documentation = new Documentation(); |
| 1069 | + private final String functionName; |
| 1070 | + private final Class<T> returnType; |
| 1071 | + private final boolean isSingle; |
| 1072 | + private final List<FunctionParameter<?>> params = new ArrayList<>(); |
| 1073 | + public JavaFunction<T> javaFunction; |
| 1074 | + private Function<Object[][],T[]> func; |
| 1075 | + |
| 1076 | + public FunctionRegistrar(String functionName, Class<T> returnType, boolean isSingle) { |
| 1077 | + this.functionName = functionName; |
| 1078 | + this.returnType = returnType; |
| 1079 | + this.isSingle = isSingle; |
| 1080 | + } |
| 1081 | + |
| 1082 | + public FunctionRegistrar<T> parameter(String name, Class<?> type, boolean single) { |
| 1083 | + FunctionParameter<?> param = new FunctionParameter<>(name, type, single); |
| 1084 | + this.params.add(param); |
| 1085 | + return this; |
| 1086 | + } |
| 1087 | + |
| 1088 | + public FunctionRegistrar<T> parameter(String name, Class<?> type) { |
| 1089 | + FunctionParameter<?> param = new FunctionParameter<>(name, type, true); |
| 1090 | + this.params.add(param); |
| 1091 | + return this; |
| 1092 | + } |
| 1093 | + |
| 1094 | + |
| 1095 | + public FunctionRegistrar<T> execute(Function<Object[][],T[]> params) { |
| 1096 | + this.func = params; |
| 1097 | + return this; |
| 1098 | + } |
| 1099 | + |
| 1100 | + @SuppressWarnings("unchecked") |
| 1101 | + public FunctionRegistrar<T> executeSingle(Function<Object[][],T> params) { |
| 1102 | + this.func = objects -> { |
| 1103 | + T[] o = (T[]) Array.newInstance(this.returnType, 1); |
| 1104 | + o[0] = params.apply(objects); |
| 1105 | + return o; |
| 1106 | + }; |
| 1107 | + return this; |
| 1108 | + } |
| 1109 | + |
| 1110 | + public FunctionRegistrar<T> noDoc() { |
| 1111 | + this.documentation.noDoc(); |
| 1112 | + return this; |
| 1113 | + } |
| 1114 | + |
| 1115 | + public FunctionRegistrar<T> experimental() { |
| 1116 | + this.documentation.experimental(); |
| 1117 | + return this; |
| 1118 | + } |
| 1119 | + |
| 1120 | + public FunctionRegistrar<T> experimental(String message) { |
| 1121 | + this.documentation.experimental(message); |
| 1122 | + return this; |
| 1123 | + } |
| 1124 | + |
| 1125 | + public FunctionRegistrar<T> name(String name) { |
| 1126 | + this.documentation.setName(name); |
| 1127 | + return this; |
| 1128 | + } |
| 1129 | + |
| 1130 | + public FunctionRegistrar<T> description(String... description) { |
| 1131 | + this.documentation.setDescription(description); |
| 1132 | + return this; |
| 1133 | + } |
| 1134 | + |
| 1135 | + public FunctionRegistrar<T> examples(String... examples) { |
| 1136 | + this.documentation.setExamples(examples); |
| 1137 | + return this; |
| 1138 | + } |
| 1139 | + |
| 1140 | + public FunctionRegistrar<T> since(String since) { |
| 1141 | + this.documentation.setSince(since); |
| 1142 | + return this; |
| 1143 | + } |
| 1144 | + |
| 1145 | + @Override |
| 1146 | + public void register() { |
| 1147 | + this.javaFunction = new JavaFunction<>(this.functionName, |
| 1148 | + this.params.toArray(new FunctionParameter[0]), |
| 1149 | + this.returnType, |
| 1150 | + this.isSingle) { |
| 1151 | + @Override |
| 1152 | + public T[] executeSimple(Object[][] params) { |
| 1153 | + return func.apply(params); |
| 1154 | + } |
| 1155 | + }; |
| 1156 | + SkriptRegistration.this.functions.add(this); |
| 1157 | + } |
| 1158 | + } |
| 1159 | + |
| 1160 | + public <T> FunctionRegistrar<T> newJavaFunction(String functionName, Class<T> returnType, boolean isSingle) { |
| 1161 | + return new FunctionRegistrar<>(functionName, returnType, isSingle); |
| 1162 | + |
| 1163 | + } |
| 1164 | + |
1051 | 1165 | public class ExpressionRegistrar<C extends Expression<? extends T>, T> extends SyntaxRegistrar<C> { |
1052 | 1166 | private final Class<T> returnType; |
1053 | 1167 | private final boolean isSingle; |
|
0 commit comments