|
| 1 | +/* |
| 2 | + * Copyright 2026 Lambda |
| 3 | + * |
| 4 | + * This program is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation, either version 3 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + */ |
| 17 | + |
| 18 | +package com.lambda.interaction.construction.simulation.processing.preprocessors.state |
| 19 | + |
| 20 | +import com.lambda.context.SafeContext |
| 21 | +import com.lambda.interaction.construction.simulation.processing.PreProcessingInfoAccumulator |
| 22 | +import com.lambda.interaction.construction.simulation.processing.StateProcessor |
| 23 | +import com.lambda.util.BlockUtils.blockState |
| 24 | +import com.lambda.util.item.ItemUtils |
| 25 | +import com.lambda.util.player.SlotUtils.hotbarAndInventoryStacks |
| 26 | +import net.minecraft.block.BlockState |
| 27 | +import net.minecraft.block.Blocks |
| 28 | +import net.minecraft.client.network.ClientPlayerEntity |
| 29 | +import net.minecraft.item.Item |
| 30 | +import net.minecraft.item.Items |
| 31 | +import net.minecraft.util.math.BlockPos |
| 32 | +import net.minecraft.util.math.Direction |
| 33 | + |
| 34 | +@Suppress("unused") |
| 35 | +object FarmlandStateProcessor : StateProcessor { |
| 36 | + private val tillResults = mapOf( |
| 37 | + Blocks.GRASS_BLOCK to Blocks.FARMLAND, |
| 38 | + Blocks.DIRT to Blocks.FARMLAND, |
| 39 | + Blocks.DIRT_PATH to Blocks.FARMLAND, |
| 40 | + Blocks.COARSE_DIRT to Blocks.DIRT, |
| 41 | + Blocks.ROOTED_DIRT to Blocks.DIRT |
| 42 | + ) |
| 43 | + |
| 44 | + private val sourceBlocks = listOf(Blocks.DIRT, Blocks.GRASS_BLOCK) |
| 45 | + |
| 46 | + override fun acceptsState(state: BlockState, targetState: BlockState) = |
| 47 | + targetState.block == Blocks.FARMLAND && state.block != targetState.block && |
| 48 | + (state.isReplaceable || state.block in tillResults) |
| 49 | + |
| 50 | + context(safeContext: SafeContext) |
| 51 | + override fun PreProcessingInfoAccumulator.preProcess(state: BlockState, targetState: BlockState, pos: BlockPos) { |
| 52 | + val player = safeContext.player |
| 53 | + noCaching() |
| 54 | + |
| 55 | + if (state.isReplaceable) { |
| 56 | + if (player.carries(Items.FARMLAND)) return |
| 57 | + val sourceBlock = sourceBlocks.firstOrNull { player.carries(it.asItem()) } ?: Blocks.DIRT |
| 58 | + setExpectedState(sourceBlock.defaultState) |
| 59 | + setItem(sourceBlock.asItem()) |
| 60 | + return |
| 61 | + } |
| 62 | + |
| 63 | + val tilledBlock = tillResults[state.block] ?: return |
| 64 | + if (!safeContext.blockState(pos.up()).isAir) { |
| 65 | + omitInteraction() |
| 66 | + return |
| 67 | + } |
| 68 | + val hoe = player.findHoe() ?: Items.NETHERITE_HOE |
| 69 | + |
| 70 | + setExpectedState(tilledBlock.defaultState) |
| 71 | + setItem(hoe) |
| 72 | + setPlacing(false) |
| 73 | + retainSides { it != Direction.DOWN } |
| 74 | + setSneak(false) |
| 75 | + } |
| 76 | + |
| 77 | + private fun ClientPlayerEntity.findHoe(): Item? = |
| 78 | + mainHandStack.item.takeIf { it in ItemUtils.hoes } |
| 79 | + ?: hotbarAndInventoryStacks.firstOrNull { it.item in ItemUtils.hoes }?.item |
| 80 | + |
| 81 | + private fun ClientPlayerEntity.carries(item: Item) = |
| 82 | + hotbarAndInventoryStacks.any { it.item == item } |
| 83 | +} |
0 commit comments