mirror of
https://github.com/FabricMC/fabric.git
synced 2025-04-21 03:10:54 -04:00
Add TagUtil (#2307)
* Add TagUtil * Allow for the registry manager to passed in allowing for dynamic tags to be used * Update JD * Remove redundant fallback * Make TagUtil final Co-authored-by: haykam821 <24855774+haykam821@users.noreply.github.com> * Make TagUtil uninititializable * Add test mod * Fix spotless for test mod * JD indent Co-authored-by: Technici4n <13494793+Technici4n@users.noreply.github.com> * Apply suggestions * Fix checkstyle * Apply feedback Co-authored-by: haykam821 <24855774+haykam821@users.noreply.github.com> Co-authored-by: Technici4n <13494793+Technici4n@users.noreply.github.com>
This commit is contained in:
parent
1e232e13ae
commit
6718a028fc
4 changed files with 164 additions and 0 deletions
fabric-convention-tags-v1
|
@ -5,6 +5,10 @@ moduleDependencies(project, [
|
|||
'fabric-api-base'
|
||||
])
|
||||
|
||||
testDependencies(project, [
|
||||
':fabric-lifecycle-events-v1',
|
||||
])
|
||||
|
||||
sourceSets {
|
||||
main {
|
||||
resources {
|
||||
|
|
|
@ -0,0 +1,83 @@
|
|||
/*
|
||||
* 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.api.tag.convention.v1;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import net.minecraft.tag.TagKey;
|
||||
import net.minecraft.util.registry.DynamicRegistryManager;
|
||||
import net.minecraft.util.registry.Registry;
|
||||
import net.minecraft.util.registry.RegistryKey;
|
||||
|
||||
/**
|
||||
* A Helper class for checking whether a {@link TagKey} contains some entry.
|
||||
* This can be useful for {@link TagKey}s whose type has no easy way of querying if they are in a tag, such as {@link net.minecraft.enchantment.Enchantment}s.
|
||||
*
|
||||
* <p>For dynamic registry entries, use {@link #isIn(DynamicRegistryManager, TagKey, Object)} with a non-null dynamic registry manager.
|
||||
* For non-dynamic registry entries, the simpler {@link #isIn(TagKey, Object)} can be used.
|
||||
*/
|
||||
public final class TagUtil {
|
||||
private TagUtil() {
|
||||
}
|
||||
|
||||
/**
|
||||
* See {@link TagUtil#isIn(DynamicRegistryManager, TagKey, Object)} to check tags that refer to entries in dynamic
|
||||
* registries, such as {@link net.minecraft.world.biome.Biome}s.
|
||||
* @return if the entry is in the provided tag.
|
||||
*/
|
||||
public static <T> boolean isIn(TagKey<T> tagKey, T entry) {
|
||||
return isIn(null, tagKey, entry);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param registryManager the registry manager instance of the client or server. If the tag refers to entries
|
||||
* within a dynamic registry, such as {@link net.minecraft.world.biome.Biome}s,
|
||||
* this must be passed to correctly evaluate the tag. Otherwise, the registry is found by
|
||||
* looking in {@link Registry#REGISTRIES}.
|
||||
* @return if the entry is in the provided tag.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> boolean isIn(@Nullable DynamicRegistryManager registryManager, TagKey<T> tagKey, T entry) {
|
||||
Optional<? extends Registry<?>> maybeRegistry;
|
||||
Objects.requireNonNull(tagKey);
|
||||
Objects.requireNonNull(entry);
|
||||
|
||||
if (registryManager != null) {
|
||||
maybeRegistry = registryManager.getOptional(tagKey.registry());
|
||||
} else {
|
||||
maybeRegistry = Registry.REGISTRIES.getOrEmpty(tagKey.registry().getValue());
|
||||
}
|
||||
|
||||
if (maybeRegistry.isPresent()) {
|
||||
if (tagKey.isOf(maybeRegistry.get().getKey())) {
|
||||
Registry<T> registry = (Registry<T>) maybeRegistry.get();
|
||||
|
||||
Optional<RegistryKey<T>> maybeKey = registry.getKey(entry);
|
||||
|
||||
// Check synced tag
|
||||
if (maybeKey.isPresent()) {
|
||||
return registry.entryOf(maybeKey.get()).isIn(tagKey);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,61 @@
|
|||
/*
|
||||
* 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.convention.v1;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import net.minecraft.block.Blocks;
|
||||
import net.minecraft.enchantment.Enchantments;
|
||||
import net.minecraft.util.registry.Registry;
|
||||
import net.minecraft.world.biome.BiomeKeys;
|
||||
|
||||
import net.fabricmc.api.ModInitializer;
|
||||
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
|
||||
import net.fabricmc.fabric.api.tag.convention.v1.ConventionalBiomeTags;
|
||||
import net.fabricmc.fabric.api.tag.convention.v1.ConventionalBlockTags;
|
||||
import net.fabricmc.fabric.api.tag.convention.v1.ConventionalEnchantmentTags;
|
||||
import net.fabricmc.fabric.api.tag.convention.v1.TagUtil;
|
||||
|
||||
public class TagUtilTest implements ModInitializer {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(TagUtilTest.class);
|
||||
|
||||
@Override
|
||||
public void onInitialize() {
|
||||
ServerLifecycleEvents.SERVER_STARTED.register(server -> {
|
||||
if (!TagUtil.isIn(ConventionalEnchantmentTags.INCREASES_BLOCK_DROPS, Enchantments.FORTUNE)) {
|
||||
throw new AssertionError("Failed to find fortune in c:fortune!");
|
||||
}
|
||||
|
||||
if (TagUtil.isIn(ConventionalBiomeTags.IN_OVERWORLD, server.getRegistryManager().get(Registry.BIOME_KEY).get(BiomeKeys.BADLANDS))) {
|
||||
throw new AssertionError("Found a dynamic entry in a static registry?!");
|
||||
}
|
||||
|
||||
// If this fails, the tag is missing a biome or the util is broken
|
||||
if (!TagUtil.isIn(server.getRegistryManager(), ConventionalBiomeTags.IN_OVERWORLD, server.getRegistryManager().get(Registry.BIOME_KEY).get(BiomeKeys.BADLANDS))) {
|
||||
throw new AssertionError("Failed to find an overworld biome (%s) in c:in_overworld!".formatted(BiomeKeys.BADLANDS));
|
||||
}
|
||||
|
||||
if (!TagUtil.isIn(server.getRegistryManager(), ConventionalBlockTags.ORES, Blocks.DIAMOND_ORE)) {
|
||||
throw new AssertionError("Failed to find diamond ore in c:ores!");
|
||||
}
|
||||
|
||||
//Success!
|
||||
LOGGER.info("Completed TagUtil tests!");
|
||||
});
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
{
|
||||
"schemaVersion": 1,
|
||||
"id": "fabric-convention-tags-testmod",
|
||||
"name": "Fabric Convention Tags (Test Mod)",
|
||||
"version": "1.0.0",
|
||||
"environment": "*",
|
||||
"license": "Apache-2.0",
|
||||
"depends": {
|
||||
"fabric-convention-tags-v1": "*"
|
||||
},
|
||||
"entrypoints": {
|
||||
"main": [
|
||||
"net.fabricmc.fabric.test.tag.convention.v1.TagUtilTest"
|
||||
]
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue