Skip to content

Commit 5d71920

Browse files
committed
!feat: Add CompositeEvent
This also breaks InteractBlockEvent.Secondary and SpawnEntityEvent.Pre
1 parent 0557374 commit 5d71920

File tree

3 files changed

+96
-34
lines changed

3 files changed

+96
-34
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package org.spongepowered.api.event;
2+
3+
import org.spongepowered.api.util.annotation.eventgen.NoFactoryMethod;
4+
5+
import java.util.List;
6+
import java.util.function.Consumer;
7+
8+
/**
9+
* A {@link CompositeEvent} is an {@link Event} that contains multiple
10+
* side effectual {@link Event Events}, which may have their own side effects
11+
* and may be {@link Cancellable}. In some cases, the interactions of this event
12+
* may be cancellable as a whole, but are not guaranteed to revert all side
13+
* effects on the {@link org.spongepowered.api.Game}. The {@link #children()} of
14+
* this event are ordered in a "best-effort" basis, and may not be guaranteed
15+
* to be in any particular order.
16+
* <p>Using {@link #setCancelled(boolean)} will perform a best effort cancellation
17+
* on each of the children events.
18+
*/
19+
@NoFactoryMethod
20+
public interface CompositeEvent<E extends Event> extends Event, Cancellable {
21+
22+
E baseEvent();
23+
24+
List<Event> children();
25+
26+
default <E extends Event> List<? extends E> event(Class<E> type) {
27+
return this.children().stream()
28+
.filter(type::isInstance)
29+
.map(type::cast)
30+
.toList();
31+
}
32+
33+
default <E extends Event> void applyTo(Class<E> type, Consumer<? super E> consumer) {
34+
this.children().stream()
35+
.filter(type::isInstance)
36+
.map(type::cast)
37+
.forEach(consumer);
38+
}
39+
40+
/**
41+
* {@inheritDoc}
42+
*
43+
* Cancels this event and all related events captured {@link #children()}.
44+
* Selectively, if individual events are wished to be cancelled,
45+
* the individual events should be cancelled instead.
46+
*
47+
* @param cancel The new cancelled state
48+
*/
49+
@Override
50+
void setCancelled(boolean cancel);
51+
}

src/main/java/org/spongepowered/api/event/block/InteractBlockEvent.java

Lines changed: 42 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,18 @@
2929
import org.spongepowered.api.block.BlockTypes;
3030
import org.spongepowered.api.entity.living.player.Player;
3131
import org.spongepowered.api.event.Cancellable;
32+
import org.spongepowered.api.event.CompositeEvent;
3233
import org.spongepowered.api.event.action.InteractEvent;
34+
import org.spongepowered.api.event.entity.SpawnEntityEvent;
35+
import org.spongepowered.api.event.item.inventory.ChangeInventoryEvent;
3336
import org.spongepowered.api.item.inventory.ItemStack;
3437
import org.spongepowered.api.util.Direction;
3538
import org.spongepowered.api.util.Tristate;
3639
import org.spongepowered.api.world.server.ServerLocation;
3740
import org.spongepowered.math.vector.Vector3d;
3841

42+
import java.util.Optional;
43+
3944
/**
4045
* Base event for all interactions involving a {@link BlockSnapshot} at a
4146
* {@link ServerLocation}.
@@ -101,7 +106,7 @@ interface Finish extends Primary, Cancellable {
101106
*
102107
* <p>This is usually right-click.</p>
103108
*/
104-
interface Secondary extends InteractBlockEvent, Cancellable {
109+
interface Secondary extends InteractBlockEvent {
105110

106111
Tristate originalUseItemResult();
107112

@@ -143,43 +148,47 @@ interface Secondary extends InteractBlockEvent, Cancellable {
143148
*/
144149
Tristate useBlockResult();
145150

146-
/**
147-
* Sets whether the {@link Player#itemInHand} should be used.
148-
*
149-
* <ul>
150-
* <li>FALSE: The {@link ItemStack} will never be used.</li>
151-
* <li>UNDEFINED: The {@link ItemStack} will be used if the block fails.
152-
* </li>
153-
* <li>TRUE: The {@link ItemStack} will always be used.</li>
154-
* </ul>
155-
*
156-
* <p>Note: These results may differ depending on implementation.</p>
157-
*
158-
* @param result Whether the {@link Player#itemInHand} should be used
159-
*/
160-
void setUseItemResult(Tristate result);
161-
162-
/**
163-
* Sets whether the interacted {@link BlockSnapshot} should be used.
164-
*
165-
* <ul>
166-
* <li>FALSE: {@link BlockSnapshot} will never be used.</li>
167-
* <li>UNDEFINED: {@link BlockSnapshot} will be used as normal.</li>
168-
* <li>TRUE: {@link BlockSnapshot} will always be used.</li>
169-
* </ul>
170-
*
171-
* <p>Note: These results may differ depending on implementation.</p>
172-
*
173-
* @param result Whether the interacted {@link BlockSnapshot} should be
174-
* used
175-
*/
176-
void setUseBlockResult(Tristate result);
177-
178151
/**
179152
* Gets the point of interaction where the interaction occurred as a {@link Vector3d}.
180153
*
181154
* @return The interaction point
182155
*/
183156
Vector3d interactionPoint();
157+
158+
interface Pre extends Secondary, Cancellable {
159+
160+
/**
161+
* Sets whether the {@link Player#itemInHand} should be used.
162+
*
163+
* <ul>
164+
* <li>FALSE: The {@link ItemStack} will never be used.</li>
165+
* <li>UNDEFINED: The {@link ItemStack} will be used if the block fails.
166+
* </li>
167+
* <li>TRUE: The {@link ItemStack} will always be used.</li>
168+
* </ul>
169+
*
170+
* <p>Note: These results may differ depending on implementation.</p>
171+
*
172+
* @param result Whether the {@link Player#itemInHand} should be used
173+
*/
174+
void setUseItemResult(Tristate result);
175+
176+
/**
177+
* Sets whether the interacted {@link BlockSnapshot} should be used.
178+
*
179+
* <ul>
180+
* <li>FALSE: {@link BlockSnapshot} will never be used.</li>
181+
* <li>UNDEFINED: {@link BlockSnapshot} will be used as normal.</li>
182+
* <li>TRUE: {@link BlockSnapshot} will always be used.</li>
183+
* </ul>
184+
*
185+
* <p>Note: These results may differ depending on implementation.</p>
186+
*
187+
* @param result Whether the interacted {@link BlockSnapshot} should be
188+
* used
189+
*/
190+
void setUseBlockResult(Tristate result);
191+
}
192+
184193
}
185194
}

src/main/java/org/spongepowered/api/event/entity/SpawnEntityEvent.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import org.spongepowered.api.entity.Entity;
2828
import org.spongepowered.api.event.Cause;
29+
import org.spongepowered.api.event.impl.entity.AbstractAffectEntityEvent;
2930
import org.spongepowered.api.event.impl.entity.AbstractSpawnEntityEvent;
3031
import org.spongepowered.api.util.annotation.eventgen.GenerateFactoryMethod;
3132
import org.spongepowered.api.util.annotation.eventgen.ImplementedBy;
@@ -51,7 +52,8 @@ public interface SpawnEntityEvent extends AffectEntityEvent {
5152
* will result in no awareness to the client that the entity was being
5253
* spawned and later cancelled.
5354
*/
54-
interface Pre extends SpawnEntityEvent {}
55+
@ImplementedBy(value = AbstractAffectEntityEvent.class, priority = 2)
56+
interface Pre extends AffectEntityEvent {}
5557

5658
interface Custom extends SpawnEntityEvent {}
5759
}

0 commit comments

Comments
 (0)