添加副手物品渲染取消机制

This commit is contained in:
17146 2024-07-16 03:01:39 +08:00
parent d5b7559ebb
commit 5853382fe3
2 changed files with 30 additions and 34 deletions

View file

@ -11,7 +11,9 @@ import net.minecraft.client.player.LocalPlayer;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.util.Mth;
import net.minecraft.util.RandomSource;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.HumanoidArm;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
@ -58,6 +60,20 @@ public class ClientEventHandler {
}
}
@SubscribeEvent
public static void onRenderHand(RenderHandEvent event) {
Player player = Minecraft.getInstance().player;
if (player == null) return;
InteractionHand hand = Minecraft.getInstance().options.mainHand().get() == HumanoidArm.RIGHT ? InteractionHand.OFF_HAND : InteractionHand.MAIN_HAND;
if (event.getHand() == hand) {
if (player.getMainHandItem().is(TargetModTags.Items.GUN)) {
event.setCanceled(true);
}
}
}
private static void handleWeaponCrossHair(LivingEntity entity) {
if (entity.getMainHandItem().is(TargetModTags.Items.GUN)) {
float fps = Minecraft.getInstance().getFps();
@ -113,7 +129,7 @@ public class ClientEventHandler {
float times = 90f / fps;
var data = entity.getPersistentData();
double move_speed = (float) Mth.clamp(entity.getDeltaMovement().horizontalDistanceSqr(),0,0.02);
double move_speed = (float) Mth.clamp(entity.getDeltaMovement().horizontalDistanceSqr(), 0, 0.02);
double on_ground;
if (entity.onGround()) {
if (entity.isSprinting()) {
@ -206,9 +222,9 @@ public class ClientEventHandler {
if (-0.8 < velocity + 0.078 && velocity + 0.078 < 0.8) {
if (data.getDouble("vy") < entity.getDeltaMovement().y() + 0.078) {
data.putDouble("vy",Mth.clamp(((data.getDouble("vy") + 0.35 * Math.pow((velocity + 0.078) - data.getDouble("vy"), 2)) * (1 - 0.8 * data.getDouble("zoom_time"))),-0.8,0.8));
data.putDouble("vy", Mth.clamp(((data.getDouble("vy") + 0.35 * Math.pow((velocity + 0.078) - data.getDouble("vy"), 2)) * (1 - 0.8 * data.getDouble("zoom_time"))), -0.8, 0.8));
} else {
data.putDouble("vy",Mth.clamp(((data.getDouble("vy") - 0.35 * Math.pow((velocity + 0.078) - data.getDouble("vy"), 2)) * (1 - 0.8 * data.getDouble("zoom_time"))),-0.8,0.8));
data.putDouble("vy", Mth.clamp(((data.getDouble("vy") - 0.35 * Math.pow((velocity + 0.078) - data.getDouble("vy"), 2)) * (1 - 0.8 * data.getDouble("zoom_time"))), -0.8, 0.8));
}
}
}

View file

@ -1,16 +1,12 @@
package net.mcreator.target.mixins;
import com.mojang.blaze3d.vertex.PoseStack;
import net.mcreator.target.item.gun.GunItem;
import net.minecraft.client.Minecraft;
import net.mcreator.target.init.TargetModTags;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.entity.layers.ItemInHandLayer;
import net.minecraft.client.renderer.entity.layers.PlayerItemInHandLayer;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.HumanoidArm;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemDisplayContext;
import net.minecraft.world.item.ItemStack;
import org.spongepowered.asm.mixin.Mixin;
@ -18,34 +14,18 @@ import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
/**
* Author: MrCrayfish
*/
@Mixin(ItemInHandLayer.class)
public class ItemInHandLayerMixin
{
@SuppressWarnings({"ConstantConditions"})
@Inject(method = "renderArmWithItem", at = @At(value = "HEAD"), cancellable = true)
private void renderArmWithItemHead(LivingEntity entity, ItemStack stack, ItemDisplayContext display, HumanoidArm arm, PoseStack poseStack, MultiBufferSource source, int light, CallbackInfo ci)
{
if(entity.getType() == EntityType.PLAYER)
{
InteractionHand hand = Minecraft.getInstance().options.mainHand().get() == arm ? InteractionHand.MAIN_HAND : InteractionHand.OFF_HAND;
if(hand == InteractionHand.OFF_HAND)
{
if(stack.getItem() instanceof GunItem)
{
ci.cancel();
return;
}
public class ItemInHandLayerMixin {
@SuppressWarnings({"ConstantConditions"})
@Inject(method = "renderArmWithItem(Lnet/minecraft/world/entity/LivingEntity;Lnet/minecraft/world/item/ItemStack;Lnet/minecraft/world/item/ItemDisplayContext;Lnet/minecraft/world/entity/HumanoidArm;Lcom/mojang/blaze3d/vertex/PoseStack;Lnet/minecraft/client/renderer/MultiBufferSource;I)V",
at = @At(value = "HEAD"), cancellable = true)
private void renderArmWithItemHead(LivingEntity entity, ItemStack stack, ItemDisplayContext display, HumanoidArm arm, PoseStack poseStack, MultiBufferSource source, int light, CallbackInfo ci) {
if (entity.getType() == EntityType.PLAYER) {
if (arm == HumanoidArm.LEFT) {
ItemStack mainHand = entity.getMainHandItem();
if (!mainHand.is(TargetModTags.Items.GUN)) return;
if(entity.getMainHandItem().getItem() instanceof GunItem) {
ci.cancel();
return;
}
}
if(stack.getItem() instanceof GunItem)
{
ci.cancel();
}
}