-
Notifications
You must be signed in to change notification settings - Fork 42
adds stripped state pre processor #330
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 1.21.11
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,7 +18,6 @@ | |
| package com.lambda.interaction.construction.simulation.processing | ||
|
|
||
| import com.lambda.context.AutomatedSafeContext | ||
| import com.lambda.context.SafeContext | ||
| import com.lambda.core.Loadable | ||
| import com.lambda.interaction.construction.simulation.SimDsl | ||
| import com.lambda.interaction.construction.verify.TargetState | ||
|
|
@@ -144,15 +143,19 @@ object ProcessorRegistry : Loadable { | |
| return PreProcessingData(preProcessingInfo, pos) | ||
| } | ||
|
|
||
| context(safeContext: SafeContext) | ||
| context(_: AutomatedSafeContext) | ||
| private fun preProcess(pos: BlockPos, state: BlockState, targetState: BlockState, itemStack: ItemStack) = | ||
| PreProcessingInfoAccumulator(targetState, itemStack.item).run { | ||
| var stateProcessing = false | ||
| stateProcessors.forEach { processor -> | ||
| if (processor.acceptsState(state, targetState)) { | ||
| with(processor) { preProcess(state, targetState, pos) } | ||
| stateProcessing = true | ||
| if (!processor.acceptsState(state, targetState)) return@forEach | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this shouldnt be altered |
||
| if (!processor.isEnabled()) { | ||
| // The verdict now hinges on a setting that can be toggled at any time. | ||
| noCaching() | ||
| return@forEach | ||
| } | ||
| with(processor) { preProcess(state, targetState, pos) } | ||
| stateProcessing = true | ||
| } | ||
| if (!omitInteraction) { | ||
| if (state.block != expectedState.block) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| /* | ||
| * Copyright 2026 Lambda | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| package com.lambda.interaction.construction.simulation.processing.preprocessors.state | ||
|
|
||
| import com.lambda.context.AutomatedSafeContext | ||
| import com.lambda.context.SafeContext | ||
| import com.lambda.interaction.construction.simulation.processing.PreProcessingInfoAccumulator | ||
| import com.lambda.interaction.construction.simulation.processing.StateProcessor | ||
| import com.lambda.util.item.ItemUtils | ||
| import net.minecraft.block.Block | ||
| import net.minecraft.block.BlockState | ||
| import net.minecraft.entity.player.PlayerEntity | ||
| import net.minecraft.item.AxeItem | ||
| import net.minecraft.item.Item | ||
| import net.minecraft.state.property.Properties | ||
| import net.minecraft.util.math.BlockPos | ||
|
|
||
| @Suppress("unused") | ||
| object StrippedStateProcessor : StateProcessor { | ||
| // [AxeItem.STRIPPED_BLOCKS] maps unstripped to stripped, we need the other direction | ||
| private val unstrippedToStripped: Map<Block, Block> by lazy { | ||
| AxeItem.STRIPPED_BLOCKS.entries.associate { (from, to) -> to to from } | ||
| } | ||
|
|
||
| context(automatedSafeContext: AutomatedSafeContext) | ||
| override fun isEnabled() = automatedSafeContext.buildConfig.stripLogs | ||
|
|
||
| override fun acceptsState(state: BlockState, targetState: BlockState): Boolean { | ||
| val unstrippedVariant = unstrippedToStripped[targetState.block] ?: return false | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this function as a whole can probably be simplified to an = function without a full block |
||
| return state.isReplaceable || state.block == unstrippedVariant | ||
|
beanbag44 marked this conversation as resolved.
|
||
| } | ||
|
|
||
| context(safeContext: SafeContext) | ||
| override fun PreProcessingInfoAccumulator.preProcess(state: BlockState, targetState: BlockState, pos: BlockPos) { | ||
| val unstrippedVariant = unstrippedToStripped[targetState.block] ?: return | ||
| val player = safeContext.player | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for something like this, i try to avoid value sets and call with(safeContext) { }, wrapping the function in the safe context. If you change it to use that, i would change the function to = without the {} and then put the with on the next line |
||
|
|
||
| // Every branch below depends on the current inventory, so this must never be cached. | ||
| noCaching() | ||
|
|
||
| if (state.isReplaceable){ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing space before the { Also, if you want the logic to mirror FlowerPotStateProcessor for parity, you could change this to state.block != unstrippedVariant and add a return in the block so you can handle the stripping logic after without indentation. The isReplaceable check is already handled in the acceptsState function |
||
| // Don't use unstripped logs if you already have stripped ones | ||
| if (player.carries(targetState.block.asItem())) return | ||
|
|
||
| val sourceItem = unstrippedVariant.asItem() | ||
| if (!player.carries(sourceItem)) return | ||
|
|
||
| setExpectedState(unstrippedVariant.withAxisOf(targetState)) | ||
| setItem(sourceItem) | ||
| } else if (state.block == unstrippedVariant) { | ||
| val axe = player.findAxe() ?: return | ||
| setItem(axe) | ||
| setPlacing(false) | ||
| // InteractSim refuses to interact while sneaking, so ask for it explicitly. | ||
| setSneak(false) | ||
| } | ||
| } | ||
|
|
||
| private fun Block.withAxisOf(targetState: BlockState) = defaultState.let { placed -> | ||
| if (Properties.AXIS in placed && Properties.AXIS in targetState) { | ||
| placed.with(Properties.AXIS, targetState.get(Properties.AXIS)) | ||
| } else placed | ||
| } | ||
|
|
||
| private fun PlayerEntity.findAxe(): Item? { | ||
| val held = mainHandStack.item | ||
| if (held in ItemUtils.axes) return held | ||
| return inventory.mainStacks.firstOrNull { it.item in ItemUtils.axes }?.item | ||
| } | ||
|
|
||
| private fun PlayerEntity.carries(item: Item) = | ||
| inventory.mainStacks.any { it.item == item } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the isEnabled check is only overridden by StrippedStateProcessor so the check should probably be moved into the acceptsState for StrippedStateProcessor and the function removed.