| 1 | package net.minecraft.world.level.storage.loot.functions;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableList;
|
| 4 | import com.google.common.collect.ImmutableList.Builder;
|
| 5 | import com.mojang.logging.LogUtils;
|
| 6 | import com.mojang.serialization.Codec;
|
| 7 | import com.mojang.serialization.DataResult;
|
| 8 | import com.mojang.serialization.MapCodec;
|
| 9 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 10 | import java.util.List;
|
| 11 | import java.util.Optional;
|
| 12 | import java.util.stream.Stream;
|
| 13 | import net.minecraft.util.ExtraCodecs;
|
| 14 | import net.minecraft.util.StringRepresentable;
|
| 15 | import org.slf4j.Logger;
|
| 16 |
|
| 17 | public interface ListOperation {
|
| 18 | MapCodec<ListOperation> UNLIMITED_CODEC = codec(Integer.MAX_VALUE);
|
| 19 |
|
| 20 | static MapCodec<ListOperation> codec(final int maxSize) {
|
| 21 | return ListOperation.Type.CODEC.<ListOperation>dispatchMap("mode", ListOperation::mode, e -> e.mapCodec).validate(op -> {
|
| 22 | if (op instanceof ListOperation.ReplaceSection section && section.size().isPresent()) {
|
| 23 | int size = section.size().get();
|
| 24 | if (size > maxSize) {
|
| 25 | return DataResult.error(() -> "Size value too large: " + size + ", max size is " + maxSize);
|
| 26 | }
|
| 27 | }
|
| 28 |
|
| 29 | return DataResult.success(op);
|
| 30 | });
|
| 31 | }
|
| 32 |
|
| 33 | ListOperation.Type mode();
|
| 34 |
|
| 35 | default <T> List<T> apply(final List<T> original, final List<T> replacement) {
|
| 36 | return this.apply(original, replacement, Integer.MAX_VALUE);
|
| 37 | }
|
| 38 |
|
| 39 | <T> List<T> apply(List<T> original, List<T> replacement, int maxSize);
|
| 40 |
|
| 41 | class Append implements ListOperation {
|
| 42 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 43 | public static final ListOperation.Append INSTANCE = new ListOperation.Append();
|
| 44 | public static final MapCodec<ListOperation.Append> MAP_CODEC = MapCodec.unit(() -> INSTANCE);
|
| 45 |
|
| 46 | private Append() {
|
| 47 | }
|
| 48 |
|
| 49 | @Override
|
| 50 | public ListOperation.Type mode() {
|
| 51 | return ListOperation.Type.APPEND;
|
| 52 | }
|
| 53 |
|
| 54 | @Override
|
| 55 | public <T> List<T> apply(final List<T> original, final List<T> replacement, final int maxSize) {
|
| 56 | if (original.size() + replacement.size() > maxSize) {
|
| 57 | LOGGER.error("Contents overflow in section append");
|
| 58 | return original;
|
| 59 | } else {
|
| 60 | return Stream.concat(original.stream(), replacement.stream()).toList();
|
| 61 | }
|
| 62 | }
|
| 63 | }
|
| 64 |
|
| 65 | record Insert(int offset) implements ListOperation {
|
| 66 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 67 | public static final MapCodec<ListOperation.Insert> MAP_CODEC = RecordCodecBuilder.mapCodec(
|
| 68 | i -> i.group(ExtraCodecs.NON_NEGATIVE_INT.optionalFieldOf("offset", 0).forGetter(ListOperation.Insert::offset)).apply(i, ListOperation.Insert::new)
|
| 69 | );
|
| 70 |
|
| 71 | @Override
|
| 72 | public ListOperation.Type mode() {
|
| 73 | return ListOperation.Type.INSERT;
|
| 74 | }
|
| 75 |
|
| 76 | @Override
|
| 77 | public <T> List<T> apply(final List<T> original, final List<T> replacement, final int maxSize) {
|
| 78 | int originalSize = original.size();
|
| 79 | if (this.offset > originalSize) {
|
| 80 | LOGGER.error("Cannot insert when offset is out of bounds");
|
| 81 | return original;
|
| 82 | } else if (originalSize + replacement.size() > maxSize) {
|
| 83 | LOGGER.error("Contents overflow in section insertion");
|
| 84 | return original;
|
| 85 | } else {
|
| 86 | Builder<T> newList = ImmutableList.builder();
|
| 87 | newList.addAll(original.subList(0, this.offset));
|
| 88 | newList.addAll(replacement);
|
| 89 | newList.addAll(original.subList(this.offset, originalSize));
|
| 90 | return newList.build();
|
| 91 | }
|
| 92 | }
|
| 93 | }
|
| 94 |
|
| 95 | class ReplaceAll implements ListOperation {
|
| 96 | public static final ListOperation.ReplaceAll INSTANCE = new ListOperation.ReplaceAll();
|
| 97 | public static final MapCodec<ListOperation.ReplaceAll> MAP_CODEC = MapCodec.unit(() -> INSTANCE);
|
| 98 |
|
| 99 | private ReplaceAll() {
|
| 100 | }
|
| 101 |
|
| 102 | @Override
|
| 103 | public ListOperation.Type mode() {
|
| 104 | return ListOperation.Type.REPLACE_ALL;
|
| 105 | }
|
| 106 |
|
| 107 | @Override
|
| 108 | public <T> List<T> apply(final List<T> original, final List<T> replacement, final int maxSize) {
|
| 109 | return replacement;
|
| 110 | }
|
| 111 | }
|
| 112 |
|
| 113 | record ReplaceSection(int offset, Optional<Integer> size) implements ListOperation {
|
| 114 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 115 | public static final MapCodec<ListOperation.ReplaceSection> MAP_CODEC = RecordCodecBuilder.mapCodec(
|
| 116 | i -> i.group(
|
| 117 | ExtraCodecs.NON_NEGATIVE_INT.optionalFieldOf("offset", 0).forGetter(ListOperation.ReplaceSection::offset),
|
| 118 | ExtraCodecs.NON_NEGATIVE_INT.optionalFieldOf("size").forGetter(ListOperation.ReplaceSection::size)
|
| 119 | )
|
| 120 | .apply(i, ListOperation.ReplaceSection::new)
|
| 121 | );
|
| 122 |
|
| 123 | public ReplaceSection(final int offset) {
|
| 124 | this(offset, Optional.empty());
|
| 125 | }
|
| 126 |
|
| 127 | @Override
|
| 128 | public ListOperation.Type mode() {
|
| 129 | return ListOperation.Type.REPLACE_SECTION;
|
| 130 | }
|
| 131 |
|
| 132 | @Override
|
| 133 | public <T> List<T> apply(final List<T> original, final List<T> replacement, final int maxSize) {
|
| 134 | int originalSize = original.size();
|
| 135 | if (this.offset > originalSize) {
|
| 136 | LOGGER.error("Cannot replace when offset is out of bounds");
|
| 137 | return original;
|
| 138 | }
|
| 139 |
|
| 140 | Builder<T> newList = ImmutableList.builder();
|
| 141 | newList.addAll(original.subList(0, this.offset));
|
| 142 | newList.addAll(replacement);
|
| 143 | int resumeIndex = this.offset + this.size.orElse(replacement.size());
|
| 144 | if (resumeIndex < originalSize) {
|
| 145 | newList.addAll(original.subList(resumeIndex, originalSize));
|
| 146 | }
|
| 147 |
|
| 148 | List<T> result = newList.build();
|
| 149 | if (result.size() > maxSize) {
|
| 150 | LOGGER.error("Contents overflow in section replacement");
|
| 151 | return original;
|
| 152 | } else {
|
| 153 | return result;
|
| 154 | }
|
| 155 | }
|
| 156 | }
|
| 157 |
|
| 158 | record StandAlone<T>(List<T> value, ListOperation operation) {
|
| 159 | public static <T> Codec<ListOperation.StandAlone<T>> codec(final Codec<T> valueCodec, final int maxSize) {
|
| 160 | return RecordCodecBuilder.create(
|
| 161 | i -> i.group(valueCodec.sizeLimitedListOf(maxSize).fieldOf("values").forGetter(f -> f.value), ListOperation.codec(maxSize).forGetter(f -> f.operation))
|
| 162 | .apply(i, ListOperation.StandAlone::new)
|
| 163 | );
|
| 164 | }
|
| 165 |
|
| 166 | public List<T> apply(final List<T> input) {
|
| 167 | return this.operation.apply(input, this.value);
|
| 168 | }
|
| 169 | }
|
| 170 |
|
| 171 | enum Type implements StringRepresentable {
|
| 172 | REPLACE_ALL("replace_all", ListOperation.ReplaceAll.MAP_CODEC),
|
| 173 | REPLACE_SECTION("replace_section", ListOperation.ReplaceSection.MAP_CODEC),
|
| 174 | INSERT("insert", ListOperation.Insert.MAP_CODEC),
|
| 175 | APPEND("append", ListOperation.Append.MAP_CODEC);
|
| 176 |
|
| 177 | public static final Codec<ListOperation.Type> CODEC = StringRepresentable.fromEnum(ListOperation.Type::values);
|
| 178 | private final String id;
|
| 179 | private final MapCodec<? extends ListOperation> mapCodec;
|
| 180 |
|
| 181 | Type(final String id, final MapCodec<? extends ListOperation> mapCodec) {
|
| 182 | this.id = id;
|
| 183 | this.mapCodec = mapCodec;
|
| 184 | }
|
| 185 |
|
| 186 | public MapCodec<? extends ListOperation> mapCodec() {
|
| 187 | return this.mapCodec;
|
| 188 | }
|
| 189 |
|
| 190 | @Override
|
| 191 | public String getSerializedName() {
|
| 192 | return this.id;
|
| 193 | }
|
| 194 | }
|
| 195 | }
|
| 196 |
|