|
| 1 | +package com.lambda.interaction.building.manager |
| 2 | + |
| 3 | +import com.lambda.context.SafeContext |
| 4 | +import com.lambda.interaction.building.material.SourceSelection |
| 5 | +import com.lambda.interaction.building.material.SourceSelection.Companion.selectSource |
| 6 | +import com.lambda.interaction.building.material.source.MaterialSource |
| 7 | +import com.lambda.interaction.building.material.source.Source |
| 8 | +import com.lambda.module.modules.client.TaskFlow.disposables |
| 9 | +import com.lambda.interaction.building.material.StackSelection |
| 10 | +import com.lambda.interaction.building.material.StackSelection.Companion.select |
| 11 | +import com.lambda.util.ItemUtils |
| 12 | +import com.lambda.util.player.SlotUtils.combined |
| 13 | +import net.minecraft.block.BlockState |
| 14 | +import net.minecraft.inventory.Inventories |
| 15 | +import net.minecraft.item.BlockItem |
| 16 | +import net.minecraft.item.Item |
| 17 | +import net.minecraft.item.ItemStack |
| 18 | +import net.minecraft.nbt.NbtElement |
| 19 | +import net.minecraft.util.collection.DefaultedList |
| 20 | + |
| 21 | +/** |
| 22 | + * Caches all the item sources and destinations and gives a simple interface to check if an item is available and where. |
| 23 | + * Sources are: |
| 24 | + * 1. Player inventory |
| 25 | + * 2. Shulker boxes |
| 26 | + * 3. Ender chest |
| 27 | + * 4. Stash |
| 28 | + */ |
| 29 | +object MaterialSourceManager { |
| 30 | + |
| 31 | + val cache: MutableSet<MaterialSource> = mutableSetOf() |
| 32 | + |
| 33 | + val SafeContext.enderChestContent: List<ItemStack> |
| 34 | + get() = emptyList() |
| 35 | + |
| 36 | + val SafeContext.stashContent: List<ItemStack> |
| 37 | + get() = emptyList() |
| 38 | + |
| 39 | + val SafeContext.playerSources: Set<MaterialSource> |
| 40 | + get() { |
| 41 | + if (player.isCreative) { |
| 42 | + return Source.CREATIVE.gather(player) |
| 43 | + } |
| 44 | + |
| 45 | + return Source.INVENTORY.gather(player) |
| 46 | + } |
| 47 | + |
| 48 | + val SafeContext.enderChestSources: Set<MaterialSource> |
| 49 | + get() = Source.ENDER_CHEST.gather(player) |
| 50 | + |
| 51 | + val SafeContext.stashSources: Set<MaterialSource> |
| 52 | + get() = Source.STASH.gather(player) |
| 53 | + |
| 54 | + val SafeContext.sources: Set<MaterialSource> |
| 55 | + get() = playerSources + enderChestSources + stashSources |
| 56 | + |
| 57 | + fun SafeContext.getAvailableCount(stackSelection: StackSelection): Int { |
| 58 | + return sources.sumOf { it.available(stackSelection) } |
| 59 | + } |
| 60 | + |
| 61 | + fun SafeContext.getSourcesWith(stackSelection: StackSelection): List<MaterialSource> { |
| 62 | + return sources.filter { it.available(stackSelection) >= stackSelection.count } |
| 63 | + } |
| 64 | + |
| 65 | + fun SafeContext.getAvailableStacks(selection: SourceSelection): List<ItemStack> { |
| 66 | + return sources.filter(selection.selection).flatMap { it.stacks } |
| 67 | + } |
| 68 | + |
| 69 | + fun SafeContext.getGroupedAvailableStacks(): Map<MaterialSource, List<ItemStack>> { |
| 70 | + return sources.associateWith { it.stacks } |
| 71 | + } |
| 72 | + |
| 73 | + fun getShulkerBoxContents(itemStack: ItemStack) = |
| 74 | + BlockItem.getBlockEntityNbt(itemStack)?.takeIf { |
| 75 | + it.contains("Items", NbtElement.LIST_TYPE.toInt()) |
| 76 | + }?.let { |
| 77 | + val list = DefaultedList.ofSize(27, ItemStack.EMPTY) |
| 78 | + Inventories.readNbt(it, list) |
| 79 | + list |
| 80 | + } ?: emptyList() |
| 81 | + |
| 82 | + fun SafeContext.getBestAvailableTool( |
| 83 | + blockState: BlockState, |
| 84 | + availableTools: Set<Item> = ItemUtils.tools, |
| 85 | + ) = |
| 86 | + availableTools.map { |
| 87 | + it to it.getMiningSpeedMultiplier(it.defaultStack, blockState) |
| 88 | + }.filter { (item, speed) -> |
| 89 | + speed > 1.0 && |
| 90 | + item.isSuitableFor(blockState) && |
| 91 | + selectSource { |
| 92 | + hasItem(item) |
| 93 | + }?.let { |
| 94 | + it.available(item.select()) > 0 |
| 95 | + } == true |
| 96 | + }.maxByOrNull { |
| 97 | + it.second |
| 98 | + }?.first |
| 99 | + |
| 100 | + fun SafeContext.nextDisposable() = player.combined.firstOrNull { it.item in disposables } |
| 101 | + |
| 102 | +// fun SafeContext.nextDisposable() = TaskFlow.disposables.firstOrNull { |
| 103 | +// val selection = selectStack { isItem(it) } |
| 104 | +// (selectSource { matching(selection) }?.available(selection) ?: 0) > 0 |
| 105 | +// } |
| 106 | +} |
0 commit comments