From f1ec1b7cc83069d025b047e8068de8219988eecf Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 4 Jul 2021 21:42:09 +0800 Subject: [PATCH] Add typist example --- .gitignore | 1 + checkstyle.xml | 1 + commons/pom.xml | 2 - pom.xml | 9 +- .../dependency-reduced-pom.xml | 153 ++++++++++++++++++ typist-example-plugin/nb-configuration.xml | 18 +++ typist-example-plugin/pom.xml | 99 ++++++++++++ .../typist/example/plugin/ExamplePlugin.java | 40 +++++ .../typist/example/plugin/TellCommand.java | 68 ++++++++ .../src/main/resources/plugin.yml | 5 + typist/pom.xml | 17 -- .../typist/generation/chunks/Header.java | 2 + .../typist/generation/chunks/Lambda.java | 6 +- .../typist/generation/chunks/HeaderTest.java | 2 + .../typist/generation/chunks/LambdaTest.java | 9 +- 15 files changed, 404 insertions(+), 28 deletions(-) create mode 100644 typist-example-plugin/dependency-reduced-pom.xml create mode 100644 typist-example-plugin/nb-configuration.xml create mode 100644 typist-example-plugin/pom.xml create mode 100644 typist-example-plugin/src/main/java/com/karuslabs/typist/example/plugin/ExamplePlugin.java create mode 100644 typist-example-plugin/src/main/java/com/karuslabs/typist/example/plugin/TellCommand.java create mode 100644 typist-example-plugin/src/main/resources/plugin.yml diff --git a/.gitignore b/.gitignore index 81fd9d516..5f8624801 100644 --- a/.gitignore +++ b/.gitignore @@ -56,3 +56,4 @@ Temporary Items *.iml /typist/target/ +/typist-example-plugin/target/ diff --git a/checkstyle.xml b/checkstyle.xml index 4013da97a..8a8760e5f 100644 --- a/checkstyle.xml +++ b/checkstyle.xml @@ -5,6 +5,7 @@ + diff --git a/commons/pom.xml b/commons/pom.xml index 415bf4ad4..1358dcb3a 100644 --- a/commons/pom.xml +++ b/commons/pom.xml @@ -13,7 +13,6 @@ Commons Karus Lab's commons library - com.karuslabs @@ -38,7 +37,6 @@ - diff --git a/pom.xml b/pom.xml index 1afdee49e..d95fa9728 100644 --- a/pom.xml +++ b/pom.xml @@ -16,13 +16,14 @@ 16 - latest + nightly annotations commons typist + typist-example-plugin @@ -187,7 +188,7 @@ - + org.apache.maven.plugins maven-javadoc-plugin @@ -198,9 +199,9 @@ https://checkerframework.org/api/ https://junit.org/junit5/docs/current/api/ - + https://repo.karuslabs.com/repository/chimera/${repository-head}/typist/apidocs diff --git a/typist-example-plugin/dependency-reduced-pom.xml b/typist-example-plugin/dependency-reduced-pom.xml new file mode 100644 index 000000000..e910976ad --- /dev/null +++ b/typist-example-plugin/dependency-reduced-pom.xml @@ -0,0 +1,153 @@ + + + + chimera + com.karuslabs + 5.0.0-SNAPSHOT + + 4.0.0 + typist-example-plugin + Typist Example Plugin + A simple plugin that demostrates the usage of Typist + + + + maven-compiler-plugin + 3.8.1 + + ${release} + + + + maven-shade-plugin + 3.3.0-SNAPSHOT + + + + shade + + + + + true + true + + + + + + + + + + spigot-repo + https://hub.spigotmc.org/nexus/content/repositories/snapshots/ + + + chimera-releases + https://repo.karuslabs.com/repository/chimera-releases/ + + + chimera-snapshots + https://repo.karuslabs.com/repository/chimera-snapshots/ + + + minecraft-libraries + https://libraries.minecraft.net + + + + + com.mojang + brigadier + 1.0.17 + provided + + + com.karuslabs + typist + 5.0.0-SNAPSHOT + provided + + + org.spigotmc + spigot-api + 1.17-R0.1-SNAPSHOT + provided + + + org.junit.jupiter + junit-jupiter-api + 5.7.2 + test + + + apiguardian-api + org.apiguardian + + + opentest4j + org.opentest4j + + + junit-platform-commons + org.junit.platform + + + + + org.junit.jupiter + junit-jupiter-engine + 5.7.2 + test + + + junit-platform-engine + org.junit.platform + + + apiguardian-api + org.apiguardian + + + + + org.junit.jupiter + junit-jupiter-params + 5.7.2 + test + + + apiguardian-api + org.apiguardian + + + + + org.mockito + mockito-core + 3.11.2 + test + + + byte-buddy + net.bytebuddy + + + byte-buddy-agent + net.bytebuddy + + + objenesis + org.objenesis + + + + + org.mockito + mockito-junit-jupiter + 3.11.2 + test + + + diff --git a/typist-example-plugin/nb-configuration.xml b/typist-example-plugin/nb-configuration.xml new file mode 100644 index 000000000..294c61099 --- /dev/null +++ b/typist-example-plugin/nb-configuration.xml @@ -0,0 +1,18 @@ + + + + + + mit_1 + + diff --git a/typist-example-plugin/pom.xml b/typist-example-plugin/pom.xml new file mode 100644 index 000000000..b739f97c4 --- /dev/null +++ b/typist-example-plugin/pom.xml @@ -0,0 +1,99 @@ + + + 4.0.0 + + com.karuslabs + chimera + 5.0.0-SNAPSHOT + + + typist-example-plugin + jar + + Typist Example Plugin + A simple plugin that demostrates the usage of Typist + + + + spigot-repo + https://hub.spigotmc.org/nexus/content/repositories/snapshots/ + + + + chimera-releases + https://repo.karuslabs.com/repository/chimera-releases/ + + + + chimera-snapshots + https://repo.karuslabs.com/repository/chimera-snapshots/ + + + + minecraft-libraries + https://libraries.minecraft.net + + + + + + com.mojang + brigadier + 1.0.17 + provided + + + + com.karuslabs + commons + ${project.version} + compile + + + + com.karuslabs + typist + ${project.version} + provided + + + + org.spigotmc + spigot-api + 1.17-R0.1-SNAPSHOT + provided + + + + + + + maven-compiler-plugin + 3.8.1 + + ${release} + + + + + maven-shade-plugin + 3.3.0-SNAPSHOT + + true + true + + + + + + + + shade + + + + + + + + \ No newline at end of file diff --git a/typist-example-plugin/src/main/java/com/karuslabs/typist/example/plugin/ExamplePlugin.java b/typist-example-plugin/src/main/java/com/karuslabs/typist/example/plugin/ExamplePlugin.java new file mode 100644 index 000000000..ac8298992 --- /dev/null +++ b/typist-example-plugin/src/main/java/com/karuslabs/typist/example/plugin/ExamplePlugin.java @@ -0,0 +1,40 @@ +/* + * The MIT License + * + * Copyright 2021 Karus Labs. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.karuslabs.typist.example.plugin; + +import com.karuslabs.commons.command.dispatcher.Dispatcher; + +import com.karuslabs.example.plugin.commands.Commands; + +import org.bukkit.plugin.java.JavaPlugin; + +public class ExamplePlugin extends JavaPlugin { + + @Override + public void onEnable() { + Dispatcher dispatcher = Dispatcher.of(this); + dispatcher.register(Commands.of(new TellCommand())); + } + +} diff --git a/typist-example-plugin/src/main/java/com/karuslabs/typist/example/plugin/TellCommand.java b/typist-example-plugin/src/main/java/com/karuslabs/typist/example/plugin/TellCommand.java new file mode 100644 index 000000000..c58b2fddc --- /dev/null +++ b/typist-example-plugin/src/main/java/com/karuslabs/typist/example/plugin/TellCommand.java @@ -0,0 +1,68 @@ +/* + * The MIT License + * + * Copyright 2021 Karus Labs. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.karuslabs.typist.example.plugin; + +import com.karuslabs.commons.command.OptionalContext; +import com.karuslabs.commons.command.types.PlayersType; + +import com.karuslabs.typist.annotations.Bind; +import com.karuslabs.typist.annotations.Command; +import com.karuslabs.typist.annotations.Let; +import com.karuslabs.typist.annotations.Pack; + +import com.mojang.brigadier.arguments.ArgumentType; +import com.mojang.brigadier.arguments.StringArgumentType; +import com.mojang.brigadier.context.CommandContext; +import com.mojang.brigadier.suggestion.SuggestionProvider; + +import java.util.List; + +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +// Denotes the package of the generated command creator class. By default, it will +// always be in the same package as the class annotated with @Pack. +@Pack(age = "com.karuslabs.example.plugin.commands") +@Command({"typist:tell|typist:t "}) // We prefixed the command to avoid a name clash with vanilla Minecraft's tell command +public class TellCommand { + + public static final @Bind(pattern = {""}) ArgumentType message = StringArgumentType.string(); + public static final @Bind(pattern = {""}) SuggestionProvider suggestions = (context, builder) -> builder.suggest("\"Hello World!\"").buildFuture(); + public final @Bind(pattern = {""}) PlayersType players = new PlayersType(); + + // Binds a Command to tell + @Bind("typist:tell") + public static int tell(CommandContext context) { + context.getSource().sendMessage("Hello darkness my old friend"); + return 1; + } + + + // Binds a Execution to + @Bind(pattern = {""}) + public void send(CommandSender source, OptionalContext context, @Let("") List recipents, @Let String message) { + recipents.forEach(p -> p.sendMessage(source.getName() + " says: " + message)); + } + +} \ No newline at end of file diff --git a/typist-example-plugin/src/main/resources/plugin.yml b/typist-example-plugin/src/main/resources/plugin.yml new file mode 100644 index 000000000..34f1774da --- /dev/null +++ b/typist-example-plugin/src/main/resources/plugin.yml @@ -0,0 +1,5 @@ +name: TypistExamplePlugin +main: com.karuslabs.typist.example.plugin.ExamplePlugin +description: A simple plugin that demostrates the usage of Typist +version: 1.0.0 +api-version: '1.17' \ No newline at end of file diff --git a/typist/pom.xml b/typist/pom.xml index 322cbef0d..dd3dcb1cc 100644 --- a/typist/pom.xml +++ b/typist/pom.xml @@ -119,23 +119,6 @@ - - \ No newline at end of file diff --git a/typist/src/main/java/com/karuslabs/typist/generation/chunks/Header.java b/typist/src/main/java/com/karuslabs/typist/generation/chunks/Header.java index 291b9abce..a26511834 100644 --- a/typist/src/main/java/com/karuslabs/typist/generation/chunks/Header.java +++ b/typist/src/main/java/com/karuslabs/typist/generation/chunks/Header.java @@ -46,6 +46,8 @@ public void emit(Source source, String pack) { } source.append(""" + // CHECKSTYLE:OFF + import com.karuslabs.commons.command.Execution; import com.karuslabs.commons.command.tree.nodes.*; diff --git a/typist/src/main/java/com/karuslabs/typist/generation/chunks/Lambda.java b/typist/src/main/java/com/karuslabs/typist/generation/chunks/Lambda.java index 84c787bd3..09932ee15 100644 --- a/typist/src/main/java/com/karuslabs/typist/generation/chunks/Lambda.java +++ b/typist/src/main/java/com/karuslabs/typist/generation/chunks/Lambda.java @@ -133,7 +133,11 @@ List desugar(Source source, Command command, Map ref var name = reference.value().identity().name() + counter[0]++; variables.add(name); - source.assign(name, "context.getArgument" + Source.arguments(quote(reference.value().identity().name()), reference.site().asType() + ".class")); + source.assign( + name, + "context.getArgument" + Source.arguments(quote(reference.value().identity().name()), + types.erasure(reference.site().asType()) + ".class") + ); } } diff --git a/typist/src/test/java/com/karuslabs/typist/generation/chunks/HeaderTest.java b/typist/src/test/java/com/karuslabs/typist/generation/chunks/HeaderTest.java index 439b435f3..4ac00b111 100644 --- a/typist/src/test/java/com/karuslabs/typist/generation/chunks/HeaderTest.java +++ b/typist/src/test/java/com/karuslabs/typist/generation/chunks/HeaderTest.java @@ -39,6 +39,8 @@ class HeaderTest { static final LocalDateTime TIME = LocalDateTime.now(); static final String HEADER = """ + // CHECKSTYLE:OFF + import com.karuslabs.commons.command.Execution; import com.karuslabs.commons.command.tree.nodes.*; diff --git a/typist/src/test/java/com/karuslabs/typist/generation/chunks/LambdaTest.java b/typist/src/test/java/com/karuslabs/typist/generation/chunks/LambdaTest.java index 56c6c9340..f5a349f6f 100644 --- a/typist/src/test/java/com/karuslabs/typist/generation/chunks/LambdaTest.java +++ b/typist/src/test/java/com/karuslabs/typist/generation/chunks/LambdaTest.java @@ -49,6 +49,7 @@ import com.karuslabs.typist.annotations.*; import com.mojang.brigadier.arguments.ArgumentType; import com.mojang.brigadier.context.CommandContext; +import java.util.*; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -57,7 +58,7 @@ class Lambdas { @Case("b") @Bind(pattern = "") - public static final ArgumentType players = null; + public static final ArgumentType> players = null; @Case("c") @Bind(pattern = "") public final ArgumentType count = null; @@ -76,7 +77,7 @@ public int instanceClosure(CommandContext context) { @Case("desugar_order") @Bind(pattern = "") - public int desugarOrder(@Case("desugar_b") @Let Player b, CommandContext context, @Case("desugar_c") @Let int c) { + public int desugarOrder(@Case("desugar_b") @Let List b, CommandContext context, @Case("desugar_c") @Let int c) { return 1; } @@ -165,7 +166,7 @@ void emit_desugared() { assertEquals(""" (context) -> { - var b0 = context.getArgument("b", org.bukkit.entity.Player.class); + var b0 = context.getArgument("b", java.util.List.class); var c1 = context.getArgument("c", int.class); return source.desugarOrder(b0, context, c1); }; @@ -210,7 +211,7 @@ void emit_instance_method_reference() { void match_throws_exception() { var closure = new Closure(types, counter, Map.of(), false); assertEquals( - "Unable to resolve parameter: org.bukkit.entity.Player b", + "Unable to resolve parameter: java.util.List b", assertThrows(IllegalStateException.class, () -> closure.match((VariableElement) cases.one("desugar_b"))).getMessage() ); }