Skip to content

Commit

Permalink
Initial port to 24w35a
Browse files Browse the repository at this point in the history
  • Loading branch information
apple502j committed Aug 29, 2024
1 parent 9c280a9 commit d894af2
Show file tree
Hide file tree
Showing 36 changed files with 256 additions and 195 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@

package net.fabricmc.fabric.test.lookup;

import net.minecraft.block.Block;
import net.minecraft.registry.RegistryKey;

import net.minecraft.registry.RegistryKeys;

import org.jetbrains.annotations.NotNull;

import net.minecraft.block.AbstractBlock;
Expand All @@ -41,30 +46,35 @@ public class FabricApiLookupTest implements ModInitializer {
public static final String MOD_ID = "fabric-lookup-api-v1-testmod";
// Chute - Block without model that transfers item from the container above to the container below.
// It's meant to work with unsided containers: chests, dispensers, droppers and hoppers.
public static final ChuteBlock CHUTE_BLOCK = new ChuteBlock(AbstractBlock.Settings.create());
public static final RegistryKey<Block> CHUTE_BLOCK_KEY = keyOf("chute");
public static final ChuteBlock CHUTE_BLOCK = new ChuteBlock(AbstractBlock.Settings.create().registryKey(CHUTE_BLOCK_KEY));
public static final BlockItem CHUTE_ITEM = new BlockItem(CHUTE_BLOCK, new Item.Settings());
public static BlockEntityType<ChuteBlockEntity> CHUTE_BLOCK_ENTITY_TYPE;
// Cobble gen - Block without model that can generate infinite cobblestone when placed above a chute.
// It's meant to test BlockApiLookup#registerSelf.
public static final CobbleGenBlock COBBLE_GEN_BLOCK = new CobbleGenBlock(AbstractBlock.Settings.create());
public static final RegistryKey<Block> COBBLE_GEN_BLOCK_KEY = keyOf("cobble_gen");
public static final CobbleGenBlock COBBLE_GEN_BLOCK = new CobbleGenBlock(AbstractBlock.Settings.create().registryKey(COBBLE_GEN_BLOCK_KEY));
public static final BlockItem COBBLE_GEN_ITEM = new BlockItem(COBBLE_GEN_BLOCK, new Item.Settings());
public static BlockEntityType<CobbleGenBlockEntity> COBBLE_GEN_BLOCK_ENTITY_TYPE;
// Testing for item api lookups is done in the `item` package.

public static final InspectorBlock INSPECTOR_BLOCK = new InspectorBlock(AbstractBlock.Settings.create());
public static final RegistryKey<Block> INSPECTOR_BLOCK_KEY = keyOf("inspector");
public static final InspectorBlock INSPECTOR_BLOCK = new InspectorBlock(AbstractBlock.Settings.create().registryKey(INSPECTOR_BLOCK_KEY));
public static final BlockItem INSPECTOR_ITEM = new BlockItem(INSPECTOR_BLOCK, new Item.Settings());

private static RegistryKey<Block> keyOf(String id) {
return RegistryKey.of(RegistryKeys.BLOCK, Identifier.of(MOD_ID, id));
}

@Override
public void onInitialize() {
Identifier chute = Identifier.of(MOD_ID, "chute");
Registry.register(Registries.BLOCK, chute, CHUTE_BLOCK);
Registry.register(Registries.ITEM, chute, CHUTE_ITEM);
CHUTE_BLOCK_ENTITY_TYPE = Registry.register(Registries.BLOCK_ENTITY_TYPE, chute, FabricBlockEntityTypeBuilder.create(ChuteBlockEntity::new, CHUTE_BLOCK).build());
Registry.register(Registries.BLOCK, CHUTE_BLOCK_KEY, CHUTE_BLOCK);
Registry.register(Registries.ITEM, CHUTE_BLOCK_KEY.getValue(), CHUTE_ITEM);
CHUTE_BLOCK_ENTITY_TYPE = Registry.register(Registries.BLOCK_ENTITY_TYPE, CHUTE_BLOCK_KEY.getValue(), FabricBlockEntityTypeBuilder.create(ChuteBlockEntity::new, CHUTE_BLOCK).build());

Identifier cobbleGen = Identifier.of(MOD_ID, "cobble_gen");
Registry.register(Registries.BLOCK, cobbleGen, COBBLE_GEN_BLOCK);
Registry.register(Registries.ITEM, cobbleGen, COBBLE_GEN_ITEM);
COBBLE_GEN_BLOCK_ENTITY_TYPE = Registry.register(Registries.BLOCK_ENTITY_TYPE, cobbleGen, FabricBlockEntityTypeBuilder.create(CobbleGenBlockEntity::new, COBBLE_GEN_BLOCK).build());
Registry.register(Registries.BLOCK, COBBLE_GEN_BLOCK_KEY, COBBLE_GEN_BLOCK);
Registry.register(Registries.ITEM, COBBLE_GEN_BLOCK_KEY.getValue(), COBBLE_GEN_ITEM);
COBBLE_GEN_BLOCK_ENTITY_TYPE = Registry.register(Registries.BLOCK_ENTITY_TYPE, COBBLE_GEN_BLOCK_KEY.getValue(), FabricBlockEntityTypeBuilder.create(CobbleGenBlockEntity::new, COBBLE_GEN_BLOCK).build());

InventoryExtractableProvider extractableProvider = new InventoryExtractableProvider();
InventoryInsertableProvider insertableProvider = new InventoryInsertableProvider();
Expand All @@ -76,9 +86,8 @@ public void onInitialize() {
testLookupRegistry();
testSelfRegistration();

Identifier inspector = Identifier.of(FabricApiLookupTest.MOD_ID, "inspector");
Registry.register(Registries.BLOCK, inspector, INSPECTOR_BLOCK);
Registry.register(Registries.ITEM, inspector, INSPECTOR_ITEM);
Registry.register(Registries.BLOCK, INSPECTOR_BLOCK_KEY, INSPECTOR_BLOCK);
Registry.register(Registries.ITEM, INSPECTOR_BLOCK_KEY.getValue(), INSPECTOR_ITEM);

FabricItemApiLookupTest.onInitialize();
FabricEntityApiLookupTest.onInitialize();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,8 @@ public void setMusic(Optional<MusicSound> sound) {
}

private class GenerationSettingsContextImpl implements GenerationSettingsContext {
private final Registry<ConfiguredCarver<?>> carvers = registries.get(RegistryKeys.CONFIGURED_CARVER);
private final Registry<PlacedFeature> features = registries.get(RegistryKeys.PLACED_FEATURE);
private final Registry<ConfiguredCarver<?>> carvers = registries.getOrThrow(RegistryKeys.CONFIGURED_CARVER);
private final Registry<PlacedFeature> features = registries.getOrThrow(RegistryKeys.PLACED_FEATURE);
private final GenerationSettings generationSettings = biome.getGenerationSettings();

boolean rebuildFeatures;
Expand Down Expand Up @@ -316,7 +316,7 @@ private <T> RegistryEntryList<T> plus(@Nullable RegistryEntryList<T> values, Reg
* forgot to data-gen the JSONs corresponding to their built-in objects.
*/
private static <T> RegistryEntry.Reference<T> getEntry(Registry<T> registry, RegistryKey<T> key) {
RegistryEntry.Reference<T> entry = registry.getEntry(key).orElse(null);
RegistryEntry.Reference<T> entry = registry.getOptional(key).orElse(null);

if (entry == null) {
// The key doesn't exist in the data packs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,13 @@ public void finalizeWorldGen(DynamicRegistryManager impl) {
BiomeModificationMarker modificationTracker = (BiomeModificationMarker) impl;
modificationTracker.fabric_markModified();

Registry<Biome> biomes = impl.get(RegistryKeys.BIOME);
Registry<Biome> biomes = impl.getOrThrow(RegistryKeys.BIOME);

// Build a list of all biome keys in ascending order of their raw-id to get a consistent result in case
// someone does something stupid.
List<RegistryKey<Biome>> keys = biomes.getEntrySet().stream()
.map(Map.Entry::getKey)
.sorted(Comparator.comparingInt(key -> biomes.getRawId(biomes.getOrThrow(key))))
.sorted(Comparator.comparingInt(key -> biomes.getRawId(biomes.getValueOrThrow(key))))
.toList();

List<ModifierRecord> sortedModifiers = getSortedModifiers();
Expand All @@ -130,7 +130,7 @@ public void finalizeWorldGen(DynamicRegistryManager impl) {
int modifiersApplied = 0;

for (RegistryKey<Biome> key : keys) {
Biome biome = biomes.getOrThrow(key);
Biome biome = biomes.getValueOrThrow(key);

biomesProcessed++;

Expand Down Expand Up @@ -159,7 +159,7 @@ public void finalizeWorldGen(DynamicRegistryManager impl) {
modificationContext.freeze();

if (modificationContext.shouldRebuildFeatures()) {
impl.get(RegistryKeys.DIMENSION).stream().forEach(dimensionOptions -> {
impl.getOrThrow(RegistryKeys.DIMENSION).stream().forEach(dimensionOptions -> {
dimensionOptions.chunkGenerator().indexedFeaturesListSupplier = Suppliers.memoize(
() -> PlacedFeatureIndexer.collectIndexedFeatures(
List.copyOf(dimensionOptions.chunkGenerator().getBiomeSource().getBiomes()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public BiomeSelectionContextImpl(DynamicRegistryManager dynamicRegistries, Regis
this.dynamicRegistries = dynamicRegistries;
this.key = key;
this.biome = biome;
this.entry = dynamicRegistries.get(RegistryKeys.BIOME).getEntry(this.key).orElseThrow();
this.entry = dynamicRegistries.getOrThrow(RegistryKeys.BIOME).getOrThrow(this.key);
}

@Override
Expand All @@ -62,19 +62,19 @@ public RegistryEntry<Biome> getBiomeRegistryEntry() {

@Override
public Optional<RegistryKey<ConfiguredFeature<?, ?>>> getFeatureKey(ConfiguredFeature<?, ?> configuredFeature) {
Registry<ConfiguredFeature<?, ?>> registry = dynamicRegistries.get(RegistryKeys.CONFIGURED_FEATURE);
Registry<ConfiguredFeature<?, ?>> registry = dynamicRegistries.getOrThrow(RegistryKeys.CONFIGURED_FEATURE);
return registry.getKey(configuredFeature);
}

@Override
public Optional<RegistryKey<PlacedFeature>> getPlacedFeatureKey(PlacedFeature placedFeature) {
Registry<PlacedFeature> registry = dynamicRegistries.get(RegistryKeys.PLACED_FEATURE);
Registry<PlacedFeature> registry = dynamicRegistries.getOrThrow(RegistryKeys.PLACED_FEATURE);
return registry.getKey(placedFeature);
}

@Override
public boolean validForStructure(RegistryKey<Structure> key) {
Structure instance = dynamicRegistries.get(RegistryKeys.STRUCTURE).get(key);
Structure instance = dynamicRegistries.getOrThrow(RegistryKeys.STRUCTURE).get(key);

if (instance == null) {
return false;
Expand All @@ -85,13 +85,13 @@ public boolean validForStructure(RegistryKey<Structure> key) {

@Override
public Optional<RegistryKey<Structure>> getStructureKey(Structure structure) {
Registry<Structure> registry = dynamicRegistries.get(RegistryKeys.STRUCTURE);
Registry<Structure> registry = dynamicRegistries.getOrThrow(RegistryKeys.STRUCTURE);
return registry.getKey(structure);
}

@Override
public boolean canGenerateIn(RegistryKey<DimensionOptions> dimensionKey) {
DimensionOptions dimension = dynamicRegistries.get(RegistryKeys.DIMENSION).get(dimensionKey);
DimensionOptions dimension = dynamicRegistries.getOrThrow(RegistryKeys.DIMENSION).get(dimensionKey);

if (dimension == null) {
return false;
Expand All @@ -102,7 +102,7 @@ public boolean canGenerateIn(RegistryKey<DimensionOptions> dimensionKey) {

@Override
public boolean hasTag(TagKey<Biome> tag) {
Registry<Biome> biomeRegistry = dynamicRegistries.get(RegistryKeys.BIOME);
return biomeRegistry.entryOf(getBiomeKey()).isIn(tag);
Registry<Biome> biomeRegistry = dynamicRegistries.getOrThrow(RegistryKeys.BIOME);
return biomeRegistry.getOrThrow(getBiomeKey()).isIn(tag);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ public static boolean isBuiltinBiome(RegistryKey<Biome> key) {
}

public static RegistryEntryLookup<Biome> biomeRegistryWrapper() {
return vanillaRegistries.getWrapperOrThrow(RegistryKeys.BIOME);
return vanillaRegistries.getOrThrow(RegistryKeys.BIOME);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private static <T> boolean isInWithLocalFallback(TagKey<T> tagKey, RegistryEntry

if (maybeRegistry.isPresent()) {
// Check the synced tag exists and use that
if (maybeRegistry.get().getEntryList(tagKey).isPresent()) {
if (maybeRegistry.get().getOptional(tagKey).isPresent()) {
return registryEntry.isIn(tagKey);
}
}
Expand Down Expand Up @@ -93,7 +93,7 @@ public static <T> Optional<? extends Registry<T>> getRegistry(TagKey<T> tagKey)
}
}

return (Optional<? extends Registry<T>>) Registries.REGISTRIES.getOrEmpty(tagKey.registry().getValue());
return (Optional<? extends Registry<T>>) Registries.REGISTRIES.getOptionalValue(tagKey.registry().getValue());
}

@SuppressWarnings("unchecked")
Expand All @@ -108,7 +108,7 @@ public static <T> Optional<RegistryEntry<T>> getRegistryEntry(TagKey<T> tagKey,

Optional<RegistryKey<T>> maybeKey = registry.getKey(entry);

return maybeKey.map(registry::entryOf);
return maybeKey.map(registry::getOrThrow);
}

public static ClientTagsLoader.LoadedTag getOrCreatePartiallySyncedTag(TagKey<?> tagKey) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public final class ContentRegistryTest implements ModInitializer {
public static final Logger LOGGER = LoggerFactory.getLogger(ContentRegistryTest.class);

public static final Identifier TEST_EVENT_ID = Identifier.of("fabric-content-registries-v0-testmod", "test_event");
public static final RegistryKey<Block> TEST_EVENT_BLOCK_KEY = RegistryKey.of(RegistryKeys.BLOCK, TEST_EVENT_ID);
public static final RegistryEntry.Reference<GameEvent> TEST_EVENT = Registry.registerReference(Registries.GAME_EVENT, TEST_EVENT_ID, new GameEvent(GameEvent.DEFAULT_RANGE));

@Override
Expand Down Expand Up @@ -136,7 +137,7 @@ public void onInitialize() {

VillagerInteractionRegistries.registerGiftLootTable(VillagerProfession.NITWIT, RegistryKey.of(RegistryKeys.LOOT_TABLE, Identifier.ofVanilla("fake_loot_table")));

Registry.register(Registries.BLOCK, TEST_EVENT_ID, new TestEventBlock(AbstractBlock.Settings.copy(Blocks.STONE)));
Registry.register(Registries.BLOCK, TEST_EVENT_BLOCK_KEY, new TestEventBlock(AbstractBlock.Settings.copy(Blocks.STONE).registryKey(TEST_EVENT_BLOCK_KEY)));
SculkSensorFrequencyRegistry.register(TEST_EVENT.registryKey(), 2);

// assert that SculkSensorFrequencyRegistry throws when registering a frequency outside the allowed range
Expand All @@ -156,8 +157,8 @@ public void onInitialize() {
* This testmod uses an accessor due to Loom limitations that prevent TAWs from applying across Gradle subproject boundaries */
FabricBrewingRecipeRegistryBuilder.BUILD.register(builder -> {
builder.registerPotionType(dirtyPotion);
builder.registerItemRecipe(Items.POTION, Ingredient.fromTag(Registries.ITEM.getEntryList(ItemTags.DIRT).get()), dirtyPotion);
builder.registerPotionRecipe(Potions.AWKWARD, Ingredient.fromTag(Registries.ITEM.getEntryList(ItemTags.SMALL_FLOWERS).get()), Potions.HEALING);
builder.registerItemRecipe(Items.POTION, Ingredient.fromTag(Registries.ITEM.getOrThrow(ItemTags.DIRT)), dirtyPotion);
builder.registerPotionRecipe(Potions.AWKWARD, Ingredient.fromTag(Registries.ITEM.getOrThrow(ItemTags.SMALL_FLOWERS)), Potions.HEALING);

if (builder.getEnabledFeatures().contains(FeatureFlags.BUNDLE)) {
builder.registerPotionRecipe(Potions.AWKWARD, Ingredient.ofItems(Items.BUNDLE), Potions.LUCK);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static <T> boolean isIn(@Nullable DynamicRegistryManager registryManager,
if (registryManager != null) {
maybeRegistry = registryManager.getOptional(tagKey.registry());
} else {
maybeRegistry = Registries.REGISTRIES.getOrEmpty(tagKey.registry().getValue());
maybeRegistry = Registries.REGISTRIES.getOptionalValue(tagKey.registry().getValue());
}

if (maybeRegistry.isPresent()) {
Expand All @@ -77,7 +77,7 @@ public static <T> boolean isIn(@Nullable DynamicRegistryManager registryManager,

// Check synced tag
if (maybeKey.isPresent()) {
return registry.entryOf(maybeKey.get()).isIn(tagKey);
return registry.getOrThrow(maybeKey.get()).isIn(tagKey);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ private static void setupUntranslatedItemTagWarning() {
// Log missing item tag translations only when world is started.
ServerLifecycleEvents.SERVER_STARTED.register(server -> {
Language language = Language.getInstance();
Registry<Item> itemRegistry = server.getRegistryManager().get(RegistryKeys.ITEM);
Registry<Item> itemRegistry = server.getRegistryManager().getOrThrow(RegistryKeys.ITEM);
List<TagKey<Item>> untranslatedItemTags = new ObjectArrayList<>();
itemRegistry.streamTags().forEach(itemTagKey -> {
// We do not translate vanilla's tags at this moment.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.function.BiConsumer;
Expand All @@ -29,7 +30,6 @@
import net.minecraft.data.DataWriter;
import net.minecraft.data.server.loottable.BlockLootTableGenerator;
import net.minecraft.loot.LootTable;
import net.minecraft.loot.LootTables;
import net.minecraft.loot.context.LootContextTypes;
import net.minecraft.registry.Registries;
import net.minecraft.registry.RegistryKey;
Expand Down Expand Up @@ -79,10 +79,6 @@ public void accept(BiConsumer<RegistryKey<LootTable>, LootTable.Builder> biConsu
for (Map.Entry<RegistryKey<LootTable>, LootTable.Builder> entry : lootTables.entrySet()) {
RegistryKey<LootTable> registryKey = entry.getKey();

if (registryKey == LootTables.EMPTY) {
continue;
}

biConsumer.accept(registryKey, entry.getValue());
}

Expand All @@ -91,10 +87,10 @@ public void accept(BiConsumer<RegistryKey<LootTable>, LootTable.Builder> biConsu

for (Identifier blockId : Registries.BLOCK.getIds()) {
if (blockId.getNamespace().equals(output.getModId())) {
RegistryKey<LootTable> blockLootTableId = Registries.BLOCK.get(blockId).getLootTableKey();
Optional<RegistryKey<LootTable>> blockLootTableId = Registries.BLOCK.get(blockId).getLootTableKey();

if (blockLootTableId.getValue().getNamespace().equals(output.getModId())) {
if (!lootTables.containsKey(blockLootTableId)) {
if (blockLootTableId.isPresent() && blockLootTableId.get().getValue().getNamespace().equals(output.getModId())) {
if (!lootTables.containsKey(blockLootTableId.get())) {
missing.add(blockId);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public static final class Entries {
this.registries = registries;
this.queuedEntries = DynamicRegistries.getDynamicRegistries().stream()
// Some modded dynamic registries might not be in the wrapper lookup, filter them out
.filter(e -> registries.getOptionalWrapper(e.key()).isPresent())
.filter(e -> registries.getOptional(e.key()).isPresent())
.collect(Collectors.toMap(
e -> e.key().getValue(),
e -> RegistryEntries.create(registries, e)
Expand All @@ -104,7 +104,7 @@ public RegistryWrapper.WrapperLookup getLookups() {
* Gets a lookup for entries from the given registry.
*/
public <T> RegistryEntryLookup<T> getLookup(RegistryKey<? extends Registry<T>> registryKey) {
return registries.getWrapperOrThrow(registryKey);
return registries.getOrThrow(registryKey);
}

/**
Expand Down Expand Up @@ -232,7 +232,7 @@ private static class RegistryEntries<T> {
}

static <T> RegistryEntries<T> create(RegistryWrapper.WrapperLookup lookups, RegistryLoader.Entry<T> loaderEntry) {
RegistryWrapper.Impl<T> lookup = lookups.getWrapperOrThrow(loaderEntry.key());
RegistryWrapper.Impl<T> lookup = lookups.getOrThrow(loaderEntry.key());
return new RegistryEntries<>(lookup, loaderEntry.key(), loaderEntry.elementCodec());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void onInitialize() {
SIMPLE_BLOCK = createBlock("simple_block", true, AbstractBlock.Settings.create());
BLOCK_WITHOUT_ITEM = createBlock("block_without_item", false, AbstractBlock.Settings.create());
BLOCK_WITHOUT_LOOT_TABLE = createBlock("block_without_loot_table", false, AbstractBlock.Settings.create());
BLOCK_WITH_VANILLA_LOOT_TABLE = createBlock("block_with_vanilla_loot_table", false, AbstractBlock.Settings.create().dropsLike(Blocks.STONE));
BLOCK_WITH_VANILLA_LOOT_TABLE = createBlock("block_with_vanilla_loot_table", false, AbstractBlock.Settings.create().lootTable(Blocks.STONE.getLootTableKey()));
BLOCK_THAT_DROPS_NOTHING = createBlock("block_that_drops_nothing", false, AbstractBlock.Settings.create().dropsNothing());

ItemGroupEvents.modifyEntriesEvent(SIMPLE_ITEM_GROUP).register(entries -> entries.add(SIMPLE_BLOCK));
Expand All @@ -85,7 +85,7 @@ public void onInitialize() {

private static Block createBlock(String name, boolean hasItem, AbstractBlock.Settings settings) {
Identifier identifier = Identifier.of(MOD_ID, name);
Block block = Registry.register(Registries.BLOCK, identifier, new Block(settings));
Block block = Registry.register(Registries.BLOCK, identifier, new Block(settings.registryKey(RegistryKey.of(RegistryKeys.BLOCK, identifier))));

if (hasItem) {
Registry.register(Registries.ITEM, identifier, new BlockItem(block, new Item.Settings()));
Expand Down
Loading

0 comments on commit d894af2

Please sign in to comment.