clean all usage of GameEvent::setSourceId

This commit is contained in:
Susucre 2024-04-28 14:21:43 +02:00
parent 8771d9b7f5
commit 868cd4d8fd
40 changed files with 114 additions and 100 deletions

View file

@ -9,21 +9,25 @@ import java.util.stream.Collectors;
/**
* Special events created by game engine to track batches of events that occur simultaneously,
* for triggers that need such information
*
* @author xenohedron
*/
public abstract class BatchEvent<T extends GameEvent> extends GameEvent {
private final Set<T> events = new HashSet<>();
private final boolean singleTargetId;
private final boolean singleSourceId;
/**
* @param eventType specific type of event
* @param eventType specific type of event
* @param singleSourceId if true, all included events must have same source id
* @param singleTargetId if true, all included events must have same target id
* @param firstEvent added to initialize the batch (batch is never empty)
* @param firstEvent added to initialize the batch (batch is never empty)
*/
protected BatchEvent(EventType eventType, boolean singleTargetId, T firstEvent) {
super(eventType, (singleTargetId ? firstEvent.getTargetId() : null), null, null);
protected BatchEvent(EventType eventType, boolean singleTargetId, boolean singleSourceId, T firstEvent) {
super(eventType, (singleTargetId ? firstEvent.getTargetId() : null), (singleSourceId ? firstEvent.getSourceId() : null), null);
this.singleTargetId = singleTargetId;
this.singleSourceId = singleSourceId;
if (firstEvent instanceof BatchEvent) { // sanity check, if you need it then think twice and research carefully
throw new UnsupportedOperationException("Wrong code usage: nesting batch events not supported");
}
@ -34,14 +38,18 @@ public abstract class BatchEvent<T extends GameEvent> extends GameEvent {
* For alternate event structure logic used by ZoneChangeBatchEvent, list of events starts empty.
*/
protected BatchEvent(EventType eventType) {
super(eventType, null, null, null);
super(eventType, null, (UUID) null, null);
this.singleTargetId = false;
this.singleSourceId = false;
}
public void addEvent(T event) {
if (singleTargetId && !getTargetId().equals(event.getTargetId())) {
throw new IllegalStateException("Wrong code usage. Batch event initiated with single target id, but trying to add event with different target id");
}
if (singleSourceId && !getSourceId().equals(event.getSourceId())) {
throw new IllegalStateException("Wrong code usage. Batch event initiated with single source id, but trying to add event with different target id");
}
this.events.add(event);
}
@ -87,8 +95,10 @@ public abstract class BatchEvent<T extends GameEvent> extends GameEvent {
}
@Override // events can store a diff value, so search it from events list instead
@Deprecated // no use case currently supported
public UUID getSourceId() {
if (singleSourceId) {
return super.getSourceId();
}
throw new IllegalStateException("Wrong code usage. Must search value from a getEvents list");
}