Skip to content

Commit 43a1a24

Browse files
authored
Merge pull request #1033 from Zhack47/dev
ignore and warn adding duplicate component, #993
2 parents 130de78 + 08b2234 commit 43a1a24

File tree

2 files changed

+14
-26
lines changed

2 files changed

+14
-26
lines changed

fxgl-entity/src/main/java/com/almasb/fxgl/entity/Entity.java

+10-12
Original file line numberDiff line numberDiff line change
@@ -389,9 +389,9 @@ public final List<Component> getComponents() {
389389
public final void addComponent(Component component) {
390390
checkNotUpdating();
391391

392-
checkRequirementsMet(component.getClass());
393-
394-
addComponentNoChecks(component);
392+
if (checkRequirementsMet(component.getClass())) {
393+
addComponentNoChecks(component);
394+
}
395395
}
396396

397397
private void addComponentNoChecks(Component component) {
@@ -533,20 +533,18 @@ private boolean isCoreComponent(Class<? extends Component> type) {
533533
return type.getAnnotation(CoreComponent.class) != null;
534534
}
535535

536-
private void checkRequirementsMet(Class<? extends Component> type) {
537-
checkNotDuplicate(type);
538-
536+
private boolean checkRequirementsMet(Class<? extends Component> type) {
537+
// check if not duplicate;
538+
if (hasComponent(type)) {
539+
log.warning("Entity already has component: " + type.getCanonicalName());
540+
return false;
541+
}
539542
for (Required r : type.getAnnotationsByType(Required.class)) {
540543
if (!hasComponent(r.value())) {
541544
throw new IllegalStateException("Required component: [" + r.value().getSimpleName() + "] for: " + type.getSimpleName() + " is missing");
542545
}
543546
}
544-
}
545-
546-
private void checkNotDuplicate(Class<? extends Component> type) {
547-
if (hasComponent(type)) {
548-
throw new IllegalArgumentException("Entity already has component: " + type.getCanonicalName());
549-
}
547+
return true;
550548
}
551549

552550
private void checkNotRequiredByAny(Class<? extends Component> type) {

fxgl-entity/src/test/kotlin/com/almasb/fxgl/entity/EntityTest.kt

+4-14
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ class EntityTest {
4848
}
4949

5050
@Test
51-
fun `Add component fails if same type already exists`() {
52-
val exception = assertThrows(IllegalArgumentException::class.java) {
51+
fun `Add component is ignored if same type already exists`() {
52+
val exception = assertDoesNotThrow {
5353
entity.addComponent(TestComponent())
5454
entity.addComponent(TestComponent())
5555
}
@@ -170,16 +170,6 @@ class EntityTest {
170170
assertThat(entity.components, hasItem(control))
171171
}
172172

173-
@Test
174-
fun `Add control fails if same type exists`() {
175-
val control = TestControl()
176-
entity.addComponent(control)
177-
178-
assertThrows(IllegalArgumentException::class.java, {
179-
entity.addComponent(control)
180-
})
181-
}
182-
183173
@Test
184174
fun `Add control fails if required module is missing`() {
185175
assertThrows(IllegalStateException::class.java, {
@@ -878,7 +868,7 @@ class EntityTest {
878868
assertThat(entity.opacityProperty().value, `is`(0.5))
879869

880870
entity.isVisible = false
881-
871+
882872
assertFalse(entity.isVisible)
883873
assertFalse(entity.viewComponent.isVisible)
884874
}
@@ -1027,4 +1017,4 @@ class EntityTest {
10271017
data = bundle.get("data")
10281018
}
10291019
}
1030-
}
1020+
}

0 commit comments

Comments
 (0)