| 1 | package net.minecraft.world.entity.npc.villager;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableSet;
|
| 4 | import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
|
| 5 | import java.util.function.Predicate;
|
| 6 | import net.minecraft.core.Holder;
|
| 7 | import net.minecraft.core.Registry;
|
| 8 | import net.minecraft.core.registries.Registries;
|
| 9 | import net.minecraft.network.chat.Component;
|
| 10 | import net.minecraft.resources.Identifier;
|
| 11 | import net.minecraft.resources.ResourceKey;
|
| 12 | import net.minecraft.sounds.SoundEvent;
|
| 13 | import net.minecraft.sounds.SoundEvents;
|
| 14 | import net.minecraft.tags.PoiTypeTags;
|
| 15 | import net.minecraft.world.entity.ai.village.poi.PoiType;
|
| 16 | import net.minecraft.world.entity.ai.village.poi.PoiTypes;
|
| 17 | import net.minecraft.world.item.Item;
|
| 18 | import net.minecraft.world.item.Items;
|
| 19 | import net.minecraft.world.item.trading.TradeSet;
|
| 20 | import net.minecraft.world.item.trading.TradeSets;
|
| 21 | import net.minecraft.world.level.block.Block;
|
| 22 | import net.minecraft.world.level.block.Blocks;
|
| 23 | import org.jspecify.annotations.Nullable;
|
| 24 |
|
| 25 | public record VillagerProfession(
|
| 26 | Component name,
|
| 27 | Predicate<Holder<PoiType>> heldJobSite,
|
| 28 | Predicate<Holder<PoiType>> acquirableJobSite,
|
| 29 | ImmutableSet<Item> requestedItems,
|
| 30 | ImmutableSet<Block> secondaryPoi,
|
| 31 | @Nullable SoundEvent workSound,
|
| 32 | Int2ObjectMap<ResourceKey<TradeSet>> tradeSetsByLevel
|
| 33 | ) {
|
| 34 | public static final Predicate<Holder<PoiType>> ALL_ACQUIRABLE_JOBS = holder -> holder.is(PoiTypeTags.ACQUIRABLE_JOB_SITE);
|
| 35 | public static final ResourceKey<VillagerProfession> NONE = createKey("none");
|
| 36 | public static final ResourceKey<VillagerProfession> ARMORER = createKey("armorer");
|
| 37 | public static final ResourceKey<VillagerProfession> BUTCHER = createKey("butcher");
|
| 38 | public static final ResourceKey<VillagerProfession> CARTOGRAPHER = createKey("cartographer");
|
| 39 | public static final ResourceKey<VillagerProfession> CLERIC = createKey("cleric");
|
| 40 | public static final ResourceKey<VillagerProfession> FARMER = createKey("farmer");
|
| 41 | public static final ResourceKey<VillagerProfession> FISHERMAN = createKey("fisherman");
|
| 42 | public static final ResourceKey<VillagerProfession> FLETCHER = createKey("fletcher");
|
| 43 | public static final ResourceKey<VillagerProfession> LEATHERWORKER = createKey("leatherworker");
|
| 44 | public static final ResourceKey<VillagerProfession> LIBRARIAN = createKey("librarian");
|
| 45 | public static final ResourceKey<VillagerProfession> MASON = createKey("mason");
|
| 46 | public static final ResourceKey<VillagerProfession> NITWIT = createKey("nitwit");
|
| 47 | public static final ResourceKey<VillagerProfession> SHEPHERD = createKey("shepherd");
|
| 48 | public static final ResourceKey<VillagerProfession> TOOLSMITH = createKey("toolsmith");
|
| 49 | public static final ResourceKey<VillagerProfession> WEAPONSMITH = createKey("weaponsmith");
|
| 50 |
|
| 51 | private static ResourceKey<VillagerProfession> createKey(final String name) {
|
| 52 | return ResourceKey.create(Registries.VILLAGER_PROFESSION, Identifier.withDefaultNamespace(name));
|
| 53 | }
|
| 54 |
|
| 55 | private static VillagerProfession register(
|
| 56 | final Registry<VillagerProfession> registry,
|
| 57 | final ResourceKey<VillagerProfession> name,
|
| 58 | final ResourceKey<PoiType> jobSite,
|
| 59 | final @Nullable SoundEvent workSound,
|
| 60 | final Int2ObjectMap<ResourceKey<TradeSet>> trades
|
| 61 | ) {
|
| 62 | return register(registry, name, poiType -> poiType.is(jobSite), poiType -> poiType.is(jobSite), workSound, trades);
|
| 63 | }
|
| 64 |
|
| 65 | private static VillagerProfession register(
|
| 66 | final Registry<VillagerProfession> registry,
|
| 67 | final ResourceKey<VillagerProfession> name,
|
| 68 | final Predicate<Holder<PoiType>> heldJobSite,
|
| 69 | final Predicate<Holder<PoiType>> acquirableJobSite,
|
| 70 | final @Nullable SoundEvent workSound
|
| 71 | ) {
|
| 72 | return register(registry, name, heldJobSite, acquirableJobSite, ImmutableSet.of(), ImmutableSet.of(), workSound, Int2ObjectMap.ofEntries());
|
| 73 | }
|
| 74 |
|
| 75 | private static VillagerProfession register(
|
| 76 | final Registry<VillagerProfession> registry,
|
| 77 | final ResourceKey<VillagerProfession> name,
|
| 78 | final Predicate<Holder<PoiType>> heldJobSite,
|
| 79 | final Predicate<Holder<PoiType>> acquirableJobSite,
|
| 80 | final @Nullable SoundEvent workSound,
|
| 81 | final Int2ObjectMap<ResourceKey<TradeSet>> trades
|
| 82 | ) {
|
| 83 | return register(registry, name, heldJobSite, acquirableJobSite, ImmutableSet.of(), ImmutableSet.of(), workSound, trades);
|
| 84 | }
|
| 85 |
|
| 86 | private static VillagerProfession register(
|
| 87 | final Registry<VillagerProfession> registry,
|
| 88 | final ResourceKey<VillagerProfession> name,
|
| 89 | final ResourceKey<PoiType> jobSite,
|
| 90 | final ImmutableSet<Item> requestedItems,
|
| 91 | final ImmutableSet<Block> secondaryPoi,
|
| 92 | final @Nullable SoundEvent workSound,
|
| 93 | final Int2ObjectMap<ResourceKey<TradeSet>> trades
|
| 94 | ) {
|
| 95 | return register(registry, name, poiType -> poiType.is(jobSite), poiType -> poiType.is(jobSite), requestedItems, secondaryPoi, workSound, trades);
|
| 96 | }
|
| 97 |
|
| 98 | private static VillagerProfession register(
|
| 99 | final Registry<VillagerProfession> registry,
|
| 100 | final ResourceKey<VillagerProfession> name,
|
| 101 | final Predicate<Holder<PoiType>> heldJobSite,
|
| 102 | final Predicate<Holder<PoiType>> acquirableJobSite,
|
| 103 | final ImmutableSet<Item> requestedItems,
|
| 104 | final ImmutableSet<Block> secondaryPoi,
|
| 105 | final @Nullable SoundEvent workSound,
|
| 106 | final Int2ObjectMap<ResourceKey<TradeSet>> trades
|
| 107 | ) {
|
| 108 | return Registry.register(
|
| 109 | registry,
|
| 110 | name,
|
| 111 | new VillagerProfession(
|
| 112 | Component.translatable("entity." + name.identifier().getNamespace() + ".villager." + name.identifier().getPath()),
|
| 113 | heldJobSite,
|
| 114 | acquirableJobSite,
|
| 115 | requestedItems,
|
| 116 | secondaryPoi,
|
| 117 | workSound,
|
| 118 | trades
|
| 119 | )
|
| 120 | );
|
| 121 | }
|
| 122 |
|
| 123 | public static VillagerProfession bootstrap(final Registry<VillagerProfession> registry) {
|
| 124 | register(registry, NONE, PoiType.NONE, ALL_ACQUIRABLE_JOBS, null);
|
| 125 | register(
|
| 126 | registry,
|
| 127 | ARMORER,
|
| 128 | PoiTypes.ARMORER,
|
| 129 | SoundEvents.VILLAGER_WORK_ARMORER,
|
| 130 | Int2ObjectMap.ofEntries(
|
| 131 | Int2ObjectMap.entry(1, TradeSets.ARMORER_LEVEL_1),
|
| 132 | Int2ObjectMap.entry(2, TradeSets.ARMORER_LEVEL_2),
|
| 133 | Int2ObjectMap.entry(3, TradeSets.ARMORER_LEVEL_3),
|
| 134 | Int2ObjectMap.entry(4, TradeSets.ARMORER_LEVEL_4),
|
| 135 | Int2ObjectMap.entry(5, TradeSets.ARMORER_LEVEL_5)
|
| 136 | )
|
| 137 | );
|
| 138 | register(
|
| 139 | registry,
|
| 140 | BUTCHER,
|
| 141 | PoiTypes.BUTCHER,
|
| 142 | SoundEvents.VILLAGER_WORK_BUTCHER,
|
| 143 | Int2ObjectMap.ofEntries(
|
| 144 | Int2ObjectMap.entry(1, TradeSets.BUTCHER_LEVEL_1),
|
| 145 | Int2ObjectMap.entry(2, TradeSets.BUTCHER_LEVEL_2),
|
| 146 | Int2ObjectMap.entry(3, TradeSets.BUTCHER_LEVEL_3),
|
| 147 | Int2ObjectMap.entry(4, TradeSets.BUTCHER_LEVEL_4),
|
| 148 | Int2ObjectMap.entry(5, TradeSets.BUTCHER_LEVEL_5)
|
| 149 | )
|
| 150 | );
|
| 151 | register(
|
| 152 | registry,
|
| 153 | CARTOGRAPHER,
|
| 154 | PoiTypes.CARTOGRAPHER,
|
| 155 | SoundEvents.VILLAGER_WORK_CARTOGRAPHER,
|
| 156 | Int2ObjectMap.ofEntries(
|
| 157 | Int2ObjectMap.entry(1, TradeSets.CARTOGRAPHER_LEVEL_1),
|
| 158 | Int2ObjectMap.entry(2, TradeSets.CARTOGRAPHER_LEVEL_2),
|
| 159 | Int2ObjectMap.entry(3, TradeSets.CARTOGRAPHER_LEVEL_3),
|
| 160 | Int2ObjectMap.entry(4, TradeSets.CARTOGRAPHER_LEVEL_4),
|
| 161 | Int2ObjectMap.entry(5, TradeSets.CARTOGRAPHER_LEVEL_5)
|
| 162 | )
|
| 163 | );
|
| 164 | register(
|
| 165 | registry,
|
| 166 | CLERIC,
|
| 167 | PoiTypes.CLERIC,
|
| 168 | SoundEvents.VILLAGER_WORK_CLERIC,
|
| 169 | Int2ObjectMap.ofEntries(
|
| 170 | Int2ObjectMap.entry(1, TradeSets.CLERIC_LEVEL_1),
|
| 171 | Int2ObjectMap.entry(2, TradeSets.CLERIC_LEVEL_2),
|
| 172 | Int2ObjectMap.entry(3, TradeSets.CLERIC_LEVEL_3),
|
| 173 | Int2ObjectMap.entry(4, TradeSets.CLERIC_LEVEL_4),
|
| 174 | Int2ObjectMap.entry(5, TradeSets.CLERIC_LEVEL_5)
|
| 175 | )
|
| 176 | );
|
| 177 | register(
|
| 178 | registry,
|
| 179 | FARMER,
|
| 180 | PoiTypes.FARMER,
|
| 181 | ImmutableSet.of(Items.WHEAT, Items.WHEAT_SEEDS, Items.BEETROOT_SEEDS, Items.BONE_MEAL),
|
| 182 | ImmutableSet.of(Blocks.FARMLAND),
|
| 183 | SoundEvents.VILLAGER_WORK_FARMER,
|
| 184 | Int2ObjectMap.ofEntries(
|
| 185 | Int2ObjectMap.entry(1, TradeSets.FARMER_LEVEL_1),
|
| 186 | Int2ObjectMap.entry(2, TradeSets.FARMER_LEVEL_2),
|
| 187 | Int2ObjectMap.entry(3, TradeSets.FARMER_LEVEL_3),
|
| 188 | Int2ObjectMap.entry(4, TradeSets.FARMER_LEVEL_4),
|
| 189 | Int2ObjectMap.entry(5, TradeSets.FARMER_LEVEL_5)
|
| 190 | )
|
| 191 | );
|
| 192 | register(
|
| 193 | registry,
|
| 194 | FISHERMAN,
|
| 195 | PoiTypes.FISHERMAN,
|
| 196 | SoundEvents.VILLAGER_WORK_FISHERMAN,
|
| 197 | Int2ObjectMap.ofEntries(
|
| 198 | Int2ObjectMap.entry(1, TradeSets.FISHERMAN_LEVEL_1),
|
| 199 | Int2ObjectMap.entry(2, TradeSets.FISHERMAN_LEVEL_2),
|
| 200 | Int2ObjectMap.entry(3, TradeSets.FISHERMAN_LEVEL_3),
|
| 201 | Int2ObjectMap.entry(4, TradeSets.FISHERMAN_LEVEL_4),
|
| 202 | Int2ObjectMap.entry(5, TradeSets.FISHERMAN_LEVEL_5)
|
| 203 | )
|
| 204 | );
|
| 205 | register(
|
| 206 | registry,
|
| 207 | FLETCHER,
|
| 208 | PoiTypes.FLETCHER,
|
| 209 | SoundEvents.VILLAGER_WORK_FLETCHER,
|
| 210 | Int2ObjectMap.ofEntries(
|
| 211 | Int2ObjectMap.entry(1, TradeSets.FLETCHER_LEVEL_1),
|
| 212 | Int2ObjectMap.entry(2, TradeSets.FLETCHER_LEVEL_2),
|
| 213 | Int2ObjectMap.entry(3, TradeSets.FLETCHER_LEVEL_3),
|
| 214 | Int2ObjectMap.entry(4, TradeSets.FLETCHER_LEVEL_4),
|
| 215 | Int2ObjectMap.entry(5, TradeSets.FLETCHER_LEVEL_5)
|
| 216 | )
|
| 217 | );
|
| 218 | register(
|
| 219 | registry,
|
| 220 | LEATHERWORKER,
|
| 221 | PoiTypes.LEATHERWORKER,
|
| 222 | SoundEvents.VILLAGER_WORK_LEATHERWORKER,
|
| 223 | Int2ObjectMap.ofEntries(
|
| 224 | Int2ObjectMap.entry(1, TradeSets.LEATHERWORKER_LEVEL_1),
|
| 225 | Int2ObjectMap.entry(2, TradeSets.LEATHERWORKER_LEVEL_2),
|
| 226 | Int2ObjectMap.entry(3, TradeSets.LEATHERWORKER_LEVEL_3),
|
| 227 | Int2ObjectMap.entry(4, TradeSets.LEATHERWORKER_LEVEL_4),
|
| 228 | Int2ObjectMap.entry(5, TradeSets.LEATHERWORKER_LEVEL_5)
|
| 229 | )
|
| 230 | );
|
| 231 | register(
|
| 232 | registry,
|
| 233 | LIBRARIAN,
|
| 234 | PoiTypes.LIBRARIAN,
|
| 235 | SoundEvents.VILLAGER_WORK_LIBRARIAN,
|
| 236 | Int2ObjectMap.ofEntries(
|
| 237 | Int2ObjectMap.entry(1, TradeSets.LIBRARIAN_LEVEL_1),
|
| 238 | Int2ObjectMap.entry(2, TradeSets.LIBRARIAN_LEVEL_2),
|
| 239 | Int2ObjectMap.entry(3, TradeSets.LIBRARIAN_LEVEL_3),
|
| 240 | Int2ObjectMap.entry(4, TradeSets.LIBRARIAN_LEVEL_4),
|
| 241 | Int2ObjectMap.entry(5, TradeSets.LIBRARIAN_LEVEL_5)
|
| 242 | )
|
| 243 | );
|
| 244 | register(
|
| 245 | registry,
|
| 246 | MASON,
|
| 247 | PoiTypes.MASON,
|
| 248 | SoundEvents.VILLAGER_WORK_MASON,
|
| 249 | Int2ObjectMap.ofEntries(
|
| 250 | Int2ObjectMap.entry(1, TradeSets.MASON_LEVEL_1),
|
| 251 | Int2ObjectMap.entry(2, TradeSets.MASON_LEVEL_2),
|
| 252 | Int2ObjectMap.entry(3, TradeSets.MASON_LEVEL_3),
|
| 253 | Int2ObjectMap.entry(4, TradeSets.MASON_LEVEL_4),
|
| 254 | Int2ObjectMap.entry(5, TradeSets.MASON_LEVEL_5)
|
| 255 | )
|
| 256 | );
|
| 257 | register(registry, NITWIT, PoiType.NONE, PoiType.NONE, null);
|
| 258 | register(
|
| 259 | registry,
|
| 260 | SHEPHERD,
|
| 261 | PoiTypes.SHEPHERD,
|
| 262 | SoundEvents.VILLAGER_WORK_SHEPHERD,
|
| 263 | Int2ObjectMap.ofEntries(
|
| 264 | Int2ObjectMap.entry(1, TradeSets.SHEPHERD_LEVEL_1),
|
| 265 | Int2ObjectMap.entry(2, TradeSets.SHEPHERD_LEVEL_2),
|
| 266 | Int2ObjectMap.entry(3, TradeSets.SHEPHERD_LEVEL_3),
|
| 267 | Int2ObjectMap.entry(4, TradeSets.SHEPHERD_LEVEL_4),
|
| 268 | Int2ObjectMap.entry(5, TradeSets.SHEPHERD_LEVEL_5)
|
| 269 | )
|
| 270 | );
|
| 271 | register(
|
| 272 | registry,
|
| 273 | TOOLSMITH,
|
| 274 | PoiTypes.TOOLSMITH,
|
| 275 | SoundEvents.VILLAGER_WORK_TOOLSMITH,
|
| 276 | Int2ObjectMap.ofEntries(
|
| 277 | Int2ObjectMap.entry(1, TradeSets.TOOLSMITH_LEVEL_1),
|
| 278 | Int2ObjectMap.entry(2, TradeSets.TOOLSMITH_LEVEL_2),
|
| 279 | Int2ObjectMap.entry(3, TradeSets.TOOLSMITH_LEVEL_3),
|
| 280 | Int2ObjectMap.entry(4, TradeSets.TOOLSMITH_LEVEL_4),
|
| 281 | Int2ObjectMap.entry(5, TradeSets.TOOLSMITH_LEVEL_5)
|
| 282 | )
|
| 283 | );
|
| 284 | return register(
|
| 285 | registry,
|
| 286 | WEAPONSMITH,
|
| 287 | PoiTypes.WEAPONSMITH,
|
| 288 | SoundEvents.VILLAGER_WORK_WEAPONSMITH,
|
| 289 | Int2ObjectMap.ofEntries(
|
| 290 | Int2ObjectMap.entry(1, TradeSets.WEAPONSMITH_LEVEL_1),
|
| 291 | Int2ObjectMap.entry(2, TradeSets.WEAPONSMITH_LEVEL_2),
|
| 292 | Int2ObjectMap.entry(3, TradeSets.WEAPONSMITH_LEVEL_3),
|
| 293 | Int2ObjectMap.entry(4, TradeSets.WEAPONSMITH_LEVEL_4),
|
| 294 | Int2ObjectMap.entry(5, TradeSets.WEAPONSMITH_LEVEL_5)
|
| 295 | )
|
| 296 | );
|
| 297 | }
|
| 298 |
|
| 299 | public @Nullable ResourceKey<TradeSet> getTrades(final int level) {
|
| 300 | return this.tradeSetsByLevel.get(level);
|
| 301 | }
|
| 302 | }
|
| 303 |
|