Skip to content

Commit

Permalink
Offhand rendering fixes (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lyfts authored Jan 19, 2025
1 parent cd2ffb3 commit 2055c0e
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,7 @@ private MixinEntityPlayer(World p_i1594_1_) {
return original;
}

@Inject(
method = "setItemInUse",
at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/player/EntityPlayer;setEating(Z)V"))
@Inject(method = "setItemInUse", at = @At(value = "TAIL"))
private void backhand$setItemInUse(ItemStack p_71008_1_, int p_71008_2_, CallbackInfo ci) {
if (Objects.equals(p_71008_1_, BackhandUtils.getOffhandItem((EntityPlayer) (Object) this))) {
backhand$updateOffhandUse(true);
Expand All @@ -88,9 +86,7 @@ private MixinEntityPlayer(World p_i1594_1_) {
}
}

@Inject(
method = "clearItemInUse",
at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/player/EntityPlayer;setEating(Z)V"))
@Inject(method = "clearItemInUse", at = @At(value = "TAIL"))
private void backhand$clearOffhand(CallbackInfo ci) {
if (isOffhandItemInUse()) {
backhand$updateOffhandUse(false);
Expand Down Expand Up @@ -140,9 +136,12 @@ private MixinEntityPlayer(World p_i1594_1_) {
@Unique
private void backhand$updateOffhandUse(boolean state) {
EntityPlayer player = (EntityPlayer) (Object) this;
Backhand.packetHandler
.sendPacketToAllTracking(player, new OffhandSyncOffhandUse(player, state).generatePacket());
setOffhandItemInUse(state);

if (!worldObj.isRemote) {
Backhand.packetHandler
.sendPacketToAllTracking(player, new OffhandSyncOffhandUse(player, state).generatePacket());
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.util.IIcon;
import net.minecraft.world.World;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;

import com.llamalad7.mixinextras.injector.ModifyReturnValue;
import com.llamalad7.mixinextras.sugar.Local;

import xonin.backhand.api.core.BackhandUtils;
import xonin.backhand.api.core.IBackhandPlayer;

@Mixin(EntityPlayer.class)
Expand All @@ -16,6 +23,12 @@ public abstract class MixinEntityPlayerClient extends EntityLivingBase implement
@Shadow
public InventoryPlayer inventory;

@Shadow
private ItemStack itemInUse;

@Shadow
private int itemInUseCount;

public MixinEntityPlayerClient(World p_i1594_1_) {
super(p_i1594_1_);
}
Expand All @@ -27,4 +40,20 @@ public float getSwingProgress(float p_70678_1_) {
}
return super.getSwingProgress(p_70678_1_);
}

@ModifyReturnValue(
method = "getItemIcon",
at = { @At(value = "RETURN", ordinal = 0), @At(value = "RETURN", ordinal = 1),
@At(value = "RETURN", ordinal = 2) })
private IIcon backhand$setItemInUse(IIcon original, @Local(argsOnly = true) ItemStack stack,
@Local(argsOnly = true) int renderPass) {
EntityPlayer player = (EntityPlayer) (Object) this;
ItemStack offhand = BackhandUtils.getOffhandItem(player);
if (offhand == null) return original;
if (!isOffhandItemInUse() && stack == offhand) {
return stack.getItem()
.getIcon(stack, renderPass, player, itemInUse, itemInUseCount);
}
return original;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.EntityClientPlayerMP;
import net.minecraft.client.renderer.ItemRenderer;
import net.minecraft.item.EnumAction;
import net.minecraft.item.ItemMap;
import net.minecraft.item.ItemStack;

Expand All @@ -12,6 +13,8 @@
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import com.llamalad7.mixinextras.injector.ModifyExpressionValue;

import xonin.backhand.api.core.BackhandUtils;
import xonin.backhand.api.core.IBackhandPlayer;
import xonin.backhand.client.utils.BackhandRenderHelper;
Expand Down Expand Up @@ -54,4 +57,31 @@ public abstract class MixinItemRenderer {
GL11.glCullFace(GL11.GL_BACK);
}

@ModifyExpressionValue(
method = "renderItemInFirstPerson",
at = @At(value = "INVOKE", target = "Lnet/minecraft/client/entity/EntityClientPlayerMP;isInvisible()Z"))
private boolean backhand$renderItemInFirstPerson(boolean original) {
if (BackhandConfigClient.RenderEmptyOffhandAtRest) return original;
EntityClientPlayerMP player = Minecraft.getMinecraft().thePlayer;
if (BackhandUtils.isUsingOffhand(player)) {
return true;
}
return original;
}

@ModifyExpressionValue(
method = "renderItemInFirstPerson",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/item/ItemStack;getItemUseAction()Lnet/minecraft/item/EnumAction;"))
private EnumAction backhand$renderItemInFirstPerson(EnumAction original) {
EntityClientPlayerMP player = Minecraft.getMinecraft().thePlayer;
ItemStack offhand = BackhandUtils.getOffhandItem(player);
if (offhand == null) return original;
if (BackhandUtils.isUsingOffhand(player)) {
return ((IBackhandPlayer) player).isOffhandItemInUse() ? offhand.getItemUseAction() : EnumAction.none;
}

return ((IBackhandPlayer) player).isOffhandItemInUse() ? EnumAction.none : original;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ public abstract class MixinModelBiped extends ModelBase {
target = "Lnet/minecraft/client/model/ModelBiped;aimedBow:Z"))
private boolean backhand$moveOffhandAimedBow(boolean original, @Local(argsOnly = true, ordinal = 2) float f3,
@Local(argsOnly = true) Entity entity) {
if (entity instanceof EntityPlayer player && entity == Minecraft.getMinecraft().thePlayer
if (original && entity instanceof EntityPlayer player
&& entity == Minecraft.getMinecraft().thePlayer
&& ((IBackhandPlayer) player).isOffhandItemInUse()) {
bipedLeftArm.rotateAngleZ = 0.0F;
bipedRightArm.rotateAngleZ = 0.0F;
Expand Down

0 comments on commit 2055c0e

Please sign in to comment.