From 15cd3ac6a402e3cffa3e4a5858b9a40d41e8dc24 Mon Sep 17 00:00:00 2001 From: Light_Quanta Date: Wed, 5 Mar 2025 19:10:57 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=81=E8=AE=B8=E4=BD=BF=E7=94=A8=E6=95=B0?= =?UTF-8?q?=E5=AD=97=E9=94=AE=E7=9B=B4=E6=8E=A5=E5=88=87=E6=8D=A2=E6=AD=A6?= =?UTF-8?q?=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../superbwarfare/client/ClickHandler.java | 2 +- .../entity/vehicle/Ah6Entity.java | 9 ++-- .../entity/vehicle/Bmp2Entity.java | 11 ++--- .../entity/vehicle/Lav150Entity.java | 9 ++-- .../entity/vehicle/Yx100Entity.java | 21 ++++----- .../vehicle/base/WeaponVehicleEntity.java | 7 +-- .../superbwarfare/mixins/MinecraftMixin.java | 43 ++++++++++++++----- .../message/SwitchVehicleWeaponMessage.java | 17 +++++--- 8 files changed, 75 insertions(+), 44 deletions(-) diff --git a/src/main/java/com/atsuishio/superbwarfare/client/ClickHandler.java b/src/main/java/com/atsuishio/superbwarfare/client/ClickHandler.java index b221e8a26..91548decd 100644 --- a/src/main/java/com/atsuishio/superbwarfare/client/ClickHandler.java +++ b/src/main/java/com/atsuishio/superbwarfare/client/ClickHandler.java @@ -160,7 +160,7 @@ public class ClickHandler { && weaponVehicle.hasWeapon(vehicle.getSeatIndex(player)) ) { int index = vehicle.getSeatIndex(player); - ModUtils.PACKET_HANDLER.sendToServer(new SwitchVehicleWeaponMessage(index, -scroll)); + ModUtils.PACKET_HANDLER.sendToServer(new SwitchVehicleWeaponMessage(index, -scroll, true)); event.setCanceled(true); } diff --git a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Ah6Entity.java b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Ah6Entity.java index dacd1d36e..a64de3fc6 100644 --- a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Ah6Entity.java +++ b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Ah6Entity.java @@ -759,18 +759,19 @@ public class Ah6Entity extends ContainerMobileVehicleEntity implements GeoEntity } @Override - public void changeWeapon(int index, int scroll) { + public void changeWeapon(int index, int value, boolean isScroll) { if (index != 0) return; - var type = (getWeaponType(0) + scroll + 2) % 2; - setWeaponType(0, type); + var type = isScroll ? (value + getWeaponType(0)) % 2 : value; var sound = switch (type) { case 0 -> ModSounds.INTO_MISSILE.get(); case 1 -> ModSounds.INTO_CANNON.get(); - default -> throw new IllegalStateException("Unexpected type: " + type); + default -> null; }; + if (sound == null) return; + setWeaponType(0, type); this.level().playSound(null, this, sound, this.getSoundSource(), 1, 1); } diff --git a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Bmp2Entity.java b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Bmp2Entity.java index bdccb972e..1e2c3c4a8 100644 --- a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Bmp2Entity.java +++ b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Bmp2Entity.java @@ -438,7 +438,7 @@ public class Bmp2Entity extends ContainerMobileVehicleEntity implements GeoEntit @Override public void travel() { Entity passenger0 = this.getFirstPassenger(); - + if (this.getEnergy() <= 0) return; if (passenger0 == null) { @@ -758,18 +758,19 @@ public class Bmp2Entity extends ContainerMobileVehicleEntity implements GeoEntit } @Override - public void changeWeapon(int index, int scroll) { + public void changeWeapon(int index, int value, boolean isScroll) { if (index != 0) return; - var type = (getWeaponType(0) + scroll + 3) % 3; - setWeaponType(0, type); + var type = isScroll ? (value + getWeaponType(0)) % 2 : value; var sound = switch (type) { case 0, 2 -> ModSounds.INTO_MISSILE.get(); case 1 -> ModSounds.INTO_CANNON.get(); - default -> throw new IllegalStateException("Unexpected type: " + type); + default -> null; }; + if (sound == null) return; + setWeaponType(0, type); this.level().playSound(null, this, sound, this.getSoundSource(), 1, 1); } diff --git a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Lav150Entity.java b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Lav150Entity.java index a3dd3dacc..89ac5b723 100644 --- a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Lav150Entity.java +++ b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Lav150Entity.java @@ -702,18 +702,19 @@ public class Lav150Entity extends ContainerMobileVehicleEntity implements GeoEnt } @Override - public void changeWeapon(int index, int scroll) { + public void changeWeapon(int index, int value, boolean isScroll) { if (index != 0) return; - var type = (getWeaponType(0) + scroll + 2) % 2; - setWeaponType(0, type); + int type = isScroll ? (value + getWeaponType(0)) % 2 : value; var sound = switch (type) { case 0 -> ModSounds.INTO_MISSILE.get(); case 1 -> ModSounds.INTO_CANNON.get(); - default -> throw new IllegalStateException("Unexpected type: " + type); + default -> null; }; + if (sound == null) return; + setWeaponType(0, type); this.level().playSound(null, this, sound, this.getSoundSource(), 1, 1); } diff --git a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Yx100Entity.java b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Yx100Entity.java index c41fe82ab..48a99fedf 100644 --- a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Yx100Entity.java +++ b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Yx100Entity.java @@ -732,9 +732,19 @@ public class Yx100Entity extends ContainerMobileVehicleEntity implements GeoEnti } @Override - public void changeWeapon(int index, int scroll) { + public void changeWeapon(int index, int value, boolean isScroll) { if (index != 0) return; + var type = isScroll ? (value + getWeaponType(0)) % 2 : value; + + var sound = switch (type) { + case 0 -> ModSounds.INTO_MISSILE.get(); + case 1 -> ModSounds.INTO_CANNON.get(); + default -> null; + }; + if (sound == null) return; + setWeaponType(0, type); + if (entityData.get(LOADED_AMMO) > 0) { if (this.getFirstPassenger() instanceof Player player && !player.getInventory().hasAnyMatching(s -> s.is(ModItems.CREATIVE_AMMO_BOX.get()))) { this.insertItem(getCurrentAmmoItem(), 1); @@ -748,15 +758,6 @@ public class Yx100Entity extends ContainerMobileVehicleEntity implements GeoEnti var clientboundstopsoundpacket = new ClientboundStopSoundPacket(ModSounds.YX_100_RELOAD.get().getLocation(), SoundSource.PLAYERS); player.connection.send(clientboundstopsoundpacket); } - - var type = (getWeaponType(0) + scroll + 2) % 2; - setWeaponType(0, type); - - var sound = switch (type) { - case 0 -> ModSounds.INTO_MISSILE.get(); - case 1 -> ModSounds.INTO_CANNON.get(); - default -> throw new IllegalStateException("Unexpected type: " + type); - }; this.level().playSound(null, this, sound, this.getSoundSource(), 1, 1); } diff --git a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/base/WeaponVehicleEntity.java b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/base/WeaponVehicleEntity.java index b55cb8c85..4081e43bf 100644 --- a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/base/WeaponVehicleEntity.java +++ b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/base/WeaponVehicleEntity.java @@ -17,10 +17,11 @@ public interface WeaponVehicleEntity extends ArmedVehicleEntity { /** * 切换武器事件 * - * @param index 武器槽位 - * @param scroll 滚动值,-1~1之间的整数 + * @param index 武器槽位 + * @param value 数值(可能为-1~1之间的滚动,或绝对数值) + * @param isScroll 是否是滚动事件 */ - default void changeWeapon(int index, int scroll) { + default void changeWeapon(int index, int value, boolean isScroll) { } /** diff --git a/src/main/java/com/atsuishio/superbwarfare/mixins/MinecraftMixin.java b/src/main/java/com/atsuishio/superbwarfare/mixins/MinecraftMixin.java index be30286b6..ca25a6ba2 100644 --- a/src/main/java/com/atsuishio/superbwarfare/mixins/MinecraftMixin.java +++ b/src/main/java/com/atsuishio/superbwarfare/mixins/MinecraftMixin.java @@ -2,7 +2,9 @@ package com.atsuishio.superbwarfare.mixins; import com.atsuishio.superbwarfare.ModUtils; import com.atsuishio.superbwarfare.entity.vehicle.base.VehicleEntity; +import com.atsuishio.superbwarfare.entity.vehicle.base.WeaponVehicleEntity; import com.atsuishio.superbwarfare.network.message.ChangeVehicleSeatMessage; +import com.atsuishio.superbwarfare.network.message.SwitchVehicleWeaponMessage; import net.minecraft.client.Minecraft; import net.minecraft.client.Options; import net.minecraft.client.gui.screens.Screen; @@ -28,22 +30,41 @@ public class MinecraftMixin { public Options options; /** - * 未按住shift且在可切换座位的载具上时,禁用快捷栏切换,发送切换座位消息 + * 在可切换座位的载具上,按下shift+数字键时切换座位 + * 在有武器的载具上,按下数字键时切换武器 */ @Inject(method = "handleKeybinds()V", at = @At("HEAD"), cancellable = true) private void handleKeybinds(CallbackInfo ci) { - if (player != null && player.getVehicle() instanceof VehicleEntity vehicle && vehicle.getMaxPassengers() > 1 - && !Screen.hasShiftDown()) { - for (int i = 0; i < 9; ++i) { - if (options.keyHotbarSlots[i].consumeClick()) { - ci.cancel(); + if (player == null || !(player.getVehicle() instanceof VehicleEntity vehicle)) return; - if (i < vehicle.getMaxPassengers() && vehicle.getNthEntity(i) == null) { - ModUtils.PACKET_HANDLER.sendToServer(new ChangeVehicleSeatMessage(i)); - vehicle.changeSeat(player, i); - } - } + var index = -1; + for (int i = 0; i < 9; ++i) { + if (options.keyHotbarSlots[i].consumeClick()) { + index = i; + break; } } + if (index == -1) return; + + // shift+数字键 座位更改 + if (vehicle.getMaxPassengers() > 1 + && Screen.hasShiftDown() + && index < vehicle.getMaxPassengers() + && vehicle.getNthEntity(index) == null + ) { + ModUtils.PACKET_HANDLER.sendToServer(new ChangeVehicleSeatMessage(index)); + vehicle.changeSeat(player, index); + ci.cancel(); + return; + } + + // 数字键 武器切换 + if (vehicle instanceof WeaponVehicleEntity weaponVehicle + && !Screen.hasShiftDown() + && weaponVehicle.hasWeapon(vehicle.getSeatIndex(player)) + ) { + ModUtils.PACKET_HANDLER.sendToServer(new SwitchVehicleWeaponMessage(vehicle.getSeatIndex(player), index, false)); + ci.cancel(); + } } } diff --git a/src/main/java/com/atsuishio/superbwarfare/network/message/SwitchVehicleWeaponMessage.java b/src/main/java/com/atsuishio/superbwarfare/network/message/SwitchVehicleWeaponMessage.java index b8371af60..b18a9df64 100644 --- a/src/main/java/com/atsuishio/superbwarfare/network/message/SwitchVehicleWeaponMessage.java +++ b/src/main/java/com/atsuishio/superbwarfare/network/message/SwitchVehicleWeaponMessage.java @@ -11,20 +11,24 @@ import java.util.function.Supplier; public class SwitchVehicleWeaponMessage { private final int index; - private final double scroll; + private final double value; - public SwitchVehicleWeaponMessage(int index, double scroll) { + private final boolean isScroll; + + public SwitchVehicleWeaponMessage(int index, double value, boolean isScroll) { this.index = index; - this.scroll = scroll; + this.value = value; + this.isScroll = isScroll; } public static void encode(SwitchVehicleWeaponMessage message, FriendlyByteBuf byteBuf) { byteBuf.writeInt(message.index); - byteBuf.writeDouble(message.scroll); + byteBuf.writeDouble(message.value); + byteBuf.writeBoolean(message.isScroll); } public static SwitchVehicleWeaponMessage decode(FriendlyByteBuf byteBuf) { - return new SwitchVehicleWeaponMessage(byteBuf.readInt(), byteBuf.readDouble()); + return new SwitchVehicleWeaponMessage(byteBuf.readInt(), byteBuf.readDouble(), byteBuf.readBoolean()); } public static void handler(SwitchVehicleWeaponMessage message, Supplier context) { @@ -35,7 +39,8 @@ public class SwitchVehicleWeaponMessage { } if (player.getVehicle() instanceof WeaponVehicleEntity weaponVehicle && weaponVehicle.isDriver(player)) { - weaponVehicle.changeWeapon(message.index, Mth.clamp(message.scroll > 0 ? Mth.ceil(message.scroll) : Mth.floor(message.scroll), -1, 1)); + var value = message.isScroll ? (Mth.clamp(message.value > 0 ? Mth.ceil(message.value) : Mth.floor(message.value), -1, 1)) : message.value; + weaponVehicle.changeWeapon(message.index, (int) value, message.isScroll); } }); context.get().setPacketHandled(true);