From 6fcb9bd17287cc419feeba02ca6911684eed2fc6 Mon Sep 17 00:00:00 2001 From: Josephine Rueckert Date: Sun, 18 Aug 2024 21:33:22 +0200 Subject: [PATCH] qa: address PMD AvoidBranchingStatementAsLastInLoop findings --- .../lifecycleEvents/BeforeEntityCreated.java | 10 ++++------ .../engine/logic/spawner/AbstractSpawner.java | 5 ++--- .../engine/world/time/WorldTimeImpl.java | 19 +++++++++++-------- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/engine/src/main/java/org/terasology/engine/entitySystem/entity/lifecycleEvents/BeforeEntityCreated.java b/engine/src/main/java/org/terasology/engine/entitySystem/entity/lifecycleEvents/BeforeEntityCreated.java index 40f8ad6d749..4e82fd938bb 100644 --- a/engine/src/main/java/org/terasology/engine/entitySystem/entity/lifecycleEvents/BeforeEntityCreated.java +++ b/engine/src/main/java/org/terasology/engine/entitySystem/entity/lifecycleEvents/BeforeEntityCreated.java @@ -80,17 +80,15 @@ private Component getNext() { if (componentsToAdd.containsKey(result.getClass())) { throw new IllegalStateException("Requested to add component that was already defined for this entity"); } - if (componentsToRemove.contains(result.getClass())) { - continue; + if (!componentsToRemove.contains(result.getClass())) { + return result; } - return result; } while (addedIterator.hasNext()) { final Component result = addedIterator.next(); - if (componentsToRemove.contains(result.getClass())) { - continue; + if (!componentsToRemove.contains(result.getClass())) { + return result; } - return result; } return null; } diff --git a/engine/src/main/java/org/terasology/engine/logic/spawner/AbstractSpawner.java b/engine/src/main/java/org/terasology/engine/logic/spawner/AbstractSpawner.java index f9a5b0fbd50..8132f972dcf 100644 --- a/engine/src/main/java/org/terasology/engine/logic/spawner/AbstractSpawner.java +++ b/engine/src/main/java/org/terasology/engine/logic/spawner/AbstractSpawner.java @@ -88,10 +88,9 @@ protected Vector3f findSpawnPosition(World world, Vector2i pos, int searchRadius // nothing above sea level found for (Vector2ic test : spiral) { Optional val = getWorld.apply(test); - if (!val.isPresent()) { - continue; + if (val.isPresent()) { + return new Vector3f(test.x(), TeraMath.floorToInt(val.get()), test.y()); } - return new Vector3f(test.x(), TeraMath.floorToInt(val.get()), test.y()); } throw new IllegalStateException("No spawn location found"); diff --git a/engine/src/main/java/org/terasology/engine/world/time/WorldTimeImpl.java b/engine/src/main/java/org/terasology/engine/world/time/WorldTimeImpl.java index 8f87777bdcc..14e060cc010 100644 --- a/engine/src/main/java/org/terasology/engine/world/time/WorldTimeImpl.java +++ b/engine/src/main/java/org/terasology/engine/world/time/WorldTimeImpl.java @@ -2,6 +2,9 @@ // SPDX-License-Identifier: Apache-2.0 package org.terasology.engine.world.time; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.terasology.engine.core.PathManager; import org.terasology.engine.core.Time; import org.terasology.engine.entitySystem.entity.EntityManager; import org.terasology.engine.entitySystem.entity.EntityRef; @@ -10,9 +13,12 @@ import org.terasology.engine.registry.In; import org.terasology.engine.world.WorldComponent; +import java.util.Iterator; +import java.util.NoSuchElementException; import java.util.concurrent.atomic.AtomicLong; public class WorldTimeImpl extends BaseComponentSystem implements WorldTime, UpdateSubscriberSystem { + private static final Logger LOGGER = LoggerFactory.getLogger(WorldTimeImpl.class); private static final float WORLD_TIME_MULTIPLIER = 48f; @@ -68,17 +74,14 @@ public void update(float delta) { if (startTick != endTick) { long tick = endTime - endTime % TICK_EVENT_RATE; - getWorldEntity().send(new WorldTimeEvent(tick)); + for (EntityRef e : entityManager.getEntitiesWith(WorldComponent.class)) { + // this may not be called if there's no entity with World Component (yet) + // TODO: consider catching / logging this case (someplace better than here) + e.send(new WorldTimeEvent(tick)); + } } // TODO: consider sending a DailyTick (independent from solar events such as midnight) } } - - private EntityRef getWorldEntity() { - for (EntityRef entity : entityManager.getEntitiesWith(WorldComponent.class)) { - return entity; - } - return EntityRef.NULL; - } }