Optimize BiomeManager storage

This commit is contained in:
themode 2020-08-20 14:41:11 +02:00
parent f560f4cb0b
commit 6c6f470b24

View File

@ -1,13 +1,14 @@
package net.minestom.server.world.biomes;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import net.minestom.server.utils.NamespaceID;
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
import org.jglrxavpok.hephaistos.nbt.NBTList;
import org.jglrxavpok.hephaistos.nbt.NBTTypes;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
/**
* Allows servers to register custom dimensions. Also used during player joining to send the list of all existing dimensions.
@ -16,7 +17,7 @@ import java.util.List;
*/
public class BiomeManager {
private final List<Biome> biomes = new LinkedList<>();
private final Int2ObjectMap<Biome> biomes = new Int2ObjectOpenHashMap<>();
public BiomeManager() {
addBiome(Biome.PLAINS);
@ -28,17 +29,16 @@ public class BiomeManager {
* @param biome the biome to add
*/
public void addBiome(Biome biome) {
biomes.add(biome);
this.biomes.put(biome.getId(), biome);
}
/**
* Removes a biome. This does NOT send the new list to players.
*
* @param biome the biome to remove
* @return true if the biome type was removed, false if it was not present before
*/
public boolean removeBiome(Biome biome) {
return biomes.remove(biome);
public void removeBiome(Biome biome) {
this.biomes.remove(biome.getId());
}
/**
@ -46,25 +46,23 @@ public class BiomeManager {
*
* @return an immutable copy of the biomes already registered
*/
public List<Biome> unmodifiableList() {
return Collections.unmodifiableList(biomes);
public Collection<Biome> unmodifiableCollection() {
return Collections.unmodifiableCollection(biomes.values());
}
// TODO optimize for fast get
/**
* Get a biome by its id
*
* @param id the id of the biome
* @return the {@link Biome} linked to this id
*/
public Biome getById(int id) {
Biome biome = null;
for (final Biome biomeT : biomes) {
if (biomeT.getId() == id) {
biome = biomeT;
break;
}
}
return biome;
return biomes.get(id);
}
public Biome getByName(NamespaceID namespaceID) {
Biome biome = null;
for (final Biome biomeT : biomes) {
for (final Biome biomeT : biomes.values()) {
if (biomeT.getName().equals(namespaceID)) {
biome = biomeT;
break;
@ -77,7 +75,7 @@ public class BiomeManager {
NBTCompound biomes = new NBTCompound();
biomes.setString("type", "minecraft:worldgen/biome");
NBTList<NBTCompound> biomesList = new NBTList<>(NBTTypes.TAG_Compound);
for (Biome biome : this.biomes) {
for (Biome biome : this.biomes.values()) {
biomesList.add(biome.toNbt());
}
biomes.set("value", biomesList);