-
-
Notifications
You must be signed in to change notification settings - Fork 342
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This also breaks InteractBlockEvent.Secondary and SpawnEntityEvent.Pre
- Loading branch information
Showing
5 changed files
with
184 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
src/main/java/org/spongepowered/api/event/CompositeEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* This file is part of SpongeAPI, licensed under the MIT License (MIT). | ||
* | ||
* Copyright (c) SpongePowered <https://www.spongepowered.org> | ||
* Copyright (c) contributors | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
package org.spongepowered.api.event; | ||
|
||
import org.spongepowered.api.event.impl.AbstractCompositeEvent; | ||
import org.spongepowered.api.util.annotation.eventgen.GenerateFactoryMethod; | ||
import org.spongepowered.api.util.annotation.eventgen.ImplementedBy; | ||
import org.spongepowered.api.util.annotation.eventgen.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 { | ||
|
||
E baseEvent(); | ||
|
||
List<Event> children(); | ||
|
||
default <E extends Event> List<? extends E> event(Class<E> type) { | ||
return this.children().stream() | ||
.filter(type::isInstance) | ||
.map(type::cast) | ||
.toList(); | ||
} | ||
|
||
default <E extends Event> void applyTo(Class<E> type, Consumer<? super E> 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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/main/java/org/spongepowered/api/event/impl/AbstractCompositeEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* This file is part of SpongeAPI, licensed under the MIT License (MIT). | ||
* | ||
* Copyright (c) SpongePowered <https://www.spongepowered.org> | ||
* Copyright (c) contributors | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
package org.spongepowered.api.event.impl; | ||
|
||
import org.spongepowered.api.event.Cancellable; | ||
import org.spongepowered.api.event.CompositeEvent; | ||
import org.spongepowered.api.event.Event; | ||
import org.spongepowered.api.util.annotation.eventgen.UseField; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public abstract class AbstractCompositeEvent<E extends Event> extends AbstractEvent implements CompositeEvent<E> { | ||
|
||
@UseField(overrideToString = true) | ||
protected List<Event> children; | ||
|
||
@UseField | ||
protected boolean cancelled; | ||
|
||
public final void postInit() { | ||
this.children = Collections.unmodifiableList(this.children); | ||
} | ||
|
||
@Override | ||
public void setCancelled(boolean cancel) { | ||
this.cancelled = cancel; | ||
this.children().forEach(event -> { | ||
if (event instanceof Cancellable cancellable) { | ||
cancellable.setCancelled(cancel); | ||
} | ||
}); | ||
} | ||
} |