优化开火模式按键处理逻辑

This commit is contained in:
17146 2025-01-01 20:46:46 +08:00
parent a8db1dd5e4
commit cdebcd298a
3 changed files with 16 additions and 33 deletions

View file

@ -209,6 +209,9 @@ public class ClickHandler {
if (key == ModKeyMappings.RELOAD.getKey().getValue()) {
ModUtils.PACKET_HANDLER.sendToServer(new ReloadMessage(0));
}
if (key == ModKeyMappings.FIRE_MODE.getKey().getValue()) {
ModUtils.PACKET_HANDLER.sendToServer(new FireModeMessage(0));
}
if (key == ModKeyMappings.EDIT_MODE.getKey().getValue() && ClientEventHandler.burstFireSize == 0) {
ClientEventHandler.holdFire = false;
ModUtils.PACKET_HANDLER.sendToServer(new EditModeMessage(0));

View file

@ -1,10 +1,9 @@
package com.atsuishio.superbwarfare.init;
import com.mojang.blaze3d.platform.InputConstants;
import com.atsuishio.superbwarfare.ModUtils;
import com.atsuishio.superbwarfare.network.message.BreathMessage;
import com.atsuishio.superbwarfare.network.message.FireModeMessage;
import com.atsuishio.superbwarfare.network.message.InteractMessage;
import com.mojang.blaze3d.platform.InputConstants;
import net.minecraft.client.KeyMapping;
import net.minecraft.client.Minecraft;
import net.minecraftforge.api.distmarker.Dist;
@ -18,22 +17,9 @@ import org.lwjgl.glfw.GLFW;
@Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.MOD, value = Dist.CLIENT)
public class ModKeyMappings {
public static final KeyMapping RELOAD = new KeyMapping("key.superbwarfare.reload", GLFW.GLFW_KEY_R, "key.categories.superbwarfare");
public static final KeyMapping FIRE_MODE = new KeyMapping("key.superbwarfare.fire_mode", GLFW.GLFW_KEY_N, "key.categories.superbwarfare") {
private boolean isDownOld = false;
@Override
public void setDown(boolean isDown) {
super.setDown(isDown);
if (isDownOld != isDown && isDown) {
ModUtils.PACKET_HANDLER.sendToServer(new FireModeMessage(0));
FireModeMessage.pressAction(Minecraft.getInstance().player, 0);
}
isDownOld = isDown;
}
};
public static final KeyMapping FIRE_MODE = new KeyMapping("key.superbwarfare.fire_mode", GLFW.GLFW_KEY_N, "key.categories.superbwarfare");
public static final KeyMapping SENSITIVITY_INCREASE = new KeyMapping("key.superbwarfare.sensitivity_increase", GLFW.GLFW_KEY_PAGE_UP, "key.categories.superbwarfare");
public static final KeyMapping SENSITIVITY_REDUCE = new KeyMapping("key.superbwarfare.sensitivity_reduce", GLFW.GLFW_KEY_PAGE_DOWN, "key.categories.superbwarfare");

View file

@ -4,12 +4,10 @@ import com.atsuishio.superbwarfare.init.ModItems;
import com.atsuishio.superbwarfare.init.ModSounds;
import com.atsuishio.superbwarfare.item.gun.GunItem;
import com.atsuishio.superbwarfare.tools.GunsTool;
import net.minecraft.core.Holder;
import com.atsuishio.superbwarfare.tools.SoundTool;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.protocol.game.ClientboundSoundPacket;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.common.capabilities.ForgeCapabilities;
@ -36,18 +34,16 @@ public class FireModeMessage {
public static void handler(FireModeMessage message, Supplier<NetworkEvent.Context> contextSupplier) {
NetworkEvent.Context context = contextSupplier.get();
context.enqueueWork(() -> pressAction(context.getSender(), message.type));
context.enqueueWork(() -> {
if (context.getSender() == null) return;
if (message.type == 0) {
changeFireMode(context.getSender());
}
});
context.setPacketHandled(true);
}
public static void pressAction(Player player, int type) {
if (player == null) return;
if (type == 0) {
changeFireMode(player);
}
}
public static void changeFireMode(Player player) {
ItemStack stack = player.getMainHandItem();
if (stack.getItem() instanceof GunItem gunItem) {
@ -121,8 +117,7 @@ public class FireModeMessage {
if (stack.getItem() == ModItems.JAVELIN.get()) {
tag.putBoolean("TopMode", !tag.getBoolean("TopMode"));
if (player instanceof ServerPlayer serverPlayer) {
serverPlayer.connection.send(new ClientboundSoundPacket(new Holder.Direct<>(ModSounds.CANNON_ZOOM_OUT.get()),
SoundSource.PLAYERS, serverPlayer.getX(), serverPlayer.getY(), serverPlayer.getZ(), 1f, 1f, serverPlayer.level().random.nextLong()));
SoundTool.playLocalSound(serverPlayer, ModSounds.CANNON_ZOOM_OUT.get());
}
}
@ -137,8 +132,7 @@ public class FireModeMessage {
private static void playChangeModeSound(Player player) {
if (player instanceof ServerPlayer serverPlayer) {
serverPlayer.connection.send(new ClientboundSoundPacket(new Holder.Direct<>(ModSounds.FIRE_RATE.get()),
SoundSource.PLAYERS, serverPlayer.getX(), serverPlayer.getY(), serverPlayer.getZ(), 1f, 1f, serverPlayer.level().random.nextLong()));
SoundTool.playLocalSound(serverPlayer, ModSounds.FIRE_RATE.get());
}
}
}