|
| 1 | +/* |
| 2 | + * Copyright 2026 TerminalMC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package dev.terminalmc.signtweaks.mixin.interact; |
| 18 | + |
| 19 | +import com.llamalad7.mixinextras.sugar.Local; |
| 20 | +import dev.terminalmc.signtweaks.SignTweaks; |
| 21 | +import dev.terminalmc.signtweaks.platform.services.PlatformServices; |
| 22 | +import net.minecraft.client.Minecraft; |
| 23 | +import net.minecraft.client.multiplayer.ClientLevel; |
| 24 | +import net.minecraft.client.player.LocalPlayer; |
| 25 | +import net.minecraft.core.BlockPos; |
| 26 | +import net.minecraft.world.InteractionHand; |
| 27 | +import net.minecraft.world.item.ItemStack; |
| 28 | +import net.minecraft.world.item.SignApplicator; |
| 29 | +import net.minecraft.world.item.SignItem; |
| 30 | +import net.minecraft.world.level.block.*; |
| 31 | +import net.minecraft.world.level.block.entity.BlockEntity; |
| 32 | +import net.minecraft.world.level.block.entity.SignBlockEntity; |
| 33 | +import net.minecraft.world.level.block.state.BlockState; |
| 34 | +import net.minecraft.world.phys.BlockHitResult; |
| 35 | +import net.minecraft.world.phys.HitResult; |
| 36 | +import org.jspecify.annotations.Nullable; |
| 37 | +import org.spongepowered.asm.mixin.Mixin; |
| 38 | +import org.spongepowered.asm.mixin.Shadow; |
| 39 | +import org.spongepowered.asm.mixin.injection.At; |
| 40 | +import org.spongepowered.asm.mixin.injection.Inject; |
| 41 | +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; |
| 42 | + |
| 43 | +import static dev.terminalmc.signtweaks.config.Config.options; |
| 44 | + |
| 45 | +@Mixin(Minecraft.class) |
| 46 | +public abstract class MinecraftMixin { |
| 47 | + |
| 48 | + @Shadow |
| 49 | + @Nullable |
| 50 | + public HitResult hitResult; |
| 51 | + |
| 52 | + @Shadow |
| 53 | + @Nullable |
| 54 | + public ClientLevel level; |
| 55 | + |
| 56 | + @Shadow |
| 57 | + @Nullable |
| 58 | + public LocalPlayer player; |
| 59 | + |
| 60 | + /** |
| 61 | + * Rudimentary click-through implementation for signs. |
| 62 | + */ |
| 63 | + @Inject( |
| 64 | + method = "startUseItem", |
| 65 | + at = @At( |
| 66 | + value = "INVOKE", |
| 67 | + target = "Lnet/minecraft/client/player/LocalPlayer;getItemInHand(Lnet/minecraft/world/InteractionHand;)Lnet/minecraft/world/item/ItemStack;" |
| 68 | + ) |
| 69 | + ) |
| 70 | + public void onGetItemInHand(CallbackInfo ci) { |
| 71 | + // must be enabled |
| 72 | + if (!options().clickThrough) |
| 73 | + return; |
| 74 | + |
| 75 | + // must not be holding dye/ink/honeycomb |
| 76 | + // (can't use if sneaking so have to allow usage while standing) |
| 77 | + if (player.getItemInHand(InteractionHand.MAIN_HAND).getItem() instanceof SignApplicator) |
| 78 | + return; |
| 79 | + |
| 80 | + // must be targeting some form of block |
| 81 | + if (!(hitResult instanceof BlockHitResult blockHitResult)) |
| 82 | + return; |
| 83 | + |
| 84 | + BlockPos hitBlockPos = blockHitResult.getBlockPos(); |
| 85 | + BlockEntity hitBlockEntity = level.getBlockEntity(hitBlockPos); |
| 86 | + |
| 87 | + // must be targeting a sign |
| 88 | + if (!(hitBlockEntity instanceof SignBlockEntity)) |
| 89 | + return; |
| 90 | + |
| 91 | + BlockState hitBlockState = level.getBlockState(hitBlockPos); |
| 92 | + Block hitBlock = hitBlockState.getBlock(); |
| 93 | + |
| 94 | + // must be targeting a wall sign |
| 95 | + if (!(hitBlock instanceof WallSignBlock)) |
| 96 | + return; |
| 97 | + |
| 98 | + BlockPos wallBlockPos = |
| 99 | + hitBlockPos.offset(hitBlockState.getValue(WallSignBlock.FACING) |
| 100 | + .getOpposite() |
| 101 | + .getUnitVec3i()); |
| 102 | + BlockState wallBlockState = level.getBlockState(wallBlockPos); |
| 103 | + Block wallBlock = wallBlockState.getBlock(); |
| 104 | + |
| 105 | + // sign must be on a block entity |
| 106 | + if (!(wallBlock instanceof BaseEntityBlock)) |
| 107 | + return; |
| 108 | + |
| 109 | + // sign must be on a block entity that it makes sense to click through to |
| 110 | + if (wallBlock instanceof AbstractBannerBlock |
| 111 | + || wallBlock instanceof AbstractSkullBlock |
| 112 | + || wallBlock instanceof SignBlock |
| 113 | + || wallBlock instanceof Portal |
| 114 | + || wallBlock instanceof SculkShriekerBlock) |
| 115 | + return; |
| 116 | + |
| 117 | + // must not be sneaking |
| 118 | + if (player.isSteppingCarefully()) { |
| 119 | + // record the time to allow keeping the editor open |
| 120 | + SignTweaks.avoidClickThroughTime = System.nanoTime(); |
| 121 | + return; |
| 122 | + } |
| 123 | + |
| 124 | + // must not have clickthrough plus |
| 125 | + if (PlatformServices.getInstance().isModLoaded("clickthrough")) |
| 126 | + return; |
| 127 | + |
| 128 | + // retarget |
| 129 | + hitResult = new BlockHitResult( |
| 130 | + blockHitResult.getLocation(), |
| 131 | + blockHitResult.getDirection(), |
| 132 | + wallBlockPos, |
| 133 | + false |
| 134 | + ); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Records the time when a sign is placed. |
| 139 | + */ |
| 140 | + @Inject( |
| 141 | + method = "startUseItem", |
| 142 | + at = @At( |
| 143 | + value = "INVOKE", |
| 144 | + target = "Lnet/minecraft/client/multiplayer/MultiPlayerGameMode;useItemOn(Lnet/minecraft/client/player/LocalPlayer;Lnet/minecraft/world/InteractionHand;Lnet/minecraft/world/phys/BlockHitResult;)Lnet/minecraft/world/InteractionResult;" |
| 145 | + ) |
| 146 | + ) |
| 147 | + private void onUseItemOn(CallbackInfo ci, @Local(name = "heldItem") ItemStack heldItem) { |
| 148 | + if (heldItem.getItem() instanceof SignItem) { |
| 149 | + SignTweaks.signPlaceTime = System.nanoTime(); |
| 150 | + } |
| 151 | + } |
| 152 | +} |
0 commit comments