Better Abstraction from internal FAPI (#2, FabricMC/fabric#2369)

This commit is contained in:
Frank 2022-06-30 13:53:19 +02:00
parent 185453b0c4
commit 59756e6dca
4 changed files with 45 additions and 74 deletions

View file

@ -1,16 +0,0 @@
package org.betterx.bclib.api.v2.levelgen.biomes;
import net.minecraft.resources.ResourceKey;
import net.minecraft.world.level.biome.Biome;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
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();
}

View file

@ -109,32 +109,32 @@ public class InternalBiomeAPI {
* Load biomes from Fabric API. For internal usage only. * Load biomes from Fabric API. For internal usage only.
*/ */
public static void loadFabricAPIBiomes() { public static void loadFabricAPIBiomes() {
FabricBiomesData.NETHER_BIOMES.forEach((key) -> { // FabricBiomesData.NETHER_BIOMES.forEach((key) -> {
if (!BiomeAPI.hasBiome(key.location())) { // if (!BiomeAPI.hasBiome(key.location())) {
Optional<Holder<Biome>> optional = BuiltinRegistries.BIOME.getHolder(key); // Optional<Holder<Biome>> optional = BuiltinRegistries.BIOME.getHolder(key);
if (optional.isPresent()) { // if (optional.isPresent()) {
BiomeAPI.registerNetherBiome(optional.get().value()); // BiomeAPI.registerNetherBiome(optional.get().value());
} // }
} // }
}); // });
//
FabricBiomesData.END_LAND_BIOMES.forEach((key, weight) -> { // FabricBiomesData.END_LAND_BIOMES.forEach((key, weight) -> {
if (!BiomeAPI.hasBiome(key.location())) { // if (!BiomeAPI.hasBiome(key.location())) {
Optional<Holder<Biome>> optional = BuiltinRegistries.BIOME.getHolder(key); // Optional<Holder<Biome>> optional = BuiltinRegistries.BIOME.getHolder(key);
if (optional.isPresent()) { // if (optional.isPresent()) {
BiomeAPI.registerEndLandBiome(optional.get(), weight); // BiomeAPI.registerEndLandBiome(optional.get(), weight);
} // }
} // }
}); // });
//
FabricBiomesData.END_VOID_BIOMES.forEach((key, weight) -> { // FabricBiomesData.END_VOID_BIOMES.forEach((key, weight) -> {
if (!BiomeAPI.hasBiome(key.location())) { // if (!BiomeAPI.hasBiome(key.location())) {
Optional<Holder<Biome>> optional = BuiltinRegistries.BIOME.getHolder(key); // Optional<Holder<Biome>> optional = BuiltinRegistries.BIOME.getHolder(key);
if (optional.isPresent()) { // if (optional.isPresent()) {
BiomeAPI.registerEndVoidBiome(optional.get(), weight); // BiomeAPI.registerEndVoidBiome(optional.get(), weight);
} // }
} // }
}); // });
} }
/** /**

View file

@ -2,7 +2,6 @@ package org.betterx.bclib.mixin.common;
import org.betterx.bclib.interfaces.TheEndBiomeDataAccessor; import org.betterx.bclib.interfaces.TheEndBiomeDataAccessor;
import net.minecraft.core.Holder;
import net.minecraft.resources.ResourceKey; import net.minecraft.resources.ResourceKey;
import net.minecraft.world.level.biome.Biome; import net.minecraft.world.level.biome.Biome;
@ -13,45 +12,29 @@ import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.Shadow;
import javax.annotation.Nullable;
import java.util.Map; import java.util.Map;
@Mixin(value = TheEndBiomeData.Overrides.class, remap = false) @Mixin(value = TheEndBiomeData.class, remap = false)
public class TheEndBiomeDataMixin implements TheEndBiomeDataAccessor { public class TheEndBiomeDataMixin implements TheEndBiomeDataAccessor {
@Shadow @Shadow
@Final @Final
@Nullable private static Map<ResourceKey<Biome>, WeightedPicker<ResourceKey<Biome>>> END_BIOMES_MAP;
private Map<Holder<Biome>, WeightedPicker<Holder<Biome>>> endBiomesMap;
@Shadow @Shadow
@Final @Final
@Nullable private static Map<ResourceKey<Biome>, WeightedPicker<ResourceKey<Biome>>> END_MIDLANDS_MAP;
private Map<Holder<Biome>, WeightedPicker<Holder<Biome>>> endMidlandsMap;
@Shadow @Shadow
@Final @Final
@Nullable private static Map<ResourceKey<Biome>, WeightedPicker<ResourceKey<Biome>>> END_BARRENS_MAP;
private Map<Holder<Biome>, WeightedPicker<Holder<Biome>>> endBarrensMap;
public boolean bcl_canGenerateAsEndBiome(ResourceKey<Biome> key) { public boolean bcl_canGenerateAsEndBiome(ResourceKey<Biome> key) {
return endBiomesMap != null && endBiomesMap return END_BIOMES_MAP != null && END_BIOMES_MAP.containsKey(key);
.keySet()
.stream()
.map(h->h.unwrapKey().orElse(null))
.anyMatch(k->k!=null && k.equals(key));
} }
public boolean bcl_canGenerateAsEndMidlandBiome(ResourceKey<Biome> key) { public boolean bcl_canGenerateAsEndMidlandBiome(ResourceKey<Biome> key) {
return endMidlandsMap != null && endMidlandsMap return END_MIDLANDS_MAP != null && END_MIDLANDS_MAP.containsKey(key);
.keySet()
.stream()
.map(h->h.unwrapKey().orElse(null))
.anyMatch(k->k!=null && k.equals(key));
} }
public boolean bcl_canGenerateAsEndBarrensBiome(ResourceKey<Biome> key) { public boolean bcl_canGenerateAsEndBarrensBiome(ResourceKey<Biome> key) {
return endBarrensMap != null && endBarrensMap return END_BARRENS_MAP != null && END_BARRENS_MAP.containsKey(key);
.keySet()
.stream()
.map(h->h.unwrapKey().orElse(null))
.anyMatch(k->k!=null && k.equals(key));
} }
} }

View file

@ -231,17 +231,21 @@ public class WorldBootstrap {
) { ) {
try { try {
var levelStorageAccess = levelSource.createAccess(levelID); var levelStorageAccess = levelSource.createAccess(levelID);
Helpers.initializeWorldConfig(levelStorageAccess, false); try {
Helpers.initializeWorldConfig(levelStorageAccess, false);
//Helpers.setupWorld(); //Helpers.setupWorld();
WorldEventsImpl.BEFORE_WORLD_LOAD.emit(e -> e.prepareWorld( WorldEventsImpl.BEFORE_WORLD_LOAD.emit(e -> e.prepareWorld(
levelStorageAccess, levelStorageAccess,
TogetherWorldPreset.loadWorldDimensions(), TogetherWorldPreset.loadWorldDimensions(),
false false
)); ));
} catch (Exception e) {
BCLib.LOGGER.error("Failed to initialize data in world", e);
}
levelStorageAccess.close(); levelStorageAccess.close();
} catch (Exception e) { } catch (Exception e) {
BCLib.LOGGER.error("Failed to initialize data in world", e); BCLib.LOGGER.error("Failed to acquire storage access", e);
} }
} }