允许使用数字键直接切换武器

This commit is contained in:
Light_Quanta 2025-03-05 19:10:57 +08:00
parent ea64a53586
commit 15cd3ac6a4
No known key found for this signature in database
GPG key ID: 11A39A1B8C890959
8 changed files with 75 additions and 44 deletions

View file

@ -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);
}

View file

@ -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);
}

View file

@ -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);
}

View file

@ -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);
}

View file

@ -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);
}

View file

@ -18,9 +18,10 @@ public interface WeaponVehicleEntity extends ArmedVehicleEntity {
* 切换武器事件
*
* @param index 武器槽位
* @param scroll 滚动值-1~1之间的整数
* @param value 数值可能为-1~1之间的滚动或绝对数值
* @param isScroll 是否是滚动事件
*/
default void changeWeapon(int index, int scroll) {
default void changeWeapon(int index, int value, boolean isScroll) {
}
/**

View file

@ -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()) {
if (player == null || !(player.getVehicle() instanceof VehicleEntity vehicle)) return;
var index = -1;
for (int i = 0; i < 9; ++i) {
if (options.keyHotbarSlots[i].consumeClick()) {
ci.cancel();
index = i;
break;
}
}
if (index == -1) return;
if (i < vehicle.getMaxPassengers() && vehicle.getNthEntity(i) == null) {
ModUtils.PACKET_HANDLER.sendToServer(new ChangeVehicleSeatMessage(i));
vehicle.changeSeat(player, i);
}
}
}
// 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();
}
}
}

View file

@ -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<NetworkEvent.Context> 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);