| 1 | package net.minecraft.world.item.crafting.display;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.MapCodec;
|
| 5 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 6 | import java.util.List;
|
| 7 | import java.util.Optional;
|
| 8 | import java.util.function.BinaryOperator;
|
| 9 | import java.util.stream.IntStream;
|
| 10 | import java.util.stream.Stream;
|
| 11 | import net.minecraft.core.Holder;
|
| 12 | import net.minecraft.core.HolderLookup;
|
| 13 | import net.minecraft.core.HolderSet;
|
| 14 | import net.minecraft.core.RegistryAccess;
|
| 15 | import net.minecraft.core.component.DataComponentType;
|
| 16 | import net.minecraft.core.component.DataComponents;
|
| 17 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 18 | import net.minecraft.core.registries.Registries;
|
| 19 | import net.minecraft.core.registries.codec.RegistryCodecs;
|
| 20 | import net.minecraft.network.RegistryFriendlyByteBuf;
|
| 21 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 22 | import net.minecraft.network.codec.StreamCodec;
|
| 23 | import net.minecraft.util.RandomSource;
|
| 24 | import net.minecraft.util.Util;
|
| 25 | import net.minecraft.util.context.ContextMap;
|
| 26 | import net.minecraft.world.flag.FeatureFlagSet;
|
| 27 | import net.minecraft.world.item.DyeColor;
|
| 28 | import net.minecraft.world.item.Item;
|
| 29 | import net.minecraft.world.item.ItemStack;
|
| 30 | import net.minecraft.world.item.ItemStackTemplate;
|
| 31 | import net.minecraft.world.item.alchemy.Potion;
|
| 32 | import net.minecraft.world.item.alchemy.PotionContents;
|
| 33 | import net.minecraft.world.item.component.DyedItemColor;
|
| 34 | import net.minecraft.world.item.crafting.SmithingTrimRecipe;
|
| 35 | import net.minecraft.world.item.equipment.trim.TrimPattern;
|
| 36 |
|
| 37 | public interface SlotDisplay {
|
| 38 | Codec<SlotDisplay> CODEC = BuiltInRegistries.SLOT_DISPLAY.byNameCodec().dispatch(SlotDisplay::type, SlotDisplay.Type::codec);
|
| 39 | StreamCodec<RegistryFriendlyByteBuf, SlotDisplay> STREAM_CODEC = ByteBufCodecs.registry(Registries.SLOT_DISPLAY)
|
| 40 | .dispatch(SlotDisplay::type, SlotDisplay.Type::streamCodec);
|
| 41 |
|
| 42 | <T> Stream<T> resolve(ContextMap context, DisplayContentsFactory<T> builder);
|
| 43 |
|
| 44 | SlotDisplay.Type<? extends SlotDisplay> type();
|
| 45 |
|
| 46 | default boolean isEnabled(final FeatureFlagSet enabledFeatures) {
|
| 47 | return true;
|
| 48 | }
|
| 49 |
|
| 50 | default List<ItemStack> resolveForStacks(final ContextMap context) {
|
| 51 | return this.resolve(context, SlotDisplay.ItemStackContentsFactory.INSTANCE).toList();
|
| 52 | }
|
| 53 |
|
| 54 | default ItemStack resolveForFirstStack(final ContextMap context) {
|
| 55 | return this.resolve(context, SlotDisplay.ItemStackContentsFactory.INSTANCE).findFirst().orElse(ItemStack.EMPTY);
|
| 56 | }
|
| 57 |
|
| 58 | private static <T> Stream<T> applyDemoTransformation(
|
| 59 | final ContextMap context,
|
| 60 | final DisplayContentsFactory<T> factory,
|
| 61 | final SlotDisplay firstDisplay,
|
| 62 | final SlotDisplay secondDisplay,
|
| 63 | final RandomSource randomSource,
|
| 64 | final BinaryOperator<ItemStack> operation
|
| 65 | ) {
|
| 66 | if (factory instanceof DisplayContentsFactory.ForStacks<T> stacks) {
|
| 67 | List<ItemStack> firstItems = firstDisplay.resolveForStacks(context);
|
| 68 | if (firstItems.isEmpty()) {
|
| 69 | return Stream.empty();
|
| 70 | }
|
| 71 |
|
| 72 | List<ItemStack> secondItems = secondDisplay.resolveForStacks(context);
|
| 73 | return secondItems.isEmpty() ? Stream.empty() : Stream.<ItemStack>generate(() -> {
|
| 74 | ItemStack first = Util.getRandom(firstItems, randomSource);
|
| 75 | ItemStack second = Util.getRandom(secondItems, randomSource);
|
| 76 | return operation.apply(first, second);
|
| 77 | }).limit(256L).filter(s -> !s.isEmpty()).limit(16L).map(stacks::forStack);
|
| 78 | } else {
|
| 79 | return Stream.empty();
|
| 80 | }
|
| 81 | }
|
| 82 |
|
| 83 | private static <T> Stream<T> applyDemoTransformation(
|
| 84 | final ContextMap context,
|
| 85 | final DisplayContentsFactory<T> factory,
|
| 86 | final SlotDisplay firstDisplay,
|
| 87 | final SlotDisplay secondDisplay,
|
| 88 | final BinaryOperator<ItemStack> operation
|
| 89 | ) {
|
| 90 | if (factory instanceof DisplayContentsFactory.ForStacks<T> stacks) {
|
| 91 | List<ItemStack> firstItems = firstDisplay.resolveForStacks(context);
|
| 92 | if (firstItems.isEmpty()) {
|
| 93 | return Stream.empty();
|
| 94 | }
|
| 95 |
|
| 96 | List<ItemStack> secondItems = secondDisplay.resolveForStacks(context);
|
| 97 | if (secondItems.isEmpty()) {
|
| 98 | return Stream.empty();
|
| 99 | }
|
| 100 |
|
| 101 | int cycle = firstItems.size() * secondItems.size();
|
| 102 | return IntStream.range(0, cycle).mapToObj(index -> {
|
| 103 | int firstItemCount = firstItems.size();
|
| 104 | int firstItemIndex = index % firstItemCount;
|
| 105 | int secondItemIndex = index / firstItemCount;
|
| 106 | ItemStack first = firstItems.get(firstItemIndex);
|
| 107 | ItemStack second = secondItems.get(secondItemIndex);
|
| 108 | return operation.apply(first, second);
|
| 109 | }).filter(s -> !s.isEmpty()).limit(16L).map(stacks::forStack);
|
| 110 | } else {
|
| 111 | return Stream.empty();
|
| 112 | }
|
| 113 | }
|
| 114 |
|
| 115 | class AnyFuel implements SlotDisplay {
|
| 116 | public static final SlotDisplay.AnyFuel INSTANCE = new SlotDisplay.AnyFuel();
|
| 117 | public static final MapCodec<SlotDisplay.AnyFuel> MAP_CODEC = MapCodec.unit(INSTANCE);
|
| 118 | public static final StreamCodec<RegistryFriendlyByteBuf, SlotDisplay.AnyFuel> STREAM_CODEC = StreamCodec.unit(INSTANCE);
|
| 119 | public static final SlotDisplay.Type<SlotDisplay.AnyFuel> TYPE = new SlotDisplay.Type<>(MAP_CODEC, STREAM_CODEC);
|
| 120 |
|
| 121 | private AnyFuel() {
|
| 122 | }
|
| 123 |
|
| 124 | @Override
|
| 125 | public SlotDisplay.Type<SlotDisplay.AnyFuel> type() {
|
| 126 | return TYPE;
|
| 127 | }
|
| 128 |
|
| 129 | @Override
|
| 130 | public String toString() {
|
| 131 | return "<any fuel>";
|
| 132 | }
|
| 133 |
|
| 134 | @Override
|
| 135 | public <T> Stream<T> resolve(final ContextMap context, final DisplayContentsFactory<T> factory) {
|
| 136 | if (factory instanceof DisplayContentsFactory.ForStacks<T> stacks) {
|
| 137 | RegistryAccess registries = context.get(SlotDisplayContext.REGISTRIES);
|
| 138 | if (registries != null) {
|
| 139 | return registries.lookupOrThrow(Registries.ITEM).componentLookup().findAll(DataComponents.COOKING_FUEL).stream().map(stacks::forStack);
|
| 140 | }
|
| 141 | }
|
| 142 |
|
| 143 | return Stream.empty();
|
| 144 | }
|
| 145 | }
|
| 146 |
|
| 147 | record Composite(List<SlotDisplay> contents) implements SlotDisplay {
|
| 148 | public static final MapCodec<SlotDisplay.Composite> MAP_CODEC = RecordCodecBuilder.mapCodec(
|
| 149 | i -> i.group(SlotDisplay.CODEC.listOf().fieldOf("contents").forGetter(SlotDisplay.Composite::contents)).apply(i, SlotDisplay.Composite::new)
|
| 150 | );
|
| 151 | public static final StreamCodec<RegistryFriendlyByteBuf, SlotDisplay.Composite> STREAM_CODEC = StreamCodec.composite(
|
| 152 | SlotDisplay.STREAM_CODEC.apply(ByteBufCodecs.list()), SlotDisplay.Composite::contents, SlotDisplay.Composite::new
|
| 153 | );
|
| 154 | public static final SlotDisplay.Type<SlotDisplay.Composite> TYPE = new SlotDisplay.Type<>(MAP_CODEC, STREAM_CODEC);
|
| 155 |
|
| 156 | @Override
|
| 157 | public SlotDisplay.Type<SlotDisplay.Composite> type() {
|
| 158 | return TYPE;
|
| 159 | }
|
| 160 |
|
| 161 | @Override
|
| 162 | public <T> Stream<T> resolve(final ContextMap context, final DisplayContentsFactory<T> factory) {
|
| 163 | return this.contents.stream().flatMap(d -> d.resolve(context, factory));
|
| 164 | }
|
| 165 |
|
| 166 | @Override
|
| 167 | public boolean isEnabled(final FeatureFlagSet enabledFeatures) {
|
| 168 | return this.contents.stream().allMatch(c -> c.isEnabled(enabledFeatures));
|
| 169 | }
|
| 170 | }
|
| 171 |
|
| 172 | record DyedSlotDemo(SlotDisplay dye, SlotDisplay target) implements SlotDisplay {
|
| 173 | public static final MapCodec<SlotDisplay.DyedSlotDemo> MAP_CODEC = RecordCodecBuilder.mapCodec(
|
| 174 | i -> i.group(
|
| 175 | SlotDisplay.CODEC.fieldOf("dye").forGetter(SlotDisplay.DyedSlotDemo::dye), SlotDisplay.CODEC.fieldOf("target").forGetter(SlotDisplay.DyedSlotDemo::target)
|
| 176 | )
|
| 177 | .apply(i, SlotDisplay.DyedSlotDemo::new)
|
| 178 | );
|
| 179 | public static final StreamCodec<RegistryFriendlyByteBuf, SlotDisplay.DyedSlotDemo> STREAM_CODEC = StreamCodec.composite(
|
| 180 | SlotDisplay.STREAM_CODEC, SlotDisplay.DyedSlotDemo::dye, SlotDisplay.STREAM_CODEC, SlotDisplay.DyedSlotDemo::target, SlotDisplay.DyedSlotDemo::new
|
| 181 | );
|
| 182 | public static final SlotDisplay.Type<SlotDisplay.DyedSlotDemo> TYPE = new SlotDisplay.Type<>(MAP_CODEC, STREAM_CODEC);
|
| 183 |
|
| 184 | @Override
|
| 185 | public SlotDisplay.Type<SlotDisplay.DyedSlotDemo> type() {
|
| 186 | return TYPE;
|
| 187 | }
|
| 188 |
|
| 189 | @Override
|
| 190 | public <T> Stream<T> resolve(final ContextMap context, final DisplayContentsFactory<T> factory) {
|
| 191 | BinaryOperator<ItemStack> transformation = (target, dye) -> {
|
| 192 | DyeColor dyeValue = dye.getOrDefault(DataComponents.DYE, DyeColor.WHITE);
|
| 193 | return DyedItemColor.applyDyes(target.copy(), List.of(dyeValue));
|
| 194 | };
|
| 195 | return SlotDisplay.applyDemoTransformation(context, factory, this.target, this.dye, transformation);
|
| 196 | }
|
| 197 | }
|
| 198 |
|
| 199 | class Empty implements SlotDisplay {
|
| 200 | public static final SlotDisplay.Empty INSTANCE = new SlotDisplay.Empty();
|
| 201 | public static final MapCodec<SlotDisplay.Empty> MAP_CODEC = MapCodec.unit(INSTANCE);
|
| 202 | public static final StreamCodec<RegistryFriendlyByteBuf, SlotDisplay.Empty> STREAM_CODEC = StreamCodec.unit(INSTANCE);
|
| 203 | public static final SlotDisplay.Type<SlotDisplay.Empty> TYPE = new SlotDisplay.Type<>(MAP_CODEC, STREAM_CODEC);
|
| 204 |
|
| 205 | private Empty() {
|
| 206 | }
|
| 207 |
|
| 208 | @Override
|
| 209 | public SlotDisplay.Type<SlotDisplay.Empty> type() {
|
| 210 | return TYPE;
|
| 211 | }
|
| 212 |
|
| 213 | @Override
|
| 214 | public String toString() {
|
| 215 | return "<empty>";
|
| 216 | }
|
| 217 |
|
| 218 | @Override
|
| 219 | public <T> Stream<T> resolve(final ContextMap context, final DisplayContentsFactory<T> factory) {
|
| 220 | return Stream.empty();
|
| 221 | }
|
| 222 | }
|
| 223 |
|
| 224 | record ItemSlotDisplay(Holder<Item> item) implements SlotDisplay {
|
| 225 | public static final MapCodec<SlotDisplay.ItemSlotDisplay> MAP_CODEC = RecordCodecBuilder.mapCodec(
|
| 226 | i -> i.group(Item.CODEC.fieldOf("item").forGetter(SlotDisplay.ItemSlotDisplay::item)).apply(i, SlotDisplay.ItemSlotDisplay::new)
|
| 227 | );
|
| 228 | public static final StreamCodec<RegistryFriendlyByteBuf, SlotDisplay.ItemSlotDisplay> STREAM_CODEC = StreamCodec.composite(
|
| 229 | Item.STREAM_CODEC, SlotDisplay.ItemSlotDisplay::item, SlotDisplay.ItemSlotDisplay::new
|
| 230 | );
|
| 231 | public static final SlotDisplay.Type<SlotDisplay.ItemSlotDisplay> TYPE = new SlotDisplay.Type<>(MAP_CODEC, STREAM_CODEC);
|
| 232 |
|
| 233 | public ItemSlotDisplay(final Item item) {
|
| 234 | this(item.builtInRegistryHolder());
|
| 235 | }
|
| 236 |
|
| 237 | @Override
|
| 238 | public SlotDisplay.Type<SlotDisplay.ItemSlotDisplay> type() {
|
| 239 | return TYPE;
|
| 240 | }
|
| 241 |
|
| 242 | @Override
|
| 243 | public <T> Stream<T> resolve(final ContextMap context, final DisplayContentsFactory<T> factory) {
|
| 244 | return factory instanceof DisplayContentsFactory.ForStacks<T> stacks ? Stream.of(stacks.forStack(this.item)) : Stream.empty();
|
| 245 | }
|
| 246 |
|
| 247 | @Override
|
| 248 | public boolean isEnabled(final FeatureFlagSet enabledFeatures) {
|
| 249 | return this.item.value().isEnabled(enabledFeatures);
|
| 250 | }
|
| 251 | }
|
| 252 |
|
| 253 | class ItemStackContentsFactory implements DisplayContentsFactory.ForStacks<ItemStack> {
|
| 254 | public static final SlotDisplay.ItemStackContentsFactory INSTANCE = new SlotDisplay.ItemStackContentsFactory();
|
| 255 |
|
| 256 | public ItemStack forStack(final ItemStack stack) {
|
| 257 | return stack;
|
| 258 | }
|
| 259 | }
|
| 260 |
|
| 261 | record ItemStackSlotDisplay(ItemStackTemplate stack) implements SlotDisplay {
|
| 262 | public static final MapCodec<SlotDisplay.ItemStackSlotDisplay> MAP_CODEC = RecordCodecBuilder.mapCodec(
|
| 263 | i -> i.group(ItemStackTemplate.CODEC.fieldOf("item").forGetter(SlotDisplay.ItemStackSlotDisplay::stack)).apply(i, SlotDisplay.ItemStackSlotDisplay::new)
|
| 264 | );
|
| 265 | public static final StreamCodec<RegistryFriendlyByteBuf, SlotDisplay.ItemStackSlotDisplay> STREAM_CODEC = StreamCodec.composite(
|
| 266 | ItemStackTemplate.STREAM_CODEC, SlotDisplay.ItemStackSlotDisplay::stack, SlotDisplay.ItemStackSlotDisplay::new
|
| 267 | );
|
| 268 | public static final SlotDisplay.Type<SlotDisplay.ItemStackSlotDisplay> TYPE = new SlotDisplay.Type<>(MAP_CODEC, STREAM_CODEC);
|
| 269 |
|
| 270 | @Override
|
| 271 | public SlotDisplay.Type<SlotDisplay.ItemStackSlotDisplay> type() {
|
| 272 | return TYPE;
|
| 273 | }
|
| 274 |
|
| 275 | @Override
|
| 276 | public <T> Stream<T> resolve(final ContextMap context, final DisplayContentsFactory<T> factory) {
|
| 277 | return factory instanceof DisplayContentsFactory.ForStacks<T> stacks ? Stream.of(stacks.forStack(this.stack.create())) : Stream.empty();
|
| 278 | }
|
| 279 |
|
| 280 | @Override
|
| 281 | public boolean isEnabled(final FeatureFlagSet enabledFeatures) {
|
| 282 | return this.stack.item().value().isEnabled(enabledFeatures);
|
| 283 | }
|
| 284 | }
|
| 285 |
|
| 286 | record OnlyWithComponent(SlotDisplay source, DataComponentType<?> component) implements SlotDisplay {
|
| 287 | public static final MapCodec<SlotDisplay.OnlyWithComponent> MAP_CODEC = RecordCodecBuilder.mapCodec(
|
| 288 | i -> i.group(
|
| 289 | SlotDisplay.CODEC.fieldOf("contents").forGetter(SlotDisplay.OnlyWithComponent::source),
|
| 290 | DataComponentType.CODEC.fieldOf("component").forGetter(SlotDisplay.OnlyWithComponent::component)
|
| 291 | )
|
| 292 | .apply(i, SlotDisplay.OnlyWithComponent::new)
|
| 293 | );
|
| 294 | public static final StreamCodec<RegistryFriendlyByteBuf, SlotDisplay.OnlyWithComponent> STREAM_CODEC = StreamCodec.composite(
|
| 295 | SlotDisplay.STREAM_CODEC,
|
| 296 | SlotDisplay.OnlyWithComponent::source,
|
| 297 | DataComponentType.STREAM_CODEC,
|
| 298 | SlotDisplay.OnlyWithComponent::component,
|
| 299 | SlotDisplay.OnlyWithComponent::new
|
| 300 | );
|
| 301 | public static final SlotDisplay.Type<SlotDisplay.OnlyWithComponent> TYPE = new SlotDisplay.Type<>(MAP_CODEC, STREAM_CODEC);
|
| 302 |
|
| 303 | @Override
|
| 304 | public <T> Stream<T> resolve(final ContextMap context, final DisplayContentsFactory<T> builder) {
|
| 305 | return builder instanceof DisplayContentsFactory.ForStacks<T> stacks
|
| 306 | ? this.source.resolve(context, SlotDisplay.ItemStackContentsFactory.INSTANCE).filter(s -> s.has(this.component)).map(stacks::forStack)
|
| 307 | : Stream.empty();
|
| 308 | }
|
| 309 |
|
| 310 | @Override
|
| 311 | public SlotDisplay.Type<SlotDisplay.OnlyWithComponent> type() {
|
| 312 | return TYPE;
|
| 313 | }
|
| 314 | }
|
| 315 |
|
| 316 | record SmithingTrimDemoSlotDisplay(SlotDisplay base, SlotDisplay material, Holder<TrimPattern> pattern) implements SlotDisplay {
|
| 317 | public static final MapCodec<SlotDisplay.SmithingTrimDemoSlotDisplay> MAP_CODEC = RecordCodecBuilder.mapCodec(
|
| 318 | i -> i.group(
|
| 319 | SlotDisplay.CODEC.fieldOf("base").forGetter(SlotDisplay.SmithingTrimDemoSlotDisplay::base),
|
| 320 | SlotDisplay.CODEC.fieldOf("material").forGetter(SlotDisplay.SmithingTrimDemoSlotDisplay::material),
|
| 321 | TrimPattern.CODEC.fieldOf("pattern").forGetter(SlotDisplay.SmithingTrimDemoSlotDisplay::pattern)
|
| 322 | )
|
| 323 | .apply(i, SlotDisplay.SmithingTrimDemoSlotDisplay::new)
|
| 324 | );
|
| 325 | public static final StreamCodec<RegistryFriendlyByteBuf, SlotDisplay.SmithingTrimDemoSlotDisplay> STREAM_CODEC = StreamCodec.composite(
|
| 326 | SlotDisplay.STREAM_CODEC,
|
| 327 | SlotDisplay.SmithingTrimDemoSlotDisplay::base,
|
| 328 | SlotDisplay.STREAM_CODEC,
|
| 329 | SlotDisplay.SmithingTrimDemoSlotDisplay::material,
|
| 330 | TrimPattern.STREAM_CODEC,
|
| 331 | SlotDisplay.SmithingTrimDemoSlotDisplay::pattern,
|
| 332 | SlotDisplay.SmithingTrimDemoSlotDisplay::new
|
| 333 | );
|
| 334 | public static final SlotDisplay.Type<SlotDisplay.SmithingTrimDemoSlotDisplay> TYPE = new SlotDisplay.Type<>(MAP_CODEC, STREAM_CODEC);
|
| 335 |
|
| 336 | @Override
|
| 337 | public SlotDisplay.Type<SlotDisplay.SmithingTrimDemoSlotDisplay> type() {
|
| 338 | return TYPE;
|
| 339 | }
|
| 340 |
|
| 341 | @Override
|
| 342 | public <T> Stream<T> resolve(final ContextMap context, final DisplayContentsFactory<T> factory) {
|
| 343 | RandomSource randomSource = RandomSource.createThreadLocalInstance(System.identityHashCode(this));
|
| 344 | BinaryOperator<ItemStack> transformation = (base, material) -> SmithingTrimRecipe.applyTrim(base, material, this.pattern);
|
| 345 | return SlotDisplay.applyDemoTransformation(context, factory, this.base, this.material, randomSource, transformation);
|
| 346 | }
|
| 347 | }
|
| 348 |
|
| 349 | record TagSlotDisplay(HolderSet<Item> tag) implements SlotDisplay {
|
| 350 | public static final MapCodec<SlotDisplay.TagSlotDisplay> MAP_CODEC = RecordCodecBuilder.mapCodec(
|
| 351 | i -> i.group(RegistryCodecs.holderSet(Registries.ITEM).fieldOf("tag").forGetter(SlotDisplay.TagSlotDisplay::tag)).apply(i, SlotDisplay.TagSlotDisplay::new)
|
| 352 | );
|
| 353 | public static final StreamCodec<RegistryFriendlyByteBuf, SlotDisplay.TagSlotDisplay> STREAM_CODEC = StreamCodec.composite(
|
| 354 | ByteBufCodecs.holderSet(Registries.ITEM), SlotDisplay.TagSlotDisplay::tag, SlotDisplay.TagSlotDisplay::new
|
| 355 | );
|
| 356 | public static final SlotDisplay.Type<SlotDisplay.TagSlotDisplay> TYPE = new SlotDisplay.Type<>(MAP_CODEC, STREAM_CODEC);
|
| 357 |
|
| 358 | @Override
|
| 359 | public SlotDisplay.Type<SlotDisplay.TagSlotDisplay> type() {
|
| 360 | return TYPE;
|
| 361 | }
|
| 362 |
|
| 363 | @Override
|
| 364 | public <T> Stream<T> resolve(final ContextMap context, final DisplayContentsFactory<T> factory) {
|
| 365 | return factory instanceof DisplayContentsFactory.ForStacks<T> stacks ? this.tag.stream().map(stacks::forStack) : Stream.empty();
|
| 366 | }
|
| 367 | }
|
| 368 |
|
| 369 | record Type<T extends SlotDisplay>(MapCodec<T> codec, StreamCodec<RegistryFriendlyByteBuf, T> streamCodec) {
|
| 370 | }
|
| 371 |
|
| 372 | record WithAnyPotion(SlotDisplay display) implements SlotDisplay {
|
| 373 | public static final MapCodec<SlotDisplay.WithAnyPotion> MAP_CODEC = RecordCodecBuilder.mapCodec(
|
| 374 | i -> i.group(SlotDisplay.CODEC.fieldOf("contents").forGetter(SlotDisplay.WithAnyPotion::display)).apply(i, SlotDisplay.WithAnyPotion::new)
|
| 375 | );
|
| 376 | public static final StreamCodec<RegistryFriendlyByteBuf, SlotDisplay.WithAnyPotion> STREAM_CODEC = StreamCodec.composite(
|
| 377 | SlotDisplay.STREAM_CODEC, SlotDisplay.WithAnyPotion::display, SlotDisplay.WithAnyPotion::new
|
| 378 | );
|
| 379 | public static final SlotDisplay.Type<SlotDisplay.WithAnyPotion> TYPE = new SlotDisplay.Type<>(MAP_CODEC, STREAM_CODEC);
|
| 380 |
|
| 381 | @Override
|
| 382 | public SlotDisplay.Type<SlotDisplay.WithAnyPotion> type() {
|
| 383 | return TYPE;
|
| 384 | }
|
| 385 |
|
| 386 | @Override
|
| 387 | public <T> Stream<T> resolve(final ContextMap context, final DisplayContentsFactory<T> factory) {
|
| 388 | if (factory instanceof DisplayContentsFactory.ForStacks<T> stacks) {
|
| 389 | List<ItemStack> displayItems = this.display.resolveForStacks(context);
|
| 390 | Optional<? extends HolderLookup.RegistryLookup<Potion>> potions = Optional.ofNullable(context.get(SlotDisplayContext.REGISTRIES))
|
| 391 | .flatMap(r -> r.lookup(Registries.POTION));
|
| 392 | return potions.stream().flatMap(HolderLookup::listElements).flatMap(potion -> {
|
| 393 | PotionContents potionContents = new PotionContents(potion);
|
| 394 | return displayItems.stream().map(item -> {
|
| 395 | ItemStack itemCopy = item.copy();
|
| 396 | itemCopy.set(DataComponents.POTION_CONTENTS, potionContents);
|
| 397 | return stacks.forStack(itemCopy);
|
| 398 | });
|
| 399 | });
|
| 400 | } else {
|
| 401 | return Stream.empty();
|
| 402 | }
|
| 403 | }
|
| 404 | }
|
| 405 |
|
| 406 | record WithRemainder(SlotDisplay input, SlotDisplay remainder) implements SlotDisplay {
|
| 407 | public static final MapCodec<SlotDisplay.WithRemainder> MAP_CODEC = RecordCodecBuilder.mapCodec(
|
| 408 | i -> i.group(
|
| 409 | SlotDisplay.CODEC.fieldOf("input").forGetter(SlotDisplay.WithRemainder::input),
|
| 410 | SlotDisplay.CODEC.fieldOf("remainder").forGetter(SlotDisplay.WithRemainder::remainder)
|
| 411 | )
|
| 412 | .apply(i, SlotDisplay.WithRemainder::new)
|
| 413 | );
|
| 414 | public static final StreamCodec<RegistryFriendlyByteBuf, SlotDisplay.WithRemainder> STREAM_CODEC = StreamCodec.composite(
|
| 415 | SlotDisplay.STREAM_CODEC, SlotDisplay.WithRemainder::input, SlotDisplay.STREAM_CODEC, SlotDisplay.WithRemainder::remainder, SlotDisplay.WithRemainder::new
|
| 416 | );
|
| 417 | public static final SlotDisplay.Type<SlotDisplay.WithRemainder> TYPE = new SlotDisplay.Type<>(MAP_CODEC, STREAM_CODEC);
|
| 418 |
|
| 419 | @Override
|
| 420 | public SlotDisplay.Type<SlotDisplay.WithRemainder> type() {
|
| 421 | return TYPE;
|
| 422 | }
|
| 423 |
|
| 424 | @Override
|
| 425 | public <T> Stream<T> resolve(final ContextMap context, final DisplayContentsFactory<T> factory) {
|
| 426 | if (factory instanceof DisplayContentsFactory.ForRemainders<T> remainders) {
|
| 427 | List<T> resolvedRemainders = this.remainder.resolve(context, factory).toList();
|
| 428 | return this.input.resolve(context, factory).map(input -> remainders.addRemainder((T)input, resolvedRemainders));
|
| 429 | } else {
|
| 430 | return this.input.resolve(context, factory);
|
| 431 | }
|
| 432 | }
|
| 433 |
|
| 434 | @Override
|
| 435 | public boolean isEnabled(final FeatureFlagSet enabledFeatures) {
|
| 436 | return this.input.isEnabled(enabledFeatures) && this.remainder.isEnabled(enabledFeatures);
|
| 437 | }
|
| 438 | }
|
| 439 | }
|
| 440 |
|