重构战术冲刺

This commit is contained in:
17146 2025-04-12 22:12:02 +08:00 committed by Light_Quanta
parent fec470315b
commit 233bdf6998
No known key found for this signature in database
GPG key ID: 11A39A1B8C890959
6 changed files with 75 additions and 12 deletions

View file

@ -26,6 +26,7 @@ public class PlayerVariable implements INBTSerializable<CompoundTag> {
public int shotgunAmmo = 0;
public int sniperAmmo = 0;
public int heavyAmmo = 0;
public boolean tacticalSprint = false;
public boolean edit = false;
public void sync(Entity entity) {
@ -34,8 +35,8 @@ public class PlayerVariable implements INBTSerializable<CompoundTag> {
var newVariable = entity.getData(ModAttachments.PLAYER_VARIABLE);
if (old != null && old.equals(newVariable)) return;
if (entity instanceof ServerPlayer) {
PacketDistributor.sendToAllPlayers(new PlayerVariablesSyncMessage(entity.getId(), newVariable.writeToNBT()));
if (entity instanceof ServerPlayer serverPlayer) {
PacketDistributor.sendToPlayer(serverPlayer, new PlayerVariablesSyncMessage(entity.getId(), newVariable.writeToNBT()));
}
}
@ -58,6 +59,7 @@ public class PlayerVariable implements INBTSerializable<CompoundTag> {
type.set(nbt, type.get(this));
}
nbt.putBoolean("TacticalSprint", tacticalSprint);
nbt.putBoolean("EditMode", edit);
return nbt;
@ -68,6 +70,7 @@ public class PlayerVariable implements INBTSerializable<CompoundTag> {
type.set(this, type.get(tag));
}
tacticalSprint = tag.getBoolean("TacticalSprint");
edit = tag.getBoolean("EditMode");
return this;
@ -82,6 +85,8 @@ public class PlayerVariable implements INBTSerializable<CompoundTag> {
clone.sniperAmmo = this.sniperAmmo;
clone.heavyAmmo = this.heavyAmmo;
clone.edit = this.edit;
clone.tacticalSprint = this.tacticalSprint;
return clone;
}

View file

@ -351,6 +351,12 @@ public class ClientEventHandler {
if (zoom) {
tacticalSprint = false;
}
if (tacticalSprint && player.onGround()) {
PacketDistributor.sendToServer(new TacticalSprintMessage(true));
} else {
PacketDistributor.sendToServer(new TacticalSprintMessage(false));
}
}
/**

View file

@ -1,5 +1,6 @@
package com.atsuishio.superbwarfare.event;
import com.atsuishio.superbwarfare.Mod;
import com.atsuishio.superbwarfare.config.common.GameplayConfig;
import com.atsuishio.superbwarfare.init.ModAttachments;
import com.atsuishio.superbwarfare.init.ModItems;
@ -11,11 +12,14 @@ import com.atsuishio.superbwarfare.tools.AmmoType;
import com.atsuishio.superbwarfare.tools.GunsTool;
import com.atsuishio.superbwarfare.tools.InventoryTool;
import com.atsuishio.superbwarfare.tools.NBTTool;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.neoforged.bus.api.SubscribeEvent;
@ -27,6 +31,9 @@ import net.neoforged.neoforge.network.PacketDistributor;
@EventBusSubscriber
public class PlayerEventHandler {
public static final ResourceLocation TACTICAL_SPRINT = Mod.loc("tactical_sprint");
@SubscribeEvent
public static void onPlayerLoggedIn(PlayerEvent.PlayerLoggedInEvent event) {
Player player = event.getEntity();
@ -71,6 +78,10 @@ public class PlayerEventHandler {
if (stack.is(ModTags.Items.GUN)) {
handleSpecialWeaponAmmo(player);
}
if (!player.level().isClientSide) {
handleTacticalAttribute(player);
}
}
private static void handleSpecialWeaponAmmo(Player player) {
@ -176,6 +187,23 @@ public class PlayerEventHandler {
}
}
public static void handleTacticalAttribute(Player player) {
if (player == null) {
return;
}
var attr = player.getAttribute(Attributes.MOVEMENT_SPEED);
if (attr == null) return;
if (attr.getModifier(TACTICAL_SPRINT) != null) {
attr.removeModifier(TACTICAL_SPRINT);
}
if (player.getData(ModAttachments.PLAYER_VARIABLE).tacticalSprint) {
player.setSprinting(true);
attr.addTransientModifier(new AttributeModifier(TACTICAL_SPRINT, 0.25, AttributeModifier.Operation.ADD_MULTIPLIED_BASE));
}
}
@SubscribeEvent
public static void onAnvilUpdate(AnvilUpdateEvent event) {
ItemStack left = event.getLeft();

View file

@ -1,6 +1,5 @@
package com.atsuishio.superbwarfare.mixins;
import com.atsuishio.superbwarfare.event.ClientEventHandler;
import com.atsuishio.superbwarfare.init.ModItems;
import com.atsuishio.superbwarfare.init.ModMobEffects;
import com.atsuishio.superbwarfare.tools.NBTTool;
@ -17,11 +16,6 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
@Mixin(KeyboardInput.class)
public abstract class KeyboardInputMixin extends Input {
// @Shadow
// private static float calculateImpulse(boolean pInput, boolean pOtherInput) {
// return 0;
// }
// 按键修改mixin
@Inject(method = "tick", at = @At("RETURN"))
public void tick(boolean pIsSneaking, float pSneakingSpeedMultiplier, CallbackInfo ci) {
@ -29,10 +23,6 @@ public abstract class KeyboardInputMixin extends Input {
Player player = mc.player;
if (player == null) return;
if (ClientEventHandler.tacticalSprint && player.onGround()) {
this.forwardImpulse *= 2f;
}
ItemStack stack = player.getMainHandItem();
var tag = NBTTool.getTag(stack);

View file

@ -54,5 +54,6 @@ public class NetworkRegistry {
registrar.playToServer(AdjustMortarAngleMessage.TYPE, AdjustMortarAngleMessage.STREAM_CODEC, AdjustMortarAngleMessage::handler);
registrar.playToServer(ChangeVehicleSeatMessage.TYPE, ChangeVehicleSeatMessage.STREAM_CODEC, ChangeVehicleSeatMessage::handler);
registrar.playToServer(ShowChargingRangeMessage.TYPE, ShowChargingRangeMessage.STREAM_CODEC, ShowChargingRangeMessage::handler);
registrar.playToServer(TacticalSprintMessage.TYPE, TacticalSprintMessage.STREAM_CODEC, TacticalSprintMessage::handler);
}
}

View file

@ -0,0 +1,33 @@
package com.atsuishio.superbwarfare.network.message.send;
import com.atsuishio.superbwarfare.Mod;
import com.atsuishio.superbwarfare.init.ModAttachments;
import io.netty.buffer.ByteBuf;
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.neoforged.neoforge.network.handling.IPayloadContext;
import org.jetbrains.annotations.NotNull;
public record TacticalSprintMessage(boolean sprint) implements CustomPacketPayload {
public static final Type<TacticalSprintMessage> TYPE = new Type<>(Mod.loc("vehicle_fire"));
public static final StreamCodec<ByteBuf, TacticalSprintMessage> STREAM_CODEC = StreamCodec.composite(
ByteBufCodecs.BOOL,
TacticalSprintMessage::sprint,
TacticalSprintMessage::new
);
public static void handler(TacticalSprintMessage message, final IPayloadContext context) {
var player = context.player();
var cap = player.getData(ModAttachments.PLAYER_VARIABLE).watch();
cap.tacticalSprint = message.sprint;
cap.sync(player);
}
@Override
public @NotNull Type<? extends CustomPacketPayload> type() {
return TYPE;
}
}