Skip to content

Commit 91a11c2

Browse files
committed
fix locked container preview persisting when the itemstack changes
1 parent 5a42837 commit 91a11c2

File tree

5 files changed

+29
-25
lines changed

5 files changed

+29
-25
lines changed

src/main/java/com/lambda/mixin/render/DrawContextMixin.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ private void injectDrawMap(TextRenderer textRenderer, ItemStack stack, int i, in
8888
@Inject(method = "drawTooltip(Lnet/minecraft/client/font/TextRenderer;Ljava/util/List;Ljava/util/Optional;IILnet/minecraft/util/Identifier;)V", at = @At("HEAD"), cancellable = true)
8989
private void onDrawTooltip(TextRenderer textRenderer, List<Text> text, Optional<TooltipData> data, int x, int y, @Nullable Identifier texture, CallbackInfo ci) {
9090
if (!ContainerPreview.INSTANCE.isEnabled()) return;
91-
9291
if (ContainerPreview.isRenderingSubTooltip()) return;
9392

9493
if (ContainerPreview.isLocked()) {
@@ -97,9 +96,9 @@ private void onDrawTooltip(TextRenderer textRenderer, List<Text> text, Optional<
9796
return;
9897
}
9998

100-
if (data.isPresent() && data.get() instanceof ContainerPreview.ContainerComponent component) {
99+
if (data.isPresent()) {
101100
ci.cancel();
102-
ContainerPreview.renderShulkerTooltip((DrawContext)(Object)this, textRenderer, component, x, y);
101+
ContainerPreview.renderShulkerTooltip((DrawContext)(Object)this, textRenderer, x, y);
103102
}
104103
}
105104
}

src/main/kotlin/com/lambda/command/commands/PrefixCommand.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ object PrefixCommand : LambdaCommand(
5252
}
5353
val prefixChar = prefix.first()
5454
val configurable = Configuration.configurableByName("command") ?: return@executeWithResult failure("No command configurable found.")
55+
@Suppress("UNCHECKED_CAST")
5556
val setting = configurable.settings.find { it.name == "prefix" } as? Setting<*, Char>
5657
?: return@executeWithResult failure("Prefix setting is not a Char or can not be found.")
5758
setting.trySetValue(prefixChar)

src/main/kotlin/com/lambda/config/MutableAutomationConfig.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class MutableAutomationConfigImpl : MutableAutomationConfig {
6060
if (setting.core.type != newSetting.core.type)
6161
throw IllegalStateException("Settings with the same name do not have the same type.")
6262
@Suppress("UNCHECKED_CAST")
63-
(setting as Setting<SettingCore<*>, Any>).core = newSetting.core as SettingCore<Any>
63+
(setting as Setting<SettingCore<Any>, Any>).core = newSetting.core as SettingCore<Any>
6464
}
6565
}
6666
}

src/main/kotlin/com/lambda/interaction/construction/simulation/result/Contextual.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ interface Contextual : ComparableResult<Rank> {
4545
if (it is InteractContext) it.cachedState.fluidState.level
4646
else Int.MIN_VALUE
4747
}.thenByDescending {
48-
it.cachedState.block != Blocks.OBSERVER
48+
it.expectedState.block != Blocks.OBSERVER
4949
}.thenByDescending {
5050
context.sorter == ActionConfig.SortMode.Tool && it.hotbarIndex == HotbarManager.serverSlot
5151
}.thenBy {

src/main/kotlin/com/lambda/module/modules/render/ContainerPreview.kt

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import com.lambda.config.settings.complex.Bind
2222
import com.lambda.interaction.material.container.containers.EnderChestContainer
2323
import com.lambda.module.Module
2424
import com.lambda.module.tag.ModuleTag
25+
import com.lambda.threading.runSafe
26+
import com.lambda.util.InputUtils.isSatisfied
2527
import com.lambda.util.KeyCode
2628
import com.lambda.util.item.ItemStackUtils.shulkerBoxContents
2729
import com.lambda.util.item.ItemUtils.shulkerBoxes
@@ -30,13 +32,14 @@ import net.minecraft.client.font.TextRenderer
3032
import net.minecraft.client.gui.DrawContext
3133
import net.minecraft.client.gui.tooltip.TooltipComponent
3234
import net.minecraft.client.gl.RenderPipelines
35+
import net.minecraft.client.gui.screen.ingame.HandledScreen
3336
import net.minecraft.item.BlockItem
3437
import net.minecraft.item.ItemStack
3538
import net.minecraft.item.Items
3639
import net.minecraft.item.tooltip.TooltipData
40+
import net.minecraft.screen.slot.Slot
3741
import net.minecraft.util.DyeColor
3842
import net.minecraft.util.Identifier
39-
import org.lwjgl.glfw.GLFW
4043

4144
object ContainerPreview : Module(
4245
name = "ContainerPreview",
@@ -48,7 +51,12 @@ object ContainerPreview : Module(
4851

4952
private val background = Identifier.ofVanilla("textures/gui/container/shulker_box.png")
5053

54+
private var lockedSlot: Slot? = null
5155
private var lockedStack: ItemStack? = null
56+
set(value) {
57+
if (value !== field) lockedSlot = null
58+
field = value
59+
}
5260
private var lockedX: Int = 0
5361
private var lockedY: Int = 0
5462

@@ -64,14 +72,7 @@ object ContainerPreview : Module(
6472

6573
@JvmStatic
6674
val isLocked: Boolean
67-
get() = lockedStack != null
68-
69-
@JvmStatic
70-
fun isLockKeyPressed(): Boolean {
71-
if (!isEnabled) return false
72-
val handle = mc.window.handle
73-
return GLFW.glfwGetKey(handle, lockKey.key) == GLFW.GLFW_PRESS
74-
}
75+
get() = lockedSlot != null
7576

7677
private fun getTooltipWidth() = PADDING + COLS * SLOT_SIZE + PADDING
7778
private fun getTooltipHeight() = TITLE_HEIGHT + ROWS * SLOT_SIZE + PADDING
@@ -104,38 +105,41 @@ object ContainerPreview : Module(
104105
fun renderShulkerTooltip(
105106
context: DrawContext,
106107
textRenderer: TextRenderer,
107-
component: ContainerComponent,
108108
mouseX: Int,
109109
mouseY: Int
110-
) {
110+
) = runSafe {
111+
val handledScreen = mc.currentScreen as? HandledScreen<*> ?: return@runSafe
112+
val slot = handledScreen.focusedSlot ?: return@runSafe
113+
111114
val width = getTooltipWidth()
112115
val height = getTooltipHeight()
113116

114-
val lockKeyPressed = isLockKeyPressed()
117+
val lockKeyPressed = lockKey.isSatisfied()
115118

116-
if (lockKeyPressed && lockedStack == null) {
117-
lockedStack = component.stack.copy()
119+
if (lockKeyPressed && lockedSlot == null) {
120+
lockedSlot = slot
118121
lockedX = calculateTooltipX(mouseX, width)
119122
lockedY = calculateTooltipY(mouseY, height)
120-
} else if (!lockKeyPressed && lockedStack != null) {
121-
lockedStack = null
123+
} else if (!lockKeyPressed && lockedSlot != null) {
124+
lockedSlot = null
122125
}
123126

124127
if (isLocked) {
125128
renderLockedTooltipInternal(context, textRenderer)
126-
return
129+
return@runSafe
127130
}
128131

129-
renderTooltipForStack(context, textRenderer, component.stack, calculateTooltipX(mouseX, width), calculateTooltipY(mouseY, height), false)
132+
renderTooltipForStack(context, textRenderer, slot.stack, calculateTooltipX(mouseX, width), calculateTooltipY(mouseY, height), false)
130133
}
131134

132135
/**
133136
* Render the locked tooltip - called from mixin when we're locked
134137
*/
135138
@JvmStatic
136139
fun renderLockedTooltip(context: DrawContext, textRenderer: TextRenderer) {
137-
if (!isLockKeyPressed()) {
138-
lockedStack = null
140+
lockedStack = lockedSlot?.stack
141+
if (!lockKey.isSatisfied() || lockedSlot?.stack?.isEmpty == true) {
142+
lockedSlot = null
139143
return
140144
}
141145
renderLockedTooltipInternal(context, textRenderer)

0 commit comments

Comments
 (0)