| 1 | package net.minecraft.world.level.storage.loot.providers.number.ints;
|
| 2 |
|
| 3 | import com.mojang.datafixers.util.Either;
|
| 4 | import com.mojang.serialization.Codec;
|
| 5 | import java.util.List;
|
| 6 | import net.minecraft.advancements.predicates.StatePropertiesPredicate;
|
| 7 | import net.minecraft.core.Holder;
|
| 8 | import net.minecraft.core.HolderGetter;
|
| 9 | import net.minecraft.core.HolderSet;
|
| 10 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 11 | import net.minecraft.core.registries.Registries;
|
| 12 | import net.minecraft.core.registries.codec.RegistryCodecs;
|
| 13 | import net.minecraft.data.worldgen.BootstrapContext;
|
| 14 | import net.minecraft.resources.Identifier;
|
| 15 | import net.minecraft.resources.ResourceKey;
|
| 16 | import net.minecraft.util.random.WeightedList;
|
| 17 | import net.minecraft.world.level.block.Block;
|
| 18 | import net.minecraft.world.level.block.Blocks;
|
| 19 | import net.minecraft.world.level.block.ComposterBlock;
|
| 20 | import net.minecraft.world.level.storage.loot.LootContext;
|
| 21 | import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;
|
| 22 | import net.minecraft.world.level.storage.loot.predicates.LootPredicates;
|
| 23 | import net.minecraft.world.level.storage.loot.predicates.MatchBlock;
|
| 24 | import net.minecraft.world.level.storage.loot.providers.number.DispatcherProvider;
|
| 25 | import net.minecraft.world.level.storage.loot.providers.number.floats.ContextFloatProvider;
|
| 26 | import net.minecraft.world.level.storage.loot.providers.number.floats.ContextFloatProviders;
|
| 27 | import net.minecraft.world.level.storage.loot.providers.score.ContextScoreboardNameProvider;
|
| 28 |
|
| 29 | /**
|
| 30 | * Access widened by fabric-loot-api-v3 to accessible
|
| 31 | */
|
| 32 | public class ContextIntProviders {
|
| 33 | public static final Codec<ContextIntProvider> DIRECT_CODEC = Codec.lazyInitialized(
|
| 34 | () -> {
|
| 35 | Codec<ContextIntProvider> typedCodec = BuiltInRegistries.CONTEXT_INT_PROVIDER_TYPE.byNameCodec().dispatch(ContextIntProvider::codec, c -> c);
|
| 36 | return Codec.either(ConstantValue.INLINE_CODEC, typedCodec)
|
| 37 | .xmap(Either::unwrap, provider -> provider instanceof ConstantValue constant ? Either.left(constant) : Either.right(provider));
|
| 38 | }
|
| 39 | );
|
| 40 | public static final Codec<Holder<ContextIntProvider>> CODEC = RegistryCodecs.holder(Registries.CONTEXT_INT_PROVIDER, DIRECT_CODEC);
|
| 41 | public static final Codec<HolderSet<ContextIntProvider>> LIST_CODEC = RegistryCodecs.holderSet(Registries.CONTEXT_INT_PROVIDER, DIRECT_CODEC);
|
| 42 | public static final ResourceKey<ContextIntProvider> COMPOSTABLE_LOW = createKey("compostable/low");
|
| 43 | public static final ResourceKey<ContextIntProvider> COMPOSTABLE_LOW_MEDIUM = createKey("compostable/low_medium");
|
| 44 | public static final ResourceKey<ContextIntProvider> COMPOSTABLE_MEDIUM = createKey("compostable/medium");
|
| 45 | public static final ResourceKey<ContextIntProvider> COMPOSTABLE_MEDIUM_HIGH = createKey("compostable/medium_high");
|
| 46 | public static final ResourceKey<ContextIntProvider> COMPOSTABLE_ALWAYS_ADD_ONE = createKey("compostable/always_add_one");
|
| 47 | public static final ResourceKey<ContextIntProvider> COOKING_TIME_BAMBOO = createKey("cooking/time_bamboo");
|
| 48 | public static final ResourceKey<ContextIntProvider> COOKING_TIME_WOOL_SLABS = createKey("cooking/time_wool_slabs");
|
| 49 | public static final ResourceKey<ContextIntProvider> COOKING_TIME_WOOL_CARPETS = createKey("cooking/time_wool_carpets");
|
| 50 | public static final ResourceKey<ContextIntProvider> COOKING_TIME_DRY_PLANTS = createKey("cooking/time_dry_plants");
|
| 51 | public static final ResourceKey<ContextIntProvider> COOKING_TIME_WOOD_ITEMS_EXTRA_SMALL = createKey("cooking/time_wood_items_extra_small");
|
| 52 | public static final ResourceKey<ContextIntProvider> COOKING_TIME_WOOL = createKey("cooking/time_wool");
|
| 53 | public static final ResourceKey<ContextIntProvider> COOKING_TIME_WOOD_SLABS = createKey("cooking/time_wood_slabs");
|
| 54 | public static final ResourceKey<ContextIntProvider> COOKING_TIME_WOOD_ITEMS_LARGE = createKey("cooking/time_wood_items_large");
|
| 55 | public static final ResourceKey<ContextIntProvider> COOKING_TIME_WOOD_ITEMS_SMALL = createKey("cooking/time_wood_items_small");
|
| 56 | public static final ResourceKey<ContextIntProvider> COOKING_TIME_ROOTS = createKey("cooking/time_roots");
|
| 57 | public static final ResourceKey<ContextIntProvider> COOKING_TIME_WOOD_BLOCKS = createKey("cooking/time_wood_blocks");
|
| 58 | public static final ResourceKey<ContextIntProvider> COOKING_TIME_HANGING_SIGNS = createKey("cooking/time_hanging_signs");
|
| 59 | public static final ResourceKey<ContextIntProvider> COOKING_TIME_BOATS = createKey("cooking/time_boats");
|
| 60 | public static final ResourceKey<ContextIntProvider> COOKING_TIME_COAL = createKey("cooking/time_coal");
|
| 61 | public static final ResourceKey<ContextIntProvider> COOKING_TIME_BLAZE_ROD = createKey("cooking/time_blaze_rod");
|
| 62 | public static final ResourceKey<ContextIntProvider> COOKING_TIME_DRIED_KELP_BLOCK = createKey("cooking/time_dried_kelp_block");
|
| 63 | public static final ResourceKey<ContextIntProvider> COOKING_TIME_COAL_BLOCK = createKey("cooking/time_coal_block");
|
| 64 | public static final ResourceKey<ContextIntProvider> COOKING_TIME_LAVA_BUCKET = createKey("cooking/time_lava_bucket");
|
| 65 | public static final ResourceKey<ContextIntProvider> COOKING_NORMAL_BURN_TIME_REDUCTION_FACTOR = createKey("cooking/normal_burn_time_reduction_factor");
|
| 66 | public static final ResourceKey<ContextIntProvider> COOKING_FAST_BURN_TIME_REDUCTION_FACTOR = createKey("cooking/fast_burn_time_reduction_factor");
|
| 67 | public static final ResourceKey<ContextIntProvider> BREWING_DEFAULT_USES = createKey("brewing/uses_default");
|
| 68 |
|
| 69 | private static ResourceKey<ContextIntProvider> createKey(final String location) {
|
| 70 | return ResourceKey.create(Registries.CONTEXT_INT_PROVIDER, Identifier.withDefaultNamespace(location));
|
| 71 | }
|
| 72 |
|
| 73 | public static void bootstrap(final BootstrapContext<ContextIntProvider> context) {
|
| 74 | HolderGetter<Block> blocks = context.lookup(Registries.BLOCK);
|
| 75 | HolderGetter<LootItemCondition> predicates = context.lookup(Registries.PREDICATE);
|
| 76 | Holder.Reference<ContextIntProvider> normalBurnTime = context.register(COOKING_NORMAL_BURN_TIME_REDUCTION_FACTOR, new ConstantValue(1));
|
| 77 | Holder.Reference<ContextIntProvider> fastBurnTime = context.register(COOKING_FAST_BURN_TIME_REDUCTION_FACTOR, new ConstantValue(2));
|
| 78 | context.register(COMPOSTABLE_LOW, compostable(blocks, 30));
|
| 79 | context.register(COMPOSTABLE_LOW_MEDIUM, compostable(blocks, 50));
|
| 80 | context.register(COMPOSTABLE_MEDIUM, compostable(blocks, 65));
|
| 81 | context.register(COMPOSTABLE_MEDIUM_HIGH, compostable(blocks, 85));
|
| 82 | context.register(COMPOSTABLE_ALWAYS_ADD_ONE, compostable(blocks, 100));
|
| 83 | context.register(COOKING_TIME_BAMBOO, cooking(predicates, normalBurnTime, fastBurnTime, 50));
|
| 84 | context.register(COOKING_TIME_WOOL_SLABS, cooking(predicates, normalBurnTime, fastBurnTime, 50));
|
| 85 | context.register(COOKING_TIME_WOOL_CARPETS, cooking(predicates, normalBurnTime, fastBurnTime, 67));
|
| 86 | context.register(COOKING_TIME_DRY_PLANTS, cooking(predicates, normalBurnTime, fastBurnTime, 100));
|
| 87 | context.register(COOKING_TIME_WOOD_ITEMS_EXTRA_SMALL, cooking(predicates, normalBurnTime, fastBurnTime, 100));
|
| 88 | context.register(COOKING_TIME_WOOL, cooking(predicates, normalBurnTime, fastBurnTime, 100));
|
| 89 | context.register(COOKING_TIME_WOOD_SLABS, cooking(predicates, normalBurnTime, fastBurnTime, 150));
|
| 90 | context.register(COOKING_TIME_WOOD_ITEMS_LARGE, cooking(predicates, normalBurnTime, fastBurnTime, 200));
|
| 91 | context.register(COOKING_TIME_ROOTS, cooking(predicates, normalBurnTime, fastBurnTime, 300));
|
| 92 | context.register(COOKING_TIME_WOOD_BLOCKS, cooking(predicates, normalBurnTime, fastBurnTime, 300));
|
| 93 | context.register(COOKING_TIME_WOOD_ITEMS_SMALL, cooking(predicates, normalBurnTime, fastBurnTime, 300));
|
| 94 | context.register(COOKING_TIME_HANGING_SIGNS, cooking(predicates, normalBurnTime, fastBurnTime, 800));
|
| 95 | context.register(COOKING_TIME_BOATS, cooking(predicates, normalBurnTime, fastBurnTime, 1200));
|
| 96 | context.register(COOKING_TIME_COAL, cooking(predicates, normalBurnTime, fastBurnTime, 1600));
|
| 97 | context.register(COOKING_TIME_BLAZE_ROD, cooking(predicates, normalBurnTime, fastBurnTime, 2400));
|
| 98 | context.register(COOKING_TIME_DRIED_KELP_BLOCK, cooking(predicates, normalBurnTime, fastBurnTime, 4001));
|
| 99 | context.register(COOKING_TIME_COAL_BLOCK, cooking(predicates, normalBurnTime, fastBurnTime, 16000));
|
| 100 | context.register(COOKING_TIME_LAVA_BUCKET, cooking(predicates, normalBurnTime, fastBurnTime, 20000));
|
| 101 | context.register(BREWING_DEFAULT_USES, new ConstantValue(20));
|
| 102 | }
|
| 103 |
|
| 104 | /**
|
| 105 | * Access widened by fabric-loot-api-v3 to accessible
|
| 106 | */
|
| 107 | public static ContextIntProvider compostable(final HolderGetter<Block> blocks, final int layerIncreaseChance) {
|
| 108 | if (layerIncreaseChance >= 100) {
|
| 109 | return new ConstantValue(1);
|
| 110 | }
|
| 111 |
|
| 112 | DispatcherProvider.Case<ContextIntProvider> emptyCase = new DispatcherProvider.Case<>(
|
| 113 | Holder.direct(MatchBlock.blockMatches(blocks, Blocks.COMPOSTER, StatePropertiesPredicate.Builder.properties().hasProperty(ComposterBlock.LEVEL, 0)).build()),
|
| 114 | exactly(1)
|
| 115 | );
|
| 116 | WeightedList<Holder<ContextIntProvider>> cases = WeightedList.<Holder<ContextIntProvider>>builder()
|
| 117 | .add(exactly(1), layerIncreaseChance)
|
| 118 | .add(exactly(0), 100 - layerIncreaseChance)
|
| 119 | .build();
|
| 120 | return new NumberDispatcher(List.of(emptyCase), weighted(cases));
|
| 121 | }
|
| 122 |
|
| 123 | /**
|
| 124 | * Access widened by fabric-loot-api-v3 to accessible
|
| 125 | */
|
| 126 | public static ContextIntProvider cooking(
|
| 127 | final HolderGetter<LootItemCondition> predicates,
|
| 128 | final Holder.Reference<ContextIntProvider> normalBurnTimeDivisor,
|
| 129 | final Holder.Reference<ContextIntProvider> fastBurnTimeDivisor,
|
| 130 | final int timeSeconds
|
| 131 | ) {
|
| 132 | Holder<LootItemCondition> fasterCookingBlocks = predicates.getOrThrow(LootPredicates.FAST_FURNACE);
|
| 133 | ContextIntProvider fastConditional = new ConditionalValue(fasterCookingBlocks, fastBurnTimeDivisor, normalBurnTimeDivisor);
|
| 134 | return div(exactly(timeSeconds), Holder.direct(fastConditional)).value();
|
| 135 | }
|
| 136 |
|
| 137 | public static Holder<ContextIntProvider> fromFloat(final Holder<ContextFloatProvider> input) {
|
| 138 | return Holder.direct(new FromFloat(input));
|
| 139 | }
|
| 140 |
|
| 141 | public static Holder<ContextIntProvider> binomial(final int n, final float p) {
|
| 142 | return Holder.direct(new BinomialDistributionGenerator(exactly(n), ContextFloatProviders.exactly(p)));
|
| 143 | }
|
| 144 |
|
| 145 | public static Holder<ContextIntProvider> exactly(final int value) {
|
| 146 | return Holder.direct(new ConstantValue(value));
|
| 147 | }
|
| 148 |
|
| 149 | public static Holder<ContextIntProvider> fromScoreboard(final LootContext.EntityTarget entityTarget, final String score) {
|
| 150 | return Holder.direct(new ScoreboardValue(ContextScoreboardNameProvider.forTarget(entityTarget), score, exactly(0)));
|
| 151 | }
|
| 152 |
|
| 153 | @SafeVarargs
|
| 154 | public static Holder<ContextIntProvider> avg(final Holder<ContextIntProvider>... inputs) {
|
| 155 | return Holder.direct(new Average(HolderSet.direct(inputs)));
|
| 156 | }
|
| 157 |
|
| 158 | public static Holder<ContextIntProvider> sub(final Holder<ContextIntProvider> left, final Holder<ContextIntProvider> right) {
|
| 159 | return Holder.direct(new Difference(left, right));
|
| 160 | }
|
| 161 |
|
| 162 | @SafeVarargs
|
| 163 | public static Holder<ContextIntProvider> max(final Holder<ContextIntProvider>... inputs) {
|
| 164 | return Holder.direct(new Maximum(HolderSet.direct(inputs)));
|
| 165 | }
|
| 166 |
|
| 167 | @SafeVarargs
|
| 168 | public static Holder<ContextIntProvider> min(final Holder<ContextIntProvider>... inputs) {
|
| 169 | return Holder.direct(new Minimum(HolderSet.direct(inputs)));
|
| 170 | }
|
| 171 |
|
| 172 | public static Holder<ContextIntProvider> mod(final Holder<ContextIntProvider> left, final Holder<ContextIntProvider> right) {
|
| 173 | return Holder.direct(new Modulus(left, right));
|
| 174 | }
|
| 175 |
|
| 176 | public static Holder<ContextIntProvider> floorMod(final Holder<ContextIntProvider> left, final Holder<ContextIntProvider> right) {
|
| 177 | return Holder.direct(new FloorModulus(left, right));
|
| 178 | }
|
| 179 |
|
| 180 | public static Holder<ContextIntProvider> negate(final Holder<ContextIntProvider> input) {
|
| 181 | return Holder.direct(new Negate(input));
|
| 182 | }
|
| 183 |
|
| 184 | @SafeVarargs
|
| 185 | public static Holder<ContextIntProvider> mul(final Holder<ContextIntProvider>... inputs) {
|
| 186 | return Holder.direct(new Product(HolderSet.direct(inputs)));
|
| 187 | }
|
| 188 |
|
| 189 | public static Holder<ContextIntProvider> div(final Holder<ContextIntProvider> left, final Holder<ContextIntProvider> right) {
|
| 190 | return Holder.direct(new Quotient(left, right));
|
| 191 | }
|
| 192 |
|
| 193 | public static Holder<ContextIntProvider> floorDiv(final Holder<ContextIntProvider> left, final Holder<ContextIntProvider> right) {
|
| 194 | return Holder.direct(new FloorQuotient(left, right));
|
| 195 | }
|
| 196 |
|
| 197 | @SafeVarargs
|
| 198 | public static Holder<ContextIntProvider> add(final Holder<ContextIntProvider>... inputs) {
|
| 199 | return Holder.direct(new Sum(HolderSet.direct(inputs)));
|
| 200 | }
|
| 201 |
|
| 202 | public static Holder<ContextIntProvider> between(final int min, final int max) {
|
| 203 | return Holder.direct(new UniformGenerator(exactly(min), exactly(max)));
|
| 204 | }
|
| 205 |
|
| 206 | public static Holder<ContextIntProvider> weighted(final WeightedList<Holder<ContextIntProvider>> cases) {
|
| 207 | return Holder.direct(new WeightedListValue(cases));
|
| 208 | }
|
| 209 | }
|
| 210 |
|