Skip to content

Commit 6980e9a

Browse files
Merge branch 'fix-group' into 'master'
[FIX][SDK] exception now throws when one tries to add the same entity to two groups See merge request codingame/game-engine!268
2 parents 72e73a3 + 3529573 commit 6980e9a

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

engine/modules/entities/src/main/java/com/codingame/gameengine/module/entities/ContainerBasedEntity.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,19 @@
88
/**
99
*
1010
*
11-
* @param <T> a subclass inheriting Entity, used in order to return <b>this</b> as a T instead of a <code>ContainerBasedEntity</code>.
11+
* @param <T>
12+
* a subclass inheriting Entity, used in order to return <b>this</b> as a T instead of a <code>ContainerBasedEntity</code>.
1213
*/
1314
public abstract class ContainerBasedEntity<T extends Entity<?>> extends Entity<T> {
1415

1516
private Set<Entity<?>> entities;
16-
17+
1718
ContainerBasedEntity() {
1819
super();
1920

2021
entities = new HashSet<>();
2122
}
23+
2224
/**
2325
* Separates the given entity from this <code>ContainerBasedEntity</code>.
2426
*
@@ -45,19 +47,27 @@ public void remove(Entity<?> entity) {
4547
*/
4648
public void add(Entity<?>... entities) {
4749
Stream.of(entities).forEach(entity -> {
48-
if (entity.parent != null) {
49-
throw new IllegalArgumentException();
50+
if (entity.getParent().isPresent()) {
51+
throw new IllegalArgumentException(
52+
String.format(
53+
"Cannot add entity %d to container %d: it is already in container %d",
54+
entity.getId(),
55+
getId(),
56+
entity.getParent().get().getId()
57+
)
58+
);
5059
}
5160
this.entities.add(entity);
61+
entity.parent = this;
5262
});
5363

5464
set("children", asString(this.entities), null);
5565
}
5666

5767
private String asString(Set<Entity<?>> entities) {
5868
return entities.stream()
59-
.map(e -> String.valueOf(e.getId()))
60-
.collect(Collectors.joining(","));
69+
.map(e -> String.valueOf(e.getId()))
70+
.collect(Collectors.joining(","));
6171
}
6272

6373
}

engine/modules/entities/src/main/java/com/codingame/gameengine/module/entities/Entity.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,4 +516,11 @@ protected static void requireValidColor(int color) {
516516
throw new IllegalArgumentException(color + "is not a valid RGB integer.");
517517
}
518518
}
519+
520+
/**
521+
* @return the Group or BufferedGroup which contains this entity
522+
*/
523+
public Optional<ContainerBasedEntity<?>> getParent() {
524+
return Optional.ofNullable(parent);
525+
}
519526
}

playground/misc/misc-3-release-notes.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@
22

33
The CodinGame SDK is regularly updated and improved. This document lets you know what changed in the latest releases.
44

5+
## Next Release
6+
7+
### 🎁 Features
8+
9+
- `Entity` now exposes the `getParent` method to acces the container an entity has been added into
10+
11+
### 🐞 Bug fix
12+
13+
- An exception is now thrown when one tries to add the same entity to two different groups
14+
- `Group::remove` now functions correctly
15+
516
## 3.12.0
617

718
### 🎁 Features

0 commit comments

Comments
 (0)