Skip to content

plugin-development: Require specifying minecraftVersion for runServer #102

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

Merged
merged 1 commit into from
Mar 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public class SpongePluginExtension implements MetadataContainerConfiguration {

// Dependency management
private final Property<SpongePlatform> platform;
private final Property<String> minecraftVersion;
private final Property<String> apiVersion;
private final Property<Boolean> injectRepositories;

Expand All @@ -58,6 +59,7 @@ public SpongePluginExtension(final ObjectFactory factory) {
this.plugins = factory.domainObjectContainer(PluginConfiguration.class);

this.platform = factory.property(SpongePlatform.class).convention(SpongePlatform.VANILLA);
this.minecraftVersion = factory.property(String.class);
this.apiVersion = factory.property(String.class);
this.injectRepositories = factory.property(Boolean.class);
}
Expand Down Expand Up @@ -95,6 +97,14 @@ public void platform(final SpongePlatform platform) {
this.platform.set(platform);
}

protected Property<String> minecraftVersion() {
return this.minecraftVersion;
}

public void minecraftVersion(final String minecraftVersion) {
this.minecraftVersion.set(minecraftVersion);
}

protected Property<String> apiVersion() {
return this.apiVersion;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,22 +98,35 @@ public void applyToProject(
final TaskProvider<JavaExec> runServer = this.createRunTask(spongeRuntime, sponge);

project.afterEvaluate(a -> {
if (sponge.apiVersion().isPresent()) {
if (sponge.minecraftVersion().isPresent() && sponge.apiVersion().isPresent()) {
// TODO: client run task
/*project.getLogger().lifecycle("SpongeAPI '{}' has been set within the 'spongeApi' configuration. 'runClient' and 'runServer'"
+ " tasks will be available. You may use these to test your plugin.", sponge.version().get());*/

spongeRuntime.configure(config -> {
final String minecraftVersion = sponge.minecraftVersion().get();
final String apiVersion = sponge.apiVersion().get();

config.getAttributes().attribute(
SpongeVersioningMetadataRule.MINECRAFT_TARGET,
minecraftVersion
);

config.getAttributes().attribute(
SpongeVersioningMetadataRule.API_TARGET,
generateApiReleasedVersion(apiVersion)
);
});
} else {
project.getLogger().info("SpongeAPI version has not been set within the 'sponge' configuration via the 'version' task. No "
+ "tasks will be available to run a client or server session for debugging.");
if (!sponge.minecraftVersion().isPresent()) {
project.getLogger().info("Minecraft version has not been set within the 'sponge' configuration via the 'minecraftVersion' task. No "
+ "tasks will be available to run a client or server session for debugging.");
}

if (!sponge.apiVersion().isPresent()) {
project.getLogger().info("SpongeAPI version has not been set within the 'sponge' configuration via the 'version' task. No "
+ "tasks will be available to run a client or server session for debugging.");
}
runServer.configure(t -> t.setEnabled(false));
}
if (sponge.injectRepositories().get()) {
Expand Down Expand Up @@ -184,13 +197,14 @@ private void addApiDependency(final SpongePluginExtension sponge) {

private NamedDomainObjectProvider<Configuration> addRuntimeDependency(final SpongePluginExtension sponge) {
this.project.getDependencies().getComponents().withModule("org.spongepowered:spongevanilla", SpongeVersioningMetadataRule.class);
this.project.getDependencies().getAttributesSchema().attribute(SpongeVersioningMetadataRule.MINECRAFT_TARGET).getCompatibilityRules().add(ApiVersionCompatibilityRule.class);
this.project.getDependencies().getAttributesSchema().attribute(SpongeVersioningMetadataRule.API_TARGET).getCompatibilityRules().add(ApiVersionCompatibilityRule.class);
return this.project.getConfigurations().register("spongeRuntime", conf -> {
conf.defaultDependencies(a -> {
final Dependency dep = this.project.getDependencies().create(
Constants.Dependencies.SPONGE_GROUP
+ ":" + sponge.platform().get().artifactId()
+ ":+:universal");
+ ":" + sponge.minecraftVersion().getOrElse("") + "+:universal");

a.add(dep);
});
Expand Down