diff --git a/src/main/java/org/terasology/biomesAPI/BiomeManager.java b/src/main/java/org/terasology/biomesAPI/BiomeManager.java index 6ddec81..e5d9f17 100644 --- a/src/main/java/org/terasology/biomesAPI/BiomeManager.java +++ b/src/main/java/org/terasology/biomesAPI/BiomeManager.java @@ -23,17 +23,23 @@ import org.terasology.entitySystem.event.ReceiveEvent; import org.terasology.entitySystem.systems.BaseComponentSystem; import org.terasology.entitySystem.systems.RegisterSystem; +import org.terasology.logic.location.LocationComponent; import org.terasology.logic.players.PlayerCharacterComponent; +import org.terasology.logic.players.event.OnPlayerSpawnedEvent; import org.terasology.math.geom.Vector3f; import org.terasology.math.geom.Vector3i; import org.terasology.physics.events.MovedEvent; +import org.terasology.registry.CoreRegistry; import org.terasology.registry.In; import org.terasology.registry.Share; import org.terasology.world.WorldProvider; import org.terasology.world.block.Block; import org.terasology.world.chunks.CoreChunk; +import org.terasology.world.chunks.blockdata.ExtraBlockDataManager; import org.terasology.world.chunks.blockdata.ExtraDataSystem; import org.terasology.world.chunks.blockdata.RegisterExtraData; +import org.terasology.rendering.nui.NUIManager; +import org.terasology.rendering.nui.layers.ingame.metrics.DebugMetricsSystem; import java.util.Collection; import java.util.HashMap; @@ -49,13 +55,23 @@ public class BiomeManager extends BaseComponentSystem implements BiomeRegistry { @In private EntityManager entityManager; + @In + private NUIManager nuiManager; + @In private WorldProvider worldProvider; + @In + DebugMetricsSystem debugMetricsSystem; + private final Map biomeMap = new HashMap<>(); private static final Logger LOGGER = LoggerFactory.getLogger(BiomeManager.class); + private BiomesMetricsMode metricsMode; + + private int biomeHashIndex; + @Override public Optional getBiome(Vector3i pos) { return getBiome(pos.x, pos.y, pos.z); @@ -79,7 +95,9 @@ public void setBiome(Biome biome, int x, int y, int z) { @Override public void setBiome(Biome biome, CoreChunk chunk, int relX, int relY, int relZ) { - setBiome(biome, chunk.chunkToWorldPosition(relX, relY, relZ)); + Preconditions.checkArgument(biomeMap.containsKey(biome.biomeHash()), "Trying to use non-registered biome!"); + biomeHashIndex = CoreRegistry.get(ExtraBlockDataManager.class).getSlotNumber("BiomesAPI.biomeHash"); + chunk.setExtraData(biomeHashIndex, new Vector3i(relX, relY, relZ), biome.biomeHash()); } @Override @@ -89,7 +107,7 @@ public void setBiome(Biome biome, Vector3i pos) { @Override public void setBiome(Biome biome, CoreChunk chunk, Vector3i pos) { - setBiome(biome, chunk.chunkToWorldPosition(pos)); + setBiome(biome, chunk, pos.x, pos.y, pos.z); } @Override @@ -97,6 +115,12 @@ public List getRegisteredBiomes(Class biomeClass) { return biomeMap.values().stream().filter(biomeClass::isInstance).map(biomeClass::cast).collect(Collectors.toList()); } + @Override + public void preBegin() { + metricsMode = new BiomesMetricsMode(); + debugMetricsSystem.register(metricsMode); + } + /** * Blocks have id, no matter what kind of blocks they are. */ @@ -128,11 +152,21 @@ public void checkBiomeChangeEvent(MovedEvent event, EntityRef entity) { if (!oldBiomeOptional.isPresent()) { return; } + Biome newBiome = newBiomeOptional.get(); Biome oldBiome = oldBiomeOptional.get(); if (oldBiome != newBiome) { entity.send(new OnBiomeChangedEvent(oldBiome, newBiome)); + metricsMode.setBiome(newBiome.getId()); } } } + + @ReceiveEvent(components = PlayerCharacterComponent.class) + public void checkPlayerSpawnedEvent(OnPlayerSpawnedEvent event, EntityRef entity, LocationComponent locationComponent) { + Vector3i spawnPos = new Vector3i(locationComponent.getWorldPosition()); + getBiome(spawnPos) + .ifPresent(spawnBiome -> metricsMode.setBiome(spawnBiome.getId())); + + } } diff --git a/src/main/java/org/terasology/biomesAPI/BiomesMetricsMode.java b/src/main/java/org/terasology/biomesAPI/BiomesMetricsMode.java new file mode 100644 index 0000000..084e15b --- /dev/null +++ b/src/main/java/org/terasology/biomesAPI/BiomesMetricsMode.java @@ -0,0 +1,52 @@ +/* + * Copyright 2020 MovingBlocks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.terasology.biomesAPI; + +import org.terasology.rendering.nui.layers.ingame.metrics.MetricsMode; + +/** + * Display the name of the biome the player is currently located in in the debug overlay. + *

+ * Current biome is set when OnBiomeChanged event is triggered + * biomeName is polled whenever MetricsMode values are updated + */ +class BiomesMetricsMode extends MetricsMode { + + private String biomeName; + + BiomesMetricsMode() { + super("\n- Biome Info -"); + } + + @Override + public String getMetrics() { + return getName()+"\n"+biomeName; + } + + @Override + public boolean isAvailable() { + return true; + } + + @Override + public boolean isPerformanceManagerMode() { + return false; + } + + public void setBiome(String biomeName) { + this.biomeName = biomeName; + } +}