Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 8 additions & 21 deletions src/main/java/org/terasology/biomesAPI/Biome.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,8 @@
/*
* Copyright 2018 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.
*/
// Copyright 2020 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.biomesAPI;

import org.terasology.naming.Name;
import org.terasology.gestalt.naming.Name;

/**
* Biomes can be assigned to different blocks during worldgen as well as on runtime, to provide additional metadata
Expand All @@ -28,8 +15,8 @@
public interface Biome {

/**
* @return An identifier that includes both the Module the biome originates from
* and a unique biome id (unique to that module).
* @return An identifier that includes both the Module the biome originates from and a unique biome id (unique to
* that module).
*/
Name getId();

Expand All @@ -41,9 +28,9 @@ public interface Biome {
/**
* Biome hashCode must be deterministic, non-zero, and unique for every biome.
* <p>
* Please consider overriding this method to return constant values, hard-coded for each of the biomes.
* No assumptions should however be made from any external module using biomes about their constant value,
* i.e. modules should always retrieve biome hash using this function, and not hard-code any constant values.
* Please consider overriding this method to return constant values, hard-coded for each of the biomes. No
* assumptions should however be made from any external module using biomes about their constant value, i.e. modules
* should always retrieve biome hash using this function, and not hard-code any constant values.
*
* @return Hashcode of the biome
*/
Expand Down
98 changes: 40 additions & 58 deletions src/main/java/org/terasology/biomesAPI/BiomeManager.java
Original file line number Diff line number Diff line change
@@ -1,47 +1,33 @@
/*
* Copyright 2018 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.
*/
// Copyright 2020 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.biomesAPI;

import com.google.common.base.Preconditions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.terasology.entitySystem.entity.EntityManager;
import org.terasology.entitySystem.entity.EntityRef;
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.engine.entitySystem.entity.EntityManager;
import org.terasology.engine.entitySystem.entity.EntityRef;
import org.terasology.engine.entitySystem.event.ReceiveEvent;
import org.terasology.engine.entitySystem.systems.BaseComponentSystem;
import org.terasology.engine.entitySystem.systems.RegisterSystem;
import org.terasology.engine.logic.location.LocationComponent;
import org.terasology.engine.logic.players.PlayerCharacterComponent;
import org.terasology.engine.logic.players.event.OnPlayerSpawnedEvent;
import org.terasology.engine.physics.events.MovedEvent;
import org.terasology.engine.registry.CoreRegistry;
import org.terasology.engine.registry.In;
import org.terasology.engine.registry.Share;
import org.terasology.engine.rendering.nui.NUIManager;
import org.terasology.engine.rendering.nui.layers.ingame.metrics.DebugMetricsSystem;
import org.terasology.engine.world.WorldProvider;
import org.terasology.engine.world.block.Block;
import org.terasology.engine.world.chunks.CoreChunk;
import org.terasology.engine.world.chunks.blockdata.ExtraBlockDataManager;
import org.terasology.engine.world.chunks.blockdata.ExtraDataSystem;
import org.terasology.engine.world.chunks.blockdata.RegisterExtraData;
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;
import java.util.List;
import java.util.Map;
Expand All @@ -52,26 +38,28 @@
@RegisterSystem
@ExtraDataSystem
public class BiomeManager extends BaseComponentSystem implements BiomeRegistry {
private static final Logger LOGGER = LoggerFactory.getLogger(BiomeManager.class);
private final Map<Short, Biome> biomeMap = new HashMap<>();
@In
DebugMetricsSystem debugMetricsSystem;
@In
private EntityManager entityManager;

@In
private NUIManager nuiManager;

@In
private WorldProvider worldProvider;

@In
DebugMetricsSystem debugMetricsSystem;

private final Map<Short, Biome> biomeMap = new HashMap<>();

private static final Logger LOGGER = LoggerFactory.getLogger(BiomeManager.class);

private BiomesMetricsMode metricsMode;

private int biomeHashIndex;

/**
* Blocks have id, no matter what kind of blocks they are.
*/
@RegisterExtraData(name = "BiomesAPI.biomeHash", bitSize = 16)
public static boolean hasBiome(Block block) {
return true;
}

@Override
public Optional<Biome> getBiome(Vector3i pos) {
return getBiome(pos.x, pos.y, pos.z);
Expand Down Expand Up @@ -121,17 +109,10 @@ public void preBegin() {
debugMetricsSystem.register(metricsMode);
}

/**
* Blocks have id, no matter what kind of blocks they are.
*/
@RegisterExtraData(name = "BiomesAPI.biomeHash", bitSize = 16)
public static boolean hasBiome(Block block) {
return true;
}

@Override
public void registerBiome(Biome biome) {
Preconditions.checkArgument(!biomeMap.containsKey(biome.biomeHash()), "Registering biome with same hash as one of previously registered biomes!");
Preconditions.checkArgument(!biomeMap.containsKey(biome.biomeHash()), "Registering biome with same hash as " +
"one of previously registered biomes!");
biomeMap.put(biome.biomeHash(), biome);
LOGGER.info("Registered biome " + biome.getId() + " with id " + biome.biomeHash());
}
Expand Down Expand Up @@ -163,10 +144,11 @@ public void checkBiomeChangeEvent(MovedEvent event, EntityRef entity) {
}

@ReceiveEvent(components = PlayerCharacterComponent.class)
public void checkPlayerSpawnedEvent(OnPlayerSpawnedEvent event, EntityRef entity, LocationComponent locationComponent) {
public void checkPlayerSpawnedEvent(OnPlayerSpawnedEvent event, EntityRef entity,
LocationComponent locationComponent) {
Vector3i spawnPos = new Vector3i(locationComponent.getWorldPosition());
getBiome(spawnPos)
.ifPresent(spawnBiome -> metricsMode.setBiome(spawnBiome.getId().toString()));
.ifPresent(spawnBiome -> metricsMode.setBiome(spawnBiome.getId().toString()));

}
}
41 changes: 14 additions & 27 deletions src/main/java/org/terasology/biomesAPI/BiomeRegistry.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,10 @@
/*
* Copyright 2018 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.
*/
// Copyright 2020 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.biomesAPI;

import org.terasology.entitySystem.systems.ComponentSystem;
import org.terasology.engine.entitySystem.systems.ComponentSystem;
import org.terasology.engine.world.chunks.CoreChunk;
import org.terasology.math.geom.Vector3i;
import org.terasology.world.chunks.CoreChunk;

import java.util.Collection;
import java.util.Optional;
Expand All @@ -26,8 +13,8 @@ public interface BiomeRegistry {
/**
* This method must be called on every startup to register biomes with this module.
* <p>
* Biomes should be ideally registered through new {@link ComponentSystem}, in the method
* {@link ComponentSystem#preBegin()}.
* Biomes should be ideally registered through new {@link ComponentSystem}, in the method {@link
* ComponentSystem#preBegin()}.
*
* @param biome Biome to register
*/
Expand All @@ -37,9 +24,9 @@ public interface BiomeRegistry {
* Sets specified biome at position in world.
*
* @param biome Biome to set
* @param x x position of the block to set
* @param y y position of the block to set
* @param z z position of the block to set
* @param x x position of the block to set
* @param y y position of the block to set
* @param z z position of the block to set
*/
void setBiome(Biome biome, int x, int y, int z);

Expand All @@ -48,17 +35,17 @@ public interface BiomeRegistry {
*
* @param biome Biome to set
* @param chunk Chunk where to set the biome
* @param relX x position of the block to set, relative to the chunk
* @param relY y position of the block to set, relative to the chunk
* @param relZ z position of the block to set, relative to the chunk
* @param relX x position of the block to set, relative to the chunk
* @param relY y position of the block to set, relative to the chunk
* @param relZ z position of the block to set, relative to the chunk
*/
void setBiome(Biome biome, CoreChunk chunk, int relX, int relY, int relZ);

/**
* Sets specified biome at position in world.
*
* @param biome Biome to set
* @param pos Position of the block to set
* @param pos Position of the block to set
*/
void setBiome(Biome biome, Vector3i pos);

Expand All @@ -67,7 +54,7 @@ public interface BiomeRegistry {
*
* @param biome Biome to set
* @param chunk Chunk where to set the biome
* @param pos Position of the block to set
* @param pos Position of the block to set
*/
void setBiome(Biome biome, CoreChunk chunk, Vector3i pos);

Expand Down
25 changes: 6 additions & 19 deletions src/main/java/org/terasology/biomesAPI/BiomesMetricsMode.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,14 @@
/*
* 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.
*/
// Copyright 2020 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.biomesAPI;

import org.terasology.rendering.nui.layers.ingame.metrics.MetricsMode;
import org.terasology.engine.rendering.nui.layers.ingame.metrics.MetricsMode;

/**
* Display the name of the biome the player is currently located in in the debug overlay.
* <p>
* Current biome is set when OnBiomeChanged event is triggered
* biomeName is polled whenever MetricsMode values are updated
* Current biome is set when OnBiomeChanged event is triggered biomeName is polled whenever MetricsMode values are
* updated
*/
class BiomesMetricsMode extends MetricsMode {

Expand All @@ -33,7 +20,7 @@ class BiomesMetricsMode extends MetricsMode {

@Override
public String getMetrics() {
return getName()+"\n"+biomeName;
return getName() + "\n" + biomeName;
}

@Override
Expand Down
23 changes: 5 additions & 18 deletions src/main/java/org/terasology/biomesAPI/OnBiomeChangedEvent.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,16 @@
/*
* Copyright 2018 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.
*/
// Copyright 2020 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.biomesAPI;

import org.terasology.entitySystem.event.Event;
import org.terasology.engine.entitySystem.event.Event;

/**
* This event is thrown to entities with {@link org.terasology.logic.players.PlayerCharacterComponent} whenever they
* change the biome they are in.
*/
public class OnBiomeChangedEvent implements Event {
private Biome oldBiome;
private Biome newBiome;
private final Biome oldBiome;
private final Biome newBiome;

public OnBiomeChangedEvent(Biome oldBiome, Biome newBiome) {
this.oldBiome = oldBiome;
Expand Down