-
Notifications
You must be signed in to change notification settings - Fork 421
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
260 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
fabric-tag-api-v1/src/testmod/java/net/fabricmc/fabric/test/tag/TagAliasTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
/* | ||
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package net.fabricmc.fabric.test.tag; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import net.minecraft.block.Block; | ||
import net.minecraft.block.Blocks; | ||
import net.minecraft.item.Item; | ||
import net.minecraft.item.Items; | ||
import net.minecraft.loot.LootTable; | ||
import net.minecraft.registry.Registry; | ||
import net.minecraft.registry.RegistryEntryLookup; | ||
import net.minecraft.registry.RegistryKey; | ||
import net.minecraft.registry.RegistryKeys; | ||
import net.minecraft.registry.RegistryWrapper; | ||
import net.minecraft.registry.entry.RegistryEntry; | ||
import net.minecraft.registry.entry.RegistryEntryList; | ||
import net.minecraft.registry.tag.TagKey; | ||
import net.minecraft.util.Identifier; | ||
import net.minecraft.world.biome.Biome; | ||
import net.minecraft.world.biome.BiomeKeys; | ||
|
||
import net.fabricmc.api.ModInitializer; | ||
import net.fabricmc.fabric.api.event.lifecycle.v1.CommonLifecycleEvents; | ||
|
||
public final class TagAliasTest implements ModInitializer { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(TagAliasTest.class); | ||
|
||
// Test 1: Alias two non-empty tags | ||
public static final TagKey<Item> GEMS = tagKey(RegistryKeys.ITEM, "gems"); | ||
public static final TagKey<Item> EXPENSIVE_ROCKS = tagKey(RegistryKeys.ITEM, "expensive_rocks"); | ||
|
||
// Test 2: Alias a non-empty tag and an empty tag | ||
public static final TagKey<Item> REDSTONE_DUSTS = tagKey(RegistryKeys.ITEM, "redstone_dusts"); | ||
public static final TagKey<Item> REDSTONE_POWDERS = tagKey(RegistryKeys.ITEM, "redstone_powders"); | ||
|
||
// Test 3: Alias a non-empty tag and a missing tag | ||
public static final TagKey<Item> BEETROOTS = tagKey(RegistryKeys.ITEM, "beetroots"); | ||
public static final TagKey<Item> MISSING_BEETROOTS = tagKey(RegistryKeys.ITEM, "missing_beetroots"); | ||
|
||
// Test 4: Given tags A, B, C, make alias groups A+B and B+C. They should get merged. | ||
public static final TagKey<Block> BRICK_BLOCKS = tagKey(RegistryKeys.BLOCK, "brick_blocks"); | ||
public static final TagKey<Block> MORE_BRICK_BLOCKS = tagKey(RegistryKeys.BLOCK, "more_brick_blocks"); | ||
public static final TagKey<Block> BRICKS = tagKey(RegistryKeys.BLOCK, "bricks"); | ||
|
||
// Test 5: Merge tags from a world generation dynamic registry | ||
public static final TagKey<Biome> CLASSIC_BIOMES = tagKey(RegistryKeys.BIOME, "classic"); | ||
public static final TagKey<Biome> TRADITIONAL_BIOMES = tagKey(RegistryKeys.BIOME, "traditional"); | ||
|
||
// Test 6: Merge tags from a reloadable registry | ||
public static final TagKey<LootTable> NETHER_BRICKS_1 = tagKey(RegistryKeys.LOOT_TABLE, "nether_bricks_1"); | ||
public static final TagKey<LootTable> NETHER_BRICKS_2 = tagKey(RegistryKeys.LOOT_TABLE, "nether_bricks_2"); | ||
|
||
private static <T> TagKey<T> tagKey(RegistryKey<? extends Registry<T>> registryRef, String name) { | ||
return TagKey.of(registryRef, Identifier.of("fabric-tag-api-v1-testmod", name)); | ||
} | ||
|
||
@Override | ||
public void onInitialize() { | ||
CommonLifecycleEvents.TAGS_LOADED.register((registries, client) -> { | ||
LOGGER.info("Running tag alias tests on the {}...", client ? "client" : "server"); | ||
|
||
assertTagContent(registries, List.of(GEMS, EXPENSIVE_ROCKS), Item::getRegistryEntry, | ||
Items.DIAMOND, Items.EMERALD); | ||
assertTagContent(registries, List.of(REDSTONE_DUSTS, REDSTONE_POWDERS), Item::getRegistryEntry, | ||
Items.REDSTONE); | ||
assertTagContent(registries, List.of(BEETROOTS, MISSING_BEETROOTS), Item::getRegistryEntry, | ||
Items.BEETROOT); | ||
assertTagContent(registries, List.of(BRICK_BLOCKS, MORE_BRICK_BLOCKS, BRICKS), Block::getRegistryEntry, | ||
Blocks.BRICKS, Blocks.STONE_BRICKS, Blocks.NETHER_BRICKS, Blocks.RED_NETHER_BRICKS); | ||
assertTagContent(registries, List.of(CLASSIC_BIOMES, TRADITIONAL_BIOMES), | ||
BiomeKeys.PLAINS, BiomeKeys.DESERT); | ||
|
||
// The loot table registry isn't synced to the client. | ||
if (!client) { | ||
assertTagContent(registries, List.of(NETHER_BRICKS_1, NETHER_BRICKS_2), | ||
Blocks.NETHER_BRICKS.getLootTableKey().orElseThrow(), | ||
Blocks.RED_NETHER_BRICKS.getLootTableKey().orElseThrow()); | ||
} | ||
|
||
LOGGER.info("Tag alias tests completed successfully!"); | ||
}); | ||
} | ||
|
||
@SafeVarargs | ||
private static <T> void assertTagContent(RegistryWrapper.WrapperLookup registries, List<TagKey<T>> tags, Function<T, RegistryEntry<T>> entryExtractor, T... expected) { | ||
Set<RegistryEntry<T>> entries = Arrays.stream(expected) | ||
.map(entryExtractor) | ||
.collect(Collectors.toSet());; | ||
assertTagContent(registries, tags, entries); | ||
} | ||
|
||
@SafeVarargs | ||
private static <T> void assertTagContent(RegistryWrapper.WrapperLookup registries, List<TagKey<T>> tags, RegistryKey<T>... expected) { | ||
RegistryEntryLookup<T> lookup = registries.getOrThrow(tags.getFirst().registryRef()); | ||
Set<RegistryEntry.Reference<T>> entries = Arrays.stream(expected) | ||
.map(lookup::getOrThrow) | ||
.collect(Collectors.toSet()); | ||
assertTagContent(registries, tags, entries); | ||
} | ||
|
||
private static <T> void assertTagContent(RegistryWrapper.WrapperLookup registries, List<TagKey<T>> tags, Set<? extends RegistryEntry<T>> expected) { | ||
RegistryEntryLookup<T> lookup = registries.getOrThrow(tags.getFirst().registryRef()); | ||
|
||
for (TagKey<T> tag : tags) { | ||
RegistryEntryList.Named<T> tagEntryList = lookup.getOrThrow(tag); | ||
|
||
if (!Set.copyOf(tagEntryList.entries).equals(expected)) { | ||
throw new AssertionError("Expected tag %s to have contents %s, but it had %s instead" | ||
.formatted(tag, expected, tagEntryList.entries)); | ||
} | ||
} | ||
|
||
LOGGER.info("Tags {} / {} were successfully aliased together", tags.getFirst().registryRef().getValue(), tags.stream() | ||
.map(TagKey::id) | ||
.map(Identifier::toString) | ||
.collect(Collectors.joining(", "))); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
...rc/testmod/resources/data/fabric-tag-api-v1-testmod/fabric/tag_alias/block/bricks_ab.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"tags": [ | ||
"fabric-tag-api-v1-testmod:bricks", | ||
"fabric-tag-api-v1-testmod:brick_blocks" | ||
] | ||
} |
6 changes: 6 additions & 0 deletions
6
...rc/testmod/resources/data/fabric-tag-api-v1-testmod/fabric/tag_alias/block/bricks_bc.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"tags": [ | ||
"fabric-tag-api-v1-testmod:brick_blocks", | ||
"fabric-tag-api-v1-testmod:more_brick_blocks" | ||
] | ||
} |
6 changes: 6 additions & 0 deletions
6
...src/testmod/resources/data/fabric-tag-api-v1-testmod/fabric/tag_alias/item/beetroots.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"tags": [ | ||
"fabric-tag-api-v1-testmod:beetroots", | ||
"fabric-tag-api-v1-testmod:missing_beetroots" | ||
] | ||
} |
6 changes: 6 additions & 0 deletions
6
...i-v1/src/testmod/resources/data/fabric-tag-api-v1-testmod/fabric/tag_alias/item/gems.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"tags": [ | ||
"fabric-tag-api-v1-testmod:expensive_rocks", | ||
"fabric-tag-api-v1-testmod:gems" | ||
] | ||
} |
6 changes: 6 additions & 0 deletions
6
...estmod/resources/data/fabric-tag-api-v1-testmod/fabric/tag_alias/item/redstone_dusts.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"tags": [ | ||
"fabric-tag-api-v1-testmod:redstone_dusts", | ||
"fabric-tag-api-v1-testmod:redstone_powders" | ||
] | ||
} |
6 changes: 6 additions & 0 deletions
6
...d/resources/data/fabric-tag-api-v1-testmod/fabric/tag_alias/loot_table/nether_bricks.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"tags": [ | ||
"fabric-tag-api-v1-testmod:nether_bricks_1", | ||
"fabric-tag-api-v1-testmod:nether_bricks_2" | ||
] | ||
} |
6 changes: 6 additions & 0 deletions
6
...mod/resources/data/fabric-tag-api-v1-testmod/fabric/tag_alias/worldgen/biome/classic.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"tags": [ | ||
"fabric-tag-api-v1-testmod:classic", | ||
"fabric-tag-api-v1-testmod:traditional" | ||
] | ||
} |
6 changes: 6 additions & 0 deletions
6
...-api-v1/src/testmod/resources/data/fabric-tag-api-v1-testmod/tags/block/brick_blocks.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"replace": false, | ||
"values": [ | ||
"minecraft:nether_bricks" | ||
] | ||
} |
7 changes: 7 additions & 0 deletions
7
...ic-tag-api-v1/src/testmod/resources/data/fabric-tag-api-v1-testmod/tags/block/bricks.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"replace": false, | ||
"values": [ | ||
"minecraft:bricks", | ||
"minecraft:stone_bricks" | ||
] | ||
} |
6 changes: 6 additions & 0 deletions
6
...v1/src/testmod/resources/data/fabric-tag-api-v1-testmod/tags/block/more_brick_blocks.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"replace": false, | ||
"values": [ | ||
"minecraft:red_nether_bricks" | ||
] | ||
} |
6 changes: 6 additions & 0 deletions
6
...-tag-api-v1/src/testmod/resources/data/fabric-tag-api-v1-testmod/tags/item/beetroots.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"replace": false, | ||
"values": [ | ||
"minecraft:beetroot" | ||
] | ||
} |
6 changes: 6 additions & 0 deletions
6
...pi-v1/src/testmod/resources/data/fabric-tag-api-v1-testmod/tags/item/expensive_rocks.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"replace": false, | ||
"values": [ | ||
"minecraft:emerald" | ||
] | ||
} |
6 changes: 6 additions & 0 deletions
6
fabric-tag-api-v1/src/testmod/resources/data/fabric-tag-api-v1-testmod/tags/item/gems.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"replace": false, | ||
"values": [ | ||
"minecraft:diamond" | ||
] | ||
} |
6 changes: 6 additions & 0 deletions
6
...api-v1/src/testmod/resources/data/fabric-tag-api-v1-testmod/tags/item/redstone_dusts.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"replace": false, | ||
"values": [ | ||
"minecraft:redstone" | ||
] | ||
} |
5 changes: 5 additions & 0 deletions
5
...i-v1/src/testmod/resources/data/fabric-tag-api-v1-testmod/tags/item/redstone_powders.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"replace": false, | ||
"values": [ | ||
] | ||
} |
6 changes: 6 additions & 0 deletions
6
...src/testmod/resources/data/fabric-tag-api-v1-testmod/tags/loot_table/nether_bricks_1.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"replace": false, | ||
"values": [ | ||
"minecraft:blocks/nether_bricks" | ||
] | ||
} |
6 changes: 6 additions & 0 deletions
6
...src/testmod/resources/data/fabric-tag-api-v1-testmod/tags/loot_table/nether_bricks_2.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"replace": false, | ||
"values": [ | ||
"minecraft:blocks/red_nether_bricks" | ||
] | ||
} |
6 changes: 6 additions & 0 deletions
6
...-v1/src/testmod/resources/data/fabric-tag-api-v1-testmod/tags/worldgen/biome/classic.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"replace": false, | ||
"values": [ | ||
"minecraft:plains" | ||
] | ||
} |
6 changes: 6 additions & 0 deletions
6
...src/testmod/resources/data/fabric-tag-api-v1-testmod/tags/worldgen/biome/traditional.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"replace": false, | ||
"values": [ | ||
"minecraft:desert" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
}, | ||
"entrypoints": { | ||
"main": [ | ||
"net.fabricmc.fabric.test.tag.TagAliasTest" | ||
] | ||
} | ||
} |