Modernize biome types

Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
TheMode 2021-11-12 18:23:39 +01:00
parent ba77a9ea10
commit 9198252dc1
3 changed files with 91 additions and 263 deletions

View File

@ -1,18 +1,15 @@
package net.minestom.server.world.biomes; package net.minestom.server.world.biomes;
import net.minestom.server.instance.Chunk;
import net.minestom.server.utils.NamespaceID; import net.minestom.server.utils.NamespaceID;
import net.minestom.server.utils.validate.Check; import net.minestom.server.utils.validate.Check;
import net.minestom.server.world.DimensionType;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jglrxavpok.hephaistos.nbt.NBTCompound; import org.jglrxavpok.hephaistos.nbt.NBTCompound;
import java.util.Locale;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
public class Biome { public final class Biome {
public static final AtomicInteger ID_COUNTER = new AtomicInteger(0); public static final AtomicInteger ID_COUNTER = new AtomicInteger(0);
private static final BiomeEffects DEFAULT_EFFECTS = BiomeEffects.builder() private static final BiomeEffects DEFAULT_EFFECTS = BiomeEffects.builder()
.fogColor(0xC0D8FF) .fogColor(0xC0D8FF)
.skyColor(0x78A7FF) .skyColor(0x78A7FF)
@ -41,9 +38,9 @@ public class Biome {
private final Category category; private final Category category;
private final BiomeEffects effects; private final BiomeEffects effects;
private final Precipitation precipitation; private final Precipitation precipitation;
private final TemperatureModifier temperature_modifier; private final TemperatureModifier temperatureModifier;
Biome(NamespaceID name, float depth, float temperature, float scale, float downfall, Category category, BiomeEffects effects, Precipitation precipitation, TemperatureModifier temperature_modifier) { Biome(NamespaceID name, float depth, float temperature, float scale, float downfall, Category category, BiomeEffects effects, Precipitation precipitation, TemperatureModifier temperatureModifier) {
this.name = name; this.name = name;
this.depth = depth; this.depth = depth;
this.temperature = temperature; this.temperature = temperature;
@ -52,127 +49,90 @@ public class Biome {
this.category = category; this.category = category;
this.effects = effects; this.effects = effects;
this.precipitation = precipitation; this.precipitation = precipitation;
this.temperature_modifier = temperature_modifier; this.temperatureModifier = temperatureModifier;
} }
public static BiomeBuilder builder() { public static Builder builder() {
return new BiomeBuilder(); return new Builder();
} }
@NotNull public @NotNull NBTCompound toNbt() {
public NBTCompound toNbt() {
Check.notNull(name, "The biome namespace cannot be null"); Check.notNull(name, "The biome namespace cannot be null");
Check.notNull(effects, "The biome effects cannot be null"); Check.notNull(effects, "The biome effects cannot be null");
NBTCompound nbt = new NBTCompound(); NBTCompound nbt = new NBTCompound();
nbt.setString("name", name.toString()); nbt.setString("name", name.toString());
nbt.setInt("id", getId()); nbt.setInt("id", id());
NBTCompound element = new NBTCompound(); NBTCompound element = new NBTCompound();
element.setFloat("depth", depth); element.setFloat("depth", depth);
element.setFloat("temperature", temperature); element.setFloat("temperature", temperature);
element.setFloat("scale", scale); element.setFloat("scale", scale);
element.setFloat("downfall", downfall); element.setFloat("downfall", downfall);
element.setString("category", category.getType()); element.setString("category", category.name().toLowerCase(Locale.ROOT));
element.setString("precipitation", precipitation.getType()); element.setString("precipitation", precipitation.name().toLowerCase(Locale.ROOT));
if (temperature_modifier != TemperatureModifier.NONE) if (temperatureModifier != TemperatureModifier.NONE)
element.setString("temperature_modifier", temperature_modifier.getType()); element.setString("temperature_modifier", temperatureModifier.name().toLowerCase(Locale.ROOT));
element.set("effects", effects.toNbt()); element.set("effects", effects.toNbt());
nbt.set("element", element); nbt.set("element", element);
return nbt; return nbt;
} }
public int getId() { public int id() {
return this.id; return this.id;
} }
public NamespaceID getName() { public NamespaceID name() {
return this.name; return this.name;
} }
public float getDepth() { public float depth() {
return this.depth; return this.depth;
} }
public float getTemperature() { public float temperature() {
return this.temperature; return this.temperature;
} }
public float getScale() { public float scale() {
return this.scale; return this.scale;
} }
public float getDownfall() { public float downfall() {
return this.downfall; return this.downfall;
} }
public Category getCategory() { public Category category() {
return this.category; return this.category;
} }
public BiomeEffects getEffects() { public BiomeEffects effects() {
return this.effects; return this.effects;
} }
public Precipitation getPrecipitation() { public Precipitation precipitation() {
return this.precipitation; return this.precipitation;
} }
public TemperatureModifier getTemperature_modifier() { public TemperatureModifier temperatureModifier() {
return this.temperature_modifier; return this.temperatureModifier;
} }
public enum Precipitation { public enum Precipitation {
RAIN("rain"), NONE("none"), SNOW("snow"); RAIN, NONE, SNOW;
String type;
Precipitation(String type) {
this.type = type;
}
public String getType() {
return this.type;
}
} }
public enum Category { public enum Category {
NONE("none"), TAIGA("taiga"), EXTREME_HILLS("extreme_hills"), JUNGLE("jungle"), MESA("mesa"), PLAINS("plains"), NONE, TAIGA, EXTREME_HILLS, JUNGLE, MESA, PLAINS,
SAVANNA("savanna"), ICY("icy"), THE_END("the_end"), BEACH("beach"), FOREST("forest"), OCEAN("ocean"), SAVANNA, ICY, THE_END, BEACH, FOREST, OCEAN,
DESERT("desert"), RIVER("river"), SWAMP("swamp"), MUSHROOM("mushroom"), NETHER("nether"); DESERT, RIVER, SWAMP, MUSHROOM, NETHER;
String type;
Category(String type) {
this.type = type;
}
public String getType() {
return type;
}
} }
public enum TemperatureModifier { public enum TemperatureModifier {
NONE("none"), FROZEN("frozen"); NONE, FROZEN;
String type;
TemperatureModifier(String type) {
this.type = type;
}
public String getType() {
return type;
}
} }
public static int getBiomeCount(DimensionType dimensionType) { public static final class Builder {
final int height = dimensionType.getLogicalHeight();
return 4 * 4 * 4 * (height / Chunk.CHUNK_SECTION_SIZE);
}
public static class BiomeBuilder {
private NamespaceID name; private NamespaceID name;
private float depth = 0.2f; private float depth = 0.2f;
private float temperature = 0.25f; private float temperature = 0.25f;
@ -183,50 +143,50 @@ public class Biome {
private Precipitation precipitation = Precipitation.RAIN; private Precipitation precipitation = Precipitation.RAIN;
private TemperatureModifier temperatureModifier = TemperatureModifier.NONE; private TemperatureModifier temperatureModifier = TemperatureModifier.NONE;
BiomeBuilder() { Builder() {
} }
public Biome.BiomeBuilder name(NamespaceID name) { public Builder name(NamespaceID name) {
this.name = name; this.name = name;
return this; return this;
} }
public Biome.BiomeBuilder depth(float depth) { public Builder depth(float depth) {
this.depth = depth; this.depth = depth;
return this; return this;
} }
public Biome.BiomeBuilder temperature(float temperature) { public Builder temperature(float temperature) {
this.temperature = temperature; this.temperature = temperature;
return this; return this;
} }
public Biome.BiomeBuilder scale(float scale) { public Builder scale(float scale) {
this.scale = scale; this.scale = scale;
return this; return this;
} }
public Biome.BiomeBuilder downfall(float downfall) { public Builder downfall(float downfall) {
this.downfall = downfall; this.downfall = downfall;
return this; return this;
} }
public Biome.BiomeBuilder category(Category category) { public Builder category(Category category) {
this.category = category; this.category = category;
return this; return this;
} }
public Biome.BiomeBuilder effects(BiomeEffects effects) { public Builder effects(BiomeEffects effects) {
this.effects = effects; this.effects = effects;
return this; return this;
} }
public Biome.BiomeBuilder precipitation(Precipitation precipitation) { public Builder precipitation(Precipitation precipitation) {
this.precipitation = precipitation; this.precipitation = precipitation;
return this; return this;
} }
public Biome.BiomeBuilder temperatureModifier(TemperatureModifier temperatureModifier) { public Builder temperatureModifier(TemperatureModifier temperatureModifier) {
this.temperatureModifier = temperatureModifier; this.temperatureModifier = temperatureModifier;
return this; return this;
} }

View File

@ -1,154 +1,50 @@
package net.minestom.server.world.biomes; package net.minestom.server.world.biomes;
import net.minestom.server.utils.NamespaceID; import net.minestom.server.utils.NamespaceID;
import org.jetbrains.annotations.NotNull;
import org.jglrxavpok.hephaistos.nbt.NBTCompound; import org.jglrxavpok.hephaistos.nbt.NBTCompound;
public class BiomeEffects { import java.util.Locale;
private final int fog_color; public record BiomeEffects(int fogColor, int skyColor, int waterColor, int waterFogColor, int foliageColor,
private final int sky_color; int grassColor,
private final int water_color; GrassColorModifier grassColorModifier, BiomeParticles biomeParticles,
private final int water_fog_color; NamespaceID ambientSound, MoodSound moodSound, AdditionsSound additionsSound,
Music music) {
private final int foliage_color; public static Builder builder() {
private final int grass_color; return new Builder();
private final GrassColorModifier grass_color_modifier;
private final BiomeParticles biomeParticles;
private final NamespaceID ambient_sound;
private final MoodSound mood_sound;
private final AdditionsSound additions_sound;
private final Music music;
BiomeEffects(int fog_color, int sky_color, int water_color, int water_fog_color, int foliage_color, int grass_color, GrassColorModifier grass_color_modifier, BiomeParticles biomeParticles, NamespaceID ambient_sound, MoodSound mood_sound, AdditionsSound additions_sound, Music music) {
this.fog_color = fog_color;
this.sky_color = sky_color;
this.water_color = water_color;
this.water_fog_color = water_fog_color;
this.foliage_color = foliage_color;
this.grass_color = grass_color;
this.grass_color_modifier = grass_color_modifier;
this.biomeParticles = biomeParticles;
this.ambient_sound = ambient_sound;
this.mood_sound = mood_sound;
this.additions_sound = additions_sound;
this.music = music;
}
public static BiomeEffectsBuilder builder() {
return new BiomeEffectsBuilder();
} }
public NBTCompound toNbt() { public NBTCompound toNbt() {
NBTCompound nbt = new NBTCompound(); NBTCompound nbt = new NBTCompound();
nbt.setInt("fog_color", fog_color); nbt.setInt("fog_color", fogColor);
if (foliage_color != -1) if (foliageColor != -1) nbt.setInt("foliage_color", foliageColor);
nbt.setInt("foliage_color", foliage_color); if (grassColor != -1) nbt.setInt("grass_color", grassColor);
if (grass_color != -1) nbt.setInt("sky_color", skyColor);
nbt.setInt("grass_color", grass_color); nbt.setInt("water_color", waterColor);
nbt.setInt("sky_color", sky_color); nbt.setInt("water_fog_color", waterFogColor);
nbt.setInt("water_color", water_color); if (grassColorModifier != null)
nbt.setInt("water_fog_color", water_fog_color); nbt.setString("grass_color_modifier", grassColorModifier.name().toLowerCase(Locale.ROOT));
if (grass_color_modifier != null)
nbt.setString("grass_color_modifier", grass_color_modifier.getType());
if (biomeParticles != null) if (biomeParticles != null)
nbt.set("particle", biomeParticles.toNbt()); nbt.set("particle", biomeParticles.toNbt());
if (ambient_sound != null) if (ambientSound != null)
nbt.setString("ambient_sound", ambient_sound.toString()); nbt.setString("ambient_sound", ambientSound.toString());
if (mood_sound != null) if (moodSound != null)
nbt.set("mood_sound", mood_sound.toNbt()); nbt.set("mood_sound", moodSound.toNbt());
if (additions_sound != null) if (additionsSound != null)
nbt.set("additions_sound", additions_sound.toNbt()); nbt.set("additions_sound", additionsSound.toNbt());
if (music != null) if (music != null)
nbt.set("music", music.toNbt()); nbt.set("music", music.toNbt());
return nbt; return nbt;
} }
public int getFog_color() {
return this.fog_color;
}
public int getSky_color() {
return this.sky_color;
}
public int getWater_color() {
return this.water_color;
}
public int getWater_fog_color() {
return this.water_fog_color;
}
public int getFoliage_color() {
return this.foliage_color;
}
public int getGrass_color() {
return this.grass_color;
}
public GrassColorModifier getGrass_color_modifier() {
return this.grass_color_modifier;
}
public BiomeParticles getBiomeParticles() {
return this.biomeParticles;
}
public NamespaceID getAmbient_sound() {
return this.ambient_sound;
}
public MoodSound getMood_sound() {
return this.mood_sound;
}
public AdditionsSound getAdditions_sound() {
return this.additions_sound;
}
public Music getMusic() {
return this.music;
}
public String toString() {
return "BiomeEffects(fog_color=" + this.getFog_color() + ", sky_color=" +
this.getSky_color() + ", water_color=" + this.getWater_color() + ", water_fog_color=" +
this.getWater_fog_color() + ", foliage_color=" + this.getFoliage_color() + ", grass_color=" +
this.getGrass_color() + ", grass_color_modifier=" + this.getGrass_color_modifier() + ", biomeParticles=" +
this.getBiomeParticles() + ", ambient_sound=" + this.getAmbient_sound() + ", mood_sound=" +
this.getMood_sound() + ", additions_sound=" + this.getAdditions_sound() + ", music=" + this.getMusic() + ")";
}
public enum GrassColorModifier { public enum GrassColorModifier {
NONE("none"), DARK_FOREST("dark_forest"), SWAMP("swamp"); NONE, DARK_FOREST, SWAMP;
String type;
GrassColorModifier(String type) {
this.type = type;
}
public String getType() {
return this.type;
}
} }
public static class MoodSound { public record MoodSound(NamespaceID sound, int tickDelay, int blockSearchExtent, double offset) {
public @NotNull NBTCompound toNbt() {
private final NamespaceID sound;
private final int tickDelay;
private final int blockSearchExtent;
private final double offset;
public MoodSound(NamespaceID sound, int tickDelay, int blockSearchExtent, double offset) {
this.sound = sound;
this.tickDelay = tickDelay;
this.blockSearchExtent = blockSearchExtent;
this.offset = offset;
}
public NBTCompound toNbt() {
NBTCompound nbt = new NBTCompound(); NBTCompound nbt = new NBTCompound();
nbt.setString("sound", sound.toString()); nbt.setString("sound", sound.toString());
nbt.setInt("tick_delay", tickDelay); nbt.setInt("tick_delay", tickDelay);
@ -156,43 +52,19 @@ public class BiomeEffects {
nbt.setDouble("offset", offset); nbt.setDouble("offset", offset);
return nbt; return nbt;
} }
} }
public static class AdditionsSound { public record AdditionsSound(NamespaceID sound, double tickChance) {
public @NotNull NBTCompound toNbt() {
private final NamespaceID sound;
private final double tickChance;
public AdditionsSound(NamespaceID sound, double tickChance) {
this.sound = sound;
this.tickChance = tickChance;
}
public NBTCompound toNbt() {
NBTCompound nbt = new NBTCompound(); NBTCompound nbt = new NBTCompound();
nbt.setString("sound", sound.toString()); nbt.setString("sound", sound.toString());
nbt.setDouble("tick_chance", tickChance); nbt.setDouble("tick_chance", tickChance);
return nbt; return nbt;
} }
} }
public static class Music { public record Music(NamespaceID sound, int minDelay, int maxDelay, boolean replaceCurrentMusic) {
public @NotNull NBTCompound toNbt() {
private final NamespaceID sound;
private final int minDelay;
private final int maxDelay;
private final boolean replaceCurrentMusic;
public Music(NamespaceID sound, int minDelay, int maxDelay, boolean replaceCurrentMusic) {
this.sound = sound;
this.minDelay = minDelay;
this.maxDelay = maxDelay;
this.replaceCurrentMusic = replaceCurrentMusic;
}
public NBTCompound toNbt() {
NBTCompound nbt = new NBTCompound(); NBTCompound nbt = new NBTCompound();
nbt.setString("sound", sound.toString()); nbt.setString("sound", sound.toString());
nbt.setInt("min_delay", minDelay); nbt.setInt("min_delay", minDelay);
@ -200,16 +72,13 @@ public class BiomeEffects {
nbt.setByte("replace_current_music", replaceCurrentMusic ? (byte) 1 : (byte) 0); nbt.setByte("replace_current_music", replaceCurrentMusic ? (byte) 1 : (byte) 0);
return nbt; return nbt;
} }
} }
public static class BiomeEffectsBuilder { public static final class Builder {
private int fogColor; private int fogColor;
private int skyColor; private int skyColor;
private int waterColor; private int waterColor;
private int waterFogColor; private int waterFogColor;
private int foliageColor = -1; private int foliageColor = -1;
private int grassColor = -1; private int grassColor = -1;
private GrassColorModifier grassColorModifier; private GrassColorModifier grassColorModifier;
@ -219,65 +88,65 @@ public class BiomeEffects {
private AdditionsSound additionsSound; private AdditionsSound additionsSound;
private Music music; private Music music;
BiomeEffectsBuilder() { Builder() {
} }
public BiomeEffects.BiomeEffectsBuilder fogColor(int fogColor) { public Builder fogColor(int fogColor) {
this.fogColor = fogColor; this.fogColor = fogColor;
return this; return this;
} }
public BiomeEffects.BiomeEffectsBuilder skyColor(int skyColor) { public Builder skyColor(int skyColor) {
this.skyColor = skyColor; this.skyColor = skyColor;
return this; return this;
} }
public BiomeEffects.BiomeEffectsBuilder waterColor(int waterColor) { public Builder waterColor(int waterColor) {
this.waterColor = waterColor; this.waterColor = waterColor;
return this; return this;
} }
public BiomeEffects.BiomeEffectsBuilder waterFogColor(int waterFogColor) { public Builder waterFogColor(int waterFogColor) {
this.waterFogColor = waterFogColor; this.waterFogColor = waterFogColor;
return this; return this;
} }
public BiomeEffects.BiomeEffectsBuilder foliageColor(int foliageColor) { public Builder foliageColor(int foliageColor) {
this.foliageColor = foliageColor; this.foliageColor = foliageColor;
return this; return this;
} }
public BiomeEffects.BiomeEffectsBuilder grassColor(int grassColor) { public Builder grassColor(int grassColor) {
this.grassColor = grassColor; this.grassColor = grassColor;
return this; return this;
} }
public BiomeEffects.BiomeEffectsBuilder grassColorModifier(GrassColorModifier grassColorModifier) { public Builder grassColorModifier(GrassColorModifier grassColorModifier) {
this.grassColorModifier = grassColorModifier; this.grassColorModifier = grassColorModifier;
return this; return this;
} }
public BiomeEffects.BiomeEffectsBuilder biomeParticles(BiomeParticles biomeParticles) { public Builder biomeParticles(BiomeParticles biomeParticles) {
this.biomeParticles = biomeParticles; this.biomeParticles = biomeParticles;
return this; return this;
} }
public BiomeEffects.BiomeEffectsBuilder ambientSound(NamespaceID ambientSound) { public Builder ambientSound(NamespaceID ambientSound) {
this.ambientSound = ambientSound; this.ambientSound = ambientSound;
return this; return this;
} }
public BiomeEffects.BiomeEffectsBuilder moodSound(MoodSound moodSound) { public Builder moodSound(MoodSound moodSound) {
this.moodSound = moodSound; this.moodSound = moodSound;
return this; return this;
} }
public BiomeEffects.BiomeEffectsBuilder additionsSound(AdditionsSound additionsSound) { public Builder additionsSound(AdditionsSound additionsSound) {
this.additionsSound = additionsSound; this.additionsSound = additionsSound;
return this; return this;
} }
public BiomeEffects.BiomeEffectsBuilder music(Music music) { public Builder music(Music music) {
this.music = music; this.music = music;
return this; return this;
} }

View File

@ -16,7 +16,6 @@ import java.util.Collections;
* Contains {@link Biome#PLAINS} by default but can be removed. * Contains {@link Biome#PLAINS} by default but can be removed.
*/ */
public final class BiomeManager { public final class BiomeManager {
private final Int2ObjectMap<Biome> biomes = new Int2ObjectOpenHashMap<>(); private final Int2ObjectMap<Biome> biomes = new Int2ObjectOpenHashMap<>();
public BiomeManager() { public BiomeManager() {
@ -29,7 +28,7 @@ public final class BiomeManager {
* @param biome the biome to add * @param biome the biome to add
*/ */
public synchronized void addBiome(Biome biome) { public synchronized void addBiome(Biome biome) {
this.biomes.put(biome.getId(), biome); this.biomes.put(biome.id(), biome);
} }
/** /**
@ -38,7 +37,7 @@ public final class BiomeManager {
* @param biome the biome to remove * @param biome the biome to remove
*/ */
public synchronized void removeBiome(Biome biome) { public synchronized void removeBiome(Biome biome) {
this.biomes.remove(biome.getId()); this.biomes.remove(biome.id());
} }
/** /**
@ -46,7 +45,7 @@ public final class BiomeManager {
* *
* @return an immutable copy of the biomes already registered * @return an immutable copy of the biomes already registered
*/ */
public Collection<Biome> unmodifiableCollection() { public synchronized Collection<Biome> unmodifiableCollection() {
return Collections.unmodifiableCollection(biomes.values()); return Collections.unmodifiableCollection(biomes.values());
} }
@ -56,14 +55,14 @@ public final class BiomeManager {
* @param id the id of the biome * @param id the id of the biome
* @return the {@link Biome} linked to this id * @return the {@link Biome} linked to this id
*/ */
public Biome getById(int id) { public synchronized Biome getById(int id) {
return biomes.get(id); return biomes.get(id);
} }
public Biome getByName(NamespaceID namespaceID) { public synchronized Biome getByName(NamespaceID namespaceID) {
Biome biome = null; Biome biome = null;
for (final Biome biomeT : biomes.values()) { for (final Biome biomeT : biomes.values()) {
if (biomeT.getName().equals(namespaceID)) { if (biomeT.name().equals(namespaceID)) {
biome = biomeT; biome = biomeT;
break; break;
} }
@ -71,7 +70,7 @@ public final class BiomeManager {
return biome; return biome;
} }
public NBTCompound toNBT() { public synchronized NBTCompound toNBT() {
NBTCompound biomes = new NBTCompound(); NBTCompound biomes = new NBTCompound();
biomes.setString("type", "minecraft:worldgen/biome"); biomes.setString("type", "minecraft:worldgen/biome");
NBTList<NBTCompound> biomesList = new NBTList<>(NBTTypes.TAG_Compound); NBTList<NBTCompound> biomesList = new NBTList<>(NBTTypes.TAG_Compound);