Skip to content

Commit 3481b71

Browse files
Yeregorixdualspiral
authored andcommitted
Minor changes to AffectEntityEvent and ExplosionEvent.Detonate (#2401)
* Make AffectEntityEvent#entities() unmodifiable * Make Detonate#affectedLocations() unmodifiable * Fix SpawnEntityEvent and ExplosionEvent.Detonate impl priority * Exclude SpawnEntityEvent and all sub-events from SpongeEventFactoryTest#testCreate * Make filterEntities and filterAffectedLocations return void
1 parent b5076e9 commit 3481b71

File tree

7 files changed

+93
-60
lines changed

7 files changed

+93
-60
lines changed

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

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@
3535
import org.spongepowered.api.world.explosion.Explosion;
3636
import org.spongepowered.api.world.server.ServerLocation;
3737

38-
import java.util.ArrayList;
39-
import java.util.Iterator;
4038
import java.util.List;
4139
import java.util.function.Predicate;
4240

@@ -68,11 +66,13 @@ public interface AffectEntityEvent extends Event, Cancellable {
6866
List<EntitySnapshot> entitySnapshots() throws IllegalStateException;
6967

7068
/**
71-
* Gets the {@link List} who will be affected after event
69+
* Gets the {@link List} of entities who will be affected after event
7270
* resolution.
71+
* This list can only be modified using {@link AffectEntityEvent#filterEntities(Predicate)}.
7372
*
74-
* @return The List
73+
* @return The list of entities that will be affected.
7574
*/
75+
@PropertySettings(requiredParameter = true, generateMethods = false)
7676
List<Entity> entities();
7777

7878
/**
@@ -83,20 +83,9 @@ public interface AffectEntityEvent extends Event, Cancellable {
8383
* be removed from {@link #entities()}.</p>
8484
*
8585
* @param predicate The predicate to use for filtering
86-
* @return The entities removed from {@link #entities()}
8786
*/
88-
default List<Entity> filterEntityLocations(Predicate<ServerLocation> predicate) {
89-
final List<Entity> removedEntities = new ArrayList<>();
90-
91-
final Iterator<Entity> i = this.entities().iterator();
92-
while (i.hasNext()) {
93-
final Entity entity = i.next();
94-
if (!entity.location().onServer().map(predicate::test).orElse(false)) {
95-
i.remove();
96-
removedEntities.add(entity);
97-
}
98-
}
99-
return removedEntities;
87+
default void filterEntityLocations(Predicate<ServerLocation> predicate) {
88+
filterEntities(entity -> entity.location().onServer().map(predicate::test).orElse(false));
10089
}
10190

10291
/**
@@ -107,19 +96,6 @@ default List<Entity> filterEntityLocations(Predicate<ServerLocation> predicate)
10796
* be removed from {@link #entities()}.</p>
10897
*
10998
* @param predicate The predicate to use for filtering
110-
* @return The entities removed from {@link #entities()}
11199
*/
112-
default List<? extends Entity> filterEntities(Predicate<Entity> predicate) {
113-
final List<Entity> removedEntities = new ArrayList<>();
114-
115-
final Iterator<Entity> i = this.entities().iterator();
116-
while (i.hasNext()) {
117-
final Entity entity = i.next();
118-
if (!predicate.test(entity)) {
119-
i.remove();
120-
removedEntities.add(entity);
121-
}
122-
}
123-
return removedEntities;
124-
}
100+
void filterEntities(Predicate<Entity> predicate);
125101
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
* recommended event to interact with connecting players.</p>
4545
*/
4646
@GenerateFactoryMethod
47-
@ImplementedBy(AbstractSpawnEntityEvent.class)
47+
@ImplementedBy(value = AbstractSpawnEntityEvent.class, priority = 2)
4848
public interface SpawnEntityEvent extends AffectEntityEvent {
4949

5050
/**

src/main/java/org/spongepowered/api/event/impl/entity/AbstractAffectEntityEvent.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@
3232
import org.spongepowered.api.event.impl.AbstractEvent;
3333
import org.spongepowered.api.util.annotation.eventgen.UseField;
3434

35+
import java.util.Collections;
3536
import java.util.List;
37+
import java.util.function.Predicate;
3638

3739
public abstract class AbstractAffectEntityEvent extends AbstractEvent implements AffectEntityEvent {
3840

@@ -56,4 +58,14 @@ public List<EntitySnapshot> entitySnapshots() {
5658
}
5759
return this.entitySnapshots;
5860
}
61+
62+
@Override
63+
public List<Entity> entities() {
64+
return Collections.unmodifiableList(this.entities);
65+
}
66+
67+
@Override
68+
public void filterEntities(Predicate<Entity> predicate) {
69+
this.entities.removeIf(entity -> !predicate.test(entity));
70+
}
5971
}

src/main/java/org/spongepowered/api/event/impl/entity/AbstractSpawnEntityEvent.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@
2626

2727
import org.spongepowered.api.event.EventContextKeys;
2828
import org.spongepowered.api.event.entity.SpawnEntityEvent;
29-
import org.spongepowered.api.event.impl.AbstractEvent;
3029

31-
public abstract class AbstractSpawnEntityEvent extends AbstractEvent implements SpawnEntityEvent {
30+
public abstract class AbstractSpawnEntityEvent extends AbstractAffectEntityEvent implements SpawnEntityEvent {
3231

3332
@Override
3433
protected void init() {
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* This file is part of SpongeAPI, licensed under the MIT License (MIT).
3+
*
4+
* Copyright (c) SpongePowered <https://www.spongepowered.org>
5+
* Copyright (c) contributors
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
package org.spongepowered.api.event.impl.world;
26+
27+
import org.spongepowered.api.event.impl.entity.AbstractAffectEntityEvent;
28+
import org.spongepowered.api.event.world.ExplosionEvent;
29+
import org.spongepowered.api.util.annotation.eventgen.UseField;
30+
import org.spongepowered.api.world.server.ServerLocation;
31+
32+
import java.util.Collections;
33+
import java.util.List;
34+
import java.util.function.Predicate;
35+
36+
public abstract class AbstractDetonateEvent extends AbstractAffectEntityEvent implements ExplosionEvent.Detonate {
37+
38+
@UseField
39+
protected List<ServerLocation> affectedLocations;
40+
41+
@Override
42+
public List<ServerLocation> affectedLocations() {
43+
return Collections.unmodifiableList(this.affectedLocations);
44+
}
45+
46+
@Override
47+
public void filterAffectedLocations(Predicate<ServerLocation> predicate) {
48+
this.affectedLocations.removeIf(location -> !predicate.test(location));
49+
}
50+
}

src/main/java/org/spongepowered/api/event/world/ExplosionEvent.java

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@
2626

2727
import org.spongepowered.api.event.Cancellable;
2828
import org.spongepowered.api.event.Event;
29-
import org.spongepowered.api.event.block.ChangeBlockEvent;
3029
import org.spongepowered.api.event.entity.AffectEntityEvent;
30+
import org.spongepowered.api.event.impl.world.AbstractDetonateEvent;
31+
import org.spongepowered.api.util.annotation.eventgen.ImplementedBy;
32+
import org.spongepowered.api.util.annotation.eventgen.PropertySettings;
3133
import org.spongepowered.api.world.World;
3234
import org.spongepowered.api.world.explosion.Explosion;
3335
import org.spongepowered.api.world.server.ServerLocation;
3436
import org.spongepowered.api.world.server.ServerWorld;
3537

36-
import java.util.ArrayList;
37-
import java.util.Iterator;
3838
import java.util.List;
3939
import java.util.function.Predicate;
4040

@@ -80,6 +80,7 @@ interface Pre extends ExplosionEvent {
8080
* already calculated all the blocks and entities the explosion should
8181
* affect.
8282
*/
83+
@ImplementedBy(value = AbstractDetonateEvent.class, priority = 2)
8384
interface Detonate extends ExplosionEvent, AffectEntityEvent {
8485

8586
/**
@@ -91,11 +92,12 @@ interface Detonate extends ExplosionEvent, AffectEntityEvent {
9192

9293
/**
9394
* Gets the list of calculated affected locations for blocks that will
94-
* be removed due to the explosion. Note that the list is mutable.
95-
* However, adding new locations may cause unknown effects.
95+
* be removed due to the explosion.
96+
* This list can only be modified using {@link #filterAffectedLocations(Predicate)}.
9697
*
9798
* @return The list of blocks that will be affected by the explosion
9899
*/
100+
@PropertySettings(requiredParameter = true, generateMethods = false)
99101
List<ServerLocation> affectedLocations();
100102

101103
/**
@@ -106,23 +108,8 @@ interface Detonate extends ExplosionEvent, AffectEntityEvent {
106108
* be removed from {@link #affectedLocations()}.</p>
107109
*
108110
* @param predicate The predicate to use for filtering
109-
* @return The locations removed from {@link #affectedLocations()}
110111
*/
111-
default List<ServerLocation> filterAffectedLocations(Predicate<ServerLocation> predicate) {
112-
final List<ServerLocation> removedLocations = new ArrayList<>();
113-
114-
final Iterator<ServerLocation> iter = this.affectedLocations().iterator();
115-
while (iter.hasNext()) {
116-
final ServerLocation location = iter.next();
117-
118-
if (!predicate.test(location)) {
119-
iter.remove();
120-
removedLocations.add(location);
121-
}
122-
}
123-
124-
return removedLocations;
125-
}
112+
void filterAffectedLocations(Predicate<ServerLocation> predicate);
126113
}
127114

128115
}

src/test/java/org/spongepowered/api/event/SpongeEventFactoryTest.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.spongepowered.api.data.DataTransactionResult;
4141
import org.spongepowered.api.event.entity.AttackEntityEvent;
4242
import org.spongepowered.api.event.entity.DamageEntityEvent;
43+
import org.spongepowered.api.event.entity.SpawnEntityEvent;
4344
import org.spongepowered.api.event.entity.ai.goal.GoalEvent;
4445
import org.spongepowered.api.event.impl.AbstractEvent;
4546
import org.spongepowered.api.util.Color;
@@ -75,12 +76,11 @@ class SpongeEventFactoryTest {
7576
/**
7677
* Events with types that aren't able to be mocked for one reason or another.
7778
*/
78-
private static final Set<Class<?>> EXCLUDED_EVENTS = UnmodifiableCollections.toSet(
79+
private static final List<Class<?>> EXCLUDED_EVENTS = UnmodifiableCollections.toList(
7980
DamageEntityEvent.class,
8081
GoalEvent.class,
81-
GoalEvent.Add.class,
82-
GoalEvent.Remove.class,
83-
AttackEntityEvent.class
82+
AttackEntityEvent.class,
83+
SpawnEntityEvent.class
8484
);
8585

8686
private static final Set<String> EXCLUDED_METHODS = UnmodifiableCollections.toSet("entitySnapshots");
@@ -104,10 +104,19 @@ class SpongeEventFactoryTest {
104104
return SpongeEventFactoryTest.mockParam(returnType);
105105
};
106106

107+
static boolean isExcludedEvent(Class<?> eventClass) {
108+
for (Class<?> excludedClass : SpongeEventFactoryTest.EXCLUDED_EVENTS) {
109+
if (excludedClass.isAssignableFrom(eventClass)) {
110+
return true;
111+
}
112+
}
113+
return false;
114+
}
115+
107116
static Stream<Object[]> eventMethods() {
108117
return Arrays.stream(SpongeEventFactory.class.getMethods())
109118
.filter(method -> method.getName().startsWith("create") && Modifier.isStatic(method.getModifiers())
110-
&& !SpongeEventFactoryTest.EXCLUDED_EVENTS.contains(method.getReturnType()))
119+
&& !isExcludedEvent(method.getReturnType()))
111120
.map(method -> new Object[] { method.getReturnType().getName(), method});
112121
}
113122

0 commit comments

Comments
 (0)