完美实现(枪械)按X交互
This commit is contained in:
parent
779b4a0db1
commit
31fa3ff792
2 changed files with 46 additions and 34 deletions
|
@ -20,6 +20,7 @@ import com.atsuishio.superbwarfare.tools.SeekTool;
|
|||
import com.atsuishio.superbwarfare.tools.TraceTool;
|
||||
import com.mojang.blaze3d.platform.InputConstants;
|
||||
import net.minecraft.ChatFormatting;
|
||||
import net.minecraft.client.KeyMapping;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.gui.screens.Screen;
|
||||
import net.minecraft.core.component.DataComponents;
|
||||
|
@ -82,12 +83,23 @@ public class ClickHandler {
|
|||
}
|
||||
}
|
||||
|
||||
private static boolean cancelFireKey(Player player, ItemStack stack) {
|
||||
return stack.getItem() instanceof GunItem || stack.is(ModItems.MONITOR.get()) || stack.is(ModItems.LUNGE_MINE.get()) || player.hasEffect(ModMobEffects.SHOCK)
|
||||
|| (player.getVehicle() instanceof ArmedVehicleEntity iArmedVehicle && iArmedVehicle.banHand(player));
|
||||
}
|
||||
|
||||
private static boolean cancelZoomKey(Player player, ItemStack stack) {
|
||||
return stack.getItem() instanceof GunItem
|
||||
|| (player.getVehicle() instanceof ArmedVehicleEntity iArmedVehicle && iArmedVehicle.isDriver(player) && stack.get(DataComponents.FOOD) != null);
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public static void onButtonPressed(InputEvent.MouseButton.Pre event) {
|
||||
if (notInGame()) return;
|
||||
if (event.getAction() != InputConstants.PRESS) return;
|
||||
|
||||
Player player = Minecraft.getInstance().player;
|
||||
var mc = Minecraft.getInstance();
|
||||
Player player = mc.player;
|
||||
if (player == null) return;
|
||||
if (player.isSpectator()) return;
|
||||
|
||||
|
@ -95,20 +107,22 @@ public class ClickHandler {
|
|||
|
||||
int button = event.getButton();
|
||||
|
||||
if (stack.getItem() instanceof GunItem || stack.is(ModItems.MONITOR.get()) || stack.is(ModItems.LUNGE_MINE.get()) || player.hasEffect(ModMobEffects.SHOCK)
|
||||
|| (player.getVehicle() instanceof ArmedVehicleEntity iArmedVehicle && iArmedVehicle.banHand(player))) {
|
||||
if (button == GLFW.GLFW_MOUSE_BUTTON_LEFT) {
|
||||
event.setCanceled(true);
|
||||
}
|
||||
var fireKey = ModKeyMappings.FIRE.getKey();
|
||||
if (fireKey.getType() == InputConstants.Type.MOUSE
|
||||
&& fireKey.getValue() == button
|
||||
&& cancelFireKey(player, stack)
|
||||
) {
|
||||
event.setCanceled(true);
|
||||
}
|
||||
|
||||
if (player.hasEffect(ModMobEffects.SHOCK)) return;
|
||||
|
||||
if (button == GLFW.GLFW_MOUSE_BUTTON_RIGHT) {
|
||||
if (stack.getItem() instanceof GunItem
|
||||
|| (player.getVehicle() instanceof ArmedVehicleEntity iArmedVehicle && iArmedVehicle.isDriver(player) && stack.get(DataComponents.FOOD) != null)) {
|
||||
event.setCanceled(true);
|
||||
}
|
||||
var zoomKey = ModKeyMappings.HOLD_ZOOM.getKey();
|
||||
if (zoomKey.getType() == InputConstants.Type.MOUSE
|
||||
&& zoomKey.getValue() == button
|
||||
&& cancelZoomKey(player, stack)
|
||||
) {
|
||||
event.setCanceled(true);
|
||||
}
|
||||
|
||||
if (button == GLFW.GLFW_MOUSE_BUTTON_MIDDLE) {
|
||||
|
@ -139,6 +153,15 @@ public class ClickHandler {
|
|||
}
|
||||
}
|
||||
|
||||
// 枪械交互时禁止挥舞手臂
|
||||
@SubscribeEvent
|
||||
public static void stopSwing(InputEvent.InteractionKeyMappingTriggered event) {
|
||||
var player = Minecraft.getInstance().player;
|
||||
if (player != null && player.getItemInHand(event.getHand()).getItem() instanceof GunItem) {
|
||||
event.setSwingHand(false);
|
||||
}
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public static void onMouseScrolling(InputEvent.MouseScrollingEvent event) {
|
||||
Player player = Minecraft.getInstance().player;
|
||||
|
@ -227,7 +250,12 @@ public class ClickHandler {
|
|||
PacketDistributor.sendToServer(new FireModeMessage(0));
|
||||
}
|
||||
if (key == ModKeyMappings.INTERACT.getKey().getValue()) {
|
||||
PacketDistributor.sendToServer(new InteractMessage(0));
|
||||
var mc = Minecraft.getInstance();
|
||||
if (mc.player.getMainHandItem().getItem() instanceof GunItem) {
|
||||
KeyMapping.click(mc.options.keyUse.getKey());
|
||||
} else if (mc.player.getMainHandItem().is(ModItems.MONITOR.get())) {
|
||||
PacketDistributor.sendToServer(new InteractMessage(0));
|
||||
}
|
||||
}
|
||||
if (key == ModKeyMappings.DISMOUNT.getKey().getValue()) {
|
||||
handleDismountPress(player);
|
||||
|
|
|
@ -3,7 +3,6 @@ package com.atsuishio.superbwarfare.network.message.send;
|
|||
import com.atsuishio.superbwarfare.Mod;
|
||||
import com.atsuishio.superbwarfare.entity.vehicle.DroneEntity;
|
||||
import com.atsuishio.superbwarfare.init.ModItems;
|
||||
import com.atsuishio.superbwarfare.item.gun.GunItem;
|
||||
import com.atsuishio.superbwarfare.tools.EntityFindUtil;
|
||||
import com.atsuishio.superbwarfare.tools.NBTTool;
|
||||
import com.atsuishio.superbwarfare.tools.TraceTool;
|
||||
|
@ -14,12 +13,11 @@ import net.minecraft.network.codec.ByteBufCodecs;
|
|||
import net.minecraft.network.codec.StreamCodec;
|
||||
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
|
||||
import net.minecraft.world.InteractionHand;
|
||||
import net.minecraft.world.ItemInteractionResult;
|
||||
import net.minecraft.world.entity.Entity;
|
||||
import net.minecraft.world.entity.player.Player;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import net.minecraft.world.level.ClipContext;
|
||||
import net.minecraft.world.level.Level;
|
||||
import net.minecraft.world.level.block.BellBlock;
|
||||
import net.minecraft.world.phys.BlockHitResult;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import net.neoforged.neoforge.network.handling.IPayloadContext;
|
||||
|
@ -39,27 +37,10 @@ public record InteractMessage(int msgType) implements CustomPacketPayload {
|
|||
}
|
||||
|
||||
public static void handleInteract(Player player, int type) {
|
||||
Level level = player.level();
|
||||
|
||||
ItemStack stack = player.getMainHandItem();
|
||||
var tag = NBTTool.getTag(stack);
|
||||
if (stack.getItem() instanceof GunItem) {
|
||||
double blockRange = player.blockInteractionRange();
|
||||
double entityRange = player.blockInteractionRange();
|
||||
|
||||
Vec3 looking = Vec3.atLowerCornerOf(player.level().clip(new ClipContext(player.getEyePosition(), player.getEyePosition().add(player.getLookAngle().scale(blockRange)), ClipContext.Block.OUTLINE, ClipContext.Fluid.NONE, player)).getBlockPos());
|
||||
BlockPos blockPos = BlockPos.containing(looking.x(), looking.y(), looking.z());
|
||||
level.getBlockState(blockPos).useItemOn(player.getMainHandItem(), player.level(), player, InteractionHand.MAIN_HAND, BlockHitResult.miss(new Vec3(blockPos.getX(), blockPos.getY(), blockPos.getZ()), Direction.UP, blockPos));
|
||||
|
||||
if ((level.getBlockState(BlockPos.containing(looking.x(), looking.y(), looking.z()))).getBlock() instanceof BellBlock bell) {
|
||||
bell.attemptToRing(level, blockPos, player.getDirection().getOpposite());
|
||||
}
|
||||
|
||||
Entity lookingEntity = TraceTool.findLookingEntity(player, entityRange);
|
||||
if (lookingEntity == null) return;
|
||||
|
||||
player.interactOn(lookingEntity, InteractionHand.MAIN_HAND);
|
||||
} else if (stack.is(ModItems.MONITOR.get())
|
||||
if (stack.is(ModItems.MONITOR.get())
|
||||
&& tag.getBoolean("Using")
|
||||
&& tag.getBoolean("Linked")
|
||||
&& !player.getCooldowns().isOnCooldown(stack.getItem())
|
||||
|
@ -69,7 +50,10 @@ public record InteractMessage(int msgType) implements CustomPacketPayload {
|
|||
if (drone != null) {
|
||||
Vec3 looking = Vec3.atLowerCornerOf(player.level().clip(new ClipContext(drone.getEyePosition(), drone.getEyePosition().add(drone.getLookAngle().scale(2)), ClipContext.Block.OUTLINE, ClipContext.Fluid.NONE, player)).getBlockPos());
|
||||
BlockPos blockPos = BlockPos.containing(looking.x(), looking.y(), looking.z());
|
||||
player.level().getBlockState(blockPos).useItemOn(player.getMainHandItem(), player.level(), player, InteractionHand.MAIN_HAND, BlockHitResult.miss(new Vec3(blockPos.getX(), blockPos.getY(), blockPos.getZ()), Direction.UP, blockPos));
|
||||
var result = player.level().getBlockState(blockPos).useItemOn(player.getMainHandItem(), player.level(), player, InteractionHand.MAIN_HAND, BlockHitResult.miss(new Vec3(blockPos.getX(), blockPos.getY(), blockPos.getZ()), Direction.UP, blockPos));
|
||||
if (result == ItemInteractionResult.PASS_TO_DEFAULT_BLOCK_INTERACTION) {
|
||||
player.level().getBlockState(blockPos).useWithoutItem(player.level(), player, BlockHitResult.miss(new Vec3(blockPos.getX(), blockPos.getY(), blockPos.getZ()), Direction.UP, blockPos));
|
||||
}
|
||||
|
||||
Entity lookingEntity = TraceTool.findLookingEntity(drone, 2);
|
||||
if (lookingEntity == null) return;
|
||||
|
|
Loading…
Add table
Reference in a new issue