Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport to 1.21.1: Add a biome villager types data map to replace VillagerType#BY_BIOME #1947

Merged
merged 2 commits into from
Feb 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions patches/net/minecraft/world/entity/npc/VillagerType.java.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--- a/net/minecraft/world/entity/npc/VillagerType.java
+++ b/net/minecraft/world/entity/npc/VillagerType.java
@@ -21,6 +_,8 @@
public static final VillagerType SWAMP = register("swamp");
public static final VillagerType TAIGA = register("taiga");
private final String name;
+ /** Neo: use the {@link net.neoforged.neoforge.registries.datamaps.builtin.NeoForgeDataMaps#VILLAGER_TYPES data map} instead as this field will be ignored in future versions */
+ @Deprecated
private static final Map<ResourceKey<Biome>, VillagerType> BY_BIOME = Util.make(Maps.newHashMap(), p_35834_ -> {
p_35834_.put(Biomes.BADLANDS, DESERT);
p_35834_.put(Biomes.DESERT, DESERT);
@@ -67,6 +_,9 @@
}

public static VillagerType byBiome(Holder<Biome> p_204074_) {
+ var fromDataMap = p_204074_.getData(net.neoforged.neoforge.registries.datamaps.builtin.NeoForgeDataMaps.VILLAGER_TYPES);
+ if (fromDataMap != null) return fromDataMap.type();
+ // TODO - 1.22: remove fallback
return p_204074_.unwrapKey().map(BY_BIOME::get).orElse(PLAINS);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
{
"values": {
"minecraft:badlands": {
"villager_type": "minecraft:desert"
},
"minecraft:bamboo_jungle": {
"villager_type": "minecraft:jungle"
},
"minecraft:deep_frozen_ocean": {
"villager_type": "minecraft:snow"
},
"minecraft:desert": {
"villager_type": "minecraft:desert"
},
"minecraft:eroded_badlands": {
"villager_type": "minecraft:desert"
},
"minecraft:frozen_ocean": {
"villager_type": "minecraft:snow"
},
"minecraft:frozen_peaks": {
"villager_type": "minecraft:snow"
},
"minecraft:frozen_river": {
"villager_type": "minecraft:snow"
},
"minecraft:grove": {
"villager_type": "minecraft:snow"
},
"minecraft:ice_spikes": {
"villager_type": "minecraft:snow"
},
"minecraft:jagged_peaks": {
"villager_type": "minecraft:snow"
},
"minecraft:jungle": {
"villager_type": "minecraft:jungle"
},
"minecraft:mangrove_swamp": {
"villager_type": "minecraft:swamp"
},
"minecraft:old_growth_pine_taiga": {
"villager_type": "minecraft:taiga"
},
"minecraft:old_growth_spruce_taiga": {
"villager_type": "minecraft:taiga"
},
"minecraft:savanna": {
"villager_type": "minecraft:savanna"
},
"minecraft:savanna_plateau": {
"villager_type": "minecraft:savanna"
},
"minecraft:snowy_beach": {
"villager_type": "minecraft:snow"
},
"minecraft:snowy_plains": {
"villager_type": "minecraft:snow"
},
"minecraft:snowy_slopes": {
"villager_type": "minecraft:snow"
},
"minecraft:snowy_taiga": {
"villager_type": "minecraft:snow"
},
"minecraft:sparse_jungle": {
"villager_type": "minecraft:jungle"
},
"minecraft:swamp": {
"villager_type": "minecraft:swamp"
},
"minecraft:taiga": {
"villager_type": "minecraft:taiga"
},
"minecraft:windswept_forest": {
"villager_type": "minecraft:taiga"
},
"minecraft:windswept_gravelly_hills": {
"villager_type": "minecraft:taiga"
},
"minecraft:windswept_hills": {
"villager_type": "minecraft:taiga"
},
"minecraft:windswept_savanna": {
"villager_type": "minecraft:savanna"
},
"minecraft:wooded_badlands": {
"villager_type": "minecraft:desert"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
import net.minecraft.world.entity.ai.behavior.WorkAtComposter;
import net.minecraft.world.entity.animal.Parrot;
import net.minecraft.world.entity.npc.VillagerProfession;
import net.minecraft.world.entity.npc.VillagerType;
import net.minecraft.world.item.HoneycombItem;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.biome.Biome;
import net.minecraft.world.level.block.ComposterBlock;
import net.minecraft.world.level.block.WeatheringCopper;
import net.minecraft.world.level.block.entity.AbstractFurnaceBlockEntity;
Expand All @@ -35,6 +37,7 @@
import net.minecraft.world.level.storage.loot.LootTable;
import net.neoforged.fml.util.ObfuscationReflectionHelper;
import net.neoforged.neoforge.common.data.DataMapProvider;
import net.neoforged.neoforge.registries.datamaps.builtin.BiomeVillagerType;
import net.neoforged.neoforge.registries.datamaps.builtin.Compostable;
import net.neoforged.neoforge.registries.datamaps.builtin.FurnaceFuel;
import net.neoforged.neoforge.registries.datamaps.builtin.MonsterRoomMob;
Expand All @@ -56,6 +59,10 @@ protected void gather() {
final List<Item> villagerCompostables = ObfuscationReflectionHelper.getPrivateValue(WorkAtComposter.class, null, "COMPOSTABLE_ITEMS");
ComposterBlock.COMPOSTABLES.forEach((item, chance) -> compostables.add(item.asItem().builtInRegistryHolder(), new Compostable(chance, villagerCompostables.contains(item.asItem())), false));

final var biomeVillagers = builder(NeoForgeDataMaps.VILLAGER_TYPES);
ObfuscationReflectionHelper.<Map<ResourceKey<Biome>, VillagerType>, VillagerType>getPrivateValue(VillagerType.class, null, "BY_BIOME")
.forEach((biome, type) -> biomeVillagers.add(biome, new BiomeVillagerType(type), false));

final var fuels = builder(NeoForgeDataMaps.FURNACE_FUELS);
AbstractFurnaceBlockEntity.buildFuels((value, time) -> value.ifLeft(item -> fuels.add(item.builtInRegistryHolder(), new FurnaceFuel(time), false))
.ifRight(tag -> fuels.add(tag, new FurnaceFuel(time), false)));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (c) NeoForged and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/

package net.neoforged.neoforge.registries.datamaps.builtin;

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.world.entity.npc.VillagerType;

/**
* Data map value for {@linkplain NeoForgeDataMaps#VILLAGER_TYPES biome villager types}.
*
* @param type the type of the villagers present in this biome
*/
public record BiomeVillagerType(VillagerType type) {
public static final Codec<BiomeVillagerType> TYPE_CODEC = BuiltInRegistries.VILLAGER_TYPE.byNameCodec()
.xmap(BiomeVillagerType::new, BiomeVillagerType::type);
public static final Codec<BiomeVillagerType> CODEC = Codec.withAlternative(
RecordCodecBuilder.create(in -> in.group(
BuiltInRegistries.VILLAGER_TYPE.byNameCodec().fieldOf("villager_type").forGetter(BiomeVillagerType::type)).apply(in, BiomeVillagerType::new)),
TYPE_CODEC);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
import net.minecraft.world.entity.ai.behavior.GiveGiftToHero;
import net.minecraft.world.entity.animal.Parrot;
import net.minecraft.world.entity.npc.VillagerProfession;
import net.minecraft.world.entity.npc.VillagerType;
import net.minecraft.world.item.HoneycombItem;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.RecipeType;
import net.minecraft.world.level.biome.Biome;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.ComposterBlock;
import net.minecraft.world.level.block.WeatheringCopper;
Expand Down Expand Up @@ -128,6 +130,19 @@ public class NeoForgeDataMaps {
public static final DataMapType<GameEvent, VibrationFrequency> VIBRATION_FREQUENCIES = DataMapType.builder(
id("vibration_frequencies"), Registries.GAME_EVENT, VibrationFrequency.CODEC).synced(VibrationFrequency.FREQUENCY_CODEC, false).build();

/**
* The {@linkplain Biome} data map that replaces {@link VillagerType#BY_BIOME}.
* <p>
* The location of this data map is {@code neoforge/data_maps/worldgen/biome/villager_types.json}, and the values are objects with 1 field:
* <ul>
* <li>{@code villager_type}, villager type ID - the type of the villagers present in the biome</li>
* </ul>
*
* The use of a string as the value is also possible, though discouraged in case more options are added in the future.
*/
public static final DataMapType<Biome, BiomeVillagerType> VILLAGER_TYPES = DataMapType.builder(
id("villager_types"), Registries.BIOME, BiomeVillagerType.CODEC).synced(BiomeVillagerType.TYPE_CODEC, false).build();

/**
* The {@linkplain Block} data map that replaces {@link HoneycombItem#WAXABLES}.
* <p>
Expand All @@ -154,6 +169,7 @@ private static void register(final RegisterDataMapTypesEvent event) {
event.register(PARROT_IMITATIONS);
event.register(RAID_HERO_GIFTS);
event.register(VIBRATION_FREQUENCIES);
event.register(VILLAGER_TYPES);
event.register(WAXABLES);
}
}