规范命名FireMessage
This commit is contained in:
parent
229a1050ce
commit
8000fd40b2
5 changed files with 30 additions and 17 deletions
|
@ -8,6 +8,7 @@ import net.minecraft.core.HolderLookup;
|
||||||
import net.minecraft.nbt.CompoundTag;
|
import net.minecraft.nbt.CompoundTag;
|
||||||
import net.minecraft.server.level.ServerPlayer;
|
import net.minecraft.server.level.ServerPlayer;
|
||||||
import net.minecraft.world.entity.Entity;
|
import net.minecraft.world.entity.Entity;
|
||||||
|
import net.minecraft.world.entity.player.Player;
|
||||||
import net.neoforged.bus.api.SubscribeEvent;
|
import net.neoforged.bus.api.SubscribeEvent;
|
||||||
import net.neoforged.fml.common.EventBusSubscriber;
|
import net.neoforged.fml.common.EventBusSubscriber;
|
||||||
import net.neoforged.neoforge.common.util.INBTSerializable;
|
import net.neoforged.neoforge.common.util.INBTSerializable;
|
||||||
|
@ -18,6 +19,7 @@ import org.jetbrains.annotations.NotNull;
|
||||||
import javax.annotation.ParametersAreNonnullByDefault;
|
import javax.annotation.ParametersAreNonnullByDefault;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
|
||||||
@EventBusSubscriber(modid = Mod.MODID)
|
@EventBusSubscriber(modid = Mod.MODID)
|
||||||
|
@ -51,6 +53,15 @@ public class PlayerVariable implements INBTSerializable<CompoundTag> {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 编辑并同步玩家变量
|
||||||
|
*/
|
||||||
|
public void modify(Player player, Consumer<PlayerVariable> consumer) {
|
||||||
|
watch();
|
||||||
|
consumer.accept(this);
|
||||||
|
sync(player);
|
||||||
|
}
|
||||||
|
|
||||||
public CompoundTag writeToNBT() {
|
public CompoundTag writeToNBT() {
|
||||||
CompoundTag nbt = new CompoundTag();
|
CompoundTag nbt = new CompoundTag();
|
||||||
|
|
||||||
|
|
|
@ -345,7 +345,7 @@ public class ClickHandler {
|
||||||
ClientEventHandler.burstFireAmount = 0;
|
ClientEventHandler.burstFireAmount = 0;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
PacketDistributor.sendToServer(new FireMessage(0, handTimer, zoom));
|
PacketDistributor.sendToServer(new FireKeyMessage(0, handTimer, zoom));
|
||||||
if ((!data.reloading()
|
if ((!data.reloading()
|
||||||
&& !data.charging()
|
&& !data.charging()
|
||||||
&& !data.bolt.needed.get())
|
&& !data.bolt.needed.get())
|
||||||
|
@ -365,7 +365,7 @@ public class ClickHandler {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void handleWeaponFireRelease() {
|
public static void handleWeaponFireRelease() {
|
||||||
PacketDistributor.sendToServer(new FireMessage(1, handTimer, zoom));
|
PacketDistributor.sendToServer(new FireKeyMessage(1, handTimer, zoom));
|
||||||
bowPull = false;
|
bowPull = false;
|
||||||
holdFire = false;
|
holdFire = false;
|
||||||
holdFireVehicle = false;
|
holdFireVehicle = false;
|
||||||
|
|
|
@ -40,7 +40,7 @@ import javax.annotation.ParametersAreNonnullByDefault;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
import static com.atsuishio.superbwarfare.network.message.send.FireMessage.spawnBullet;
|
import static com.atsuishio.superbwarfare.network.message.send.FireKeyMessage.spawnBullet;
|
||||||
|
|
||||||
public class BocekItem extends GunItem implements GeoItem, ReleaseSpecialWeapon {
|
public class BocekItem extends GunItem implements GeoItem, ReleaseSpecialWeapon {
|
||||||
|
|
||||||
|
|
|
@ -40,7 +40,7 @@ public class NetworkRegistry {
|
||||||
registrar.playToServer(SwitchVehicleWeaponMessage.TYPE, SwitchVehicleWeaponMessage.STREAM_CODEC, SwitchVehicleWeaponMessage::handler);
|
registrar.playToServer(SwitchVehicleWeaponMessage.TYPE, SwitchVehicleWeaponMessage.STREAM_CODEC, SwitchVehicleWeaponMessage::handler);
|
||||||
registrar.playToServer(AdjustZoomFovMessage.TYPE, AdjustZoomFovMessage.STREAM_CODEC, AdjustZoomFovMessage::handler);
|
registrar.playToServer(AdjustZoomFovMessage.TYPE, AdjustZoomFovMessage.STREAM_CODEC, AdjustZoomFovMessage::handler);
|
||||||
registrar.playToServer(SwitchScopeMessage.TYPE, SwitchScopeMessage.STREAM_CODEC, SwitchScopeMessage::handler);
|
registrar.playToServer(SwitchScopeMessage.TYPE, SwitchScopeMessage.STREAM_CODEC, SwitchScopeMessage::handler);
|
||||||
registrar.playToServer(FireMessage.TYPE, FireMessage.STREAM_CODEC, FireMessage::handler);
|
registrar.playToServer(FireKeyMessage.TYPE, FireKeyMessage.STREAM_CODEC, FireKeyMessage::handler);
|
||||||
registrar.playToServer(ReloadMessage.TYPE, ReloadMessage.STREAM_CODEC, ReloadMessage::handler);
|
registrar.playToServer(ReloadMessage.TYPE, ReloadMessage.STREAM_CODEC, ReloadMessage::handler);
|
||||||
registrar.playToServer(FireModeMessage.TYPE, FireModeMessage.STREAM_CODEC, FireModeMessage::handler);
|
registrar.playToServer(FireModeMessage.TYPE, FireModeMessage.STREAM_CODEC, FireModeMessage::handler);
|
||||||
registrar.playToServer(PlayerStopRidingMessage.TYPE, PlayerStopRidingMessage.STREAM_CODEC, PlayerStopRidingMessage::handler);
|
registrar.playToServer(PlayerStopRidingMessage.TYPE, PlayerStopRidingMessage.STREAM_CODEC, PlayerStopRidingMessage::handler);
|
||||||
|
|
|
@ -25,21 +25,24 @@ import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
public record FireMessage(int msgType, double power, boolean zoom) implements CustomPacketPayload {
|
/**
|
||||||
public static final Type<FireMessage> TYPE = new Type<>(Mod.loc("fire"));
|
* 开火按键按下/松开时的处理
|
||||||
|
*/
|
||||||
|
public record FireKeyMessage(int msgType, double power, boolean zoom) implements CustomPacketPayload {
|
||||||
|
public static final Type<FireKeyMessage> TYPE = new Type<>(Mod.loc("fire"));
|
||||||
|
|
||||||
public static final StreamCodec<ByteBuf, FireMessage> STREAM_CODEC = StreamCodec.composite(
|
public static final StreamCodec<ByteBuf, FireKeyMessage> STREAM_CODEC = StreamCodec.composite(
|
||||||
ByteBufCodecs.INT,
|
ByteBufCodecs.INT,
|
||||||
FireMessage::msgType,
|
FireKeyMessage::msgType,
|
||||||
ByteBufCodecs.DOUBLE,
|
ByteBufCodecs.DOUBLE,
|
||||||
FireMessage::power,
|
FireKeyMessage::power,
|
||||||
ByteBufCodecs.BOOL,
|
ByteBufCodecs.BOOL,
|
||||||
FireMessage::zoom,
|
FireKeyMessage::zoom,
|
||||||
FireMessage::new
|
FireKeyMessage::new
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
public static void handler(FireMessage message, final IPayloadContext context) {
|
public static void handler(FireKeyMessage message, final IPayloadContext context) {
|
||||||
pressAction(context.player(), message.msgType, message.power, message.zoom);
|
pressAction(context.player(), message.msgType, message.power, message.zoom);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -51,25 +54,24 @@ public record FireMessage(int msgType, double power, boolean zoom) implements Cu
|
||||||
|
|
||||||
handleGunBolt(player, stack);
|
handleGunBolt(player, stack);
|
||||||
|
|
||||||
var cap = player.getData(ModAttachments.PLAYER_VARIABLE).watch();
|
|
||||||
|
|
||||||
if (type == 0) {
|
if (type == 0) {
|
||||||
if (data.reload.prepareTimer.get() == 0 && data.reloading() && data.ammo.get() > 0) {
|
if (data.reload.prepareTimer.get() == 0 && data.reloading() && data.ammo.get() > 0) {
|
||||||
data.forceStop.set(true);
|
data.forceStop.set(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
cap.edit = false;
|
player.getData(ModAttachments.PLAYER_VARIABLE).modify(player, cap -> cap.edit = false);
|
||||||
|
|
||||||
// 按下开火
|
// 按下开火
|
||||||
if (!(stack.getItem() instanceof ReleaseSpecialWeapon releaseSpecialWeapon)) return;
|
if (stack.getItem() instanceof ReleaseSpecialWeapon releaseSpecialWeapon) {
|
||||||
releaseSpecialWeapon.fireOnPress(player, data, zoom);
|
releaseSpecialWeapon.fireOnPress(player, data, zoom);
|
||||||
|
}
|
||||||
} else if (type == 1) {
|
} else if (type == 1) {
|
||||||
// 松开开火
|
// 松开开火
|
||||||
if (stack.getItem() instanceof ReleaseSpecialWeapon releaseSpecialWeapon) {
|
if (stack.getItem() instanceof ReleaseSpecialWeapon releaseSpecialWeapon) {
|
||||||
releaseSpecialWeapon.fireOnRelease(player, data, power, zoom);
|
releaseSpecialWeapon.fireOnRelease(player, data, power, zoom);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cap.sync(player);
|
|
||||||
data.save();
|
data.save();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue