Skip to content

Commit

Permalink
Better Fabric biome API support
Browse files Browse the repository at this point in the history
  • Loading branch information
paulevsGitch committed Aug 14, 2021
1 parent 67be2ce commit 895b605
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 66 deletions.
65 changes: 47 additions & 18 deletions src/main/java/ru/bclib/api/BiomeAPI.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package ru.bclib.api;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.impl.biome.InternalBiomeData;
import net.fabricmc.fabric.impl.biome.WeightedBiomePicker;
import net.minecraft.client.Minecraft;
import net.minecraft.core.Registry;
import net.minecraft.data.BuiltinRegistries;
Expand All @@ -16,13 +14,11 @@
import net.minecraft.world.level.biome.Biome.ClimateParameters;
import net.minecraft.world.level.biome.Biomes;
import org.jetbrains.annotations.Nullable;
import ru.bclib.interfaces.BiomeListProvider;
import ru.bclib.util.MHelper;
import ru.bclib.world.biomes.BCLBiome;
import ru.bclib.world.biomes.FabricBiomesData;
import ru.bclib.world.generator.BiomePicker;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Random;

Expand Down Expand Up @@ -155,6 +151,21 @@ public static BCLBiome registerEndLandBiome(Biome biome) {
return bclBiome;
}

/**
* Register {@link BCLBiome} wrapper for {@link Biome}.
* After that biome will be added to BCLib End Biome Generator and into Fabric Biome API as a land biome (will generate only on islands).
* @param biome {@link BCLBiome};
* @param weight float generation chance.
* @return {@link BCLBiome}
*/
public static BCLBiome registerEndLandBiome(Biome biome, float weight) {
ResourceKey<Biome> key = BuiltinRegistries.BIOME.getResourceKey(biome).get();
BCLBiome bclBiome = new BCLBiome(key.location(), biome, 1, weight);
END_LAND_BIOME_PICKER.addBiome(bclBiome);
registerBiome(bclBiome);
return bclBiome;
}

/**
* Register {@link BCLBiome} instance and its {@link Biome} if necessary.
* After that biome will be added to BCLib End Biome Generator and into Fabric Biome API as a void biome (will generate only in the End void - between islands).
Expand Down Expand Up @@ -184,6 +195,21 @@ public static BCLBiome registerEndVoidBiome(Biome biome) {
return bclBiome;
}

/**
* Register {@link BCLBiome} instance and its {@link Biome} if necessary.
* After that biome will be added to BCLib End Biome Generator and into Fabric Biome API as a void biome (will generate only in the End void - between islands).
* @param biome {@link BCLBiome};
* @param weight float generation chance.
* @return {@link BCLBiome}
*/
public static BCLBiome registerEndVoidBiome(Biome biome, float weight) {
ResourceKey<Biome> key = BuiltinRegistries.BIOME.getResourceKey(biome).get();
BCLBiome bclBiome = new BCLBiome(key.location(), biome, 1, weight);
END_VOID_BIOME_PICKER.addBiome(bclBiome);
registerBiome(bclBiome);
return bclBiome;
}

/**
* Get {@link BCLBiome} from {@link Biome} instance on server. Used to convert world biomes to BCLBiomes.
*
Expand Down Expand Up @@ -250,20 +276,23 @@ public static boolean hasBiome(ResourceLocation biomeID) {
* Load biomes from Fabric API. For internal usage only.
*/
public static void loadFabricAPIBiomes() {
List<ResourceKey<Biome>> biomes = Lists.newArrayList();
biomes.addAll(getBiomes(InternalBiomeData.getEndBiomesMap().get(Biomes.SMALL_END_ISLANDS)));
biomes.addAll(getBiomes(InternalBiomeData.getEndBarrensMap().get(Biomes.END_BARRENS)));
biomes.forEach((key) -> registerEndVoidBiome(BuiltinRegistries.BIOME.get(key.location())));
FabricBiomesData.NETHER_BIOMES.forEach((key) -> {
if (!hasBiome(key.location())) {
registerNetherBiome(BuiltinRegistries.BIOME.get(key.location()));
}
});

biomes.clear();
biomes.addAll(getBiomes(InternalBiomeData.getEndBiomesMap().get(Biomes.END_MIDLANDS)));
biomes.addAll(getBiomes(InternalBiomeData.getEndBiomesMap().get(Biomes.END_HIGHLANDS)));
biomes.forEach((key) -> registerEndLandBiome(BuiltinRegistries.BIOME.get(key.location())));
}

private static List<ResourceKey<Biome>> getBiomes(WeightedBiomePicker picker) {
BiomeListProvider biomeList = (BiomeListProvider) (Object) picker;
return biomeList == null ? Collections.emptyList() : biomeList.getBiomes();
FabricBiomesData.END_LAND_BIOMES.forEach((key, weight) -> {
if (!hasBiome(key.location())) {
registerEndLandBiome(BuiltinRegistries.BIOME.get(key.location()), weight);
}
});

FabricBiomesData.END_VOID_BIOMES.forEach((key, weight) -> {
if (!hasBiome(key.location())) {
registerEndVoidBiome(BuiltinRegistries.BIOME.get(key.location()), weight);
}
});
}

@Nullable
Expand Down
10 changes: 0 additions & 10 deletions src/main/java/ru/bclib/interfaces/BiomeListProvider.java

This file was deleted.

40 changes: 40 additions & 0 deletions src/main/java/ru/bclib/mixin/common/InternalBiomeDataMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package ru.bclib.mixin.common;

import net.fabricmc.fabric.impl.biome.InternalBiomeData;
import net.minecraft.resources.ResourceKey;
import net.minecraft.world.level.biome.Biome;
import net.minecraft.world.level.biome.Biomes;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import ru.bclib.world.biomes.FabricBiomesData;

@Mixin(value = InternalBiomeData.class, remap = false)
public class InternalBiomeDataMixin {
@Inject(method = "addEndBiomeReplacement", at = @At(value = "HEAD"))
private static void bclib_addEndBiomeReplacement(ResourceKey<Biome> replaced, ResourceKey<Biome> variant, double weight, CallbackInfo info) {
if (replaced == Biomes.END_BARRENS || replaced == Biomes.SMALL_END_ISLANDS) {
FabricBiomesData.END_VOID_BIOMES.put(variant, (float) weight);
}
else {
FabricBiomesData.END_LAND_BIOMES.put(variant, (float) weight);
}
}

@Inject(method = "addEndMidlandsReplacement", at = @At(value = "HEAD"))
private static void bclib_addEndMidlandsReplacement(ResourceKey<Biome> highlands, ResourceKey<Biome> midlands, double weight, CallbackInfo info) {
FabricBiomesData.END_LAND_BIOMES.put(midlands, (float) weight);
}

@Inject(method = "addEndBarrensReplacement", at = @At(value = "HEAD"))
private static void bclib_addEndBarrensReplacement(ResourceKey<Biome> highlands, ResourceKey<Biome> barrens, double weight, CallbackInfo info) {
FabricBiomesData.END_LAND_BIOMES.put(barrens, (float) weight);
FabricBiomesData.END_VOID_BIOMES.put(barrens, (float) weight);
}

@Inject(method = "addNetherBiome", at = @At(value = "HEAD"))
private static void bclib_addNetherBiome(ResourceKey<Biome> biome, Biome.ClimateParameters spawnNoisePoint, CallbackInfo info) {
FabricBiomesData.NETHER_BIOMES.add(biome);
}
}
37 changes: 0 additions & 37 deletions src/main/java/ru/bclib/mixin/common/WeightedBiomePickerMixin.java

This file was deleted.

15 changes: 15 additions & 0 deletions src/main/java/ru/bclib/world/biomes/FabricBiomesData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package ru.bclib.world.biomes;

import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import net.minecraft.resources.ResourceKey;
import net.minecraft.world.level.biome.Biome;

import java.util.Map;
import java.util.Set;

public class FabricBiomesData {
public static final Map<ResourceKey<Biome>, Float> END_LAND_BIOMES = Maps.newHashMap();
public static final Map<ResourceKey<Biome>, Float> END_VOID_BIOMES = Maps.newHashMap();
public static final Set<ResourceKey<Biome>> NETHER_BIOMES = Sets.newHashSet();
}
2 changes: 1 addition & 1 deletion src/main/resources/bclib.mixins.common.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"compatibilityLevel": "JAVA_16",
"mixins": [
"SimpleReloadableResourceManagerMixin",
"WeightedBiomePickerMixin",
"InternalBiomeDataMixin",
"ComposterBlockAccessor",
"PotionBrewingAccessor",
"RecipeManagerAccessor",
Expand Down

0 comments on commit 895b605

Please sign in to comment.