Skip to content

Commit

Permalink
Fix some item uis not opening when something is in offhand (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lyfts authored Jan 27, 2025
1 parent 3eecde0 commit b4849d7
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 9 deletions.
9 changes: 0 additions & 9 deletions src/main/java/xonin/backhand/client/ClientEventHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.client.event.RenderGameOverlayEvent;
import net.minecraftforge.client.event.RenderHandEvent;
import net.minecraftforge.client.event.RenderLivingEvent;
import net.minecraftforge.client.event.RenderPlayerEvent;

Expand All @@ -39,11 +38,8 @@ public class ClientEventHandler {

public static boolean prevInvTweaksAutoRefill;
public static boolean prevInvTweaksBreakRefill;

public static int invTweaksDelay;

public static int renderPass;

@SubscribeEvent
public static void renderHotbarOverlay(RenderGameOverlayEvent event) {
if (event.type == RenderGameOverlayEvent.ElementType.HOTBAR) {
Expand Down Expand Up @@ -132,11 +128,6 @@ private static void renderOffhandInventorySlot(int p_73832_2_, int p_73832_3_, f
}
}

@SubscribeEvent
public static void onRenderHand(RenderHandEvent event) {
renderPass = event.renderPass;
}

/**
* Bend the models when the item in left hand is used
* And stop the right hand inappropriate bending
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,22 @@
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.network.NetHandlerPlayServer;
import net.minecraft.network.play.client.C08PacketPlayerBlockPlacement;
import net.minecraft.server.management.ItemInWorldManager;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import com.llamalad7.mixinextras.injector.ModifyExpressionValue;
import com.llamalad7.mixinextras.injector.v2.WrapWithCondition;

import xonin.backhand.api.core.BackhandUtils;
import xonin.backhand.api.core.IOffhandInventory;
import xonin.backhand.packet.BackhandPacketHandler;
import xonin.backhand.packet.OffhandCancelUsage;
import xonin.backhand.utils.BackhandConfig;

@Mixin(NetHandlerPlayServer.class)
Expand Down Expand Up @@ -42,6 +47,24 @@ public abstract class MixinNetHandlerPlayServer {
return BackhandConfig.OffhandBreakBlocks || !BackhandUtils.isUsingOffhand(playerEntity);
}

@Inject(
method = "processPlayerBlockPlacement",
at = @At(
value = "INVOKE",
target = "Lnet/minecraftforge/event/ForgeEventFactory;onPlayerInteract(Lnet/minecraft/entity/player/EntityPlayer;Lnet/minecraftforge/event/entity/player/PlayerInteractEvent$Action;IIIILnet/minecraft/world/World;)Lnet/minecraftforge/event/entity/player/PlayerInteractEvent;",
remap = false),
cancellable = true)
private void backhand$itemUse(C08PacketPlayerBlockPlacement packetIn, CallbackInfo ci) {
if (BackhandUtils.isUsingOffhand(playerEntity)
&& playerEntity.openContainer != playerEntity.inventoryContainer) {
// Client-side might still think it's using the offhand item, so we need to cancel the usage
if (!playerEntity.isUsingItem()) {
BackhandPacketHandler.sendPacketToPlayer(new OffhandCancelUsage(), playerEntity);
}
ci.cancel();
}
}

@WrapWithCondition(
method = "processUseEntity",
at = @At(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public static void init() {
NETWORK.registerMessage(OffhandSyncItemPacket.Handler.class, OffhandSyncItemPacket.class, 0, Side.CLIENT);
NETWORK.registerMessage(OffhandSyncOffhandUse.Handler.class, OffhandSyncOffhandUse.class, 1, Side.CLIENT);
NETWORK.registerMessage(OffhandAnimationPacket.Handler.class, OffhandAnimationPacket.class, 2, Side.CLIENT);
NETWORK.registerMessage(OffhandCancelUsage.Handler.class, OffhandCancelUsage.class, 3, Side.CLIENT);
}

public static void sendPacketToPlayer(IMessage packet, EntityPlayer player) {
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/xonin/backhand/packet/OffhandCancelUsage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package xonin.backhand.packet;

import net.minecraft.client.Minecraft;

import cpw.mods.fml.common.network.simpleimpl.IMessage;
import cpw.mods.fml.common.network.simpleimpl.IMessageHandler;
import cpw.mods.fml.common.network.simpleimpl.MessageContext;
import io.netty.buffer.ByteBuf;

public class OffhandCancelUsage implements IMessage {

public OffhandCancelUsage() {}

@Override
public void fromBytes(ByteBuf buf) {}

@Override
public void toBytes(ByteBuf buf) {}

public static class Handler implements IMessageHandler<OffhandCancelUsage, IMessage> {

@Override
public IMessage onMessage(OffhandCancelUsage message, MessageContext ctx) {
Minecraft.getMinecraft().thePlayer.clearItemInUse();
return null;
}
}
}

0 comments on commit b4849d7

Please sign in to comment.