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

feat!: Add CompositeEvent #2495

Open
wants to merge 5 commits into
base: api-12
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ MANIFEST.MF
dependency-reduced-pom.xml
.checkstyle
!.checkstyle/
# We exclude the generated events from git to avoid
# temptation to edit said generated files.
src/main/generated/

# Compiled #
############
Expand Down
61 changes: 31 additions & 30 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@ import org.jetbrains.gradle.ext.delegateActions
import org.jetbrains.gradle.ext.settings
import org.jetbrains.gradle.ext.taskTriggers

buildscript {
dependencies {
classpath(libs.spoon) // bump for EIG
}
}

plugins {
eclipse
Expand All @@ -16,12 +11,14 @@ plugins {
alias(libs.plugins.indra.crossdoc)
alias(libs.plugins.indra.publishing)
alias(libs.plugins.indra.publishing.sonatype)
alias(libs.plugins.eventImplGen)
alias(libs.plugins.ideaExt)
alias(libs.plugins.errorprone)
alias(libs.plugins.nexusPublish)
}

// Enable event generation to be considered part of
// the main source set
val generatedEventSourcesDir = project.file("src/main/generated")
val javaTarget: String by project
val ap by sourceSets.registering {
compileClasspath += sourceSets.main.get().compileClasspath + sourceSets.main.get().output
Expand All @@ -34,12 +31,30 @@ configurations {
}
}
}
tasks {
register("printSourceDirs") {
doLast {
sourceSets.forEach { set ->
println("SourceSet: ${set.name}")
println("Java Source Dirs: ${set.java.srcDirs}" )
}
}
}
}

sourceSets {
main {
java.srcDirs(generatedEventSourcesDir)
}
}

// Project dependencies
dependencies {
// Directly tied to what's available from Minecraft
api(libs.log4j.api)
api(libs.gson)
api(libs.eventImplGen.annotations)
annotationProcessor(libs.eventImplGen.processor)

// Adventure
api(platform(libs.adventure.bom))
Expand Down Expand Up @@ -131,26 +146,15 @@ dependencies {
}

tasks {
genEventImpl {
sourceCompatibility = "17" // TODO use javaTarget here
destinationDirectory = project.layout.buildDirectory.dir("generated/event-factory")

outputFactory = "org.spongepowered.api.event.SpongeEventFactory"
include("org/spongepowered/api/event/*/**/*")
exclude("org/spongepowered/api/event/action/InteractEvent.java")
exclude("org/spongepowered/api/event/cause/")
exclude("org/spongepowered/api/event/entity/AffectEntityEvent.java")
exclude("org/spongepowered/api/event/filter/")
exclude("org/spongepowered/api/event/impl/")
exclude("org/spongepowered/api/event/lifecycle/ProvideServiceEvent.java")
exclude("org/spongepowered/api/event/lifecycle/RegisterBuilderEvent.java")
exclude("org/spongepowered/api/event/lifecycle/RegisterRegistryEvent.java")
exclude("org/spongepowered/api/event/lifecycle/RegisterRegistryValueEvent.java")
exclude("org/spongepowered/api/event/lifecycle/RegisterCommandEvent.java")
exclude("org/spongepowered/api/event/lifecycle/RegisterFactoryEvent.java")
exclude("org/spongepowered/api/event/lifecycle/RegisterWorldEvent.java")
inclusiveAnnotations = setOf("org.spongepowered.api.util.annotation.eventgen.GenerateFactoryMethod")
exclusiveAnnotations = setOf("org.spongepowered.api.util.annotation.eventgen.NoFactoryMethod")

compileJava {
options.generatedSourceOutputDirectory = generatedEventSourcesDir
options.compilerArgs.addAll(listOf(
"-AeventGenInclusiveFolders=org/spongepowered/api/event",
"-AeventGenExclusiveFolders=org/spongepowered/api/event/cause,org/spongepowered/api/event/filter,org/spongepowered/api/event/impl,org/spongepowered/api/event/lifecycle",
"-AeventGenFactory=org.spongepowered.api.event.SpongeEventFactory",
"-AeventGenDebug=true",
))
}

jar {
Expand Down Expand Up @@ -215,15 +219,12 @@ idea {
delegateBuildRunToGradle = false
testRunner = org.jetbrains.gradle.ext.ActionDelegationConfig.TestRunner.PLATFORM
}
taskTriggers {
beforeBuild(tasks.genEventImpl)
}
}
}
}

eclipse {
synchronizationTasks(tasks.genEventImpl)
// synchronizationTasks(tasks.genEventImpl)
}

val organization: String by project
Expand Down
3 changes: 2 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,14 @@ guice = { module = "com.google.inject:guice", version.ref = "guice" }
log4j-api = { module = "org.apache.logging.log4j:log4j-api", version.ref = "log4j" }
math = { module = "org.spongepowered:math", version.ref = "math" }
pluginSpi = { module = "org.spongepowered:plugin-spi", version.ref = "pluginSpi" }
eventImplGen-processor = {module = "org.spongepowered:event-impl-gen", version = "8.0.0-SNAPSHOT"}
eventImplGen-annotations = { module = "org.spongepowered:event-impl-gen-annotations", version = "8.0.0-SNAPSHOT"}

# build-time/annotations

checkerQual = { module = "org.checkerframework:checker-qual", version.ref = "checker" }
errorprone = { module = "com.google.errorprone:error_prone_core", version.ref = "errorprone" }
errorprone-annotations = { module = "com.google.errorprone:error_prone_annotations", version.ref = "errorprone" }
spoon = { module = "fr.inria.gforge.spoon:spoon-core", version = "10.4.2" } # bump for EIG

# testing

Expand Down
52 changes: 52 additions & 0 deletions gradle/verification-metadata.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
<configuration>
<verify-metadata>true</verify-metadata>
<verify-signatures>false</verify-signatures>
<trusted-artifacts>
<trust group="org.spongepowered" name="event-impl-gen" reason="SNAPSHOT"/>
<trust group="org.spongepowered" name="event-impl-gen-annotations" reason="SNAPSHOT"/>
</trusted-artifacts>
</configuration>
<components>
<component group="aopalliance" name="aopalliance" version="1.0">
Expand Down Expand Up @@ -252,6 +256,22 @@
<sha256 value="893d56afcea1b22f83220fd7e49a6668c5b8901e39bd59dc57b42f55673721ce" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="com.google.dagger" name="dagger" version="2.42">
<artifact name="dagger-2.42.jar">
<sha256 value="7ad8147fdb5dfb481b7cde8c70da73afbaa21430ce757fcd96709fcfb6a4d56b" origin="Generated by Gradle"/>
</artifact>
<artifact name="dagger-2.42.pom">
<sha256 value="ca6529bd6d79090f0ab6f5da452eb4fbb29377b48e6a806e7612396dcb2998e0" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="com.google.dagger" name="dagger" version="2.52">
<artifact name="dagger-2.52.jar">
<sha256 value="dfb8e83020786daefbd64335ce0e0ebf01646a1b407fb77a27e052baf42df458" origin="Generated by Gradle"/>
</artifact>
<artifact name="dagger-2.52.pom">
<sha256 value="5cb60843f46bbccf97bd832d5490d26ab192af2e663fad7e5971969bf20fb94e" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="com.google.errorprone" name="error_prone_annotation" version="2.24.1">
<artifact name="error_prone_annotation-2.24.1.jar">
<sha256 value="dc202da451680aa8cdda7c68d959f27c112dca8f8fdeaf7c597de73cb2b1a0a6" origin="Generated by Gradle"/>
Expand Down Expand Up @@ -557,6 +577,14 @@
<sha256 value="f6ac2b6de9ed3823c8d68977df6352f3936af5db7baeb12303cdc9d3c4f46590" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="com.squareup" name="javapoet" version="1.13.0">
<artifact name="javapoet-1.13.0.jar">
<sha256 value="4c7517e848a71b36d069d12bb3bf46a70fd4cda3105d822b0ed2e19c00b69291" origin="Generated by Gradle"/>
</artifact>
<artifact name="javapoet-1.13.0.pom">
<sha256 value="54a34fa8502a46bc90efdb49262600591fa80bf9a34f5a4c798311aec16ca977" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="com.squareup.okhttp3" name="okhttp" version="4.10.0">
<artifact name="okhttp-4.10.0.jar">
<sha256 value="7580f14fa1691206e37081ad3f92063b1603b328da0bb316f2fef02e0562e7ec" origin="Generated by Gradle"/>
Expand Down Expand Up @@ -728,6 +756,22 @@
<sha256 value="5a65e06a159d20d3b4800624a570c646dbee710b2b539b3449a268558c289e9c" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="io.soabase.java-composer" name="java-composer" version="1.0">
<artifact name="java-composer-1.0.jar">
<sha256 value="d914e52079c41509cde32d8fa03b5a865d8a227867ed6fd5d8121639518d28a1" origin="Generated by Gradle"/>
</artifact>
<artifact name="java-composer-1.0.pom">
<sha256 value="741a35966a3055c80c10631736c6cfec82d7b5e4212b3f72ded29f98935bbb5e" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="jakarta.inject" name="jakarta.inject-api" version="2.0.1">
<artifact name="jakarta.inject-api-2.0.1.jar">
<sha256 value="f7dc98062fccf14126abb751b64fab12c312566e8cbdc8483598bffcea93af7c" origin="Generated by Gradle"/>
</artifact>
<artifact name="jakarta.inject-api-2.0.1.pom">
<sha256 value="e7fd7232e96307a575b2494c9367d68cf43ec98244aace3ccc23e1773ffa6fda" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="jakarta.platform" name="jakarta.jakartaee-bom" version="9.1.0">
<artifact name="jakarta.jakartaee-bom-9.1.0.pom">
<sha256 value="df98e0266219fdbb82562826d79a3a20776a8ba02aa787f0f0765a538654c8a4" origin="Generated by Gradle"/>
Expand Down Expand Up @@ -1492,6 +1536,9 @@
<artifact name="apiguardian-api-1.1.2.module">
<sha256 value="e08028131375b357d1d28734e9a4fb4216da84b240641cb3ef7e7c7d628223fc" origin="Generated by Gradle"/>
</artifact>
<artifact name="apiguardian-api-1.1.2.pom">
<sha256 value="32355081d109095c3d5d374d5a43b4f4c1b75d549e983ef50723e2772e5302a0" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="org.checkerframework" name="checker-qual" version="2.10.0">
<artifact name="checker-qual-2.10.0.jar">
Expand Down Expand Up @@ -1601,6 +1648,11 @@
<sha256 value="5e583878df905b5f33a230ef690a52b8f19dab9cc892bedee069f3d8af4e960a" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="org.eclipse.ee4j" name="project" version="1.0.6">
<artifact name="project-1.0.6.pom">
<sha256 value="4e7d8329d8da7dcf30779d824241be145f27108932f5a5a24eb907677bc8d72d" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="org.eclipse.ee4j" name="project" version="1.0.7">
<artifact name="project-1.0.7.pom">
<sha256 value="205c039a42cbae3556efbeb04a483eb3a3cf9550bd75bf84260dc8f28218f105" origin="Generated by Gradle"/>
Expand Down
3 changes: 3 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ plugins {
}

dependencyResolutionManagement {
repositories {
mavenLocal()
}
repositoriesMode = RepositoriesMode.FAIL_ON_PROJECT_REPOS
pluginManagement.repositories.forEach(repositories::add)
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import org.spongepowered.api.data.value.Value;
import org.spongepowered.api.data.value.ValueContainer;
import org.spongepowered.api.entity.Entity;
import org.spongepowered.api.util.annotation.eventgen.TransformWith;
import org.spongepowered.eventgen.annotations.TransformWith;
import org.spongepowered.api.world.World;

import java.util.Arrays;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/spongepowered/api/event/Cancellable.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*/
package org.spongepowered.api.event;

import org.spongepowered.api.util.annotation.eventgen.PropertySettings;
import org.spongepowered.eventgen.annotations.PropertySettings;

/**
* Represents an event that can be cancelled.
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/spongepowered/api/event/Cause.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.api.util.CopyableBuilder;
import org.spongepowered.api.util.annotation.DoNotStore;
import org.spongepowered.eventgen.annotations.NoFactoryMethod;

import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -54,6 +55,7 @@
*/
@DoNotStore
@SuppressWarnings("unchecked")
@NoFactoryMethod
public final class Cause implements Iterable<Object> {

/**
Expand Down
57 changes: 57 additions & 0 deletions src/main/java/org/spongepowered/api/event/CompositeEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package org.spongepowered.api.event;

import org.spongepowered.api.event.impl.AbstractCompositeEvent;
import org.spongepowered.eventgen.annotations.GenerateFactoryMethod;
import org.spongepowered.eventgen.annotations.ImplementedBy;
import org.spongepowered.eventgen.annotations.PropertySettings;

import java.util.List;
import java.util.function.Consumer;

/**
* A {@link CompositeEvent} is an {@link Event} that contains multiple
* side effectual {@link Event Events}, which may have their own side effects
* and may be {@link Cancellable}. In some cases, the interactions of this event
* may be cancellable as a whole, but are not guaranteed to revert all side
* effects on the {@link org.spongepowered.api.Game}. The {@link #children()} of
* this event are ordered in a "best-effort" basis, and may not be guaranteed
* to be in any particular order.
* <p>Using {@link #setCancelled(boolean)} will perform a best effort cancellation
* on each of the children events.
*/
@GenerateFactoryMethod
@ImplementedBy(AbstractCompositeEvent.class)
public interface CompositeEvent<E extends Event> extends Event, Cancellable {

@PropertySettings(useInToString = false)
E baseEvent();

List<Event> children();

default <A extends Event> List<? extends A> event(Class<A> type) {
return this.children().stream()
.filter(type::isInstance)
.map(type::cast)
.toList();
}

default <A extends Event> void applyTo(Class<A> type, Consumer<? super A> consumer) {
this.children().stream()
.filter(type::isInstance)
.map(type::cast)
.forEach(consumer);
}

/**
* {@inheritDoc}
*
* Cancels this event and all related events captured {@link #children()}.
* Selectively, if individual events are wished to be cancelled,
* the individual events should be cancelled instead.
*
* @param cancel The new cancelled state
*/
@PropertySettings(generateMethods = false)
@Override
void setCancelled(boolean cancel);
}
4 changes: 2 additions & 2 deletions src/main/java/org/spongepowered/api/event/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@

import org.spongepowered.api.event.impl.AbstractEvent;
import org.spongepowered.api.util.annotation.DoNotStore;
import org.spongepowered.api.util.annotation.eventgen.AbsoluteSortPosition;
import org.spongepowered.api.util.annotation.eventgen.ImplementedBy;
import org.spongepowered.eventgen.annotations.AbsoluteSortPosition;
import org.spongepowered.eventgen.annotations.ImplementedBy;

/**
* An event called within Sponge.
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/spongepowered/api/event/EventContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.api.util.CopyableBuilder;
import org.spongepowered.api.util.annotation.DoNotStore;
import org.spongepowered.eventgen.annotations.NoFactoryMethod;

import java.util.HashMap;
import java.util.Map;
Expand All @@ -42,6 +43,7 @@
* in the event's {@link Cause}.
*/
@DoNotStore
@NoFactoryMethod
public final class EventContext {

private static final EventContext EMPTY_CONTEXT = new EventContext(Map.of());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import org.spongepowered.api.world.portal.PortalLogic;
import org.spongepowered.api.world.server.ServerLocation;
import org.spongepowered.api.world.server.ServerWorld;
import org.spongepowered.eventgen.annotations.NoFactoryMethod;
import org.spongepowered.math.vector.Vector3d;
import org.spongepowered.plugin.PluginContainer;

Expand All @@ -62,6 +63,7 @@
* Standard keys for use within {@link EventContext}s.
*/
@SuppressWarnings("unused")
@NoFactoryMethod
public final class EventContextKeys {

// @formatter:off
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/org/spongepowered/api/event/Listener.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
*/
package org.spongepowered.api.event;

import org.spongepowered.eventgen.annotations.NoFactoryMethod;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
Expand All @@ -34,6 +36,7 @@
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@NoFactoryMethod
public @interface Listener {

/**
Expand Down
Loading
Loading