Skip to content

Commit

Permalink
🎨 Refactored packages, improved readme, removed version property from…
Browse files Browse the repository at this point in the history
… annotations
  • Loading branch information
itsTyrion committed Mar 1, 2024
1 parent b588ed5 commit 2f7f067
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 189 deletions.
178 changes: 10 additions & 168 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,193 +1,35 @@
[![](https://jitpack.io/v/de.itsTyrion/PluginAnnotationProcessor.svg)](https://jitpack.io/#de.itsTyrion/PluginAnnotationProcessor)
# Plugin Annotation Processor

A Bukkit/Spigot or BungeeCord plugin that simplifies the generation of `plugin.yml` file using annotations.
A toolkit that simplifies the generation of Minecraft server plugin meta files using annotations.

## Overview

This tool provides a convenient way to generate the `plugin.yml` file for your Spigot/Paper/BungeeCord/Waterfall plugin.
It utilizes annotations to define essential information directly in your code.
This tool provides a convenient way to generate the `plugin.yml`/`bungee.yml`/`velocity-plugin.json` file for your plugin.
It utilizes annotations to define the required information directly in your code and fill in the version automatically.

## Prerequisites
- Java 8 or higher
- Bukkit/Spigot/Paper latest recommended but should work down to 1.7
- BungeeCord/Waterfall latest - it supports down to MC 1.8 as of 2023
- Bukkit/Spigot/Paper/Pufferfish/Purpur - latest version recommended but should down to 1.8 and further
- BungeeCord/Waterfall latest - it supports down to MC 1.8 as of 2024
- Velocity latest (but any 3.x should work) - it supports down to MC 1.8 as of 2024

## Features
- **Annotation-based Configuration**: Use the `@Plugin` and `@BungeePlugin` annotations to define plugin information directly in your Java/Kotlin code.
- **Cross-Compatibility**: Works with Bukkit, Spigot, Paper, BungeeCord, and Waterfall.
- **Annotation-based Configuration**: Use the `@BukkitPlugin`/`@BungeePlugin`/`@VelocityPlugin` annotations to define plugin information directly in your Java/Kotlin code.
- **Cross-Compatibility**: Works with Bukkit, Spigot, Paper, Pufferfish, Purpur, BungeeCord, Waterfall and Velocity.
- **Versioning**: Version number is derived from the Gradle/Maven project version. (Optional)

---
## Usage
Moved to [the wiki](https://github.com/itsTyrion/PluginAnnotationProcessor/wiki)

### Bukkit/Spigot/Paper
(This example does not show all properties)
```java
import org.bukkit.plugin.java.JavaPlugin;

import de.itsTyrion.pluginAnnotation.Plugin;

@Plugin(
name = "MyPlugin",
description = "Awesome Bukkit plugin",
authors = {"Your Name"},
depend = {"aDependency"}
)
public class MyPlugin extends JavaPlugin {
// Your plugin code here
}
```

### BungeeCord/Waterfall
```java
import net.md_5.bungee.api.plugin.Plugin;

import de.itsTyrion.pluginAnnotation.BungeePlugin;

@Plugin(
name = "MyPlugin",
description = "Awesome Bungee/Waterfall plugin",
author = "Your Name",
depends = {"aDependency"}
)
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
repositories {
// Add your other repositories here
maven { url 'https://jitpack.io' }
}
```
---
If you want to use the Spigot Library Loader (plugin.yml `libraries` section), create a new dependency config like so:
(The name is up to you, I used `spigotLib` here)
**Keep in mind it can only load from Maven Central!**
```groovy
configurations {
spigotLib
compileOnly { extendsFrom spigotLib }
}
// Change the dependency scope from `implementation`/`compileOnly` to the config name of your choice. Example:
spigotLib 'com.google.inject:guice:7.0.0' // example dependency
```
Then, pass the list to the compiler/annotation processor according to the sections below.

---
#### For Java sources:
```groovy
dependencies {
// Add your other dependencies here
compileOnly 'de.itsTyrion:PluginAnnotationProcessor:1.1'
annotationProcessor 'de.itsTyrion:PluginAnnotationProcessor:1.1'
}
tasks.withType(JavaCompile).configureEach {
// Add other annotation processor arguments as/if needed or use different values, this is just what I use.
options.compilerArgs +=
"-AmcPluginVersion=${"$version".contains("SNAPSHOT") ? (version + new Date().format('yyyyMMdd_HHmm')) : version}"
// This is only needed if you use the Spigot Library Loader. If you use a different name, replace `spigotLib` with it.
options.compilerArgs +=
"-AspigotLibraries=${configurations.spigotLib.dependencies.collect { "$it.group:$it.name:$it.version" }.join(' ')}"
}
```


#### For Kotlin sources:
```groovy
plugins {
// Add your other gradle plugins here
id 'org.jetbrains.kotlin.kapt' version 'current.kotlin.version'
}
dependencies {
// Add your other dependencies here
compileOnly 'de.itsTyrion:PluginAnnotationProcessor:1.1'
kapt 'de.itsTyrion:PluginAnnotationProcessor:1.1'
}
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
// This is only needed if you use the Spigot Library Loader. If you use a different name, replace `spigotLib` with it.
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>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
```
#### For Java sources:
```xml
<dependency>
<groupId>de.itsTyrion</groupId>
<artifactId>PluginAnnotationProcessor</artifactId>
<version>1.1</version>
</dependency>
```
```xml
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<source>17</source>
<target>17</target>
<!-- Pass the project version as an argument to the compiler -->
<compilerArgs>
<arg>-Aproject.version=${project.version}</arg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>
```
#### For Kotlin sources (I recommend you use Gradle):
(Copied from [the docs](https://kotlinlang.org/docs/kapt.html#use-in-maven))
Add an execution of the kapt goal from kotlin-maven-plugin before compile:
```xml
<execution>
<id>kapt</id>
<goals>
<goal>kapt</goal> <!-- You can skip the <goals> element
if you enable extensions for the plugin -->
</goals>
<configuration>
<sourceDirs>
<sourceDir>src/main/kotlin</sourceDir>
<sourceDir>src/main/java</sourceDir>
</sourceDirs>
<annotationProcessorPaths>
<annotationProcessorPath>
<groupId>de.itsTyrion</groupId>
<artifactId>PluginAnnotationProcessor</artifactId>
<version>1.1</version>
</annotationProcessorPath>
</annotationProcessorPaths>
</configuration>
</execution>
```

## Contributing
Feel free to contribute to the development of this plugin by opening issues or submitting pull requests.

## API Reference
Just add it to the plugin's main class
Parameter names match plugin.yml/bungee.yml
Parameter names match plugin.yml/bungee.yml and Velocity's own `@Plugin` annotation

## License
This project is licensed under the MIT License.
2 changes: 1 addition & 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.3'
version '1.4'

dependencies {
implementation 'jakarta.annotation:jakarta.annotation-api:2.1.1'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package de.itsTyrion.pluginAnnotation;

import de.itsTyrion.pluginAnnotation.bukkit.BukkitPlugin;
import de.itsTyrion.pluginAnnotation.bukkit.CommandInfo;
import de.itsTyrion.pluginAnnotation.bungee.BungeePlugin;
import de.itsTyrion.pluginAnnotation.util.Generator;
import de.itsTyrion.pluginAnnotation.velocity.VelocityPlugin;
import lombok.val;
Expand All @@ -10,7 +13,6 @@
import javax.annotation.processing.SupportedOptions;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.TypeElement;
import javax.tools.Diagnostic;
import javax.tools.Diagnostic.Kind;
import javax.tools.StandardLocation;
import java.io.IOException;
Expand All @@ -19,9 +21,10 @@

@SupportedOptions(value = {"mcPluginVersion", "spigotLibraries"})
@SupportedAnnotationTypes({
"de.itsTyrion.pluginAnnotation.Plugin",
"de.itsTyrion.pluginAnnotation.BungeePlugin",
"de.itsTyrion.pluginAnnotation.velocity.VelocityPlugin"})
"de.itsTyrion.pluginAnnotation.bukkit.BukkitPlugin",
"de.itsTyrion.pluginAnnotation.bungee.BungeePlugin",
"de.itsTyrion.pluginAnnotation.velocity.VelocityPlugin"
})
public class PluginAnnotationProcessor extends AbstractProcessor {

private String pluginMainClassFound = null;
Expand All @@ -40,7 +43,7 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
return false;
}

for (val element : roundEnv.getElementsAnnotatedWith(Plugin.class)) {
for (val element : roundEnv.getElementsAnnotatedWith(BukkitPlugin.class)) {
// fully qualified name, required for plugin.yml `main` property
val fqName = ((TypeElement) element).getQualifiedName().toString();

Expand All @@ -58,7 +61,7 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
val commandInfos = roundEnv.getElementsAnnotatedWith(CommandInfo.class).stream()
.map(element1 -> element1.getAnnotation(CommandInfo.class)).toArray(CommandInfo[]::new);

val pluginAnnotation = element.getAnnotation(Plugin.class);
val pluginAnnotation = element.getAnnotation(BukkitPlugin.class);
val content = Generator.pluginYML(pluginAnnotation, fqName, projectVersion, libraries, commandInfos);
writeResource("plugin.yml", content, fqName);
}
Expand Down Expand Up @@ -92,7 +95,7 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment

val plugin = element.getAnnotation(VelocityPlugin.class);
if (!Generator.VELOCITY_ID_PATTERN.matcher(plugin.id()).matches()) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR,
processingEnv.getMessager().printMessage(Kind.ERROR,
"Invalid ID for plugin " + fqName + ". IDs must start alphabetically," +
"have lowercase alphanumeric characters, and can contain dashes or underscores.");
return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package de.itsTyrion.pluginAnnotation;
package de.itsTyrion.pluginAnnotation.bukkit;

import java.lang.annotation.ElementType;
import java.lang.annotation.Target;

@Target(ElementType.TYPE)
public @interface Plugin {
public @interface BukkitPlugin {
String name();
String[] depend() default {};
String description() default "";
Expand All @@ -16,7 +16,6 @@
String[] loadBefore() default {};
String logPrefix() default "";
String[] provides() default {};
String version() default "%mcPluginVersion%";
LoadAt load() default LoadAt.POSTWORLD;

@SuppressWarnings("unused")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package de.itsTyrion.pluginAnnotation;
package de.itsTyrion.pluginAnnotation.bukkit;

public @interface CommandInfo {
String name();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package de.itsTyrion.pluginAnnotation;
package de.itsTyrion.pluginAnnotation.bungee;

import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
Expand All @@ -10,5 +10,4 @@
String description() default "";
String author() default "";
String[] softDepends() default {};
String version() default "%mcPluginVersion%";
}
12 changes: 6 additions & 6 deletions src/main/java/de/itsTyrion/pluginAnnotation/util/Generator.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package de.itsTyrion.pluginAnnotation.util;

import com.grack.nanojson.JsonWriter;
import de.itsTyrion.pluginAnnotation.BungeePlugin;
import de.itsTyrion.pluginAnnotation.CommandInfo;
import de.itsTyrion.pluginAnnotation.Plugin;
import de.itsTyrion.pluginAnnotation.bungee.BungeePlugin;
import de.itsTyrion.pluginAnnotation.bukkit.CommandInfo;
import de.itsTyrion.pluginAnnotation.bukkit.BukkitPlugin;
import de.itsTyrion.pluginAnnotation.velocity.VelocityPlugin;
import lombok.val;

Expand All @@ -16,11 +16,11 @@ public final class Generator {
private Generator() {}


public static String pluginYML(Plugin plugin, String fqName, String version, String[] libraries,
public static String pluginYML(BukkitPlugin plugin, String fqName, String version, String[] libraries,
CommandInfo[] commands) {
val builder = new StringBuilder()
.append("name: ").append(plugin.name()).append('\n')
.append("version: ").append(plugin.version().replace("%mcPluginVersion%", version)).append('\n')
.append("version: ").append(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 Down Expand Up @@ -63,7 +63,7 @@ public static String pluginYML(Plugin plugin, String fqName, String version, Str
public static String bungeeYML(BungeePlugin plugin, String fqName, String version) {
val builder = new StringBuilder()
.append("name: ").append(plugin.name()).append('\n')
.append("version: ").append(plugin.version().replace("%mcPluginVersion%", version)).append('\n')
.append("version: ").append(version).append('\n')
.append("main: ").append(fqName).append('\n');

appendIfPresent(builder, "depends", plugin.depends());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
package de.itsTyrion.pluginAnnotation.velocity;

import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
import java.lang.annotation.Target;

@Target(ElementType.TYPE)
public @interface VelocityPlugin {
Expand Down

0 comments on commit 2f7f067

Please sign in to comment.