|
| 1 | +/* |
| 2 | + * Copyright (c) Forge Development LLC and contributors |
| 3 | + * SPDX-License-Identifier: LGPL-2.1-only |
| 4 | + */ |
| 5 | + |
| 6 | +package net.neoforged.neoforge.common.conditions; |
| 7 | + |
| 8 | +import java.util.List; |
| 9 | +import net.minecraft.core.Registry; |
| 10 | +import net.minecraft.core.registries.Registries; |
| 11 | +import net.minecraft.resources.ResourceKey; |
| 12 | +import net.minecraft.resources.ResourceLocation; |
| 13 | +import net.minecraft.tags.TagKey; |
| 14 | +import net.minecraft.world.flag.FeatureFlag; |
| 15 | +import net.minecraft.world.flag.FeatureFlagSet; |
| 16 | +import org.apache.commons.lang3.ArrayUtils; |
| 17 | + |
| 18 | +public final class NeoForgeConditions { |
| 19 | + public static ICondition and(ICondition... values) { |
| 20 | + return new AndCondition(List.of(values)); |
| 21 | + } |
| 22 | + |
| 23 | + public static ICondition never() { |
| 24 | + return NeverCondition.INSTANCE; |
| 25 | + } |
| 26 | + |
| 27 | + public static ICondition always() { |
| 28 | + return AlwaysCondition.INSTANCE; |
| 29 | + } |
| 30 | + |
| 31 | + public static ICondition not(ICondition value) { |
| 32 | + return new NotCondition(value); |
| 33 | + } |
| 34 | + |
| 35 | + public static ICondition or(ICondition... values) { |
| 36 | + return new OrCondition(List.of(values)); |
| 37 | + } |
| 38 | + |
| 39 | + public static <TRegistry> ICondition registered(ResourceKey<TRegistry> registryKey) { |
| 40 | + return new RegisteredCondition<>(registryKey); |
| 41 | + } |
| 42 | + |
| 43 | + public static <TRegistry> ICondition registered(ResourceKey<? extends Registry<TRegistry>> registryType, ResourceLocation registryName) { |
| 44 | + return registered(ResourceKey.create(registryType, registryName)); |
| 45 | + } |
| 46 | + |
| 47 | + public static ICondition registered(ResourceLocation registryTypeName, ResourceLocation registryName) { |
| 48 | + return registered(ResourceKey.createRegistryKey(registryTypeName), registryName); |
| 49 | + } |
| 50 | + |
| 51 | + public static ICondition itemRegistered(ResourceLocation itemName) { |
| 52 | + return registered(Registries.ITEM, itemName); |
| 53 | + } |
| 54 | + |
| 55 | + public static ICondition itemRegistered(String namespace, String path) { |
| 56 | + return itemRegistered(ResourceLocation.fromNamespaceAndPath(namespace, path)); |
| 57 | + } |
| 58 | + |
| 59 | + public static ICondition itemRegistered(String itemName) { |
| 60 | + return itemRegistered(ResourceLocation.parse(itemName)); |
| 61 | + } |
| 62 | + |
| 63 | + public static ICondition modLoaded(String modid) { |
| 64 | + return new ModLoadedCondition(modid); |
| 65 | + } |
| 66 | + |
| 67 | + public static <TRegistry> ICondition tagEmpty(TagKey<TRegistry> tag) { |
| 68 | + return new TagEmptyCondition<>(tag); |
| 69 | + } |
| 70 | + |
| 71 | + public static <TRegistry> ICondition tagEmpty(ResourceKey<? extends Registry<TRegistry>> tagType, ResourceLocation tagName) { |
| 72 | + return tagEmpty(TagKey.create(tagType, tagName)); |
| 73 | + } |
| 74 | + |
| 75 | + public static ICondition itemTagEmpty(ResourceLocation tagName) { |
| 76 | + return tagEmpty(Registries.ITEM, tagName); |
| 77 | + } |
| 78 | + |
| 79 | + public static ICondition itemTagEmpty(String namespace, String tagPath) { |
| 80 | + return itemTagEmpty(ResourceLocation.fromNamespaceAndPath(namespace, tagPath)); |
| 81 | + } |
| 82 | + |
| 83 | + public static ICondition itemTagEmpty(String tagName) { |
| 84 | + return itemTagEmpty(ResourceLocation.parse(tagName)); |
| 85 | + } |
| 86 | + |
| 87 | + public static ICondition featureFlagsEnabled(FeatureFlagSet requiredFeatures) { |
| 88 | + return new FeatureFlagsEnabledCondition(requiredFeatures); |
| 89 | + } |
| 90 | + |
| 91 | + public static ICondition featureFlagsEnabled(FeatureFlag... requiredFlags) { |
| 92 | + if (requiredFlags.length == 0) { |
| 93 | + throw new IllegalArgumentException("FeatureFlagsEnabledCondition requires at least one feature flag."); |
| 94 | + } |
| 95 | + if (requiredFlags.length == 1) { |
| 96 | + return new FeatureFlagsEnabledCondition(FeatureFlagSet.of(requiredFlags[0])); |
| 97 | + } else { |
| 98 | + return new FeatureFlagsEnabledCondition(FeatureFlagSet.of(requiredFlags[0], ArrayUtils.remove(requiredFlags, 0))); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + private NeoForgeConditions() { |
| 103 | + // NOOP - Utility class, never to be constructed |
| 104 | + } |
| 105 | +} |
0 commit comments