|
| 1 | +package com.ref.aea.util; |
| 2 | + |
| 3 | +import appeng.api.stacks.AEFluidKey; |
| 4 | +import appeng.api.stacks.AEItemKey; |
| 5 | +import appeng.api.stacks.AEKey; |
| 6 | +import appeng.api.stacks.GenericStack; |
| 7 | +import appeng.core.definitions.AEItems; |
| 8 | +import com.direwolf20.buildinggadgets2.setup.Registration; |
| 9 | +import com.direwolf20.buildinggadgets2.util.GadgetUtils; |
| 10 | +import com.direwolf20.buildinggadgets2.util.datatypes.StatePos; |
| 11 | +import java.util.*; |
| 12 | +import net.minecraft.core.BlockPos; |
| 13 | +import net.minecraft.network.chat.Component; |
| 14 | +import net.minecraft.server.level.ServerLevel; |
| 15 | +import net.minecraft.server.level.ServerPlayer; |
| 16 | +import net.minecraft.world.item.ItemStack; |
| 17 | +import net.minecraft.world.level.block.state.BlockState; |
| 18 | +import org.jetbrains.annotations.NotNull; |
| 19 | + |
| 20 | +public class EncodedPatternUtil { |
| 21 | + public static ItemStack fromBuildList( |
| 22 | + ServerLevel level, ServerPlayer player, ArrayList<StatePos> buildList, String templateName) { |
| 23 | + if (buildList == null || buildList.isEmpty()) return ItemStack.EMPTY; |
| 24 | + Map<AEKey, Long> counts = getAeKeyLongMap(level, player, buildList); |
| 25 | + |
| 26 | + if (counts.isEmpty()) return ItemStack.EMPTY; |
| 27 | + |
| 28 | + List<GenericStack> inputs = |
| 29 | + counts.entrySet().stream() |
| 30 | + .map(e -> new GenericStack(e.getKey(), e.getValue())) |
| 31 | + .sorted((a, b) -> Long.compare(b.amount(), a.amount())) |
| 32 | + .limit(81) |
| 33 | + .toList(); |
| 34 | + |
| 35 | + ItemStack barrierStack = new ItemStack(Registration.Template.get()); |
| 36 | + if (templateName != null && !templateName.isBlank()) { |
| 37 | + barrierStack.setHoverName(Component.literal(templateName)); |
| 38 | + } |
| 39 | + |
| 40 | + AEKey outputKey = AEItemKey.of(barrierStack); |
| 41 | + if (outputKey == null) return ItemStack.EMPTY; |
| 42 | + |
| 43 | + GenericStack[] outputs = new GenericStack[] {new GenericStack(outputKey, 1)}; |
| 44 | + |
| 45 | + return AEItems.PROCESSING_PATTERN.asItem().encode(inputs.toArray(new GenericStack[0]), outputs); |
| 46 | + } |
| 47 | + |
| 48 | + private static @NotNull Map<AEKey, Long> getAeKeyLongMap( |
| 49 | + ServerLevel level, ServerPlayer player, ArrayList<StatePos> buildList) { |
| 50 | + Map<AEKey, Long> counts = new HashMap<>(); |
| 51 | + Map<BlockState, Map<AEKey, Long>> cacheMap = new HashMap<>(); |
| 52 | + for (StatePos sp : buildList) { |
| 53 | + if (sp.state.isAir()) continue; |
| 54 | + Map<AEKey, Long> requirementsForState = |
| 55 | + cacheMap.computeIfAbsent( |
| 56 | + sp.state, |
| 57 | + state -> { |
| 58 | + Map<AEKey, Long> singleStateReqs = new HashMap<>(); |
| 59 | + if (!state.getFluidState().isEmpty() && state.getFluidState().isSource()) { |
| 60 | + AEKey key = AEFluidKey.of(state.getFluidState().getType()); |
| 61 | + singleStateReqs.put(key, 1000L); |
| 62 | + } else { |
| 63 | + List<ItemStack> drops = |
| 64 | + GadgetUtils.getDropsForBlockState(level, BlockPos.ZERO, state, player); |
| 65 | + for (ItemStack req : drops) { |
| 66 | + AEKey key = AEItemKey.of(req); |
| 67 | + if (key != null) { |
| 68 | + singleStateReqs.merge(key, (long) req.getCount(), Long::sum); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + return singleStateReqs; |
| 73 | + }); |
| 74 | + requirementsForState.forEach((key, amount) -> counts.merge(key, amount, Long::sum)); |
| 75 | + } |
| 76 | + return counts; |
| 77 | + } |
| 78 | +} |
0 commit comments