Skip to content

Commit

Permalink
✨ added support for the libraries property to load libs on Spigot-b…
Browse files Browse the repository at this point in the history
…ased servers and changed placeholder names
  • Loading branch information
itsTyrion committed Nov 28, 2023
1 parent 47dae45 commit 33ae55d
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 28 deletions.
37 changes: 23 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ public class MyBungeePlugin extends Plugin {
// Your plugin code here
}
```

### Custom version/version format (all platforms):
Set the `version` parameter to either something completely different (why?) or e.g. `"aPrefix-%mcPluginVersion%-aSuffix"`
## Gradle
#### Add the Jitpack repo if you haven't already:
```groovy
Expand All @@ -69,14 +70,22 @@ repositories {
```groovy
dependencies {
// Add your other dependencies here
compileOnly 'de.itsTyrion:PluginAnnotationProcessor:1.0'
annotationProcessor 'de.itsTyrion:PluginAnnotationProcessor:1.0'
compileOnly 'de.itsTyrion:PluginAnnotationProcessor:1.1'
annotationProcessor 'de.itsTyrion:PluginAnnotationProcessor:1.1'
}
tasks.withType(JavaCompile).configureEach {
// Add your other options here
options.compilerArgs += ('-Aproject.version=' + project.version)
// Add other annotation processor arguments as/if needed or use different values, this is just what I use.
def versionString = version.contains("SNAPSHOT") ? (version + new Date().format('yyyyMMdd_HHmm')) : version
options.compilerArgs += ('-Aproject.version=' + versionString)
}
// If you want to use the Spigot Library Loader (`libraries` section), add this and change `compileOnly` to `spigotLib`.
// Keep in mind it can only load from Maven Central!
configurations {
spigotLib
compileOnly { extendsFrom spigotLib }
}
options.compilerArgs += '-AspigotLibraries=' + configurations.spigotLib.dependencies.collect { "$it.group:$it.name:$it.version" }
```
#### For Kotlin sources:
```groovy
Expand All @@ -87,19 +96,19 @@ plugins {
dependencies {
// Add your other dependencies here
compileOnly 'de.itsTyrion:PluginAnnotationProcessor:1.0'
kapt 'de.itsTyrion:PluginAnnotationProcessor:1.0'
compileOnly 'de.itsTyrion:PluginAnnotationProcessor:1.1'
kapt 'de.itsTyrion:PluginAnnotationProcessor:1.1'
}
kapt {
arguments {
// Add other annotation processor arguments if needed
arg('project.version', project.version)
}
kapt.arguments {
// Add other annotation processor arguments as/if needed or use different values, this is just what I use.
arg 'mcPluginVersion', version.contains("SNAPSHOT") ? (version + new Date().format('yyyyMMdd_HHmm')) : version
arg 'spigotLibraries', configurations.spigotLib.dependencies.collect { "$it.group:$it.name:$it.version" }.join(';')
}
```

## Maven
Hint: I have no idea how to do the `libraries` part with maven, tips or a PR are welcome.
#### Add the Jitpack repo if you haven't already:
```xml
<repository>
Expand All @@ -112,7 +121,7 @@ kapt {
<dependency>
<groupId>de.itsTyrion</groupId>
<artifactId>PluginAnnotationProcessor</artifactId>
<version>1.0</version>
<version>1.1</version>
</dependency>
```
```xml
Expand Down Expand Up @@ -153,7 +162,7 @@ if you enable extensions for the plugin -->
<annotationProcessorPath>
<groupId>de.itsTyrion</groupId>
<artifactId>PluginAnnotationProcessor</artifactId>
<version>1.0</version>
<version>1.1</version>
</annotationProcessorPath>
</annotationProcessorPaths>
</configuration>
Expand Down
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ repositories {
}

group 'de.itsTyrion'
version '1.0'
version '1.1'

dependencies {
implementation 'javax.annotation:javax.annotation-api:1.3.2'
Expand All @@ -29,6 +29,7 @@ jar {

publishing {
publications {
//noinspection GroovyAssignabilityCheck
mavenJava(MavenPublication) {
from components.java
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

public @interface BungeePlugin {
String name();
String version() default "{project.version}";
String[] depends() default {};
String description() default "";
String author() default "";
String[] softDepends() default {};
String version() default "%mcPluginVersion%";
}
3 changes: 2 additions & 1 deletion src/main/java/de/itsTyrion/pluginAnnotation/Plugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

public @interface Plugin {
String name();
String version() default "{project.version}";
String[] depend() default {};
String description() default "";
String apiVersion() default "1.20";
Expand All @@ -13,7 +12,9 @@
String[] loadBefore() default {};
String logPrefix() default "";
String[] provides() default {};
String version() default "%mcPluginVersion%";
LoadAt load() default LoadAt.POSTWORLD;

@SuppressWarnings("unused")
enum LoadAt {STARTUP, POSTWORLD}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,18 @@
import java.util.Arrays;
import java.util.Set;

@SupportedOptions(value = "project.version")
@SupportedAnnotationTypes({"de.itsTyrion.pluginAnnotation.Plugin", "de.itsTyrion.pluginAnnotation.BungeePlugin"})
@SuppressWarnings("Since15") // This is only about the SupportedSourceVersion annotation
@SupportedSourceVersion(SourceVersion.RELEASE_17)
@SupportedOptions(value = {"mcPluginVersion", "spigotLibraries"})
@SupportedAnnotationTypes({"de.itsTyrion.pluginAnnotation.Plugin", "de.itsTyrion.pluginAnnotation.BungeePlugin"})
public class PluginAnnotationProcessor extends AbstractProcessor {

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
// Retrieve the project version from the processor options, required for plugin.yml `version` property
val projectVersion = processingEnv.getOptions().get("project.version");
val projectVersion = processingEnv.getOptions().get("mcPluginVersion");
if (projectVersion == null) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "project.version unset!");
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "`mcPluginVersion` unset!");
return false;
}

Expand All @@ -33,8 +34,12 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
// fully qualified name, required for plugin.yml `main` property
val fullyQualifiedName = ((TypeElement) element).getQualifiedName().toString();

val pluginYmlContent = generatePluginYmlContent(pluginAnnotation, fullyQualifiedName, projectVersion);
writeYml("plugin.yml", pluginYmlContent, fullyQualifiedName);
// `libraries` section of plugin.yml, used by Spigot-based servers to DL dependencies as needed
val librariesString = processingEnv.getOptions().get("spigotLibraries");
val libraries = librariesString != null ? librariesString.split(";") : new String[0];

val content = generatePluginYmlContent(pluginAnnotation, fullyQualifiedName, projectVersion, libraries);
writeYml("plugin.yml", content, fullyQualifiedName);
}
}
for (val element : roundEnv.getElementsAnnotatedWith(BungeePlugin.class)) {
Expand All @@ -44,8 +49,8 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
// fully qualified name, required for bungee.yml `main` property
val fullyQualifiedName = ((TypeElement) element).getQualifiedName().toString();

val pluginYmlContent = generateBungeeYmlContent(pluginAnnotation, fullyQualifiedName, projectVersion);
writeYml("bungee.yml", pluginYmlContent, fullyQualifiedName);
val content = generateBungeeYmlContent(pluginAnnotation, fullyQualifiedName, projectVersion);
writeYml("bungee.yml", content, fullyQualifiedName);
}
}

Expand All @@ -65,10 +70,10 @@ private void writeYml(String name, String content, String fqName) {
}
}

private String generatePluginYmlContent(Plugin plugin, String fqName, String version) {
private String generatePluginYmlContent(Plugin plugin, String fqName, String version, String[] libraries) {
val builder = new StringBuilder()
.append("name: ").append(plugin.name()).append('\n')
.append("version: ").append(plugin.version().replace("{project.version}", version)).append('\n')
.append("version: ").append(plugin.version().replace("%mcPluginVersion%", version)).append('\n')
.append("main: ").append(fqName).append('\n')
.append("api-version: ").append(plugin.apiVersion()).append('\n')
.append("load: ").append(plugin.load().name()).append('\n');
Expand All @@ -79,6 +84,7 @@ private String generatePluginYmlContent(Plugin plugin, String fqName, String ver
appendIfPresent(builder, "loadbefore", plugin.loadBefore());
appendIfPresent(builder, "provides", plugin.provides());
appendIfPresent(builder, "softdepend", plugin.softDepend());
appendIfPresent(builder, "libraries", libraries);

appendIfPresent(builder, "website", plugin.website());
appendIfPresent(builder, "description", plugin.description());
Expand All @@ -90,7 +96,7 @@ private String generatePluginYmlContent(Plugin plugin, String fqName, String ver
private String generateBungeeYmlContent(BungeePlugin plugin, String fqName, String version) {
val builder = new StringBuilder()
.append("name: ").append(plugin.name()).append('\n')
.append("version: ").append(plugin.version().replace("{project.version}", version)).append('\n')
.append("version: ").append(plugin.version().replace("%mcPluginVersion%", version)).append('\n')
.append("main: ").append(fqName).append('\n');

appendIfPresent(builder, "depends", plugin.depends());
Expand Down

0 comments on commit 33ae55d

Please sign in to comment.