| 1 | package net.minecraft.world.level.storage.loot;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableList;
|
| 4 | import com.google.common.collect.Lists;
|
| 5 | import com.mojang.logging.LogUtils;
|
| 6 | import com.mojang.serialization.Codec;
|
| 7 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 8 | import it.unimi.dsi.fastutil.objects.ObjectArrayList;
|
| 9 | import java.util.Iterator;
|
| 10 | import java.util.List;
|
| 11 | import java.util.Optional;
|
| 12 | import java.util.function.Consumer;
|
| 13 | import net.fabricmc.fabric.api.loot.v3.FabricLootTableBuilder;
|
| 14 | import net.minecraft.core.Holder;
|
| 15 | import net.minecraft.core.HolderSet;
|
| 16 | import net.minecraft.core.registries.Registries;
|
| 17 | import net.minecraft.core.registries.codec.RegistryCodecs;
|
| 18 | import net.minecraft.resources.Identifier;
|
| 19 | import net.minecraft.resources.ResourceKey;
|
| 20 | import net.minecraft.server.level.ServerLevel;
|
| 21 | import net.minecraft.util.Mth;
|
| 22 | import net.minecraft.util.RandomSource;
|
| 23 | import net.minecraft.util.Util;
|
| 24 | import net.minecraft.util.context.ContextKeySet;
|
| 25 | import net.minecraft.world.Container;
|
| 26 | import net.minecraft.world.item.ItemStack;
|
| 27 | import net.minecraft.world.level.storage.loot.functions.FunctionUserBuilder;
|
| 28 | import net.minecraft.world.level.storage.loot.functions.LootItemFunction;
|
| 29 | import net.minecraft.world.level.storage.loot.functions.LootItemFunctions;
|
| 30 | import net.minecraft.world.level.storage.loot.parameters.LootContextParamSets;
|
| 31 | import org.slf4j.Logger;
|
| 32 |
|
| 33 | public class LootTable implements Validatable {
|
| 34 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 35 | public static final Codec<ResourceKey<LootTable>> KEY_CODEC = ResourceKey.codec(Registries.LOOT_TABLE);
|
| 36 | public static final ContextKeySet DEFAULT_PARAM_SET = LootContextParamSets.ALL_PARAMS;
|
| 37 | public static final long RANDOMIZE_SEED = 0L;
|
| 38 | public static final Codec<LootTable> DIRECT_CODEC = Codec.lazyInitialized(
|
| 39 | () -> RecordCodecBuilder.create(
|
| 40 | i -> i.group(
|
| 41 | LootContextParamSets.CODEC.lenientOptionalFieldOf("type", DEFAULT_PARAM_SET).forGetter(t -> t.paramSet),
|
| 42 | Identifier.CODEC.optionalFieldOf("random_sequence").forGetter(t -> t.randomSequence),
|
| 43 | LootPool.CODEC.listOf().optionalFieldOf("pools", List.of()).forGetter(t -> t.pools),
|
| 44 | LootItemFunctions.CODEC.optionalFieldOf("modifier").forGetter(t -> t.modifier)
|
| 45 | )
|
| 46 | .apply(i, LootTable::new)
|
| 47 | )
|
| 48 | );
|
| 49 | public static final Codec<Holder<LootTable>> CODEC = RegistryCodecs.holder(Registries.LOOT_TABLE, DIRECT_CODEC);
|
| 50 | public static final Codec<HolderSet<LootTable>> LIST_CODEC = RegistryCodecs.holderSet(Registries.LOOT_TABLE, DIRECT_CODEC);
|
| 51 | public static final LootTable EMPTY = new LootTable(LootContextParamSets.EMPTY, Optional.empty(), List.of(), Optional.empty());
|
| 52 | private final ContextKeySet paramSet;
|
| 53 | private final Optional<Identifier> randomSequence;
|
| 54 | private final List<LootPool> pools;
|
| 55 | private final Optional<Holder<LootItemFunction>> modifier;
|
| 56 |
|
| 57 | private LootTable(
|
| 58 | final ContextKeySet paramSet, final Optional<Identifier> randomSequence, final List<LootPool> pools, final Optional<Holder<LootItemFunction>> modifier
|
| 59 | ) {
|
| 60 | this.paramSet = paramSet;
|
| 61 | this.randomSequence = randomSequence;
|
| 62 | this.pools = pools;
|
| 63 | this.modifier = modifier;
|
| 64 | }
|
| 65 |
|
| 66 | public static Consumer<ItemStack> createStackSplitter(final ServerLevel level, final Consumer<ItemStack> output) {
|
| 67 | return result -> {
|
| 68 | if (result.isItemEnabled(level.enabledFeatures())) {
|
| 69 | if (result.getCount() < result.getMaxStackSize()) {
|
| 70 | output.accept(result);
|
| 71 | } else {
|
| 72 | int count = result.getCount();
|
| 73 |
|
| 74 | while (count > 0) {
|
| 75 | ItemStack copy = result.copyWithCount(Math.min(result.getMaxStackSize(), count));
|
| 76 | count -= copy.getCount();
|
| 77 | output.accept(copy);
|
| 78 | }
|
| 79 | }
|
| 80 | }
|
| 81 | };
|
| 82 | }
|
| 83 |
|
| 84 | public void getRandomItemsRaw(final LootParams params, final Consumer<ItemStack> output) {
|
| 85 | this.getRandomItemsRaw(new LootContext.Builder(params).create(this.randomSequence), output);
|
| 86 | }
|
| 87 |
|
| 88 | public void getRandomItemsRaw(final LootContext context, final Consumer<ItemStack> output) {
|
| 89 | LootContext.VisitedEntry<?> breadcrumb = LootContext.createVisitedEntry(this);
|
| 90 | if (context.pushVisitedElement(breadcrumb)) {
|
| 91 | Consumer<ItemStack> decoratedOutput = LootItemFunction.decorate(this.modifier, output, context);
|
| 92 |
|
| 93 | for (LootPool pool : this.pools) {
|
| 94 | pool.addRandomItems(decoratedOutput, context);
|
| 95 | }
|
| 96 |
|
| 97 | context.popVisitedElement(breadcrumb);
|
| 98 | } else {
|
| 99 | LOGGER.warn("Detected infinite loop in loot tables");
|
| 100 | }
|
| 101 | }
|
| 102 |
|
| 103 | public void getRandomItems(final LootParams params, final long optionalLootTableSeed, final Consumer<ItemStack> output) {
|
| 104 | this.getRandomItemsRaw(
|
| 105 | new LootContext.Builder(params).withOptionalRandomSeed(optionalLootTableSeed).create(this.randomSequence), createStackSplitter(params.getLevel(), output)
|
| 106 | );
|
| 107 | }
|
| 108 |
|
| 109 | public void getRandomItems(final LootParams params, final Consumer<ItemStack> output) {
|
| 110 | this.getRandomItemsRaw(params, createStackSplitter(params.getLevel(), output));
|
| 111 | }
|
| 112 |
|
| 113 | public void getRandomItems(final LootContext context, final Consumer<ItemStack> output) {
|
| 114 | this.getRandomItemsRaw(context, createStackSplitter(context.getLevel(), output));
|
| 115 | }
|
| 116 |
|
| 117 | public ObjectArrayList<ItemStack> getRandomItems(final LootParams params, final RandomSource randomSource) {
|
| 118 | return this.getRandomItems(new LootContext.Builder(params).withOptionalRandomSource(randomSource).create(this.randomSequence));
|
| 119 | }
|
| 120 |
|
| 121 | public ObjectArrayList<ItemStack> getRandomItems(final LootParams params, final long optionalLootTableSeed) {
|
| 122 | return this.getRandomItems(new LootContext.Builder(params).withOptionalRandomSeed(optionalLootTableSeed).create(this.randomSequence));
|
| 123 | }
|
| 124 |
|
| 125 | public ObjectArrayList<ItemStack> getRandomItems(final LootParams params) {
|
| 126 | return this.getRandomItems(new LootContext.Builder(params).create(this.randomSequence));
|
| 127 | }
|
| 128 |
|
| 129 | private ObjectArrayList<ItemStack> getRandomItems(final LootContext context) {
|
| 130 | ObjectArrayList<ItemStack> result = new ObjectArrayList<>();
|
| 131 | this.getRandomItems(context, result::add);
|
| 132 | return result;
|
| 133 | }
|
| 134 |
|
| 135 | public ContextKeySet getParamSet() {
|
| 136 | return this.paramSet;
|
| 137 | }
|
| 138 |
|
| 139 | @Override
|
| 140 | public void validate(final ValidationContext context) {
|
| 141 | Validatable.validate(context, "pools", this.pools);
|
| 142 | Validatable.validateHolder(context, "modifier", this.modifier);
|
| 143 | }
|
| 144 |
|
| 145 | public void fill(final Container container, final LootParams params, final long optionalRandomSeed) {
|
| 146 | LootContext context = new LootContext.Builder(params).withOptionalRandomSeed(optionalRandomSeed).create(this.randomSequence);
|
| 147 | ObjectArrayList<ItemStack> itemStacks = this.getRandomItems(context);
|
| 148 | RandomSource random = context.getRandom();
|
| 149 | List<Integer> availableSlots = this.getAvailableSlots(container, random);
|
| 150 | this.shuffleAndSplitItems(itemStacks, availableSlots.size(), random);
|
| 151 |
|
| 152 | for (ItemStack itemStack : itemStacks) {
|
| 153 | if (availableSlots.isEmpty()) {
|
| 154 | LOGGER.warn("Tried to over-fill a container");
|
| 155 | return;
|
| 156 | }
|
| 157 |
|
| 158 | if (itemStack.isEmpty()) {
|
| 159 | container.setItem(availableSlots.remove(availableSlots.size() - 1), ItemStack.EMPTY);
|
| 160 | } else {
|
| 161 | container.setItem(availableSlots.remove(availableSlots.size() - 1), itemStack);
|
| 162 | }
|
| 163 | }
|
| 164 | }
|
| 165 |
|
| 166 | private void shuffleAndSplitItems(final ObjectArrayList<ItemStack> result, final int availableSlots, final RandomSource random) {
|
| 167 | List<ItemStack> splittableItems = Lists.newArrayList();
|
| 168 | Iterator<ItemStack> iterator = result.iterator();
|
| 169 |
|
| 170 | while (iterator.hasNext()) {
|
| 171 | ItemStack itemStack = iterator.next();
|
| 172 | if (itemStack.isEmpty()) {
|
| 173 | iterator.remove();
|
| 174 | } else if (itemStack.getCount() > 1) {
|
| 175 | splittableItems.add(itemStack);
|
| 176 | iterator.remove();
|
| 177 | }
|
| 178 | }
|
| 179 |
|
| 180 | while (availableSlots - result.size() - splittableItems.size() > 0 && !splittableItems.isEmpty()) {
|
| 181 | ItemStack itemStack = splittableItems.remove(Mth.nextInt(random, 0, splittableItems.size() - 1));
|
| 182 | int remove = Mth.nextInt(random, 1, itemStack.getCount() / 2);
|
| 183 | ItemStack copy = itemStack.split(remove);
|
| 184 | if (itemStack.getCount() > 1 && random.nextBoolean()) {
|
| 185 | splittableItems.add(itemStack);
|
| 186 | } else {
|
| 187 | result.add(itemStack);
|
| 188 | }
|
| 189 |
|
| 190 | if (copy.getCount() > 1 && random.nextBoolean()) {
|
| 191 | splittableItems.add(copy);
|
| 192 | } else {
|
| 193 | result.add(copy);
|
| 194 | }
|
| 195 | }
|
| 196 |
|
| 197 | result.addAll(splittableItems);
|
| 198 | Util.shuffle(result, random);
|
| 199 | }
|
| 200 |
|
| 201 | private List<Integer> getAvailableSlots(final Container container, final RandomSource random) {
|
| 202 | ObjectArrayList<Integer> slots = new ObjectArrayList<>();
|
| 203 |
|
| 204 | for (int i = 0; i < container.getContainerSize(); i++) {
|
| 205 | if (container.getItem(i).isEmpty()) {
|
| 206 | slots.add(i);
|
| 207 | }
|
| 208 | }
|
| 209 |
|
| 210 | Util.shuffle(slots, random);
|
| 211 | return slots;
|
| 212 | }
|
| 213 |
|
| 214 | public static LootTable.Builder lootTable() {
|
| 215 | return new LootTable.Builder();
|
| 216 | }
|
| 217 |
|
| 218 | public static class Builder implements FunctionUserBuilder<LootTable.Builder>, FabricLootTableBuilder {
|
| 219 | private final ImmutableList.Builder<LootPool> pools = ImmutableList.builder();
|
| 220 | private final ImmutableList.Builder<Holder<LootItemFunction>> functions = ImmutableList.builder();
|
| 221 | private ContextKeySet paramSet = LootTable.DEFAULT_PARAM_SET;
|
| 222 | private Optional<Identifier> randomSequence = Optional.empty();
|
| 223 |
|
| 224 | public LootTable.Builder withPool(final LootPool.Builder pool) {
|
| 225 | this.pools.add(pool.build());
|
| 226 | return this;
|
| 227 | }
|
| 228 |
|
| 229 | public LootTable.Builder setParamSet(final ContextKeySet paramSet) {
|
| 230 | this.paramSet = paramSet;
|
| 231 | return this;
|
| 232 | }
|
| 233 |
|
| 234 | public LootTable.Builder setRandomSequence(final Identifier key) {
|
| 235 | this.randomSequence = Optional.of(key);
|
| 236 | return this;
|
| 237 | }
|
| 238 |
|
| 239 | public LootTable.Builder apply(final Holder<LootItemFunction> function) {
|
| 240 | this.functions.add(function);
|
| 241 | return this;
|
| 242 | }
|
| 243 |
|
| 244 | public LootTable.Builder unwrap() {
|
| 245 | return this;
|
| 246 | }
|
| 247 |
|
| 248 | public LootTable build() {
|
| 249 | return new LootTable(this.paramSet, this.randomSequence, this.pools.build(), FunctionUserBuilder.buildFunction(this.functions.build()));
|
| 250 | }
|
| 251 | }
|
| 252 | }
|
| 253 |
|