Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

Commit

Permalink
rework
Browse files Browse the repository at this point in the history
  • Loading branch information
iam4722202468 committed Feb 10, 2024
1 parent 194ee41 commit 78d6b41
Show file tree
Hide file tree
Showing 10 changed files with 323 additions and 190 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public static void main(String[] args) {
generator.generate(resource("blocks.json"), "net.minestom.server.instance.block", "Block", "BlockImpl", "Blocks");
generator.generate(resource("items.json"), "net.minestom.server.item", "Material", "MaterialImpl", "Materials");
generator.generate(resource("entities.json"), "net.minestom.server.entity", "EntityType", "EntityTypeImpl", "EntityTypes");
generator.generate(resource("biomes.json"), "net.minestom.server.world.biomes", "Biome", "BiomeImpl", "Biomes");
generator.generate(resource("enchantments.json"), "net.minestom.server.item", "Enchantment", "EnchantmentImpl", "Enchantments");
generator.generate(resource("potion_effects.json"), "net.minestom.server.potion", "PotionEffect", "PotionEffectImpl", "PotionEffects");
generator.generate(resource("potions.json"), "net.minestom.server.potion", "PotionType", "PotionTypeImpl", "PotionTypes");
Expand Down
2 changes: 1 addition & 1 deletion demo/src/main/java/net/minestom/demo/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ public class Main {

public static void main(String[] args) {
System.setProperty("minestom.experiment.pose-updates", "true");
System.setProperty("minestom.load-vanilla-biomes", "true");

MinecraftServer.setCompressionThreshold(0);

MinecraftServer minecraftServer = MinecraftServer.init();
MinecraftServer.getBiomeManager().loadVanillaBiomes();

BlockManager blockManager = MinecraftServer.getBlockManager();
blockManager.registerBlockPlacementRule(new DripstonePlacementRule());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

public class AnvilLoader implements IChunkLoader {
private final static Logger LOGGER = LoggerFactory.getLogger(AnvilLoader.class);
private static final Biome BIOME = Biome.PLAINS;
private final static Biome BIOME = MinecraftServer.getBiomeManager().getByName(NamespaceID.from("minecraft:plains"));

private final Map<String, RegionFile> alreadyLoaded = new ConcurrentHashMap<>();
private final Path path;
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/net/minestom/server/instance/DynamicChunk.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import net.minestom.server.coordinate.Point;
import net.minestom.server.coordinate.Vec;
import net.minestom.server.entity.Entity;
import net.minestom.server.entity.Player;
import net.minestom.server.entity.pathfinding.PFBlock;
import net.minestom.server.instance.block.Block;
import net.minestom.server.instance.block.BlockHandler;
Expand All @@ -24,7 +23,6 @@
import net.minestom.server.utils.MathUtils;
import net.minestom.server.utils.ObjectPool;
import net.minestom.server.utils.chunk.ChunkUtils;
import net.minestom.server.utils.validate.Check;
import net.minestom.server.world.biomes.Biome;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down
80 changes: 80 additions & 0 deletions src/main/java/net/minestom/server/registry/Registry.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public static BlockEntry block(String namespace, @NotNull Properties main) {
return new BlockEntry(namespace, main, null);
}

@ApiStatus.Internal
public static BiomeEntry biome(String namespace, Properties properties) {
return new BiomeEntry(namespace, properties, null);
}

@ApiStatus.Internal
public static MaterialEntry material(String namespace, @NotNull Properties main) {
return new MaterialEntry(namespace, main, null);
Expand Down Expand Up @@ -324,6 +329,81 @@ public Properties custom() {
}
}

public static final class BiomeEntry implements Entry {
private final Properties custom;
private final NamespaceID namespace;
private final Integer foliageColor;
private final Integer grassColor;
private final Integer skyColor;
private final Integer waterColor;
private final Integer waterFogColor;
private final Integer fogColor;
private final float temperature;
private final float downfall;
private final boolean hasPrecipitation;

private BiomeEntry(String namespace, Properties main, Properties custom) {
this.custom = custom;
this.namespace = NamespaceID.from(namespace);

this.foliageColor = main.containsKey("foliageColor") ? main.getInt("foliageColor") : null;
this.grassColor = main.containsKey("grassColor") ? main.getInt("grassColor") : null;
this.skyColor = main.containsKey("skyColor") ? main.getInt("skyColor") : null;
this.waterColor = main.containsKey("waterColor") ? main.getInt("waterColor") : null;
this.waterFogColor = main.containsKey("waterFogColor") ? main.getInt("waterFogColor") : null;
this.fogColor = main.containsKey("fogColor") ? main.getInt("fogColor") : null;

this.temperature = (float) main.getDouble("temperature", 0.5F);
this.downfall = (float) main.getDouble("downfall", 0.5F);
this.hasPrecipitation = main.getBoolean("has_precipitation", true);
}

@Override
public Properties custom() {
return custom;
}

public @NotNull NamespaceID namespace() {
return namespace;
}

public @Nullable Integer foliageColor() {
return foliageColor;
}

public @Nullable Integer grassColor() {
return grassColor;
}

public @Nullable Integer skyColor() {
return skyColor;
}

public @Nullable Integer waterColor() {
return waterColor;
}

public @Nullable Integer waterFogColor() {
return waterFogColor;
}

public @Nullable Integer fogColor() {
return fogColor;
}

public float temperature() {
return temperature;
}

public float downfall() {
return downfall;
}

public boolean hasPrecipitation() {
return hasPrecipitation;
}
}

public static final class MaterialEntry implements Entry {
private final NamespaceID namespace;
private final int id;
Expand Down
195 changes: 61 additions & 134 deletions src/main/java/net/minestom/server/world/biomes/Biome.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,156 +2,104 @@

import net.minestom.server.coordinate.Point;
import net.minestom.server.registry.ProtocolObject;
import net.minestom.server.registry.Registry;
import net.minestom.server.utils.NamespaceID;
import net.minestom.server.utils.validate.Check;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jglrxavpok.hephaistos.nbt.NBT;
import org.jglrxavpok.hephaistos.nbt.NBTCompound;

import java.util.Locale;
import java.util.concurrent.atomic.AtomicInteger;

public final class Biome implements ProtocolObject {
public static final AtomicInteger ID_COUNTER = new AtomicInteger(0);
private static final BiomeEffects DEFAULT_EFFECTS = BiomeEffects.builder()
sealed public interface Biome extends ProtocolObject permits BiomeImpl {
/**
* Returns the entity registry.
*
* @return the entity registry
*/
@Contract(pure = true)
@Nullable Registry.BiomeEntry registry();

@Override
@NotNull NamespaceID namespace();
int id();
float depth();
float temperature();
float scale();
float downfall();
BiomeEffects effects();
Precipitation precipitation();
TemperatureModifier temperatureModifier();

BiomeEffects DEFAULT_EFFECTS = BiomeEffects.builder()
.fogColor(0xC0D8FF)
.skyColor(0x78A7FF)
.waterColor(0x3F76E4)
.waterFogColor(0x50533)
.build();

//A plains biome has to be registered or else minecraft will crash
public static final Biome PLAINS = Biome.builder()
.category(Category.NONE)
.name(NamespaceID.from("minecraft:plains"))
.temperature(0.8F)
.downfall(0.4F)
.depth(0.125F)
.scale(0.05F)
.effects(DEFAULT_EFFECTS)
.build();

private final int id = ID_COUNTER.getAndIncrement();

private final NamespaceID name;
private final float depth;
private final float temperature;
private final float scale;
private final float downfall;
private final Category category;
private final BiomeEffects effects;
private final Precipitation precipitation;
private final TemperatureModifier temperatureModifier;

Biome(NamespaceID name, float depth, float temperature, float scale, float downfall, Category category, BiomeEffects effects, Precipitation precipitation, TemperatureModifier temperatureModifier) {
this.name = name;
this.depth = depth;
this.temperature = temperature;
this.scale = scale;
this.downfall = downfall;
this.category = category;
this.effects = effects;
this.precipitation = precipitation;
this.temperatureModifier = temperatureModifier;
}

public static Builder builder() {
return new Builder();
}

public @NotNull NBTCompound toNbt() {
Check.notNull(name, "The biome namespace cannot be null");
Check.notNull(effects, "The biome effects cannot be null");

return NBT.Compound(nbt -> {
nbt.setString("name", name.toString());
nbt.setInt("id", id());

nbt.set("element", NBT.Compound(element -> {
element.setFloat("depth", depth);
element.setFloat("temperature", temperature);
element.setFloat("scale", scale);
element.setFloat("downfall", downfall);
element.setString("category", category.name().toLowerCase(Locale.ROOT));
element.setByte("has_precipitation", (byte) (precipitation == Precipitation.NONE ? 0 : 1));
element.setString("precipitation", precipitation.name().toLowerCase(Locale.ROOT));
if (temperatureModifier != TemperatureModifier.NONE)
element.setString("temperature_modifier", temperatureModifier.name().toLowerCase(Locale.ROOT));
element.set("effects", effects.toNbt());
}));
});
}

public int id() {
return this.id;
}

@Override
public @NotNull NamespaceID namespace() {
return this.name;
}

public float depth() {
return this.depth;
}

public float temperature() {
return this.temperature;
enum Precipitation {
NONE, RAIN, SNOW;
}

public float scale() {
return this.scale;
enum TemperatureModifier {
NONE, FROZEN;
}

public float downfall() {
return this.downfall;
}
interface Setter {
void setBiome(int x, int y, int z, @NotNull Biome biome);

public Category category() {
return this.category;
default void setBiome(@NotNull Point blockPosition, @NotNull Biome biome) {
setBiome(blockPosition.blockX(), blockPosition.blockY(), blockPosition.blockZ(), biome);
}
}

public BiomeEffects effects() {
return this.effects;
}
interface Getter {
@NotNull Biome getBiome(int x, int y, int z);

public Precipitation precipitation() {
return this.precipitation;
default @NotNull Biome getBiome(@NotNull Point point) {
return getBiome(point.blockX(), point.blockY(), point.blockZ());
}
}

public TemperatureModifier temperatureModifier() {
return this.temperatureModifier;
}
default @NotNull NBTCompound toNbt() {
Check.notNull(name(), "The biome namespace cannot be null");
Check.notNull(effects(), "The biome effects cannot be null");

public enum Precipitation {
NONE, RAIN, SNOW;
}
return NBT.Compound(nbt -> {
nbt.setString("name", name());
nbt.setInt("id", id());

public enum Category {
NONE, TAIGA, EXTREME_HILLS, JUNGLE, MESA, PLAINS,
SAVANNA, ICY, THE_END, BEACH, FOREST, OCEAN,
DESERT, RIVER, SWAMP, MUSHROOM, NETHER, UNDERGROUND,
MOUNTAIN;
nbt.set("element", NBT.Compound(element -> {
element.setFloat("depth", depth());
element.setFloat("temperature", temperature());
element.setFloat("scale", scale());
element.setFloat("downfall", downfall());
element.setByte("has_precipitation", (byte) (precipitation() == Precipitation.NONE ? 0 : 1));
element.setString("precipitation", precipitation().name().toLowerCase(Locale.ROOT));
if (temperatureModifier() != TemperatureModifier.NONE)
element.setString("temperature_modifier", temperatureModifier().name().toLowerCase(Locale.ROOT));
element.set("effects", effects().toNbt());
}));
});
}

public enum TemperatureModifier {
NONE, FROZEN;
static Builder builder() {
return new Builder();
}

public static final class Builder {
final class Builder {
private NamespaceID name;
private float depth = 0.2f;
private float temperature = 0.25f;
private float scale = 0.2f;
private float downfall = 0.8f;
private Category category = Category.NONE;
private BiomeEffects effects = DEFAULT_EFFECTS;
private Precipitation precipitation = Precipitation.RAIN;
private TemperatureModifier temperatureModifier = TemperatureModifier.NONE;

Builder() {
}

public Builder name(NamespaceID name) {
this.name = name;
return this;
Expand All @@ -177,17 +125,12 @@ public Builder downfall(float downfall) {
return this;
}

public Builder category(Category category) {
this.category = category;
return this;
}

public Builder effects(BiomeEffects effects) {
this.effects = effects;
return this;
}

public Builder precipitation(Precipitation precipitation) {
public Builder precipitation(Biome.Precipitation precipitation) {
this.precipitation = precipitation;
return this;
}
Expand All @@ -198,23 +141,7 @@ public Builder temperatureModifier(TemperatureModifier temperatureModifier) {
}

public Biome build() {
return new Biome(name, depth, temperature, scale, downfall, category, effects, precipitation, temperatureModifier);
}
}

public interface Setter {
void setBiome(int x, int y, int z, @NotNull Biome biome);

default void setBiome(@NotNull Point blockPosition, @NotNull Biome biome) {
setBiome(blockPosition.blockX(), blockPosition.blockY(), blockPosition.blockZ(), biome);
}
}

public interface Getter {
@NotNull Biome getBiome(int x, int y, int z);

default @NotNull Biome getBiome(@NotNull Point point) {
return getBiome(point.blockX(), point.blockY(), point.blockZ());
return new BiomeImpl(name, depth, temperature, scale, downfall, effects, precipitation, temperatureModifier);
}
}
}
Loading

0 comments on commit 78d6b41

Please sign in to comment.