Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implements Feature/addons #91

Merged
merged 12 commits into from
May 1, 2024
Merged
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,6 @@ bin/
.vscode/

### Mac OS ###
.DS_Store
.DS_Store

maven/
88 changes: 80 additions & 8 deletions NookureStaff-API/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,30 +1,102 @@
plugins {
id("net.kyori.blossom") version "2.1.0"
alias(libs.plugins.grgit)
alias(libs.plugins.grgitPublish)
id("java")
id("java-library")
id("maven-publish")
}

java {
withJavadocJar()
withSourcesJar()

sourceSets["main"].java {
srcDir("src/ap/java")
}
}

dependencies {
testImplementation(platform("org.junit:junit-bom:5.9.1"))
testImplementation("org.junit.jupiter:junit-jupiter")
compileOnly(libs.paperApi)
compileOnly(libs.guava)
compileOnly(libs.apacheCommons)
compileOnly(libs.configurateYaml)
compileOnly(libs.jedis)
compileOnly(libs.adventureApi)
compileOnly(libs.miniMessage)
compileOnlyApi(libs.paperApi)
compileOnlyApi(libs.guava)
compileOnlyApi(libs.guice)
compileOnlyApi(libs.apacheCommons)
implementation(libs.configurateYaml)
compileOnlyApi(libs.jedis)
compileOnlyApi(libs.adventureApi)
compileOnlyApi(libs.miniMessage)
compileOnlyApi(libs.reflections)
compileOnlyApi(rootProject.libs.storm)
compileOnlyApi(rootProject.libs.adventureApi)
compileOnlyApi(rootProject.libs.miniMessage)
compileOnlyApi(rootProject.libs.caffeine)
compileOnlyApi(rootProject.libs.liblyBukkit)

compileOnly(libs.auto.service.annotations)
annotationProcessor(libs.auto.service)
}

publishing {
repositories {
maven {
name = "local"
url = uri("${rootProject.rootDir}/maven")
}
}

publications {
create<MavenPublication>("maven") {
groupId = "com.nookure.staff"
artifactId = "NookureStaff-API"
version = "${rootProject.version}"
from(components["java"])
}
}
}

gitPublish {
repoUri = "https://github.com/Nookure/maven.git"
branch = "main"
fetchDepth = null
commitMessage = "NookureStaff ${rootProject.version}"

contents {
from("${rootProject.rootDir}/maven")
}

preserve {
include("**")
}
}

tasks.test {
useJUnitPlatform()
}

tasks {
withType<Javadoc> {
val o = options as StandardJavadocDocletOptions
o.encoding = "UTF-8"
o.source = "17"

o.links(
"https://guava.dev/releases/${libs.guava.get().version}/api/docs/",
"https://google.github.io/guice/api-docs/${libs.guice.get().version}/javadoc/",
"https://docs.oracle.com/en/java/javase/17/docs/api/",
"https://jd.advntr.dev/api/${libs.adventureApi.get().version}/",
"https://javadoc.io/doc/com.github.ben-manes.caffeine/caffeine"
)
}
}

sourceSets {
main {
blossom {
javaSources {
property("version", rootProject.version.toString())
property("branch", grgit.branch.current().name)
property("branch", grgit.branch.current().name)
property("commit", grgit.head().abbreviatedId)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/**
* This piece of code is a processor that processes the Addon annotation
* and generates the necessary data for the addon to be loaded.
* <p>
* This code is inspired by the Velocity Plugin Annotations Processor
* </p>
*/
package com.nookure.staff.ap.addon;

import com.google.auto.service.AutoService;
import com.nookure.staff.api.addons.annotations.Addon;

import javax.annotation.processing.*;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.Name;
import javax.lang.model.element.TypeElement;
import javax.tools.Diagnostic;
import javax.tools.FileObject;
import javax.tools.StandardLocation;
import java.io.IOException;
import java.io.Writer;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;

@AutoService(Processor.class)
@SupportedAnnotationTypes({"com.nookure.staff.api.addons.annotations.Addon"})
public class AddonAnnotationProcessor extends AbstractProcessor {
private String pluginClassFound;
private boolean warnedAboutMultiplePlugins;

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
if (roundEnv.processingOver()) {
return false;
}

for (Element element : roundEnv.getElementsAnnotatedWith(Addon.class)) {
if (element.getKind() != ElementKind.CLASS) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Only classes can be annotated with @Addon", element);
return false;
}

Name qualifiedName = ((TypeElement) element).getQualifiedName();

if (Objects.equals(pluginClassFound, qualifiedName.toString())) {
if (!warnedAboutMultiplePlugins) {
processingEnv.getMessager()
.printMessage(Diagnostic.Kind.WARNING, "NookureStaff does not yet currently support "
+ "multiple addons. We are using " + pluginClassFound
+ " for your addon's main class.");
warnedAboutMultiplePlugins = true;
}
return false;
}

Addon addon = element.getAnnotation(Addon.class);

if (addon == null) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Addon annotation is null", element);
return false;
}

pluginClassFound = qualifiedName.toString();
try {
FileObject resource = processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT, "", "addon.properties", element);

try (Writer writer = resource.openWriter()) {
writer.write(String.format("""
main-class=%s
uuid=%s
""", pluginClassFound, UUID.randomUUID()));
}

} catch (IOException e) {
throw new RuntimeException(e);
}
}

return false;
}

@Override
public SourceVersion getSupportedSourceVersion() {
return SourceVersion.latestSupported();
}
}
10 changes: 10 additions & 0 deletions NookureStaff-API/src/main/java/com/nookure/staff/api/Logger.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ default void severe(String message) {
severe(Component.text(message).color(NamedTextColor.RED));
}

/**
* Log a severe error to the console
*
* @param throwable The throwable to log
*/
default void severe(Throwable throwable) {
severe(throwable.getMessage());
throwable.printStackTrace(System.err);
}

/**
* Log a debug message to the console
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.nookure.staff.api;

import com.google.inject.Injector;
import com.nookure.staff.api.command.Command;

import java.io.File;
import java.io.InputStream;
Expand All @@ -21,4 +22,8 @@ public interface NookureStaff {
Injector getInjector();

String getPrefix();

default void registerCommand(Command command) {}

default void unregisterCommand(Command command) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.nookure.staff.api.addons;

public interface AddonActions {
default void onEnable() {
// Need to be implemented
}

default void onDisable() {
// Need to be implemented
}

default void onReload() {
// Need to be implemented
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.nookure.staff.api.addons;

import com.nookure.staff.api.addons.annotations.Addon;

public interface AddonContainer {
AddonDescription getDescription();

Object getInstance();

AddonStatus getStatus();

void setStatus(AddonStatus status);

void setInstance(Object instance);

Addon getAddon();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.nookure.staff.api.addons;

import com.nookure.staff.api.addons.annotations.Addon;

import java.util.Optional;

public class AddonDescription {
private final String id;
private final String version;
private final String description;
private final Object main;
private final Addon.AddonPlatform platform;
private final Addon addon;

public AddonDescription(Addon addon, Object main) {
this.addon = addon;
this.id = addon.name();
this.version = addon.version();
this.description = addon.description();
this.main = main;
this.platform = addon.platform();
}

/**
* Gets the addon id
* @return The addon id
*/
public String getID() {
return id;
}

/**
* Gets the addon version
* @return addon version
*/
public String getVersion() {
return version;
}

/**
* Gets the addon description
* @return addon description {@link Optional}
*/
public Optional<String> getDescription() {
return Optional.ofNullable(description);
}

/**
* Gets the addon main class
* @return addon main class
*/
public Object getMain() {
return main;
}

/**
* Gets the addon platform
* @return addon platform
*/
public Addon.AddonPlatform getPlatform() {
return platform;
}

/**
* Gets the addon annotation
* @return addon annotation
*/
public Addon getAddon() {
return addon;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.nookure.staff.api.addons;

import com.nookure.staff.api.addons.annotations.Addon;

public class AddonDescriptionBuilder {
private Addon addon;
private Object main;
public AddonDescriptionBuilder setAddon(Addon addon) {
this.addon = addon;
return this;
}

public AddonDescriptionBuilder setMain(Object main) {
this.main = main;
return this;
}

public AddonDescription build() {
return new AddonDescription(addon, main);
}

}
Loading
Loading