From f5e9cea80c87d6fc4ea50f1795d84ebe16a7e169 Mon Sep 17 00:00:00 2001 From: tolziplohu Date: Mon, 16 Aug 2021 16:17:04 -0500 Subject: [PATCH] refactor: make ClimateConditions dependency optional --- module.txt | 3 +- .../org/terasology/gf/PlantGrowingSystem.java | 5 ++-- .../gf/SaplingInitializeSystem.java | 3 +- .../gf/generator/PlantGrowthDefinition.java | 14 ++++----- .../grass/AdvancedStagesGrowthDefinition.java | 22 ++++++-------- .../grass/ReplaceBlockGrowthDefinition.java | 29 +++++++++---------- .../LSystemBasedTreeGrowthDefinition.java | 8 ++--- 7 files changed, 41 insertions(+), 43 deletions(-) diff --git a/module.txt b/module.txt index f51ca94..37e9fbb 100644 --- a/module.txt +++ b/module.txt @@ -11,7 +11,8 @@ }, { "id": "ClimateConditions", - "minVersion": "1.0.0" + "minVersion": "1.0.0", + "optional": "true" }, { "id": "CoreAssets", diff --git a/src/main/java/org/terasology/gf/PlantGrowingSystem.java b/src/main/java/org/terasology/gf/PlantGrowingSystem.java index f6566bc..614cbf0 100644 --- a/src/main/java/org/terasology/gf/PlantGrowingSystem.java +++ b/src/main/java/org/terasology/gf/PlantGrowingSystem.java @@ -33,6 +33,7 @@ import org.terasology.engine.world.WorldProvider; import org.terasology.engine.world.block.BlockComponent; import org.terasology.gf.generator.PlantGrowthDefinition; +import org.terasology.gf.util.EnvironmentLocalParameters; import org.terasology.randomUpdate.RandomUpdateEvent; /** @@ -64,7 +65,7 @@ public void updatePlant(DelayedActionTriggeredEvent event, EntityRef plant, Livi PerformanceMonitor.startActivity("GrowingFlora - Updating plant"); try { PlantGrowthDefinition plantDefinition = plantRegistry.getPlantGrowthDefinition(plantComponent.type); - Long updateDelay = plantDefinition.requestedUpdatePlant(worldProvider, environmentSystem, blockEntityRegistry, plant); + Long updateDelay = plantDefinition.requestedUpdatePlant(worldProvider, new EnvironmentLocalParameters(environmentSystem, blockComponent.getPosition()), blockEntityRegistry, plant); if (updateDelay != null) { delayManager.addDelayedAction(plant, UPDATE_PLANT_ACTION_ID, updateDelay); } @@ -79,7 +80,7 @@ public void randomPlantUpdate(RandomUpdateEvent event, EntityRef plant, LivingPl PerformanceMonitor.startActivity("GrowingFlora - Updating plant"); try { PlantGrowthDefinition plantDefinition = plantRegistry.getPlantGrowthDefinition(plantComponent.type); - if (plantDefinition.randomUpdatePlant(worldProvider, environmentSystem, blockEntityRegistry, plant)) { + if (plantDefinition.randomUpdatePlant(worldProvider, new EnvironmentLocalParameters(environmentSystem, blockComponent.getPosition()), blockEntityRegistry, plant)) { if (delayManager.hasDelayedAction(plant, UPDATE_PLANT_ACTION_ID)) { delayManager.cancelDelayedAction(plant, UPDATE_PLANT_ACTION_ID); } diff --git a/src/main/java/org/terasology/gf/SaplingInitializeSystem.java b/src/main/java/org/terasology/gf/SaplingInitializeSystem.java index 0c5d873..6cf8690 100644 --- a/src/main/java/org/terasology/gf/SaplingInitializeSystem.java +++ b/src/main/java/org/terasology/gf/SaplingInitializeSystem.java @@ -33,6 +33,7 @@ import org.terasology.engine.world.WorldProvider; import org.terasology.engine.world.block.BlockComponent; import org.terasology.gf.generator.PlantGrowthDefinition; +import org.terasology.gf.util.EnvironmentLocalParameters; /** * @author Marcin Sciesinski @@ -72,7 +73,7 @@ public void plantedSapling(OnAddedComponent event, EntityRef sapling, LivingPlan Vector3i blockLocation = blockComponent.getPosition(new Vector3i()); String saplingType = livingPlant.type; PlantGrowthDefinition plantDefinition = plantRegistry.getPlantGrowthDefinition(saplingType); - Long updateDelay = plantDefinition.initializePlantedPlant(worldProvider, climateConditionsSystem, blockEntityRegistry, sapling); + Long updateDelay = plantDefinition.initializePlantedPlant(worldProvider, new EnvironmentLocalParameters(climateConditionsSystem, blockLocation), blockEntityRegistry, sapling); EntityRef blockEntity = blockEntityRegistry.getBlockEntityAt(blockLocation); if (blockEntity.hasComponent(PlantedSaplingComponent.class)) { blockEntity.removeComponent(PlantedSaplingComponent.class); diff --git a/src/main/java/org/terasology/gf/generator/PlantGrowthDefinition.java b/src/main/java/org/terasology/gf/generator/PlantGrowthDefinition.java index e66fcf8..ddc0cfc 100644 --- a/src/main/java/org/terasology/gf/generator/PlantGrowthDefinition.java +++ b/src/main/java/org/terasology/gf/generator/PlantGrowthDefinition.java @@ -15,13 +15,13 @@ */ package org.terasology.gf.generator; -import org.terasology.climateConditions.ClimateConditionsSystem; import org.terasology.engine.entitySystem.entity.EntityRef; import org.terasology.engine.world.BlockEntityRegistry; import org.terasology.engine.world.WorldProvider; import org.terasology.engine.world.chunks.Chunk; import org.terasology.engine.world.generation.Region; import org.terasology.engine.world.generator.plugin.WorldGeneratorPlugin; +import org.terasology.gf.util.LocalParameters; /** * @author Marcin Sciesinski @@ -45,31 +45,31 @@ public interface PlantGrowthDefinition extends WorldGeneratorPlugin { * Returns how long to next update (if any). If null is returned, it's considered that the sapling was not initialized. * * @param worldProvider - * @param environmentSystem + * @param localParameters * @param blockEntityRegistry * @param plant @return */ - Long initializePlantedPlant(WorldProvider worldProvider, ClimateConditionsSystem environmentSystem, BlockEntityRegistry blockEntityRegistry, EntityRef plant); + Long initializePlantedPlant(WorldProvider worldProvider, LocalParameters localParameters, BlockEntityRegistry blockEntityRegistry, EntityRef plant); /** * Returns how long to next update (if any). If null is returned, it's considered this plant requires no more updates. * * @param worldProvider - * @param environmentSystem + * @param localParameters * @param blockEntityRegistry * @param plant @return * @return */ - Long requestedUpdatePlant(WorldProvider worldProvider, ClimateConditionsSystem environmentSystem, BlockEntityRegistry blockEntityRegistry, EntityRef plant); + Long requestedUpdatePlant(WorldProvider worldProvider, LocalParameters localParameters, BlockEntityRegistry blockEntityRegistry, EntityRef plant); /** * Called randomly on a plant. If true is returned - this plant will no longer receive requested updates. * * @param worldProvider - * @param environmentSystem + * @param localParameters * @param blockEntityRegistry * @param plant * @return */ - boolean randomUpdatePlant(WorldProvider worldProvider, ClimateConditionsSystem environmentSystem, BlockEntityRegistry blockEntityRegistry, EntityRef plant); + boolean randomUpdatePlant(WorldProvider worldProvider, LocalParameters localParameters, BlockEntityRegistry blockEntityRegistry, EntityRef plant); } diff --git a/src/main/java/org/terasology/gf/grass/AdvancedStagesGrowthDefinition.java b/src/main/java/org/terasology/gf/grass/AdvancedStagesGrowthDefinition.java index 1ee3534..8f07206 100644 --- a/src/main/java/org/terasology/gf/grass/AdvancedStagesGrowthDefinition.java +++ b/src/main/java/org/terasology/gf/grass/AdvancedStagesGrowthDefinition.java @@ -5,10 +5,8 @@ import com.google.common.base.Function; import com.google.common.base.Predicate; import org.joml.Vector3i; -import org.terasology.gf.util.EnvironmentLocalParameters; import org.terasology.gf.util.GenerationLocalParameters; import org.terasology.gf.util.LocalParameters; -import org.terasology.climateConditions.ClimateConditionsSystem; import org.terasology.engine.entitySystem.entity.EntityRef; import org.terasology.engine.registry.CoreRegistry; import org.terasology.engine.world.BlockEntityRegistry; @@ -68,19 +66,17 @@ public Long initializeGeneratedPlant(WorldProvider worldProvider, BlockEntityReg } @Override - public Long initializePlantedPlant(WorldProvider worldProvider, ClimateConditionsSystem environmentSystem, BlockEntityRegistry blockEntityRegistry, EntityRef plant) { - BlockComponent block = plant.getComponent(BlockComponent.class); - Vector3i position = block.getPosition(new Vector3i()); - return growthTimeFunction.apply(new EnvironmentLocalParameters(environmentSystem, position)); + public Long initializePlantedPlant(WorldProvider worldProvider, LocalParameters localParameters, BlockEntityRegistry blockEntityRegistry, EntityRef plant) { + return growthTimeFunction.apply(localParameters); } @Override - public Long requestedUpdatePlant(WorldProvider worldProvider, ClimateConditionsSystem environmentSystem, BlockEntityRegistry blockEntityRegistry, EntityRef plant) { + public Long requestedUpdatePlant(WorldProvider worldProvider, LocalParameters localParameters, BlockEntityRegistry blockEntityRegistry, EntityRef plant) { BlockManager blockManager = CoreRegistry.get(BlockManager.class); BlockComponent block = plant.getComponent(BlockComponent.class); Vector3i position = block.getPosition(new Vector3i()); - if (shouldDie(environmentSystem, position)) { + if (shouldDie(localParameters)) { replaceBlock(worldProvider, blockManager, plant, position, deadPlantBlock, true); return null; @@ -94,7 +90,7 @@ public Long requestedUpdatePlant(WorldProvider worldProvider, ClimateConditionsS replaceBlock(worldProvider, blockManager, plant, position, nextStage, !hasMoreStages); if (hasMoreStages) { - return growthTimeFunction.apply(new EnvironmentLocalParameters(environmentSystem, position)); + return growthTimeFunction.apply(localParameters); } else { // Entered the last phase return null; @@ -103,12 +99,12 @@ public Long requestedUpdatePlant(WorldProvider worldProvider, ClimateConditionsS } @Override - public boolean randomUpdatePlant(WorldProvider worldProvider, ClimateConditionsSystem environmentSystem, BlockEntityRegistry blockEntityRegistry, EntityRef plant) { + public boolean randomUpdatePlant(WorldProvider worldProvider, LocalParameters localParameters, BlockEntityRegistry blockEntityRegistry, EntityRef plant) { BlockManager blockManager = CoreRegistry.get(BlockManager.class); BlockComponent block = plant.getComponent(BlockComponent.class); Vector3i position = block.getPosition(new Vector3i()); - if (shouldDie(environmentSystem, position)) { + if (shouldDie(localParameters)) { replaceBlock(worldProvider, blockManager, plant, position, deadPlantBlock, true); return true; @@ -120,7 +116,7 @@ protected void replaceBlock(WorldProvider worldProvider, BlockManager blockManag worldProvider.setBlock(position, blockManager.getBlock(nextStage)); } - private boolean shouldDie(ClimateConditionsSystem environmentSystem, Vector3i position) { - return deathCondition != null && deathCondition.apply(new EnvironmentLocalParameters(environmentSystem, position)); + private boolean shouldDie(LocalParameters localParameters) { + return deathCondition != null && deathCondition.apply(localParameters); } } diff --git a/src/main/java/org/terasology/gf/grass/ReplaceBlockGrowthDefinition.java b/src/main/java/org/terasology/gf/grass/ReplaceBlockGrowthDefinition.java index 8929de1..9d06f8c 100644 --- a/src/main/java/org/terasology/gf/grass/ReplaceBlockGrowthDefinition.java +++ b/src/main/java/org/terasology/gf/grass/ReplaceBlockGrowthDefinition.java @@ -5,10 +5,9 @@ import com.google.common.base.Function; import com.google.common.base.Predicate; import org.joml.Vector3i; -import org.terasology.gf.util.EnvironmentLocalParameters; +import org.joml.Vector3ic; import org.terasology.gf.util.GenerationLocalParameters; import org.terasology.gf.util.LocalParameters; -import org.terasology.climateConditions.ClimateConditionsSystem; import org.terasology.engine.entitySystem.entity.EntityRef; import org.terasology.engine.registry.CoreRegistry; import org.terasology.engine.utilities.random.FastRandom; @@ -91,7 +90,7 @@ public Long initializeGeneratedPlant(WorldProvider worldProvider, BlockEntityReg } @Override - public Long initializePlantedPlant(WorldProvider worldProvider, ClimateConditionsSystem environmentSystem, BlockEntityRegistry blockEntityRegistry, EntityRef plant) { + public Long initializePlantedPlant(WorldProvider worldProvider, LocalParameters localParameters, BlockEntityRegistry blockEntityRegistry, EntityRef plant) { if (growthIntervals.size() > 0) { return growthIntervals.get(0); } else { @@ -100,19 +99,19 @@ public Long initializePlantedPlant(WorldProvider worldProvider, ClimateCondition } @Override - public Long requestedUpdatePlant(WorldProvider worldProvider, ClimateConditionsSystem environmentSystem, BlockEntityRegistry blockEntityRegistry, EntityRef plant) { + public Long requestedUpdatePlant(WorldProvider worldProvider, LocalParameters localParameters, BlockEntityRegistry blockEntityRegistry, EntityRef plant) { BlockManager blockManager = CoreRegistry.get(BlockManager.class); BlockComponent block = plant.getComponent(BlockComponent.class); - Vector3i position = block.getPosition(new Vector3i()); + Vector3ic position = block.getPosition(); - if (shouldDie(environmentSystem, position)) { + if (shouldDie(localParameters)) { replaceBlock(worldProvider, blockManager, plant, position, deadPlantBlock, true); return null; } else { int currentIndex = plantStages.indexOf(block.getBlock().getURI()); - if (shouldGrow(plant, environmentSystem, position)) { + if (shouldGrow(plant, localParameters)) { int nextIndex = currentIndex + 1; BlockUri nextStage = plantStages.get(nextIndex); final boolean hasMoreStages = nextIndex < plantStages.size() - 1; @@ -131,12 +130,12 @@ public Long requestedUpdatePlant(WorldProvider worldProvider, ClimateConditionsS } @Override - public boolean randomUpdatePlant(WorldProvider worldProvider, ClimateConditionsSystem environmentSystem, BlockEntityRegistry blockEntityRegistry, EntityRef plant) { + public boolean randomUpdatePlant(WorldProvider worldProvider, LocalParameters localParameters, BlockEntityRegistry blockEntityRegistry, EntityRef plant) { BlockManager blockManager = CoreRegistry.get(BlockManager.class); BlockComponent block = plant.getComponent(BlockComponent.class); - Vector3i position = block.getPosition(new Vector3i()); + Vector3ic position = block.getPosition(); - if (shouldDie(environmentSystem, position)) { + if (shouldDie(localParameters)) { replaceBlock(worldProvider, blockManager, plant, position, deadPlantBlock, true); return true; @@ -144,18 +143,18 @@ public boolean randomUpdatePlant(WorldProvider worldProvider, ClimateConditionsS return false; } - protected void replaceBlock(WorldProvider worldProvider, BlockManager blockManager, EntityRef plant, Vector3i position, BlockUri nextStage, boolean isLast) { + protected void replaceBlock(WorldProvider worldProvider, BlockManager blockManager, EntityRef plant, Vector3ic position, BlockUri nextStage, boolean isLast) { worldProvider.setBlock(position, blockManager.getBlock(nextStage)); } - private boolean shouldDie(ClimateConditionsSystem environmentSystem, Vector3i position) { - return deathCondition != null && deathCondition.apply(new EnvironmentLocalParameters(environmentSystem, position)); + private boolean shouldDie(LocalParameters localParameters) { + return deathCondition != null && deathCondition.apply(localParameters); } - private boolean shouldGrow(EntityRef plant, ClimateConditionsSystem environmentSystem, Vector3i position) { + private boolean shouldGrow(EntityRef plant, LocalParameters localParameters) { float chance = 1f; if (growthChance != null) { - chance = growthChance.apply(new EnvironmentLocalParameters(environmentSystem, position)); + chance = growthChance.apply(localParameters); } GetGrowthChance event = new GetGrowthChance(chance); plant.send(event); diff --git a/src/main/java/org/terasology/gf/tree/lsystem/LSystemBasedTreeGrowthDefinition.java b/src/main/java/org/terasology/gf/tree/lsystem/LSystemBasedTreeGrowthDefinition.java index f27c7b0..97f0ef7 100644 --- a/src/main/java/org/terasology/gf/tree/lsystem/LSystemBasedTreeGrowthDefinition.java +++ b/src/main/java/org/terasology/gf/tree/lsystem/LSystemBasedTreeGrowthDefinition.java @@ -3,13 +3,13 @@ package org.terasology.gf.tree.lsystem; import org.joml.Vector3i; -import org.terasology.climateConditions.ClimateConditionsSystem; import org.terasology.engine.entitySystem.entity.EntityRef; import org.terasology.engine.world.BlockEntityRegistry; import org.terasology.engine.world.WorldProvider; import org.terasology.engine.world.chunks.Chunk; import org.terasology.engine.world.generation.Region; import org.terasology.gf.generator.ConnectedPlantGrowthDefinition; +import org.terasology.gf.util.LocalParameters; import java.util.Collection; @@ -29,17 +29,17 @@ public final Long initializeGeneratedPlant(WorldProvider worldProvider, BlockEnt } @Override - public Long initializePlantedPlant(WorldProvider worldProvider, ClimateConditionsSystem environmentSystem, BlockEntityRegistry blockEntityRegistry, EntityRef plant) { + public Long initializePlantedPlant(WorldProvider worldProvider, LocalParameters localParameters, BlockEntityRegistry blockEntityRegistry, EntityRef plant) { return getTreeDefinition().setupPlantedSapling(plant); } @Override - public final Long requestedUpdatePlant(WorldProvider worldProvider, ClimateConditionsSystem environmentSystem, BlockEntityRegistry blockEntityRegistry, EntityRef plant) { + public final Long requestedUpdatePlant(WorldProvider worldProvider, LocalParameters localParameters, BlockEntityRegistry blockEntityRegistry, EntityRef plant) { return getTreeDefinition().updateTree(worldProvider, blockEntityRegistry, plant); } @Override - public boolean randomUpdatePlant(WorldProvider worldProvider, ClimateConditionsSystem environmentSystem, BlockEntityRegistry blockEntityRegistry, EntityRef plant) { + public boolean randomUpdatePlant(WorldProvider worldProvider, LocalParameters localParameters, BlockEntityRegistry blockEntityRegistry, EntityRef plant) { // Do nothing on random update return false; }