Skip to content

Commit

Permalink
Multi-project example
Browse files Browse the repository at this point in the history
  • Loading branch information
jpenilla committed Jan 12, 2025
1 parent ffb39ce commit d7b8ab9
Show file tree
Hide file tree
Showing 17 changed files with 233 additions and 185 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ buildNumber.properties

# Common working directory
run/
run1_19_4/
run1_17_1/
build
.gradle
/*.jar
85 changes: 44 additions & 41 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,61 +1,64 @@
import xyz.jpenilla.resourcefactory.bukkit.BukkitPluginYaml
import xyz.jpenilla.runpaper.task.RunServer

plugins {
`java-library`
id("io.papermc.paperweight.userdev") version "2.0.0-beta.13"
id("xyz.jpenilla.run-paper") version "2.3.1" // Adds runServer and runMojangMappedServer tasks for testing
`my-conventions`
id("io.papermc.paperweight.userdev") version "2.0.0-beta.13" apply false
id("xyz.jpenilla.run-paper") version "2.3.1" // Adds runServer task for testing
id("xyz.jpenilla.resource-factory-bukkit-convention") version "1.2.0" // Generates plugin.yml based on the Gradle config
id("com.gradleup.shadow") version "8.3.5"
}

group = "io.papermc.paperweight"
version = "1.0.0-SNAPSHOT"
description = "Test plugin for paperweight-userdev"
java.disableAutoTargetJvm() // Allow consuming JVM 21 projects (i.e. paper_1_21_4) even though our release is 17

java {
// Configure the java toolchain. This allows gradle to auto-provision JDK 21 on systems that only have JDK 11 installed for example.
toolchain.languageVersion = JavaLanguageVersion.of(21)
}
dependencies {
compileOnly("io.papermc.paper:paper-api:1.17.1-R0.1-SNAPSHOT")

implementation(project(":paper_hooks"))

// For 1.20.4 or below, or when you care about supporting Spigot on >=1.20.5:
/*
paperweight.reobfArtifactConfiguration = io.papermc.paperweight.userdev.ReobfArtifactConfiguration.REOBF_PRODUCTION
// Shade the reobf variant
runtimeOnly(project(":paper_1_17_1", configuration = "reobf"))
runtimeOnly(project(":paper_1_19_4", configuration = "reobf"))

// For Paper 1.20.5+, we don't need to use the reobf variant.
// If you still support spigot, you will need to use the reobf variant,
// and remove the Mojang-mapped metadata from the manifest below.
runtimeOnly(project(":paper_1_21_4"))
}

tasks.assemble {
dependsOn(tasks.reobfJar)
dependsOn(tasks.shadowJar)
}
*/

dependencies {
paperweight.paperDevBundle("1.21.4-R0.1-SNAPSHOT")
// paperweight.foliaDevBundle("1.21.4-R0.1-SNAPSHOT")
// paperweight.devBundle("com.example.paperfork", "1.21.4-R0.1-SNAPSHOT")
}

tasks {
compileJava {
// Set the release flag. This configures what version bytecode the compiler will emit, as well as what JDK APIs are usable.
// See https://openjdk.java.net/jeps/247 for more information.
options.release = 21
}
javadoc {
options.encoding = Charsets.UTF_8.name() // We want UTF-8 for everything
}

// Only relevant for 1.20.4 or below, or when you care about supporting Spigot on >=1.20.5:
/*
reobfJar {
// This is an example of how you might change the output location for reobfJar. It's recommended not to do this
// for a variety of reasons, however it's asked frequently enough that an example of how to do it is included here.
outputJar = layout.buildDirectory.file("libs/PaperweightTestPlugin-${project.version}.jar")
}
*/
tasks.jar {
manifest.attributes(
"paperweight-mappings-namespace" to "mojang",
)
}

// Configure plugin.yml generation
// - name, version, and description are inherited from the Gradle project.
bukkitPluginYaml {
main = "io.papermc.paperweight.testplugin.TestPlugin"
main = "my.plugin.MyPlugin"
load = BukkitPluginYaml.PluginLoadOrder.STARTUP
authors.add("Author")
apiVersion = "1.21.4"
apiVersion = "1.17"
}

tasks.runServer {
minecraftVersion("1.21.4")
}

tasks.register("run1_17_1", RunServer::class) {
minecraftVersion("1.17.1")
pluginJars.from(tasks.shadowJar.flatMap { it.archiveFile })
runDirectory = layout.projectDirectory.dir("run1_17_1")
systemProperties["Paper.IgnoreJavaVersion"] = true
}

tasks.register("run1_19_4", RunServer::class) {
minecraftVersion("1.19.4")
pluginJars.from(tasks.shadowJar.flatMap { it.archiveFile })
runDirectory = layout.projectDirectory.dir("run1_19_4")
systemProperties["Paper.IgnoreJavaVersion"] = true
}
7 changes: 7 additions & 0 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
plugins {
`kotlin-dsl`
}

repositories {
gradlePluginPortal()
}
28 changes: 28 additions & 0 deletions buildSrc/src/main/kotlin/my-conventions.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import org.gradle.jvm.toolchain.JavaLanguageVersion
import org.gradle.kotlin.dsl.maven
import org.gradle.kotlin.dsl.repositories

plugins {
`java-library`
}

java {
// Configure the java toolchain. This allows gradle to auto-provision JDK 21 on systems that only have JDK 17 installed for example.
// If you need to compile to for example JVM 8 or 17 bytecode, adjust the 'release' option below and keep the toolchain at 21.
toolchain.languageVersion = JavaLanguageVersion.of(21)
}

repositories {
maven("https://repo.papermc.io/repository/maven-public/")
}

tasks {
withType<JavaCompile>().configureEach {
// Set the release flag. This configures what version bytecode the compiler will emit, as well as what JDK APIs are usable.
// See https://openjdk.java.net/jeps/247 for more information.
options.release = 17
}
withType<Javadoc>().configureEach {
options.encoding = Charsets.UTF_8.name() // We want UTF-8 for everything
}
}
6 changes: 6 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# Project meta
group = io.papermc.paperweight
version = 1.0.0-SNAPSHOT
description = Test plugin for paperweight-userdev

# Gradle configuration
org.gradle.parallel=true
org.gradle.caching=true
org.gradle.configuration-cache=true
10 changes: 10 additions & 0 deletions paper_1_17_1/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
plugins {
`my-conventions`
id("io.papermc.paperweight.userdev")
}

dependencies {
implementation(project(":paper_hooks"))

paperweight.paperDevBundle("1.17.1-R0.1-SNAPSHOT")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package my.plugin.hooks.paper_1_17_1;

import my.plugin.hooks.PaperHooks;
import net.minecraft.SharedConstants;
import org.slf4j.Logger;

public class PaperHooks1_17_1 implements PaperHooks {
@Override
public void doSomething(final Logger logger) {
logger.info(SharedConstants.getCurrentVersion().getName());
}
}
10 changes: 10 additions & 0 deletions paper_1_19_4/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
plugins {
`my-conventions`
id("io.papermc.paperweight.userdev")
}

dependencies {
implementation(project(":paper_hooks"))

paperweight.paperDevBundle("1.19.4-R0.1-SNAPSHOT")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package my.plugin.hooks.paper_1_19_4;

import my.plugin.hooks.PaperHooks;
import net.minecraft.SharedConstants;
import org.slf4j.Logger;

public class PaperHooks1_19_4 implements PaperHooks {
@Override
public void doSomething(final Logger logger) {
logger.info(SharedConstants.getCurrentVersion().getName());
}
}
17 changes: 17 additions & 0 deletions paper_1_21_4/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
plugins {
`my-conventions`
id("io.papermc.paperweight.userdev")
}

dependencies {
implementation(project(":paper_hooks"))

paperweight.paperDevBundle("1.21.4-R0.1-SNAPSHOT")
// paperweight.foliaDevBundle("1.21.4-R0.1-SNAPSHOT")
// paperweight.devBundle("com.example.paperfork", "1.21.4-R0.1-SNAPSHOT")
}

tasks.withType<JavaCompile>().configureEach {
// Override release for newer MC
options.release = 21
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package my.plugin.hooks.paper_1_21_4;

import my.plugin.hooks.PaperHooks;
import net.minecraft.SharedConstants;
import org.slf4j.Logger;

public class PaperHooks1_21_4 implements PaperHooks {
@Override
public void doSomething(final Logger logger) {
logger.info(SharedConstants.getCurrentVersion().getName());
}
}
7 changes: 7 additions & 0 deletions paper_hooks/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
plugins {
`my-conventions`
}

dependencies {
compileOnly("io.papermc.paper:paper-api:1.17.1-R0.1-SNAPSHOT")
}
46 changes: 46 additions & 0 deletions paper_hooks/src/main/java/my/plugin/hooks/PaperHooks.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package my.plugin.hooks;

import java.lang.reflect.Method;
import org.bukkit.Bukkit;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;

public interface PaperHooks {
void doSomething(Logger logger);

static PaperHooks get() {
return Holder.INSTANCE;
}

final class Holder {
private static final PaperHooks INSTANCE;

static {
final PaperHooks hooks;
String minecraftVersion = minecraftVersionFromServerBuildInfo();
if (minecraftVersion == null) {
minecraftVersion = Bukkit.getServer().getMinecraftVersion();
}
// You probably want to do something smarter here
try {
final String underscored = minecraftVersion.replace(".", "_");
hooks = (PaperHooks) Class.forName("my.plugin.hooks.paper_" + underscored + ".PaperHooks" + underscored).getDeclaredConstructors()[0].newInstance();
} catch (final ReflectiveOperationException e) {
throw new RuntimeException("Could not create PaperHooks for " + minecraftVersion, e);
}
INSTANCE = hooks;
}

// For bootstrap-time compatibility on applicable versions
private static @Nullable String minecraftVersionFromServerBuildInfo() {
try {
final Class<?> cls = Class.forName("io.papermc.paper.ServerBuildInfo");
final Method method = cls.getMethod("minecraftVersionId");
final Object instance = cls.getMethod("buildInfo").invoke(null);
return (String) method.invoke(instance);
} catch (final Throwable e) {
return null;
}
}
}
}
5 changes: 5 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,8 @@ plugins {
}

rootProject.name = "paperweight-test-plugin"

include("paper_hooks")
include("paper_1_17_1")
include("paper_1_19_4")
include("paper_1_21_4")

This file was deleted.

Loading

0 comments on commit d7b8ab9

Please sign in to comment.