superb-warfare/src/main/java/com/atsuishio/superbwarfare/mixins/MinecraftMixin.java
2025-03-11 01:26:38 +08:00

78 lines
2.7 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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;
import net.minecraft.client.player.LocalPlayer;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import javax.annotation.Nullable;
@Mixin(Minecraft.class)
public class MinecraftMixin {
@Shadow
@Nullable
public LocalPlayer player;
@Shadow
@Final
public Options options;
/**
* 在可切换座位的载具上按下shift+数字键时切换座位
* 在有武器的载具上,按下数字键时切换武器
*/
@Inject(method = "handleKeybinds()V", at = @At("HEAD"), cancellable = true)
private void handleKeybinds(CallbackInfo ci) {
if (player == null || !(player.getVehicle() instanceof VehicleEntity vehicle)) return;
var index = -1;
for (int i = 0; i < 9; ++i) {
if (options.keyHotbarSlots[i].isDown()) {
index = i;
break;
}
}
if (index == -1) return;
// shift+数字键 座位更改
if (vehicle.getMaxPassengers() > 1
&& Screen.hasShiftDown()
&& index < vehicle.getMaxPassengers()
&& vehicle.getNthEntity(index) == null
) {
ci.cancel();
options.keyHotbarSlots[index].consumeClick();
ModUtils.PACKET_HANDLER.sendToServer(new ChangeVehicleSeatMessage(index));
vehicle.changeSeat(player, index);
return;
}
var seatIndex = vehicle.getSeatIndex(player);
if (vehicle instanceof WeaponVehicleEntity weaponVehicle && weaponVehicle.banHand(player)) {
ci.cancel();
options.keyHotbarSlots[index].consumeClick();
// 数字键 武器切换
if (!Screen.hasShiftDown()
&& weaponVehicle.hasWeapon(seatIndex)
&& weaponVehicle.getWeaponIndex(seatIndex) != index) {
ModUtils.PACKET_HANDLER.sendToServer(new SwitchVehicleWeaponMessage(seatIndex, index, false));
}
}
}
}