From 6910646584045ad887d89e256f4f83e9e8765b2e Mon Sep 17 00:00:00 2001 From: Light_Quanta Date: Tue, 8 Apr 2025 10:50:39 +0800 Subject: [PATCH] =?UTF-8?q?=E6=AD=A3=E7=A1=AE=E5=AE=9E=E7=8E=B0PlayerVaria?= =?UTF-8?q?bles=EF=BC=8C=E4=BC=98=E5=8C=96=E5=90=8C=E6=AD=A5=E6=9C=BA?= =?UTF-8?q?=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/atsuishio/superbwarfare/Mod.java | 1 + .../superbwarfare/block/JumpPadBlock.java | 12 +- .../capability/player/PlayerVariable.java | 165 ++++++++++-------- .../superbwarfare/client/ClickHandler.java | 7 +- .../client/overlay/AmmoBarOverlay.java | 9 +- .../client/overlay/AmmoCountOverlay.java | 6 +- .../client/overlay/AmmoOverlay.java | 5 +- .../client/overlay/CrossHairOverlay.java | 5 +- .../overlay/GrenadeLauncherOverlay.java | 6 +- .../client/overlay/HandsomeFrameOverlay.java | 5 +- .../client/overlay/HelicopterHudOverlay.java | 5 +- .../client/overlay/JavelinHudOverlay.java | 5 +- .../superbwarfare/entity/TargetEntity.java | 6 +- .../event/ClientEventHandler.java | 50 +++--- .../superbwarfare/event/GunEventHandler.java | 21 +-- .../event/LivingEventHandler.java | 34 ++-- .../event/PlayerEventHandler.java | 50 +++--- .../superbwarfare/init/ModAttachments.java | 19 ++ .../superbwarfare/init/ModCapabilities.java | 6 +- .../superbwarfare/item/BeamTest.java | 2 +- .../item/common/ammo/AmmoSupplierItem.java | 11 +- .../item/common/ammo/box/AmmoBox.java | 9 +- .../superbwarfare/item/gun/GunItem.java | 29 ++- .../item/gun/handgun/Trachelium.java | 5 +- .../item/gun/heavy/Ntw20Item.java | 5 +- .../item/gun/launcher/JavelinItem.java | 4 +- .../item/gun/launcher/M79Item.java | 10 +- .../item/gun/launcher/RpgItem.java | 9 +- .../item/gun/launcher/SecondaryCataclysm.java | 5 +- .../item/gun/machinegun/RpkItem.java | 5 +- .../item/gun/rifle/AK12Item.java | 5 +- .../item/gun/rifle/AK47Item.java | 5 +- .../item/gun/rifle/Hk416Item.java | 5 +- .../superbwarfare/item/gun/rifle/M4Item.java | 5 +- .../item/gun/rifle/Mk14Item.java | 5 +- .../item/gun/rifle/Qbz95Item.java | 5 +- .../item/gun/smg/VectorItem.java | 5 +- .../item/gun/sniper/SvdItem.java | 6 +- .../item/gun/special/BocekItem.java | 18 +- .../item/gun/special/TaserItem.java | 10 +- .../receive/PlayerVariablesSyncMessage.java | 23 +-- .../network/message/send/BreathMessage.java | 11 +- .../message/send/DoubleJumpMessage.java | 8 +- .../network/message/send/EditModeMessage.java | 9 +- .../network/message/send/FireMessage.java | 30 ++-- .../network/message/send/ReloadMessage.java | 13 +- .../network/message/send/ShootMessage.java | 12 +- .../network/message/send/ZoomMessage.java | 22 ++- .../superbwarfare/tools/AmmoType.java | 11 +- .../superbwarfare/tools/GunsTool.java | 27 +-- 50 files changed, 333 insertions(+), 413 deletions(-) create mode 100644 src/main/java/com/atsuishio/superbwarfare/init/ModAttachments.java diff --git a/src/main/java/com/atsuishio/superbwarfare/Mod.java b/src/main/java/com/atsuishio/superbwarfare/Mod.java index c242616d0..71cbb21fe 100644 --- a/src/main/java/com/atsuishio/superbwarfare/Mod.java +++ b/src/main/java/com/atsuishio/superbwarfare/Mod.java @@ -56,6 +56,7 @@ public class Mod { ModArmorMaterials.MATERIALS.register(bus); ModAttributes.ATTRIBUTES.register(bus); ModCriteriaTriggers.REGISTRY.register(bus); + ModAttachments.ATTACHMENT_TYPES.register(bus); // bus.addListener(this::onCommonSetup); bus.addListener(this::onClientSetup); diff --git a/src/main/java/com/atsuishio/superbwarfare/block/JumpPadBlock.java b/src/main/java/com/atsuishio/superbwarfare/block/JumpPadBlock.java index 9eb34239b..a2fb3dff3 100644 --- a/src/main/java/com/atsuishio/superbwarfare/block/JumpPadBlock.java +++ b/src/main/java/com/atsuishio/superbwarfare/block/JumpPadBlock.java @@ -1,8 +1,8 @@ package com.atsuishio.superbwarfare.block; -import com.atsuishio.superbwarfare.init.ModCapabilities; import com.atsuishio.superbwarfare.entity.TargetEntity; import com.atsuishio.superbwarfare.entity.vehicle.base.CannonEntity; +import com.atsuishio.superbwarfare.init.ModAttachments; import com.atsuishio.superbwarfare.init.ModSounds; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; @@ -124,10 +124,10 @@ public class JumpPadBlock extends Block { level.playLocalSound(pos.getX(), pos.getY(), pos.getZ(), ModSounds.JUMP.get(), SoundSource.BLOCKS, 1, 1, false); } - var capability = entity.getCapability(ModCapabilities.PLAYER_VARIABLE); - if (capability != null) { - capability.playerDoubleJump = true; - capability.syncPlayerVariables(entity); - } + var capability = entity.getData(ModAttachments.PLAYER_VARIABLE).watch(); + capability.playerDoubleJump = true; + + entity.setData(ModAttachments.PLAYER_VARIABLE, capability); + capability.sync(entity); } } diff --git a/src/main/java/com/atsuishio/superbwarfare/capability/player/PlayerVariable.java b/src/main/java/com/atsuishio/superbwarfare/capability/player/PlayerVariable.java index a4ed89654..9133ba013 100644 --- a/src/main/java/com/atsuishio/superbwarfare/capability/player/PlayerVariable.java +++ b/src/main/java/com/atsuishio/superbwarfare/capability/player/PlayerVariable.java @@ -1,21 +1,27 @@ package com.atsuishio.superbwarfare.capability.player; import com.atsuishio.superbwarfare.Mod; -import com.atsuishio.superbwarfare.init.ModCapabilities; +import com.atsuishio.superbwarfare.init.ModAttachments; import com.atsuishio.superbwarfare.network.message.receive.PlayerVariablesSyncMessage; import com.atsuishio.superbwarfare.tools.AmmoType; +import net.minecraft.core.HolderLookup; import net.minecraft.nbt.CompoundTag; -import net.minecraft.nbt.Tag; import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.entity.Entity; import net.neoforged.bus.api.SubscribeEvent; import net.neoforged.fml.common.EventBusSubscriber; +import net.neoforged.neoforge.common.util.INBTSerializable; import net.neoforged.neoforge.event.entity.player.PlayerEvent; import net.neoforged.neoforge.network.PacketDistributor; +import org.jetbrains.annotations.NotNull; + +import javax.annotation.ParametersAreNonnullByDefault; + -// TODO 在退出世界时正确持久化弹药数量 @EventBusSubscriber(modid = Mod.MODID) -public class PlayerVariable { +public class PlayerVariable implements INBTSerializable { + private PlayerVariable old = null; + public boolean zoom = false; public boolean holdFire = false; public int rifleAmmo = 0; @@ -34,12 +40,29 @@ public class PlayerVariable { public boolean breathExhaustion = false; public boolean edit = false; - public void syncPlayerVariables(Entity entity) { + public void sync(Entity entity) { + if (!entity.hasData(ModAttachments.PLAYER_VARIABLE)) return; + + var newVariable = entity.getData(ModAttachments.PLAYER_VARIABLE); + if (old != null && old.equals(newVariable)) return; + if (entity instanceof ServerPlayer) { - PacketDistributor.sendToAllPlayers(new PlayerVariablesSyncMessage(entity.getId(), this.writeToNBT())); + PacketDistributor.sendToAllPlayers(new PlayerVariablesSyncMessage(entity.getId(), newVariable.writeToNBT())); } } + @SubscribeEvent + public static void onPlayerLoggedIn(PlayerEvent.PlayerLoggedInEvent event) { + if (!(event.getEntity() instanceof ServerPlayer player)) return; + + PacketDistributor.sendToPlayer(player, new PlayerVariablesSyncMessage(player.getId(), player.getData(ModAttachments.PLAYER_VARIABLE).writeToNBT())); + } + + public PlayerVariable watch() { + this.old = this.copy(); + return this; + } + public CompoundTag writeToNBT() { CompoundTag nbt = new CompoundTag(); nbt.putBoolean("Zoom", zoom); @@ -63,86 +86,92 @@ public class PlayerVariable { return nbt; } - public PlayerVariable readFromNBT(Tag tag) { - CompoundTag nbt = (CompoundTag) tag; - - zoom = nbt.getBoolean("Zoom"); - holdFire = nbt.getBoolean("HoldFire"); + public PlayerVariable readFromNBT(CompoundTag tag) { + zoom = tag.getBoolean("Zoom"); + holdFire = tag.getBoolean("HoldFire"); for (var type : AmmoType.values()) { - type.set(this, type.get(nbt)); + type.set(this, type.get(tag)); } - bowPullHold = nbt.getBoolean("BowPullHold"); - bowPull = nbt.getBoolean("BowPull"); - playerDoubleJump = nbt.getBoolean("DoubleJump"); - tacticalSprint = nbt.getBoolean("TacticalSprint"); - tacticalSprintTime = nbt.getInt("TacticalSprintTime"); - tacticalSprintExhaustion = nbt.getBoolean("TacticalSprintExhaustion"); - breath = nbt.getBoolean("Breath"); - breathTime = nbt.getInt("BreathTime"); - breathExhaustion = nbt.getBoolean("BreathExhaustion"); - edit = nbt.getBoolean("EditMode"); + bowPullHold = tag.getBoolean("BowPullHold"); + bowPull = tag.getBoolean("BowPull"); + playerDoubleJump = tag.getBoolean("DoubleJump"); + tacticalSprint = tag.getBoolean("TacticalSprint"); + tacticalSprintTime = tag.getInt("TacticalSprintTime"); + tacticalSprintExhaustion = tag.getBoolean("TacticalSprintExhaustion"); + breath = tag.getBoolean("Breath"); + breathTime = tag.getInt("BreathTime"); + breathExhaustion = tag.getBoolean("BreathExhaustion"); + edit = tag.getBoolean("EditMode"); return this; } - @SubscribeEvent - public static void onPlayerLoggedInSyncPlayerVariables(PlayerEvent.PlayerLoggedInEvent event) { - if (event.getEntity().level().isClientSide()) return; + public PlayerVariable copy() { + var clone = new PlayerVariable(); - var player = event.getEntity(); - var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE, null); - if (cap != null) cap.syncPlayerVariables(player); + clone.zoom = this.zoom; + clone.holdFire = this.holdFire; + clone.rifleAmmo = this.rifleAmmo; + clone.handgunAmmo = this.handgunAmmo; + clone.shotgunAmmo = this.shotgunAmmo; + clone.sniperAmmo = this.sniperAmmo; + clone.heavyAmmo = this.heavyAmmo; + clone.bowPullHold = this.bowPullHold; + clone.bowPull = this.bowPull; + clone.playerDoubleJump = this.playerDoubleJump; + clone.tacticalSprint = this.tacticalSprint; + clone.tacticalSprintTime = this.tacticalSprintTime; + clone.tacticalSprintExhaustion = this.tacticalSprintExhaustion; + clone.breath = this.breath; + clone.breathTime = this.breathTime; + clone.breathExhaustion = this.breathExhaustion; + clone.edit = this.edit; + + return clone; } - @SubscribeEvent - public static void onPlayerRespawnedSyncPlayerVariables(PlayerEvent.PlayerRespawnEvent event) { - if (event.getEntity().level().isClientSide()) return; + @Override + public boolean equals(Object obj) { + if (!(obj instanceof PlayerVariable other)) return false; - var player = event.getEntity(); - var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE, null); - if (cap != null) cap.syncPlayerVariables(player); - } - - @SubscribeEvent - public static void onPlayerChangedDimensionSyncPlayerVariables(PlayerEvent.PlayerChangedDimensionEvent event) { - if (event.getEntity().level().isClientSide()) return; - - var player = event.getEntity(); - var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE, null); - if (cap != null) cap.syncPlayerVariables(player); + return zoom == other.zoom + && holdFire == other.holdFire + && rifleAmmo == other.rifleAmmo + && handgunAmmo == other.handgunAmmo + && shotgunAmmo == other.shotgunAmmo + && sniperAmmo == other.sniperAmmo + && heavyAmmo == other.heavyAmmo + && bowPullHold == other.bowPullHold + && bowPull == other.bowPull + && playerDoubleJump == other.playerDoubleJump + && tacticalSprint == other.tacticalSprint +// && tacticalSprintTime == other.tacticalSprintTime + && tacticalSprintExhaustion == other.tacticalSprintExhaustion + && breath == other.breath +// && breathTime == other.breathTime + && breathExhaustion == other.breathExhaustion + && edit == other.edit; } @SubscribeEvent public static void clonePlayer(PlayerEvent.Clone event) { event.getOriginal().revive(); - var original = event.getOriginal().getCapability(ModCapabilities.PLAYER_VARIABLE, null); - var clone = event.getEntity().getCapability(ModCapabilities.PLAYER_VARIABLE, null); - if (clone == null || original == null) return; - - clone.zoom = original.zoom; - clone.holdFire = original.holdFire; - clone.rifleAmmo = original.rifleAmmo; - clone.handgunAmmo = original.handgunAmmo; - clone.shotgunAmmo = original.shotgunAmmo; - clone.sniperAmmo = original.sniperAmmo; - clone.heavyAmmo = original.heavyAmmo; - clone.bowPullHold = original.bowPullHold; - clone.bowPull = original.bowPull; - clone.playerDoubleJump = original.playerDoubleJump; - clone.tacticalSprint = original.tacticalSprint; - clone.tacticalSprintTime = original.tacticalSprintTime; - clone.tacticalSprintExhaustion = original.tacticalSprintExhaustion; - clone.breath = original.breath; - clone.breathTime = original.breathTime; - clone.breathExhaustion = original.breathExhaustion; - clone.edit = original.edit; - + var original = event.getOriginal().getData(ModAttachments.PLAYER_VARIABLE); if (event.getEntity().level().isClientSide()) return; - var player = event.getEntity(); - var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE, null); - if (cap != null) cap.syncPlayerVariables(player); + event.getEntity().setData(ModAttachments.PLAYER_VARIABLE, original.copy()); + } + + @Override + public CompoundTag serializeNBT(HolderLookup.@NotNull Provider provider) { + return writeToNBT(); + } + + @Override + @ParametersAreNonnullByDefault + public void deserializeNBT(HolderLookup.Provider provider, CompoundTag nbt) { + readFromNBT(nbt); } } diff --git a/src/main/java/com/atsuishio/superbwarfare/client/ClickHandler.java b/src/main/java/com/atsuishio/superbwarfare/client/ClickHandler.java index 7c71cdd7f..dff220adb 100644 --- a/src/main/java/com/atsuishio/superbwarfare/client/ClickHandler.java +++ b/src/main/java/com/atsuishio/superbwarfare/client/ClickHandler.java @@ -1,6 +1,5 @@ package com.atsuishio.superbwarfare.client; -import com.atsuishio.superbwarfare.init.ModCapabilities; import com.atsuishio.superbwarfare.config.client.ReloadConfig; import com.atsuishio.superbwarfare.entity.MortarEntity; import com.atsuishio.superbwarfare.entity.vehicle.base.ArmedVehicleEntity; @@ -224,8 +223,7 @@ public class ClickHandler { PacketDistributor.sendToServer(new EditModeMessage(0)); } - var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); - if (cap != null && cap.edit) { + if (player.getData(ModAttachments.PLAYER_VARIABLE).edit) { if (!(stack.getItem() instanceof GunItem gunItem)) return; if (ModKeyMappings.EDIT_GRIP.getKeyModifier().isActive(KeyConflictContext.IN_GAME)) { if (key == ModKeyMappings.EDIT_GRIP.getKey().getValue() && gunItem.hasCustomGrip(stack)) { @@ -402,8 +400,7 @@ public class ClickHandler { return; } - var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); - if (cap != null && cap.playerDoubleJump) { + if (player.getData(ModAttachments.PLAYER_VARIABLE).playerDoubleJump) { player.setDeltaMovement(new Vec3(player.getLookAngle().x, 0.8, player.getLookAngle().z)); level.playLocalSound(x, y, z, ModSounds.DOUBLE_JUMP.get(), SoundSource.BLOCKS, 1, 1, false); diff --git a/src/main/java/com/atsuishio/superbwarfare/client/overlay/AmmoBarOverlay.java b/src/main/java/com/atsuishio/superbwarfare/client/overlay/AmmoBarOverlay.java index 564aa885d..4f1b0cc43 100644 --- a/src/main/java/com/atsuishio/superbwarfare/client/overlay/AmmoBarOverlay.java +++ b/src/main/java/com/atsuishio/superbwarfare/client/overlay/AmmoBarOverlay.java @@ -1,10 +1,9 @@ package com.atsuishio.superbwarfare.client.overlay; import com.atsuishio.superbwarfare.Mod; -import com.atsuishio.superbwarfare.init.ModCapabilities; -import com.atsuishio.superbwarfare.capability.player.PlayerVariable; import com.atsuishio.superbwarfare.config.client.DisplayConfig; import com.atsuishio.superbwarfare.entity.vehicle.base.ArmedVehicleEntity; +import com.atsuishio.superbwarfare.init.ModAttachments; import com.atsuishio.superbwarfare.init.ModItems; import com.atsuishio.superbwarfare.init.ModKeyMappings; import com.atsuishio.superbwarfare.init.ModTags; @@ -47,8 +46,7 @@ public class AmmoBarOverlay implements LayeredDraw.Layer { ItemStack stack = player.getMainHandItem(); if (stack.getItem() == ModItems.MINIGUN.get()) { - var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); - return cap != null ? cap.rifleAmmo : 0; + return player.getData(ModAttachments.PLAYER_VARIABLE).rifleAmmo; } if (stack.getItem() == ModItems.BOCEK.get()) { @@ -65,8 +63,7 @@ public class AmmoBarOverlay implements LayeredDraw.Layer { return ""; } - var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); - if (cap == null) cap = new PlayerVariable(); + var cap = player.getData(ModAttachments.PLAYER_VARIABLE); if (!hasCreativeAmmo()) { var data = GunData.from(stack); if (stack.is(ModTags.Items.LAUNCHER) || stack.getItem() == ModItems.TASER.get()) { diff --git a/src/main/java/com/atsuishio/superbwarfare/client/overlay/AmmoCountOverlay.java b/src/main/java/com/atsuishio/superbwarfare/client/overlay/AmmoCountOverlay.java index 6157a840d..d0b89555a 100644 --- a/src/main/java/com/atsuishio/superbwarfare/client/overlay/AmmoCountOverlay.java +++ b/src/main/java/com/atsuishio/superbwarfare/client/overlay/AmmoCountOverlay.java @@ -1,10 +1,9 @@ package com.atsuishio.superbwarfare.client.overlay; import com.atsuishio.superbwarfare.Mod; -import com.atsuishio.superbwarfare.init.ModCapabilities; -import com.atsuishio.superbwarfare.capability.player.PlayerVariable; import com.atsuishio.superbwarfare.component.ModDataComponents; import com.atsuishio.superbwarfare.entity.vehicle.base.ArmedVehicleEntity; +import com.atsuishio.superbwarfare.init.ModAttachments; import com.atsuishio.superbwarfare.init.ModItems; import com.atsuishio.superbwarfare.item.common.ammo.AmmoSupplierItem; import com.atsuishio.superbwarfare.tools.AmmoType; @@ -87,8 +86,7 @@ public class AmmoCountOverlay implements LayeredDraw.Layer { var yOffset = (-h - AmmoType.values().length * fontHeight) / 2f; // 渲染总弹药数量 - var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); - if (cap == null) cap = new PlayerVariable(); + var cap = player.getData(ModAttachments.PLAYER_VARIABLE); var font = Minecraft.getInstance().font; for (var type : AmmoType.values()) { diff --git a/src/main/java/com/atsuishio/superbwarfare/client/overlay/AmmoOverlay.java b/src/main/java/com/atsuishio/superbwarfare/client/overlay/AmmoOverlay.java index 6e3aea8a8..96c6535f5 100644 --- a/src/main/java/com/atsuishio/superbwarfare/client/overlay/AmmoOverlay.java +++ b/src/main/java/com/atsuishio/superbwarfare/client/overlay/AmmoOverlay.java @@ -1,9 +1,9 @@ package com.atsuishio.superbwarfare.client.overlay; import com.atsuishio.superbwarfare.Mod; -import com.atsuishio.superbwarfare.init.ModCapabilities; import com.atsuishio.superbwarfare.component.ModDataComponents; import com.atsuishio.superbwarfare.entity.vehicle.base.ArmedVehicleEntity; +import com.atsuishio.superbwarfare.init.ModAttachments; import com.atsuishio.superbwarfare.init.ModItems; import com.atsuishio.superbwarfare.item.common.ammo.AmmoSupplierItem; import com.atsuishio.superbwarfare.item.common.ammo.box.AmmoBoxInfo; @@ -82,8 +82,7 @@ public class AmmoOverlay { var yOffset = (-h - AmmoType.values().length * fontHeight) / 2f; // 渲染总弹药数量 - var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE, null); - if (cap == null) return; + var cap = player.getData(ModAttachments.PLAYER_VARIABLE); var font = Minecraft.getInstance().font; for (var type : AmmoType.values()) { diff --git a/src/main/java/com/atsuishio/superbwarfare/client/overlay/CrossHairOverlay.java b/src/main/java/com/atsuishio/superbwarfare/client/overlay/CrossHairOverlay.java index df97a72b0..8f3480206 100644 --- a/src/main/java/com/atsuishio/superbwarfare/client/overlay/CrossHairOverlay.java +++ b/src/main/java/com/atsuishio/superbwarfare/client/overlay/CrossHairOverlay.java @@ -1,11 +1,11 @@ package com.atsuishio.superbwarfare.client.overlay; import com.atsuishio.superbwarfare.Mod; -import com.atsuishio.superbwarfare.init.ModCapabilities; import com.atsuishio.superbwarfare.config.client.DisplayConfig; import com.atsuishio.superbwarfare.entity.vehicle.Ah6Entity; import com.atsuishio.superbwarfare.entity.vehicle.base.ArmedVehicleEntity; import com.atsuishio.superbwarfare.event.ClientEventHandler; +import com.atsuishio.superbwarfare.init.ModAttachments; import com.atsuishio.superbwarfare.init.ModItems; import com.atsuishio.superbwarfare.init.ModTags; import com.atsuishio.superbwarfare.item.gun.data.GunData; @@ -54,8 +54,7 @@ public class CrossHairOverlay { return; } - var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); - if (cap != null && cap.edit) return; + if (player.getData(ModAttachments.PLAYER_VARIABLE).edit) return; if (!player.getMainHandItem().is(ModTags.Items.GUN) || (player.getVehicle() instanceof ArmedVehicleEntity iArmedVehicle && iArmedVehicle.banHand(player))) return; diff --git a/src/main/java/com/atsuishio/superbwarfare/client/overlay/GrenadeLauncherOverlay.java b/src/main/java/com/atsuishio/superbwarfare/client/overlay/GrenadeLauncherOverlay.java index c636376ba..9a097d17c 100644 --- a/src/main/java/com/atsuishio/superbwarfare/client/overlay/GrenadeLauncherOverlay.java +++ b/src/main/java/com/atsuishio/superbwarfare/client/overlay/GrenadeLauncherOverlay.java @@ -1,10 +1,10 @@ package com.atsuishio.superbwarfare.client.overlay; import com.atsuishio.superbwarfare.Mod; -import com.atsuishio.superbwarfare.init.ModCapabilities; import com.atsuishio.superbwarfare.entity.vehicle.base.ArmedVehicleEntity; import com.atsuishio.superbwarfare.entity.vehicle.base.CannonEntity; import com.atsuishio.superbwarfare.event.ClientEventHandler; +import com.atsuishio.superbwarfare.init.ModAttachments; import com.atsuishio.superbwarfare.init.ModItems; import com.mojang.blaze3d.platform.GlStateManager; import com.mojang.blaze3d.systems.RenderSystem; @@ -28,8 +28,8 @@ public class GrenadeLauncherOverlay { Player player = Minecraft.getInstance().player; if (player == null) return; - var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); - if (cap == null || cap.edit) return; + var cap = player.getData(ModAttachments.PLAYER_VARIABLE); + if (cap.edit) return; if (player.getVehicle() instanceof ArmedVehicleEntity iArmedVehicle && iArmedVehicle.banHand(player)) return; diff --git a/src/main/java/com/atsuishio/superbwarfare/client/overlay/HandsomeFrameOverlay.java b/src/main/java/com/atsuishio/superbwarfare/client/overlay/HandsomeFrameOverlay.java index a81dd5b9b..01b58ece3 100644 --- a/src/main/java/com/atsuishio/superbwarfare/client/overlay/HandsomeFrameOverlay.java +++ b/src/main/java/com/atsuishio/superbwarfare/client/overlay/HandsomeFrameOverlay.java @@ -1,10 +1,10 @@ package com.atsuishio.superbwarfare.client.overlay; import com.atsuishio.superbwarfare.Mod; -import com.atsuishio.superbwarfare.init.ModCapabilities; import com.atsuishio.superbwarfare.client.RenderHelper; import com.atsuishio.superbwarfare.entity.vehicle.base.ArmedVehicleEntity; import com.atsuishio.superbwarfare.event.ClientEventHandler; +import com.atsuishio.superbwarfare.init.ModAttachments; import com.atsuishio.superbwarfare.init.ModPerks; import com.atsuishio.superbwarfare.item.gun.GunItem; import com.atsuishio.superbwarfare.item.gun.data.GunData; @@ -43,8 +43,7 @@ public class HandsomeFrameOverlay { if (player != null) { ItemStack stack = player.getMainHandItem(); - var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); - if (cap == null || cap.edit) return; + if (player.getData(ModAttachments.PLAYER_VARIABLE).edit) return; if (player.getVehicle() instanceof ArmedVehicleEntity iArmedVehicle && iArmedVehicle.banHand(player)) return; diff --git a/src/main/java/com/atsuishio/superbwarfare/client/overlay/HelicopterHudOverlay.java b/src/main/java/com/atsuishio/superbwarfare/client/overlay/HelicopterHudOverlay.java index 1b9b50ac6..098b50e9b 100644 --- a/src/main/java/com/atsuishio/superbwarfare/client/overlay/HelicopterHudOverlay.java +++ b/src/main/java/com/atsuishio/superbwarfare/client/overlay/HelicopterHudOverlay.java @@ -1,7 +1,6 @@ package com.atsuishio.superbwarfare.client.overlay; import com.atsuishio.superbwarfare.Mod; -import com.atsuishio.superbwarfare.init.ModCapabilities; import com.atsuishio.superbwarfare.client.RenderHelper; import com.atsuishio.superbwarfare.entity.vehicle.Ah6Entity; import com.atsuishio.superbwarfare.entity.vehicle.base.HelicopterEntity; @@ -9,6 +8,7 @@ import com.atsuishio.superbwarfare.entity.vehicle.base.MobileVehicleEntity; import com.atsuishio.superbwarfare.entity.vehicle.base.VehicleEntity; import com.atsuishio.superbwarfare.entity.vehicle.base.WeaponVehicleEntity; import com.atsuishio.superbwarfare.event.ClientEventHandler; +import com.atsuishio.superbwarfare.init.ModAttachments; import com.atsuishio.superbwarfare.tools.FormatTool; import com.atsuishio.superbwarfare.tools.InventoryTool; import com.mojang.blaze3d.platform.GlStateManager; @@ -59,8 +59,7 @@ public class HelicopterHudOverlay { if (player == null) return; - var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); - if (cap == null || cap.edit) return; + if (player.getData(ModAttachments.PLAYER_VARIABLE).edit) return; if (player.getVehicle() instanceof HelicopterEntity iHelicopterEntity && player.getVehicle() instanceof MobileVehicleEntity mobileVehicle && iHelicopterEntity.isDriver(player) && player.getVehicle() instanceof WeaponVehicleEntity weaponVehicle) { poseStack.pushPose(); diff --git a/src/main/java/com/atsuishio/superbwarfare/client/overlay/JavelinHudOverlay.java b/src/main/java/com/atsuishio/superbwarfare/client/overlay/JavelinHudOverlay.java index fb4c4a524..97f6c9ad1 100644 --- a/src/main/java/com/atsuishio/superbwarfare/client/overlay/JavelinHudOverlay.java +++ b/src/main/java/com/atsuishio/superbwarfare/client/overlay/JavelinHudOverlay.java @@ -1,10 +1,10 @@ package com.atsuishio.superbwarfare.client.overlay; import com.atsuishio.superbwarfare.Mod; -import com.atsuishio.superbwarfare.init.ModCapabilities; import com.atsuishio.superbwarfare.client.RenderHelper; import com.atsuishio.superbwarfare.entity.vehicle.base.ArmedVehicleEntity; import com.atsuishio.superbwarfare.event.ClientEventHandler; +import com.atsuishio.superbwarfare.init.ModAttachments; import com.atsuishio.superbwarfare.init.ModItems; import com.atsuishio.superbwarfare.item.gun.data.GunData; import com.atsuishio.superbwarfare.tools.EntityFindUtil; @@ -49,8 +49,7 @@ public class JavelinHudOverlay { if (player != null) { ItemStack stack = player.getMainHandItem(); - var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); - if (cap == null || cap.edit) return; + if (player.getData(ModAttachments.PLAYER_VARIABLE).edit) return; if (player.getVehicle() instanceof ArmedVehicleEntity iArmedVehicle && iArmedVehicle.banHand(player)) return; diff --git a/src/main/java/com/atsuishio/superbwarfare/entity/TargetEntity.java b/src/main/java/com/atsuishio/superbwarfare/entity/TargetEntity.java index 3b760772a..3a391073a 100644 --- a/src/main/java/com/atsuishio/superbwarfare/entity/TargetEntity.java +++ b/src/main/java/com/atsuishio/superbwarfare/entity/TargetEntity.java @@ -1,7 +1,7 @@ package com.atsuishio.superbwarfare.entity; import com.atsuishio.superbwarfare.Mod; -import com.atsuishio.superbwarfare.init.ModCapabilities; +import com.atsuishio.superbwarfare.init.ModAttachments; import com.atsuishio.superbwarfare.init.ModItems; import com.atsuishio.superbwarfare.init.ModSounds; import com.atsuishio.superbwarfare.tools.FormatTool; @@ -145,9 +145,7 @@ public class TargetEntity extends LivingEntity implements GeoEntity { player.addItem(new ItemStack(ModItems.TARGET_DEPLOYER.get())); } } else { - var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE, null); - - if (cap != null && !cap.zoom) { + if (!player.getData(ModAttachments.PLAYER_VARIABLE).zoom) { this.lookAt(EntityAnchorArgument.Anchor.EYES, new Vec3((player.getX()), this.getY(), (player.getZ()))); this.setXRot(0); this.xRotO = this.getXRot(); diff --git a/src/main/java/com/atsuishio/superbwarfare/event/ClientEventHandler.java b/src/main/java/com/atsuishio/superbwarfare/event/ClientEventHandler.java index ae49f1aa5..39b491167 100644 --- a/src/main/java/com/atsuishio/superbwarfare/event/ClientEventHandler.java +++ b/src/main/java/com/atsuishio/superbwarfare/event/ClientEventHandler.java @@ -1,7 +1,6 @@ package com.atsuishio.superbwarfare.event; import com.atsuishio.superbwarfare.Mod; -import com.atsuishio.superbwarfare.init.ModCapabilities; import com.atsuishio.superbwarfare.client.ClickHandler; import com.atsuishio.superbwarfare.config.client.DisplayConfig; import com.atsuishio.superbwarfare.entity.vehicle.*; @@ -305,17 +304,18 @@ public class ClientEventHandler { public static void handleGunMelee(Player player, ItemStack stack) { if (stack.getItem() instanceof GunItem gunItem) { var data = GunData.from(stack); - var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); - if (gunItem.hasMeleeAttack(stack) && gunMelee == 0 && drawTime < 0.01 + var cap = player.getData(ModAttachments.PLAYER_VARIABLE); + if (gunItem.hasMeleeAttack(stack) + && gunMelee == 0 + && drawTime < 0.01 && ModKeyMappings.MELEE.isDown() && !(player.getVehicle() instanceof ArmedVehicleEntity iArmedVehicle && iArmedVehicle.banHand(player)) && !holdFireVehicle && !notInGame() - && cap != null && !cap.edit + && !cap.edit && !(data.reload.normal() || data.reload.empty()) && !data.reloading() - && !data.charging() - && !player.getCooldowns().isOnCooldown(stack.getItem()) + && !data.charging() && !player.getCooldowns().isOnCooldown(stack.getItem()) ) { gunMelee = 36; cantFireTime = 40; @@ -464,7 +464,7 @@ public class ClientEventHandler { revolverPreTime = Mth.clamp(revolverPreTime - 1.2 * times, 0, 1); } - var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); + var cap = player.getData(ModAttachments.PLAYER_VARIABLE); if ((holdFire || burstFireAmount > 0) && !(player.getVehicle() instanceof ArmedVehicleEntity iArmedVehicle && iArmedVehicle.banHand(player)) @@ -472,7 +472,7 @@ public class ClientEventHandler { && (stack.is(ModTags.Items.NORMAL_GUN) && cantFireTime == 0 && drawTime < 0.01 - && cap != null && !cap.edit + && !cap.edit && !notInGame() && (!(data.reload.normal() || data.reload.empty()) && !data.reloading() @@ -485,7 +485,7 @@ public class ClientEventHandler { && !player.isSprinting() && tag.getDouble("overheat") == 0 && !player.getCooldowns().isOnCooldown(stack.getItem()) && miniGunRot >= 20 - && (cap != null && cap.rifleAmmo > 0 || InventoryTool.hasCreativeAmmoBox(player)) + && (cap.rifleAmmo > 0 || InventoryTool.hasCreativeAmmoBox(player)) ))) { if (mode == 0) { if (clientTimer.getProgress() == 0) { @@ -594,12 +594,11 @@ public class ClientEventHandler { revolverPreTime = 0; revolverWheelPreTime = 0; - playGunClientSounds(player, tag); + playGunClientSounds(player); handleClientShoot(); } } else if (stack.is(ModItems.MINIGUN.get())) { - var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); - if (cap != null && cap.rifleAmmo > 0 || InventoryTool.hasCreativeAmmoBox(player)) { + if (player.getData(ModAttachments.PLAYER_VARIABLE).rifleAmmo > 0 || InventoryTool.hasCreativeAmmoBox(player)) { var perk = data.perk.get(Perk.Type.AMMO); float pitch = tag.getDouble("heat") <= 40 ? 1 : (float) (1 - 0.025 * Math.abs(40 - tag.getDouble("heat"))); @@ -661,7 +660,7 @@ public class ClientEventHandler { shakeType = 2 * (Math.random() - 0.5); } - public static void playGunClientSounds(Player player, final CompoundTag tag) { + public static void playGunClientSounds(Player player) { ItemStack stack = player.getMainHandItem(); if (!(stack.getItem() instanceof GunItem gunItem)) return; @@ -849,9 +848,9 @@ public class ClientEventHandler { double customWeight = data.customWeight(); - var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE, null); + var cap = player.getData(ModAttachments.PLAYER_VARIABLE); - if (cap != null && !cap.breath && cap.zoom) { + if (!cap.breath && cap.zoom) { float newPitch = (float) (player.getXRot() - 0.01f * Mth.sin((float) (0.03 * player.tickCount)) * pose * Mth.nextDouble(RandomSource.create(), 0.1, 1) * times * sway * (1 - 0.03 * customWeight)); player.setXRot(newPitch); player.xRotO = player.getXRot(); @@ -1017,8 +1016,7 @@ public class ClientEventHandler { onGround = 0.001; } - var cap = entity.getCapability(ModCapabilities.PLAYER_VARIABLE); - if (cap != null && !cap.edit) { + if (!entity.getData(ModAttachments.PLAYER_VARIABLE).edit) { if (Minecraft.getInstance().options.keyUp.isDown() && firePosTimer == 0) { moveRotZ = Mth.lerp(0.2f * times, moveRotZ, 0.14) * (1 - zoomTime); } else { @@ -1073,12 +1071,12 @@ public class ClientEventHandler { double weight = data.weight(); double speed = 1.5 - (0.07 * weight); - var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); + var cap = player.getData(ModAttachments.PLAYER_VARIABLE); if (zoom && !(player.getVehicle() instanceof ArmedVehicleEntity iArmedVehicle && iArmedVehicle.banHand(player)) && !notInGame() && drawTime < 0.01 - && cap != null && !cap.edit) { + && !cap.edit) { if (Minecraft.getInstance().player != null) { Minecraft.getInstance().player.getPersistentData().putDouble("noRun", 5); } @@ -1281,8 +1279,7 @@ public class ClientEventHandler { private static void handlePlayerBreath(LivingEntity entity) { float times = (float) Math.min(Minecraft.getInstance().getTimer().getRealtimeDeltaTicks(), 0.8); - var cap = entity.getCapability(ModCapabilities.PLAYER_VARIABLE); - boolean breath = cap != null && cap.breath; + boolean breath = entity.getData(ModAttachments.PLAYER_VARIABLE).breath; breathTime = Mth.lerp(0.2f * times, breathTime, breath ? 1 : 0); } @@ -1353,8 +1350,7 @@ public class ClientEventHandler { private static void handleBowPullAnimation(LivingEntity entity) { float times = 4 * (float) Math.min(Minecraft.getInstance().getTimer().getRealtimeDeltaTicks(), 0.8); - var cap = entity.getCapability(ModCapabilities.PLAYER_VARIABLE); - if (cap != null && cap.bowPull) { + if (entity.getData(ModAttachments.PLAYER_VARIABLE).bowPull) { pullTimer = Math.min(pullTimer + 0.024 * times, 1.4); bowTimer = Math.min(bowTimer + 0.018 * times, 1); handTimer = Math.min(handTimer + 0.018 * times, 1); @@ -1410,14 +1406,11 @@ public class ClientEventHandler { event.setFOV(event.getFOV() / (1.0 + p * 0.01) * (1 - 0.4 * breathTime)); fov = event.getFOV(); - var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); - // 智慧芯片 if (zoom && !notInGame() && drawTime < 0.01 - && cap != null - && !cap.edit) { + && !player.getData(ModAttachments.PLAYER_VARIABLE).edit) { if (!player.isShiftKeyDown()) { int intelligentChipLevel = data.perk.getLevel(ModPerks.INTELLIGENT_CHIP); @@ -1549,8 +1542,7 @@ public class ClientEventHandler { public static void aimAtVillager(Player player) { if (aimVillagerCountdown > 0) return; - var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); - if (cap != null && cap.zoom) { + if (player.getData(ModAttachments.PLAYER_VARIABLE).zoom) { Entity entity = TraceTool.findLookingEntity(player, 10); if (entity instanceof AbstractVillager villager) { List entities = SeekTool.seekLivingEntities(villager, villager.level(), 16, 120); diff --git a/src/main/java/com/atsuishio/superbwarfare/event/GunEventHandler.java b/src/main/java/com/atsuishio/superbwarfare/event/GunEventHandler.java index 92a1e48b5..78f4b5297 100644 --- a/src/main/java/com/atsuishio/superbwarfare/event/GunEventHandler.java +++ b/src/main/java/com/atsuishio/superbwarfare/event/GunEventHandler.java @@ -1,14 +1,9 @@ package com.atsuishio.superbwarfare.event; import com.atsuishio.superbwarfare.Mod; -import com.atsuishio.superbwarfare.init.ModCapabilities; -import com.atsuishio.superbwarfare.capability.player.PlayerVariable; import com.atsuishio.superbwarfare.entity.projectile.ProjectileEntity; import com.atsuishio.superbwarfare.event.events.ReloadEvent; -import com.atsuishio.superbwarfare.init.ModItems; -import com.atsuishio.superbwarfare.init.ModPerks; -import com.atsuishio.superbwarfare.init.ModSounds; -import com.atsuishio.superbwarfare.init.ModTags; +import com.atsuishio.superbwarfare.init.*; import com.atsuishio.superbwarfare.item.gun.GunItem; import com.atsuishio.superbwarfare.item.gun.data.AttachmentType; import com.atsuishio.superbwarfare.item.gun.data.GunData; @@ -188,8 +183,7 @@ public class GunEventHandler { float velocity = (float) ((data.velocity() + GunsTool.getGunDoubleTag(tag, "CustomVelocity")) * perkSpeed(data)); int projectileAmount = data.projectileAmount(); float bypassArmorRate = (float) data.bypassArmor(); - var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); - boolean zoom = cap != null && cap.zoom; + boolean zoom = player.getData(ModAttachments.PLAYER_VARIABLE).zoom; var perkInstance = data.perk.getInstance(Perk.Type.AMMO); var perk = perkInstance != null ? perkInstance.perk() : null; @@ -557,8 +551,7 @@ public class GunEventHandler { // 一阶段结束,检查备弹,如果有则二阶段启动,无则直接跳到三阶段 if ((tag.getDouble("PrepareTime") == 1 || tag.getDouble("PrepareLoadTime") == 1)) { if (!InventoryTool.hasCreativeAmmoBox(player)) { - var capability = player.getCapability(ModCapabilities.PLAYER_VARIABLE); - if (capability == null) capability = new PlayerVariable(); + var capability = player.getData(ModAttachments.PLAYER_VARIABLE); if (stack.is(ModTags.Items.USE_SHOTGUN_AMMO) && capability.shotgunAmmo == 0) { tag.putBoolean("ForceStartStage3", true); @@ -640,8 +633,7 @@ public class GunEventHandler { // 备弹耗尽结束 if (!InventoryTool.hasCreativeAmmoBox(player)) { - var capability = player.getCapability(ModCapabilities.PLAYER_VARIABLE); - if (capability == null) capability = new PlayerVariable(); + var capability = player.getData(ModAttachments.PLAYER_VARIABLE); if (stack.is(ModTags.Items.USE_SHOTGUN_AMMO) && capability.shotgunAmmo == 0) { gunData.reload.setStage(3); @@ -695,8 +687,7 @@ public class GunEventHandler { data.setAmmo(data.ammo() + 1); if (!InventoryTool.hasCreativeAmmoBox(player)) { - var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); - if (cap == null) return; + var cap = player.getData(ModAttachments.PLAYER_VARIABLE); ItemStack stack = player.getMainHandItem(); if (stack.is(ModTags.Items.USE_SHOTGUN_AMMO)) { @@ -713,7 +704,7 @@ public class GunEventHandler { player.getInventory().clearOrCountMatchingItems(p -> p.getItem() == ModItems.GRENADE_40MM.get(), 1, player.inventoryMenu.getCraftSlots()); } - cap.syncPlayerVariables(player); + player.setData(ModAttachments.PLAYER_VARIABLE, cap); } } diff --git a/src/main/java/com/atsuishio/superbwarfare/event/LivingEventHandler.java b/src/main/java/com/atsuishio/superbwarfare/event/LivingEventHandler.java index 7090ca17d..fd04eab0a 100644 --- a/src/main/java/com/atsuishio/superbwarfare/event/LivingEventHandler.java +++ b/src/main/java/com/atsuishio/superbwarfare/event/LivingEventHandler.java @@ -1,7 +1,5 @@ package com.atsuishio.superbwarfare.event; -import com.atsuishio.superbwarfare.init.ModCapabilities; -import com.atsuishio.superbwarfare.capability.player.PlayerVariable; import com.atsuishio.superbwarfare.component.ModDataComponents; import com.atsuishio.superbwarfare.config.common.GameplayConfig; import com.atsuishio.superbwarfare.config.server.MiscConfig; @@ -216,7 +214,6 @@ public class LivingEventHandler { var data = GunData.from(stack); double amount = Math.min(0.125 * event.getAmount(), event.getEntity().getMaxHealth()); - final var tag = NBTTool.getTag(stack); // 先处理发射器类武器或高爆弹的爆炸伤害 if (source.is(ModDamageTypes.PROJECTILE_BOOM)) { @@ -385,11 +382,10 @@ public class LivingEventHandler { oldData.charge.reset(); } - var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); - if (cap != null) { - cap.edit = false; - cap.syncPlayerVariables(player); - } + var cap = player.getData(ModAttachments.PLAYER_VARIABLE).watch(); + cap.edit = false; + player.setData(ModAttachments.PLAYER_VARIABLE, cap); + cap.sync(player); oldData.save(); } @@ -433,11 +429,10 @@ public class LivingEventHandler { PacketDistributor.sendToPlayer(serverPlayer, new DrawClientMessage(true)); } - var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); - if (cap != null) { - cap.tacticalSprint = false; - cap.syncPlayerVariables(player); - } + var cap = player.getData(ModAttachments.PLAYER_VARIABLE).watch(); + cap.tacticalSprint = false; + player.setData(ModAttachments.PLAYER_VARIABLE, cap); + cap.sync(player); newData.save(); } @@ -608,7 +603,6 @@ public class LivingEventHandler { private static void handleKillingTallyDamage(ItemStack stack, LivingIncomingDamageEvent event) { var data = GunData.from(stack); - final var tag = data.tag(); int level = data.perk.getLevel(ModPerks.KILLING_TALLY); if (level == 0) return; @@ -657,8 +651,7 @@ public class LivingEventHandler { float rate = level * 0.1f + (stack.is(ModTags.Items.SMG) || stack.is(ModTags.Items.RIFLE) ? 0.07f : 0f); - var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); - if (cap == null) return; + var cap = player.getData(ModAttachments.PLAYER_VARIABLE).watch(); int mag = data.magazine(); int ammo = data.ammo(); @@ -685,7 +678,8 @@ public class LivingEventHandler { data.setAmmo(Math.min(mag, ammo + ammoFinal)); } data.save(); - cap.syncPlayerVariables(player); + player.setData(ModAttachments.PLAYER_VARIABLE, cap); + cap.sync(player); } @@ -746,8 +740,7 @@ public class LivingEventHandler { public static void onLivingDrops(LivingDropsEvent event) { // 死亡掉落弹药盒 if (event.getEntity() instanceof Player player && !player.level().getLevelData().getGameRules().getBoolean(GameRules.RULE_KEEPINVENTORY)) { - var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); - if (cap == null) cap = new PlayerVariable(); + var cap = player.getData(ModAttachments.PLAYER_VARIABLE).watch(); boolean drop = cap.rifleAmmo + cap.handgunAmmo + cap.shotgunAmmo + cap.sniperAmmo + cap.heavyAmmo > 0; @@ -762,7 +755,8 @@ public class LivingEventHandler { var info = new AmmoBoxInfo("All", true); stack.set(ModDataComponents.AMMO_BOX_INFO, info); - cap.syncPlayerVariables(player); + player.setData(ModAttachments.PLAYER_VARIABLE, cap); + cap.sync(player); event.getDrops().add(new ItemEntity(player.level(), player.getX(), player.getY() + 1, player.getZ(), stack)); } diff --git a/src/main/java/com/atsuishio/superbwarfare/event/PlayerEventHandler.java b/src/main/java/com/atsuishio/superbwarfare/event/PlayerEventHandler.java index 721701fec..c35b32c04 100644 --- a/src/main/java/com/atsuishio/superbwarfare/event/PlayerEventHandler.java +++ b/src/main/java/com/atsuishio/superbwarfare/event/PlayerEventHandler.java @@ -1,8 +1,8 @@ package com.atsuishio.superbwarfare.event; -import com.atsuishio.superbwarfare.init.ModCapabilities; import com.atsuishio.superbwarfare.config.common.GameplayConfig; import com.atsuishio.superbwarfare.config.server.MiscConfig; +import com.atsuishio.superbwarfare.init.ModAttachments; import com.atsuishio.superbwarfare.init.ModItems; import com.atsuishio.superbwarfare.init.ModSounds; import com.atsuishio.superbwarfare.init.ModTags; @@ -52,13 +52,12 @@ public class PlayerEventHandler { public static void onPlayerRespawned(PlayerEvent.PlayerRespawnEvent event) { Player player = event.getEntity(); - var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); - if (cap != null) { - cap.zoom = false; - cap.tacticalSprintExhaustion = false; - cap.tacticalSprintTime = 600; - cap.syncPlayerVariables(player); - } + var cap = player.getData(ModAttachments.PLAYER_VARIABLE).watch(); + cap.zoom = false; + cap.tacticalSprintExhaustion = false; + cap.tacticalSprintTime = 600; + player.setData(ModAttachments.PLAYER_VARIABLE, cap); + cap.sync(player); handleRespawnReload(player); handleRespawnAutoArmor(player); @@ -79,6 +78,7 @@ public class PlayerEventHandler { public static void onPlayerTick(PlayerTickEvent.Post event) { Player player = event.getEntity(); ItemStack stack = player.getMainHandItem(); + var variable = player.getData(ModAttachments.PLAYER_VARIABLE).watch(); if (stack.is(ModTags.Items.GUN)) { handlePlayerSprint(player); @@ -90,11 +90,12 @@ public class PlayerEventHandler { handleSimulationDistance(player); handleTacticalSprint(player); handleBreath(player); + + variable.sync(player); } private static void handleBreath(Player player) { - var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); - if (cap == null) return; + var cap = player.getData(ModAttachments.PLAYER_VARIABLE).watch(); if (cap.breath) { cap.breathTime = Mth.clamp(cap.breathTime - 1, 0, 100); @@ -111,12 +112,11 @@ public class PlayerEventHandler { cap.breathExhaustion = false; } - cap.syncPlayerVariables(player); + player.setData(ModAttachments.PLAYER_VARIABLE, cap); } private static void handleTacticalSprint(Player player) { - var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); - if (cap == null) return; + var cap = player.getData(ModAttachments.PLAYER_VARIABLE).watch(); ItemStack stack = player.getMainHandItem(); int sprintCost; @@ -161,15 +161,14 @@ public class PlayerEventHandler { player.getPersistentData().putBoolean("canTacticalSprint", true); } - cap.syncPlayerVariables(player); + player.setData(ModAttachments.PLAYER_VARIABLE, cap); } /** * 判断玩家是否在奔跑 */ private static void handlePlayerSprint(Player player) { - var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); - if (cap == null) return; + var cap = player.getData(ModAttachments.PLAYER_VARIABLE); if (cap.holdFire) { player.getPersistentData().putDouble("noRun", 10); @@ -191,12 +190,11 @@ public class PlayerEventHandler { } private static void handleGround(Player player) { - var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); - if (cap == null) return; + var cap = player.getData(ModAttachments.PLAYER_VARIABLE); if (player.onGround()) { cap.playerDoubleJump = false; - cap.syncPlayerVariables(player); + player.setData(ModAttachments.PLAYER_VARIABLE, cap); } } @@ -208,15 +206,13 @@ public class PlayerEventHandler { if ((stack.is(ModItems.RPG.get()) || stack.is(ModItems.BOCEK.get())) && data.ammo() == 1) { tag.remove("IsEmpty"); - data.save(); } } private static void handleBocekPulling(Player player) { ItemStack stack = player.getMainHandItem(); - var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); - if (cap == null) return; + var cap = player.getData(ModAttachments.PLAYER_VARIABLE).watch(); var data = GunData.from(stack); final var tag = data.tag(); @@ -231,7 +227,7 @@ public class PlayerEventHandler { cap.bowPull = true; cap.tacticalSprint = false; - cap.syncPlayerVariables(player); + player.setData(ModAttachments.PLAYER_VARIABLE, cap); player.setSprinting(false); } if (GunsTool.getGunDoubleTag(tag, "Power") == 1) { @@ -245,15 +241,16 @@ public class PlayerEventHandler { GunsTool.setGunDoubleTag(tag, "Power", 0); } cap.bowPull = false; - cap.syncPlayerVariables(player); + player.setData(ModAttachments.PLAYER_VARIABLE, cap); } if (GunsTool.getGunDoubleTag(tag, "Power") > 0) { cap.tacticalSprint = false; - cap.syncPlayerVariables(player); + player.setData(ModAttachments.PLAYER_VARIABLE, cap); player.setSprinting(false); } + cap.sync(player); data.save(); } @@ -277,8 +274,7 @@ public class PlayerEventHandler { var tag = data.tag(); if (!InventoryTool.hasCreativeAmmoBox(player)) { - var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); - if (cap == null) return; + var cap = player.getData(ModAttachments.PLAYER_VARIABLE); if (stack.is(ModTags.Items.USE_SHOTGUN_AMMO) && cap.shotgunAmmo > 0) { GunsTool.reload(player, stack, data, AmmoType.SHOTGUN); diff --git a/src/main/java/com/atsuishio/superbwarfare/init/ModAttachments.java b/src/main/java/com/atsuishio/superbwarfare/init/ModAttachments.java new file mode 100644 index 000000000..a2f346799 --- /dev/null +++ b/src/main/java/com/atsuishio/superbwarfare/init/ModAttachments.java @@ -0,0 +1,19 @@ +package com.atsuishio.superbwarfare.init; + +import com.atsuishio.superbwarfare.Mod; +import com.atsuishio.superbwarfare.capability.player.PlayerVariable; +import net.neoforged.neoforge.attachment.AttachmentType; +import net.neoforged.neoforge.registries.DeferredRegister; +import net.neoforged.neoforge.registries.NeoForgeRegistries; + +import java.util.function.Supplier; + +public class ModAttachments { + public static final DeferredRegister> ATTACHMENT_TYPES = DeferredRegister.create(NeoForgeRegistries.ATTACHMENT_TYPES, Mod.MODID); + + + public static final Supplier> PLAYER_VARIABLE = ATTACHMENT_TYPES.register( + "player_variable", () -> AttachmentType.serializable(PlayerVariable::new).build() + ); + +} diff --git a/src/main/java/com/atsuishio/superbwarfare/init/ModCapabilities.java b/src/main/java/com/atsuishio/superbwarfare/init/ModCapabilities.java index c490befa3..c2b8018d6 100644 --- a/src/main/java/com/atsuishio/superbwarfare/init/ModCapabilities.java +++ b/src/main/java/com/atsuishio/superbwarfare/init/ModCapabilities.java @@ -7,8 +7,6 @@ import com.atsuishio.superbwarfare.block.entity.FuMO25BlockEntity; import com.atsuishio.superbwarfare.capability.energy.ItemEnergyStorage; import com.atsuishio.superbwarfare.capability.laser.LaserCapability; import com.atsuishio.superbwarfare.capability.laser.LaserCapabilityProvider; -import com.atsuishio.superbwarfare.capability.player.PlayerVariable; -import com.atsuishio.superbwarfare.capability.player.PlayerVariablesProvider; import com.atsuishio.superbwarfare.entity.vehicle.base.ContainerMobileVehicleEntity; import com.atsuishio.superbwarfare.entity.vehicle.base.EnergyVehicleEntity; import com.atsuishio.superbwarfare.item.CreativeChargingStationBlockItem; @@ -32,13 +30,11 @@ import java.util.ArrayList; public class ModCapabilities { public static final EntityCapability LASER_CAPABILITY = EntityCapability.createVoid(Mod.loc("laser_capability"), LaserCapability.class); - public static final EntityCapability PLAYER_VARIABLE = EntityCapability.createVoid(Mod.loc("player_variable"), PlayerVariable.class); @SubscribeEvent public static void registerCapabilities(RegisterCapabilitiesEvent event) { - // 玩家变量和激光 + // 激光 event.registerEntity(ModCapabilities.LASER_CAPABILITY, EntityType.PLAYER, new LaserCapabilityProvider()); - event.registerEntity(ModCapabilities.PLAYER_VARIABLE, EntityType.PLAYER, new PlayerVariablesProvider()); // 充电站 event.registerBlockEntity(Capabilities.EnergyStorage.BLOCK, ModBlockEntities.CHARGING_STATION.value(), ChargingStationBlockEntity::getEnergyStorage); diff --git a/src/main/java/com/atsuishio/superbwarfare/item/BeamTest.java b/src/main/java/com/atsuishio/superbwarfare/item/BeamTest.java index 2a60ac767..599328831 100644 --- a/src/main/java/com/atsuishio/superbwarfare/item/BeamTest.java +++ b/src/main/java/com/atsuishio/superbwarfare/item/BeamTest.java @@ -1,9 +1,9 @@ package com.atsuishio.superbwarfare.item; -import com.atsuishio.superbwarfare.init.ModCapabilities; import com.atsuishio.superbwarfare.capability.laser.LaserHandler; import com.atsuishio.superbwarfare.client.TooltipTool; import com.atsuishio.superbwarfare.entity.projectile.LaserEntity; +import com.atsuishio.superbwarfare.init.ModCapabilities; import com.atsuishio.superbwarfare.init.ModSounds; import com.atsuishio.superbwarfare.network.message.receive.ShakeClientMessage; import com.atsuishio.superbwarfare.network.message.send.LaserShootMessage; diff --git a/src/main/java/com/atsuishio/superbwarfare/item/common/ammo/AmmoSupplierItem.java b/src/main/java/com/atsuishio/superbwarfare/item/common/ammo/AmmoSupplierItem.java index e6b9c8eb8..c1517f2b6 100644 --- a/src/main/java/com/atsuishio/superbwarfare/item/common/ammo/AmmoSupplierItem.java +++ b/src/main/java/com/atsuishio/superbwarfare/item/common/ammo/AmmoSupplierItem.java @@ -1,6 +1,6 @@ package com.atsuishio.superbwarfare.item.common.ammo; -import com.atsuishio.superbwarfare.init.ModCapabilities; +import com.atsuishio.superbwarfare.init.ModAttachments; import com.atsuishio.superbwarfare.init.ModItems; import com.atsuishio.superbwarfare.init.ModSounds; import com.atsuishio.superbwarfare.tools.AmmoType; @@ -49,12 +49,11 @@ public class AmmoSupplierItem extends Item { if (offhandItem.is(ModItems.AMMO_BOX.get())) { this.type.add(offhandItem, ammoToAdd * count); } else { - var capability = player.getCapability(ModCapabilities.PLAYER_VARIABLE, null); + var capability = player.getData(ModAttachments.PLAYER_VARIABLE).watch(); - if (capability != null) { - this.type.add(capability, ammoToAdd * count); - capability.syncPlayerVariables(player); - } + this.type.add(capability, ammoToAdd * count); + player.setData(ModAttachments.PLAYER_VARIABLE, capability); + capability.sync(player); } if (!level.isClientSide()) { diff --git a/src/main/java/com/atsuishio/superbwarfare/item/common/ammo/box/AmmoBox.java b/src/main/java/com/atsuishio/superbwarfare/item/common/ammo/box/AmmoBox.java index f02cbe85c..5b2d35cf4 100644 --- a/src/main/java/com/atsuishio/superbwarfare/item/common/ammo/box/AmmoBox.java +++ b/src/main/java/com/atsuishio/superbwarfare/item/common/ammo/box/AmmoBox.java @@ -1,7 +1,7 @@ package com.atsuishio.superbwarfare.item.common.ammo.box; -import com.atsuishio.superbwarfare.init.ModCapabilities; import com.atsuishio.superbwarfare.component.ModDataComponents; +import com.atsuishio.superbwarfare.init.ModAttachments; import com.atsuishio.superbwarfare.init.ModSounds; import com.atsuishio.superbwarfare.tools.AmmoType; import com.atsuishio.superbwarfare.tools.FormatTool; @@ -41,8 +41,8 @@ public class AmmoBox extends Item { if (info == null) info = new AmmoBoxInfo("All", false); String selectedType = info.type(); - var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE, null); - if (cap != null && !level.isClientSide()) { + var cap = player.getData(ModAttachments.PLAYER_VARIABLE).watch(); + if (!level.isClientSide()) { var types = selectedType.equals("All") ? AmmoType.values() : new AmmoType[]{AmmoType.getType(selectedType)}; for (var type : types) { @@ -58,7 +58,8 @@ public class AmmoBox extends Item { type.set(stack, 0); } } - cap.syncPlayerVariables(player); + player.setData(ModAttachments.PLAYER_VARIABLE, cap); + cap.sync(player); level.playSound(null, player.blockPosition(), SoundEvents.ARROW_HIT_PLAYER, SoundSource.PLAYERS, 1, 1); // 取出弹药时,若弹药盒为掉落物版本,则移除弹药盒物品 diff --git a/src/main/java/com/atsuishio/superbwarfare/item/gun/GunItem.java b/src/main/java/com/atsuishio/superbwarfare/item/gun/GunItem.java index 0abb07ac4..fb3187479 100644 --- a/src/main/java/com/atsuishio/superbwarfare/item/gun/GunItem.java +++ b/src/main/java/com/atsuishio/superbwarfare/item/gun/GunItem.java @@ -1,9 +1,9 @@ package com.atsuishio.superbwarfare.item.gun; import com.atsuishio.superbwarfare.Mod; -import com.atsuishio.superbwarfare.init.ModCapabilities; import com.atsuishio.superbwarfare.client.PoseTool; import com.atsuishio.superbwarfare.client.tooltip.component.GunImageComponent; +import com.atsuishio.superbwarfare.init.ModAttachments; import com.atsuishio.superbwarfare.init.ModItems; import com.atsuishio.superbwarfare.init.ModPerks; import com.atsuishio.superbwarfare.init.ModTags; @@ -83,21 +83,20 @@ public abstract class GunItem extends Item implements CustomRendererItem { if ((hasBulletInBarrel && ammoCount > magazine + 1) || (!hasBulletInBarrel && ammoCount > magazine)) { int count = ammoCount - magazine - (hasBulletInBarrel ? 1 : 0); - var capability = entity.getCapability(ModCapabilities.PLAYER_VARIABLE); - if (capability != null) { - if (stack.is(ModTags.Items.USE_SHOTGUN_AMMO)) { - AmmoType.SHOTGUN.add(capability, count); - } else if (stack.is(ModTags.Items.USE_SNIPER_AMMO)) { - AmmoType.SNIPER.add(capability, count); - } else if (stack.is(ModTags.Items.USE_HANDGUN_AMMO)) { - AmmoType.HANDGUN.add(capability, count); - } else if (stack.is(ModTags.Items.USE_RIFLE_AMMO)) { - AmmoType.RIFLE.add(capability, count); - } else if (stack.is(ModTags.Items.USE_HEAVY_AMMO)) { - AmmoType.HEAVY.add(capability, count); - } - capability.syncPlayerVariables(entity); + var capability = entity.getData(ModAttachments.PLAYER_VARIABLE).watch(); + if (stack.is(ModTags.Items.USE_SHOTGUN_AMMO)) { + AmmoType.SHOTGUN.add(capability, count); + } else if (stack.is(ModTags.Items.USE_SNIPER_AMMO)) { + AmmoType.SNIPER.add(capability, count); + } else if (stack.is(ModTags.Items.USE_HANDGUN_AMMO)) { + AmmoType.HANDGUN.add(capability, count); + } else if (stack.is(ModTags.Items.USE_RIFLE_AMMO)) { + AmmoType.RIFLE.add(capability, count); + } else if (stack.is(ModTags.Items.USE_HEAVY_AMMO)) { + AmmoType.HEAVY.add(capability, count); } + entity.setData(ModAttachments.PLAYER_VARIABLE, capability); + capability.sync(entity); data.setAmmo(magazine + (hasBulletInBarrel ? 1 : 0)); } data.save(); diff --git a/src/main/java/com/atsuishio/superbwarfare/item/gun/handgun/Trachelium.java b/src/main/java/com/atsuishio/superbwarfare/item/gun/handgun/Trachelium.java index ea3950cc9..97673861f 100644 --- a/src/main/java/com/atsuishio/superbwarfare/item/gun/handgun/Trachelium.java +++ b/src/main/java/com/atsuishio/superbwarfare/item/gun/handgun/Trachelium.java @@ -1,9 +1,9 @@ package com.atsuishio.superbwarfare.item.gun.handgun; -import com.atsuishio.superbwarfare.init.ModCapabilities; import com.atsuishio.superbwarfare.client.TooltipTool; import com.atsuishio.superbwarfare.client.renderer.item.TracheliumItemRenderer; import com.atsuishio.superbwarfare.event.ClientEventHandler; +import com.atsuishio.superbwarfare.init.ModAttachments; import com.atsuishio.superbwarfare.init.ModSounds; import com.atsuishio.superbwarfare.init.ModTags; import com.atsuishio.superbwarfare.item.gun.GunItem; @@ -194,8 +194,7 @@ public class Trachelium extends GunItem implements GeoItem { ItemStack stack = player.getMainHandItem(); if (!stack.is(ModTags.Items.GUN)) return PlayState.STOP; - var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); - if (cap != null && cap.edit) { + if (player.getData(ModAttachments.PLAYER_VARIABLE).edit) { return event.setAndContinue(RawAnimation.begin().thenPlay("animation.trachelium.edit")); } diff --git a/src/main/java/com/atsuishio/superbwarfare/item/gun/heavy/Ntw20Item.java b/src/main/java/com/atsuishio/superbwarfare/item/gun/heavy/Ntw20Item.java index 99a34103c..999723f85 100644 --- a/src/main/java/com/atsuishio/superbwarfare/item/gun/heavy/Ntw20Item.java +++ b/src/main/java/com/atsuishio/superbwarfare/item/gun/heavy/Ntw20Item.java @@ -1,9 +1,9 @@ package com.atsuishio.superbwarfare.item.gun.heavy; import com.atsuishio.superbwarfare.Mod; -import com.atsuishio.superbwarfare.init.ModCapabilities; import com.atsuishio.superbwarfare.client.renderer.item.Ntw20Renderer; import com.atsuishio.superbwarfare.event.ClientEventHandler; +import com.atsuishio.superbwarfare.init.ModAttachments; import com.atsuishio.superbwarfare.init.ModRarity; import com.atsuishio.superbwarfare.init.ModSounds; import com.atsuishio.superbwarfare.init.ModTags; @@ -101,8 +101,7 @@ public class Ntw20Item extends GunItem implements GeoItem { ItemStack stack = player.getMainHandItem(); if (!stack.is(ModTags.Items.GUN)) return PlayState.STOP; - var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); - if (cap != null && cap.edit) { + if (player.getData(ModAttachments.PLAYER_VARIABLE).edit) { return event.setAndContinue(RawAnimation.begin().thenPlay("animation.ntw_20.edit")); } diff --git a/src/main/java/com/atsuishio/superbwarfare/item/gun/launcher/JavelinItem.java b/src/main/java/com/atsuishio/superbwarfare/item/gun/launcher/JavelinItem.java index 9c32bebcd..8208463b1 100644 --- a/src/main/java/com/atsuishio/superbwarfare/item/gun/launcher/JavelinItem.java +++ b/src/main/java/com/atsuishio/superbwarfare/item/gun/launcher/JavelinItem.java @@ -1,7 +1,6 @@ package com.atsuishio.superbwarfare.item.gun.launcher; import com.atsuishio.superbwarfare.Mod; -import com.atsuishio.superbwarfare.init.ModCapabilities; import com.atsuishio.superbwarfare.client.renderer.item.JavelinItemRenderer; import com.atsuishio.superbwarfare.client.tooltip.component.LauncherImageComponent; import com.atsuishio.superbwarfare.entity.projectile.FlareDecoyEntity; @@ -310,8 +309,7 @@ public class JavelinItem extends GunItem implements GeoItem, SpecialFireWeapon { public void fireOnPress(Player player, final GunData data) { var tag = data.tag(); - var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); - if (cap != null && !cap.zoom || data.ammo() <= 0) return; + if (!player.getData(ModAttachments.PLAYER_VARIABLE).zoom || data.ammo() <= 0) return; Entity seekingEntity = SeekTool.seekEntity(player, player.level(), 512, 8); diff --git a/src/main/java/com/atsuishio/superbwarfare/item/gun/launcher/M79Item.java b/src/main/java/com/atsuishio/superbwarfare/item/gun/launcher/M79Item.java index 3a2ef3bb7..9b498317c 100644 --- a/src/main/java/com/atsuishio/superbwarfare/item/gun/launcher/M79Item.java +++ b/src/main/java/com/atsuishio/superbwarfare/item/gun/launcher/M79Item.java @@ -1,15 +1,11 @@ package com.atsuishio.superbwarfare.item.gun.launcher; import com.atsuishio.superbwarfare.Mod; -import com.atsuishio.superbwarfare.init.ModCapabilities; import com.atsuishio.superbwarfare.client.renderer.item.M79ItemRenderer; import com.atsuishio.superbwarfare.client.tooltip.component.LauncherImageComponent; import com.atsuishio.superbwarfare.entity.projectile.GunGrenadeEntity; import com.atsuishio.superbwarfare.event.ClientEventHandler; -import com.atsuishio.superbwarfare.init.ModItems; -import com.atsuishio.superbwarfare.init.ModPerks; -import com.atsuishio.superbwarfare.init.ModSounds; -import com.atsuishio.superbwarfare.init.ModTags; +import com.atsuishio.superbwarfare.init.*; import com.atsuishio.superbwarfare.item.gun.GunItem; import com.atsuishio.superbwarfare.item.gun.SpecialFireWeapon; import com.atsuishio.superbwarfare.item.gun.data.GunData; @@ -175,9 +171,7 @@ public class M79Item extends GunItem implements GeoItem, SpecialFireWeapon { ItemStack stack = data.stack(); if (player.getCooldowns().isOnCooldown(stack.getItem()) || data.ammo() <= 0) return; - var tag = data.tag(); - var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); - boolean zooming = cap != null && cap.zoom; + boolean zooming = player.getData(ModAttachments.PLAYER_VARIABLE).zoom; double spread = data.spread(); if (player.level() instanceof ServerLevel serverLevel) { diff --git a/src/main/java/com/atsuishio/superbwarfare/item/gun/launcher/RpgItem.java b/src/main/java/com/atsuishio/superbwarfare/item/gun/launcher/RpgItem.java index 34fd9c4a0..e618dd790 100644 --- a/src/main/java/com/atsuishio/superbwarfare/item/gun/launcher/RpgItem.java +++ b/src/main/java/com/atsuishio/superbwarfare/item/gun/launcher/RpgItem.java @@ -1,15 +1,11 @@ package com.atsuishio.superbwarfare.item.gun.launcher; import com.atsuishio.superbwarfare.Mod; -import com.atsuishio.superbwarfare.init.ModCapabilities; import com.atsuishio.superbwarfare.client.renderer.item.RpgItemRenderer; import com.atsuishio.superbwarfare.client.tooltip.component.LauncherImageComponent; import com.atsuishio.superbwarfare.entity.projectile.RpgRocketEntity; import com.atsuishio.superbwarfare.event.ClientEventHandler; -import com.atsuishio.superbwarfare.init.ModItems; -import com.atsuishio.superbwarfare.init.ModPerks; -import com.atsuishio.superbwarfare.init.ModSounds; -import com.atsuishio.superbwarfare.init.ModTags; +import com.atsuishio.superbwarfare.init.*; import com.atsuishio.superbwarfare.item.gun.GunItem; import com.atsuishio.superbwarfare.item.gun.SpecialFireWeapon; import com.atsuishio.superbwarfare.item.gun.data.GunData; @@ -190,8 +186,7 @@ public class RpgItem extends GunItem implements GeoItem, SpecialFireWeapon { || data.ammo() <= 0 ) return; - var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); - boolean zoom = cap != null && cap.zoom; + boolean zoom = player.getData(ModAttachments.PLAYER_VARIABLE).zoom; double spread = data.spread(); if (player.level() instanceof ServerLevel serverLevel) { diff --git a/src/main/java/com/atsuishio/superbwarfare/item/gun/launcher/SecondaryCataclysm.java b/src/main/java/com/atsuishio/superbwarfare/item/gun/launcher/SecondaryCataclysm.java index 1033797b2..6a7f23868 100644 --- a/src/main/java/com/atsuishio/superbwarfare/item/gun/launcher/SecondaryCataclysm.java +++ b/src/main/java/com/atsuishio/superbwarfare/item/gun/launcher/SecondaryCataclysm.java @@ -1,7 +1,6 @@ package com.atsuishio.superbwarfare.item.gun.launcher; import com.atsuishio.superbwarfare.Mod; -import com.atsuishio.superbwarfare.init.ModCapabilities; import com.atsuishio.superbwarfare.client.renderer.item.SecondaryCataclysmRenderer; import com.atsuishio.superbwarfare.client.tooltip.component.SecondaryCataclysmImageComponent; import com.atsuishio.superbwarfare.entity.projectile.GunGrenadeEntity; @@ -274,9 +273,7 @@ public class SecondaryCataclysm extends GunItem implements GeoItem, SpecialFireW ItemStack stack = data.stack(); if (player.getCooldowns().isOnCooldown(stack.getItem()) || data.ammo() <= 0) return; - var tag = data.tag(); - var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); - boolean zooming = cap != null && cap.zoom; + boolean zooming = player.getData(ModAttachments.PLAYER_VARIABLE).zoom; double spread = data.spread(); var stackCap = stack.getCapability(Capabilities.EnergyStorage.ITEM); diff --git a/src/main/java/com/atsuishio/superbwarfare/item/gun/machinegun/RpkItem.java b/src/main/java/com/atsuishio/superbwarfare/item/gun/machinegun/RpkItem.java index 1fd09a8e0..5bdf68f0d 100644 --- a/src/main/java/com/atsuishio/superbwarfare/item/gun/machinegun/RpkItem.java +++ b/src/main/java/com/atsuishio/superbwarfare/item/gun/machinegun/RpkItem.java @@ -1,9 +1,9 @@ package com.atsuishio.superbwarfare.item.gun.machinegun; import com.atsuishio.superbwarfare.Mod; -import com.atsuishio.superbwarfare.init.ModCapabilities; import com.atsuishio.superbwarfare.client.renderer.item.RpkItemRenderer; import com.atsuishio.superbwarfare.event.ClientEventHandler; +import com.atsuishio.superbwarfare.init.ModAttachments; import com.atsuishio.superbwarfare.init.ModPerks; import com.atsuishio.superbwarfare.init.ModSounds; import com.atsuishio.superbwarfare.init.ModTags; @@ -118,8 +118,7 @@ public class RpkItem extends GunItem implements GeoItem { ItemStack stack = player.getMainHandItem(); if (!stack.is(ModTags.Items.GUN)) return PlayState.STOP; - var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); - if (cap != null && cap.edit) { + if (player.getData(ModAttachments.PLAYER_VARIABLE).edit) { return event.setAndContinue(RawAnimation.begin().thenPlay("animation.ak47.edit")); } diff --git a/src/main/java/com/atsuishio/superbwarfare/item/gun/rifle/AK12Item.java b/src/main/java/com/atsuishio/superbwarfare/item/gun/rifle/AK12Item.java index 98c126449..0dad8ca64 100644 --- a/src/main/java/com/atsuishio/superbwarfare/item/gun/rifle/AK12Item.java +++ b/src/main/java/com/atsuishio/superbwarfare/item/gun/rifle/AK12Item.java @@ -1,9 +1,9 @@ package com.atsuishio.superbwarfare.item.gun.rifle; import com.atsuishio.superbwarfare.Mod; -import com.atsuishio.superbwarfare.init.ModCapabilities; import com.atsuishio.superbwarfare.client.renderer.item.AK12ItemRenderer; import com.atsuishio.superbwarfare.event.ClientEventHandler; +import com.atsuishio.superbwarfare.init.ModAttachments; import com.atsuishio.superbwarfare.init.ModSounds; import com.atsuishio.superbwarfare.init.ModTags; import com.atsuishio.superbwarfare.item.gun.GunItem; @@ -111,8 +111,7 @@ public class AK12Item extends GunItem implements GeoItem { ItemStack stack = player.getMainHandItem(); if (!stack.is(ModTags.Items.GUN)) return PlayState.STOP; - var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); - if (cap != null && cap.edit) { + if (player.getData(ModAttachments.PLAYER_VARIABLE).edit) { return event.setAndContinue(RawAnimation.begin().thenPlay("animation.ak12.edit")); } diff --git a/src/main/java/com/atsuishio/superbwarfare/item/gun/rifle/AK47Item.java b/src/main/java/com/atsuishio/superbwarfare/item/gun/rifle/AK47Item.java index bc3856db2..19224f87f 100644 --- a/src/main/java/com/atsuishio/superbwarfare/item/gun/rifle/AK47Item.java +++ b/src/main/java/com/atsuishio/superbwarfare/item/gun/rifle/AK47Item.java @@ -1,9 +1,9 @@ package com.atsuishio.superbwarfare.item.gun.rifle; import com.atsuishio.superbwarfare.Mod; -import com.atsuishio.superbwarfare.init.ModCapabilities; import com.atsuishio.superbwarfare.client.renderer.item.AK47ItemRenderer; import com.atsuishio.superbwarfare.event.ClientEventHandler; +import com.atsuishio.superbwarfare.init.ModAttachments; import com.atsuishio.superbwarfare.init.ModSounds; import com.atsuishio.superbwarfare.init.ModTags; import com.atsuishio.superbwarfare.item.gun.GunItem; @@ -119,8 +119,7 @@ public class AK47Item extends GunItem implements GeoItem { ItemStack stack = player.getMainHandItem(); if (!stack.is(ModTags.Items.GUN)) return PlayState.STOP; - var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); - if (cap != null && cap.edit) { + if (player.getData(ModAttachments.PLAYER_VARIABLE).edit) { return event.setAndContinue(RawAnimation.begin().thenPlay("animation.ak47.edit")); } diff --git a/src/main/java/com/atsuishio/superbwarfare/item/gun/rifle/Hk416Item.java b/src/main/java/com/atsuishio/superbwarfare/item/gun/rifle/Hk416Item.java index 52600c327..8f43636a1 100644 --- a/src/main/java/com/atsuishio/superbwarfare/item/gun/rifle/Hk416Item.java +++ b/src/main/java/com/atsuishio/superbwarfare/item/gun/rifle/Hk416Item.java @@ -1,9 +1,9 @@ package com.atsuishio.superbwarfare.item.gun.rifle; import com.atsuishio.superbwarfare.Mod; -import com.atsuishio.superbwarfare.init.ModCapabilities; import com.atsuishio.superbwarfare.client.renderer.item.Hk416ItemRenderer; import com.atsuishio.superbwarfare.event.ClientEventHandler; +import com.atsuishio.superbwarfare.init.ModAttachments; import com.atsuishio.superbwarfare.init.ModSounds; import com.atsuishio.superbwarfare.init.ModTags; import com.atsuishio.superbwarfare.item.gun.GunItem; @@ -114,8 +114,7 @@ public class Hk416Item extends GunItem implements GeoItem { ItemStack stack = player.getMainHandItem(); if (!stack.is(ModTags.Items.GUN)) return PlayState.STOP; - var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); - if (cap != null && cap.edit) { + if (player.getData(ModAttachments.PLAYER_VARIABLE).edit) { return event.setAndContinue(RawAnimation.begin().thenPlay("animation.m4.edit")); } diff --git a/src/main/java/com/atsuishio/superbwarfare/item/gun/rifle/M4Item.java b/src/main/java/com/atsuishio/superbwarfare/item/gun/rifle/M4Item.java index 3a444bc49..0496dc28f 100644 --- a/src/main/java/com/atsuishio/superbwarfare/item/gun/rifle/M4Item.java +++ b/src/main/java/com/atsuishio/superbwarfare/item/gun/rifle/M4Item.java @@ -1,9 +1,9 @@ package com.atsuishio.superbwarfare.item.gun.rifle; import com.atsuishio.superbwarfare.Mod; -import com.atsuishio.superbwarfare.init.ModCapabilities; import com.atsuishio.superbwarfare.client.renderer.item.M4ItemRenderer; import com.atsuishio.superbwarfare.event.ClientEventHandler; +import com.atsuishio.superbwarfare.init.ModAttachments; import com.atsuishio.superbwarfare.init.ModSounds; import com.atsuishio.superbwarfare.init.ModTags; import com.atsuishio.superbwarfare.item.gun.GunItem; @@ -115,8 +115,7 @@ public class M4Item extends GunItem implements GeoItem { ItemStack stack = player.getMainHandItem(); if (!stack.is(ModTags.Items.GUN)) return PlayState.STOP; - var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); - if (cap != null && cap.edit) { + if (player.getData(ModAttachments.PLAYER_VARIABLE).edit) { return event.setAndContinue(RawAnimation.begin().thenPlay("animation.m4.edit")); } diff --git a/src/main/java/com/atsuishio/superbwarfare/item/gun/rifle/Mk14Item.java b/src/main/java/com/atsuishio/superbwarfare/item/gun/rifle/Mk14Item.java index 4162aa155..3dc422961 100644 --- a/src/main/java/com/atsuishio/superbwarfare/item/gun/rifle/Mk14Item.java +++ b/src/main/java/com/atsuishio/superbwarfare/item/gun/rifle/Mk14Item.java @@ -1,9 +1,9 @@ package com.atsuishio.superbwarfare.item.gun.rifle; import com.atsuishio.superbwarfare.Mod; -import com.atsuishio.superbwarfare.init.ModCapabilities; import com.atsuishio.superbwarfare.client.renderer.item.Mk14ItemRenderer; import com.atsuishio.superbwarfare.event.ClientEventHandler; +import com.atsuishio.superbwarfare.init.ModAttachments; import com.atsuishio.superbwarfare.init.ModSounds; import com.atsuishio.superbwarfare.init.ModTags; import com.atsuishio.superbwarfare.item.gun.GunItem; @@ -114,8 +114,7 @@ public class Mk14Item extends GunItem implements GeoItem { ItemStack stack = player.getMainHandItem(); if (!stack.is(ModTags.Items.GUN)) return PlayState.STOP; - var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); - if (cap != null && cap.edit) { + if (player.getData(ModAttachments.PLAYER_VARIABLE).edit) { return event.setAndContinue(RawAnimation.begin().thenPlay("animation.m14.edit")); } diff --git a/src/main/java/com/atsuishio/superbwarfare/item/gun/rifle/Qbz95Item.java b/src/main/java/com/atsuishio/superbwarfare/item/gun/rifle/Qbz95Item.java index f78c38573..40abe219a 100644 --- a/src/main/java/com/atsuishio/superbwarfare/item/gun/rifle/Qbz95Item.java +++ b/src/main/java/com/atsuishio/superbwarfare/item/gun/rifle/Qbz95Item.java @@ -1,9 +1,9 @@ package com.atsuishio.superbwarfare.item.gun.rifle; import com.atsuishio.superbwarfare.Mod; -import com.atsuishio.superbwarfare.init.ModCapabilities; import com.atsuishio.superbwarfare.client.renderer.item.Qbz95ItemRenderer; import com.atsuishio.superbwarfare.event.ClientEventHandler; +import com.atsuishio.superbwarfare.init.ModAttachments; import com.atsuishio.superbwarfare.init.ModSounds; import com.atsuishio.superbwarfare.init.ModTags; import com.atsuishio.superbwarfare.item.gun.GunItem; @@ -117,8 +117,7 @@ public class Qbz95Item extends GunItem implements GeoItem { ItemStack stack = player.getMainHandItem(); if (!stack.is(ModTags.Items.GUN)) return PlayState.STOP; - var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); - if (cap != null && cap.edit) { + if (player.getData(ModAttachments.PLAYER_VARIABLE).edit) { return event.setAndContinue(RawAnimation.begin().thenPlay("animation.qbz95.edit")); } diff --git a/src/main/java/com/atsuishio/superbwarfare/item/gun/smg/VectorItem.java b/src/main/java/com/atsuishio/superbwarfare/item/gun/smg/VectorItem.java index 534d28d2b..5aa411c44 100644 --- a/src/main/java/com/atsuishio/superbwarfare/item/gun/smg/VectorItem.java +++ b/src/main/java/com/atsuishio/superbwarfare/item/gun/smg/VectorItem.java @@ -1,9 +1,9 @@ package com.atsuishio.superbwarfare.item.gun.smg; import com.atsuishio.superbwarfare.Mod; -import com.atsuishio.superbwarfare.init.ModCapabilities; import com.atsuishio.superbwarfare.client.renderer.item.VectorItemRenderer; import com.atsuishio.superbwarfare.event.ClientEventHandler; +import com.atsuishio.superbwarfare.init.ModAttachments; import com.atsuishio.superbwarfare.init.ModSounds; import com.atsuishio.superbwarfare.init.ModTags; import com.atsuishio.superbwarfare.item.gun.GunItem; @@ -87,8 +87,7 @@ public class VectorItem extends GunItem implements GeoItem { ItemStack stack = player.getMainHandItem(); if (!stack.is(ModTags.Items.GUN)) return PlayState.STOP; - var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); - if (cap != null && cap.edit) { + if (player.getData(ModAttachments.PLAYER_VARIABLE).edit) { return event.setAndContinue(RawAnimation.begin().thenPlay("animation.vector.edit")); } diff --git a/src/main/java/com/atsuishio/superbwarfare/item/gun/sniper/SvdItem.java b/src/main/java/com/atsuishio/superbwarfare/item/gun/sniper/SvdItem.java index 94c96b713..410d9da58 100644 --- a/src/main/java/com/atsuishio/superbwarfare/item/gun/sniper/SvdItem.java +++ b/src/main/java/com/atsuishio/superbwarfare/item/gun/sniper/SvdItem.java @@ -1,9 +1,9 @@ package com.atsuishio.superbwarfare.item.gun.sniper; import com.atsuishio.superbwarfare.Mod; -import com.atsuishio.superbwarfare.init.ModCapabilities; import com.atsuishio.superbwarfare.client.renderer.item.SvdItemRenderer; import com.atsuishio.superbwarfare.event.ClientEventHandler; +import com.atsuishio.superbwarfare.init.ModAttachments; import com.atsuishio.superbwarfare.init.ModSounds; import com.atsuishio.superbwarfare.init.ModTags; import com.atsuishio.superbwarfare.item.gun.GunItem; @@ -78,9 +78,7 @@ public class SvdItem extends GunItem implements GeoItem { ItemStack stack = player.getMainHandItem(); if (!stack.is(ModTags.Items.GUN)) return PlayState.STOP; - var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); - - if (cap != null && cap.edit) { + if (player.getData(ModAttachments.PLAYER_VARIABLE).edit) { return event.setAndContinue(RawAnimation.begin().thenPlay("animation.svd.edit")); } diff --git a/src/main/java/com/atsuishio/superbwarfare/item/gun/special/BocekItem.java b/src/main/java/com/atsuishio/superbwarfare/item/gun/special/BocekItem.java index 08fc0e1ce..51d0290c7 100644 --- a/src/main/java/com/atsuishio/superbwarfare/item/gun/special/BocekItem.java +++ b/src/main/java/com/atsuishio/superbwarfare/item/gun/special/BocekItem.java @@ -1,14 +1,10 @@ package com.atsuishio.superbwarfare.item.gun.special; import com.atsuishio.superbwarfare.Mod; -import com.atsuishio.superbwarfare.init.ModCapabilities; import com.atsuishio.superbwarfare.client.renderer.item.BocekItemRenderer; import com.atsuishio.superbwarfare.client.tooltip.component.BocekImageComponent; import com.atsuishio.superbwarfare.event.ClientEventHandler; -import com.atsuishio.superbwarfare.init.ModItems; -import com.atsuishio.superbwarfare.init.ModPerks; -import com.atsuishio.superbwarfare.init.ModSounds; -import com.atsuishio.superbwarfare.init.ModTags; +import com.atsuishio.superbwarfare.init.*; import com.atsuishio.superbwarfare.item.gun.GunItem; import com.atsuishio.superbwarfare.item.gun.SpecialFireWeapon; import com.atsuishio.superbwarfare.item.gun.data.GunData; @@ -183,8 +179,7 @@ public class BocekItem extends GunItem implements GeoItem, SpecialFireWeapon { } if (GunsTool.getGunDoubleTag(tag, "Power") >= 6) { - var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); - if (cap != null && cap.zoom) { + if (player.getData(ModAttachments.PLAYER_VARIABLE).zoom) { spawnBullet(player, tag); SoundTool.playLocalSound(player, ModSounds.BOCEK_ZOOM_FIRE_1P.get(), 10, 1); @@ -218,10 +213,9 @@ public class BocekItem extends GunItem implements GeoItem, SpecialFireWeapon { @Override public void fireOnPress(Player player, final GunData data) { - var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); - if (cap != null) { - cap.bowPullHold = true; - cap.syncPlayerVariables(player); - } + var cap = player.getData(ModAttachments.PLAYER_VARIABLE).watch(); + cap.bowPullHold = true; + player.setData(ModAttachments.PLAYER_VARIABLE, cap); + cap.sync(player); } } \ No newline at end of file diff --git a/src/main/java/com/atsuishio/superbwarfare/item/gun/special/TaserItem.java b/src/main/java/com/atsuishio/superbwarfare/item/gun/special/TaserItem.java index cbb93c433..bbb08ee90 100644 --- a/src/main/java/com/atsuishio/superbwarfare/item/gun/special/TaserItem.java +++ b/src/main/java/com/atsuishio/superbwarfare/item/gun/special/TaserItem.java @@ -1,15 +1,11 @@ package com.atsuishio.superbwarfare.item.gun.special; import com.atsuishio.superbwarfare.Mod; -import com.atsuishio.superbwarfare.init.ModCapabilities; import com.atsuishio.superbwarfare.client.renderer.item.TaserItemRenderer; import com.atsuishio.superbwarfare.client.tooltip.component.EnergyImageComponent; import com.atsuishio.superbwarfare.entity.projectile.TaserBulletEntity; import com.atsuishio.superbwarfare.event.ClientEventHandler; -import com.atsuishio.superbwarfare.init.ModItems; -import com.atsuishio.superbwarfare.init.ModPerks; -import com.atsuishio.superbwarfare.init.ModSounds; -import com.atsuishio.superbwarfare.init.ModTags; +import com.atsuishio.superbwarfare.init.*; import com.atsuishio.superbwarfare.item.EnergyStorageItem; import com.atsuishio.superbwarfare.item.gun.GunItem; import com.atsuishio.superbwarfare.item.gun.SpecialFireWeapon; @@ -225,7 +221,6 @@ public class TaserItem extends GunItem implements GeoItem, SpecialFireWeapon, En public void fireOnPress(Player player, final GunData data) { if (data.reloading()) return; ItemStack stack = data.stack(); - var tag = data.tag(); int perkLevel = data.perk.getLevel(ModPerks.VOLT_OVERLOAD); var energyStorage = stack.getCapability(Capabilities.EnergyStorage.ITEM); @@ -239,8 +234,7 @@ public class TaserItem extends GunItem implements GeoItem, SpecialFireWeapon, En player.getCooldowns().addCooldown(stack.getItem(), 5); if (player instanceof ServerPlayer serverPlayer) { - var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); - boolean zoom = cap != null && cap.zoom; + boolean zoom = player.getData(ModAttachments.PLAYER_VARIABLE).zoom; double spread = data.spread(); int volt = data.perk.getLevel(ModPerks.VOLT_OVERLOAD); diff --git a/src/main/java/com/atsuishio/superbwarfare/network/message/receive/PlayerVariablesSyncMessage.java b/src/main/java/com/atsuishio/superbwarfare/network/message/receive/PlayerVariablesSyncMessage.java index 288fcfc03..6fd924fa4 100644 --- a/src/main/java/com/atsuishio/superbwarfare/network/message/receive/PlayerVariablesSyncMessage.java +++ b/src/main/java/com/atsuishio/superbwarfare/network/message/receive/PlayerVariablesSyncMessage.java @@ -1,8 +1,8 @@ package com.atsuishio.superbwarfare.network.message.receive; import com.atsuishio.superbwarfare.Mod; -import com.atsuishio.superbwarfare.init.ModCapabilities; import com.atsuishio.superbwarfare.capability.player.PlayerVariable; +import com.atsuishio.superbwarfare.init.ModAttachments; import io.netty.buffer.ByteBuf; import net.minecraft.client.Minecraft; import net.minecraft.nbt.CompoundTag; @@ -31,26 +31,7 @@ public record PlayerVariablesSyncMessage(int target, CompoundTag data) implement var entity = Minecraft.getInstance().player.level().getEntity(message.target()); if (entity == null) return; - var variables = entity.getCapability(ModCapabilities.PLAYER_VARIABLE, null); - if (variables == null) return; - - variables.zoom = data.zoom; - variables.holdFire = data.holdFire; - variables.rifleAmmo = data.rifleAmmo; - variables.handgunAmmo = data.handgunAmmo; - variables.shotgunAmmo = data.shotgunAmmo; - variables.sniperAmmo = data.sniperAmmo; - variables.heavyAmmo = data.heavyAmmo; - variables.bowPullHold = data.bowPullHold; - variables.bowPull = data.bowPull; - variables.playerDoubleJump = data.playerDoubleJump; - variables.tacticalSprint = data.tacticalSprint; - variables.tacticalSprintTime = data.tacticalSprintTime; - variables.tacticalSprintExhaustion = data.tacticalSprintExhaustion; - variables.breath = data.breath; - variables.breathTime = data.breathTime; - variables.breathExhaustion = data.breathExhaustion; - variables.edit = data.edit; + entity.setData(ModAttachments.PLAYER_VARIABLE, data); } @Override diff --git a/src/main/java/com/atsuishio/superbwarfare/network/message/send/BreathMessage.java b/src/main/java/com/atsuishio/superbwarfare/network/message/send/BreathMessage.java index c9564d2f1..a7c530e15 100644 --- a/src/main/java/com/atsuishio/superbwarfare/network/message/send/BreathMessage.java +++ b/src/main/java/com/atsuishio/superbwarfare/network/message/send/BreathMessage.java @@ -1,7 +1,7 @@ package com.atsuishio.superbwarfare.network.message.send; import com.atsuishio.superbwarfare.Mod; -import com.atsuishio.superbwarfare.init.ModCapabilities; +import com.atsuishio.superbwarfare.init.ModAttachments; import io.netty.buffer.ByteBuf; import net.minecraft.network.codec.ByteBufCodecs; import net.minecraft.network.codec.StreamCodec; @@ -23,20 +23,21 @@ public record BreathMessage(boolean msgType) implements CustomPacketPayload { public static void handler(final BreathMessage message, final IPayloadContext context) { ServerPlayer player = (ServerPlayer) context.player(); - var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); - if (cap == null) return; + var cap = player.getData(ModAttachments.PLAYER_VARIABLE).watch(); if (message.msgType && !cap.breathExhaustion && cap.zoom && player.getPersistentData().getDouble("NoBreath") == 0 ) { cap.breath = true; - cap.syncPlayerVariables(player); + player.setData(ModAttachments.PLAYER_VARIABLE, cap); + cap.sync(player); } if (!message.msgType) { cap.breath = false; - cap.syncPlayerVariables(player); + player.setData(ModAttachments.PLAYER_VARIABLE, cap); + cap.sync(player); } } diff --git a/src/main/java/com/atsuishio/superbwarfare/network/message/send/DoubleJumpMessage.java b/src/main/java/com/atsuishio/superbwarfare/network/message/send/DoubleJumpMessage.java index 8e1e0be3d..97068344e 100644 --- a/src/main/java/com/atsuishio/superbwarfare/network/message/send/DoubleJumpMessage.java +++ b/src/main/java/com/atsuishio/superbwarfare/network/message/send/DoubleJumpMessage.java @@ -1,7 +1,7 @@ package com.atsuishio.superbwarfare.network.message.send; import com.atsuishio.superbwarfare.Mod; -import com.atsuishio.superbwarfare.init.ModCapabilities; +import com.atsuishio.superbwarfare.init.ModAttachments; import com.atsuishio.superbwarfare.init.ModSounds; import io.netty.buffer.ByteBuf; import net.minecraft.core.BlockPos; @@ -33,11 +33,11 @@ public record DoubleJumpMessage(boolean canDoubleJump) implements CustomPacketPa level.playSound(null, BlockPos.containing(x, y, z), ModSounds.DOUBLE_JUMP.get(), SoundSource.BLOCKS, 1, 1); - var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); - if (cap == null) return; + var cap = player.getData(ModAttachments.PLAYER_VARIABLE).watch(); cap.playerDoubleJump = message.canDoubleJump; - cap.syncPlayerVariables(player); + player.setData(ModAttachments.PLAYER_VARIABLE, cap); + cap.sync(player); } @Override diff --git a/src/main/java/com/atsuishio/superbwarfare/network/message/send/EditModeMessage.java b/src/main/java/com/atsuishio/superbwarfare/network/message/send/EditModeMessage.java index 34c81b82f..cd14a4793 100644 --- a/src/main/java/com/atsuishio/superbwarfare/network/message/send/EditModeMessage.java +++ b/src/main/java/com/atsuishio/superbwarfare/network/message/send/EditModeMessage.java @@ -1,7 +1,7 @@ package com.atsuishio.superbwarfare.network.message.send; import com.atsuishio.superbwarfare.Mod; -import com.atsuishio.superbwarfare.init.ModCapabilities; +import com.atsuishio.superbwarfare.init.ModAttachments; import com.atsuishio.superbwarfare.init.ModSounds; import com.atsuishio.superbwarfare.item.gun.GunItem; import com.atsuishio.superbwarfare.tools.SoundTool; @@ -33,14 +33,15 @@ public record EditModeMessage(int msgType) implements CustomPacketPayload { ItemStack mainHandItem = player.getMainHandItem(); if (!(mainHandItem.getItem() instanceof GunItem gunItem)) return; - var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); + var cap = player.getData(ModAttachments.PLAYER_VARIABLE).watch(); - if (gunItem.isCustomizable(mainHandItem) && cap != null) { + if (gunItem.isCustomizable(mainHandItem)) { if (!cap.edit) { SoundTool.playLocalSound(player, ModSounds.EDIT_MODE.get(), 1f, 1f); } cap.edit = !cap.edit; - cap.syncPlayerVariables(player); + player.setData(ModAttachments.PLAYER_VARIABLE, cap); + cap.sync(player); } } diff --git a/src/main/java/com/atsuishio/superbwarfare/network/message/send/FireMessage.java b/src/main/java/com/atsuishio/superbwarfare/network/message/send/FireMessage.java index 310e3d439..bc0d29cf5 100644 --- a/src/main/java/com/atsuishio/superbwarfare/network/message/send/FireMessage.java +++ b/src/main/java/com/atsuishio/superbwarfare/network/message/send/FireMessage.java @@ -1,9 +1,9 @@ package com.atsuishio.superbwarfare.network.message.send; import com.atsuishio.superbwarfare.Mod; -import com.atsuishio.superbwarfare.init.ModCapabilities; import com.atsuishio.superbwarfare.entity.projectile.ProjectileEntity; import com.atsuishio.superbwarfare.event.GunEventHandler; +import com.atsuishio.superbwarfare.init.ModAttachments; import com.atsuishio.superbwarfare.init.ModPerks; import com.atsuishio.superbwarfare.init.ModTags; import com.atsuishio.superbwarfare.item.gun.SpecialFireWeapon; @@ -49,39 +49,36 @@ public record FireMessage(int msgType) implements CustomPacketPayload { handleGunBolt(player, stack); - var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); + var cap = player.getData(ModAttachments.PLAYER_VARIABLE).watch(); + if (type == 0) { if (tag.getDouble("PrepareTime") == 0 && data.reloading() && data.ammo() > 0) { tag.putBoolean("ForceStop", true); } - if (cap != null) { - cap.edit = false; - } + cap.edit = false; // 按下开火 if (!(stack.getItem() instanceof SpecialFireWeapon specialFireWeapon)) { - if (cap != null) cap.syncPlayerVariables(player); + player.setData(ModAttachments.PLAYER_VARIABLE, cap); + cap.sync(player); return; } specialFireWeapon.fireOnPress(player, data); - if (cap != null) { - cap.holdFire = true; - cap.syncPlayerVariables(player); - } + cap.holdFire = true; + player.setData(ModAttachments.PLAYER_VARIABLE, cap); } else if (type == 1) { - if (cap != null) { - cap.bowPullHold = false; - cap.holdFire = false; - cap.syncPlayerVariables(player); - } + cap.bowPullHold = false; + cap.holdFire = false; + player.setData(ModAttachments.PLAYER_VARIABLE, cap); // 松开开火 if (stack.getItem() instanceof SpecialFireWeapon specialFireWeapon) { specialFireWeapon.fireOnRelease(player, data); } } + cap.sync(player); data.save(); } @@ -122,8 +119,7 @@ public record FireMessage(int msgType) implements CustomPacketPayload { float bypassArmorRate = (float) data.bypassArmor(); double damage; - var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); - boolean zoom = cap != null && cap.zoom; + boolean zoom = player.getData(ModAttachments.PLAYER_VARIABLE).zoom; float spread; if (zoom) { diff --git a/src/main/java/com/atsuishio/superbwarfare/network/message/send/ReloadMessage.java b/src/main/java/com/atsuishio/superbwarfare/network/message/send/ReloadMessage.java index e1b963d84..0a68941d7 100644 --- a/src/main/java/com/atsuishio/superbwarfare/network/message/send/ReloadMessage.java +++ b/src/main/java/com/atsuishio/superbwarfare/network/message/send/ReloadMessage.java @@ -1,7 +1,7 @@ package com.atsuishio.superbwarfare.network.message.send; import com.atsuishio.superbwarfare.Mod; -import com.atsuishio.superbwarfare.init.ModCapabilities; +import com.atsuishio.superbwarfare.init.ModAttachments; import com.atsuishio.superbwarfare.init.ModItems; import com.atsuishio.superbwarfare.init.ModTags; import com.atsuishio.superbwarfare.item.gun.GunItem; @@ -31,11 +31,10 @@ public record ReloadMessage(int msgType) implements CustomPacketPayload { public static void pressAction(Player player, int type) { if (type != 0) return; - var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); - if (cap != null) { - cap.edit = false; - cap.syncPlayerVariables(player); - } + var cap = player.getData(ModAttachments.PLAYER_VARIABLE).watch(); + cap.edit = false; + player.setData(ModAttachments.PLAYER_VARIABLE, cap); + cap.sync(player); ItemStack stack = player.getMainHandItem(); if (!(stack.getItem() instanceof GunItem gunItem)) return; @@ -56,7 +55,7 @@ public record ReloadMessage(int msgType) implements CustomPacketPayload { // 检查备弹 boolean hasCreativeAmmoBox = player.getInventory().hasAnyMatching(item -> item.is(ModItems.CREATIVE_AMMO_BOX.get())); - if (!hasCreativeAmmoBox && cap != null) { + if (!hasCreativeAmmoBox) { if (stack.is(ModTags.Items.USE_SHOTGUN_AMMO) && cap.shotgunAmmo == 0) { return; } else if (stack.is(ModTags.Items.USE_SNIPER_AMMO) && cap.sniperAmmo == 0) { diff --git a/src/main/java/com/atsuishio/superbwarfare/network/message/send/ShootMessage.java b/src/main/java/com/atsuishio/superbwarfare/network/message/send/ShootMessage.java index df8d424e7..a34dae908 100644 --- a/src/main/java/com/atsuishio/superbwarfare/network/message/send/ShootMessage.java +++ b/src/main/java/com/atsuishio/superbwarfare/network/message/send/ShootMessage.java @@ -1,12 +1,8 @@ package com.atsuishio.superbwarfare.network.message.send; import com.atsuishio.superbwarfare.Mod; -import com.atsuishio.superbwarfare.init.ModCapabilities; import com.atsuishio.superbwarfare.event.GunEventHandler; -import com.atsuishio.superbwarfare.init.ModItems; -import com.atsuishio.superbwarfare.init.ModPerks; -import com.atsuishio.superbwarfare.init.ModSounds; -import com.atsuishio.superbwarfare.init.ModTags; +import com.atsuishio.superbwarfare.init.*; import com.atsuishio.superbwarfare.item.gun.data.GunData; import com.atsuishio.superbwarfare.perk.AmmoPerk; import com.atsuishio.superbwarfare.perk.Perk; @@ -96,8 +92,7 @@ public record ShootMessage(double spread) implements CustomPacketPayload { GunEventHandler.playGunSounds(player); } } else if (stack.is(ModItems.MINIGUN.get())) { - var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); - if (cap == null) return; + var cap = player.getData(ModAttachments.PLAYER_VARIABLE).watch(); if (cap.rifleAmmo > 0 || InventoryTool.hasCreativeAmmoBox(player)) { tag.putDouble("heat", (tag.getDouble("heat") + 0.1)); @@ -127,7 +122,8 @@ public record ShootMessage(double spread) implements CustomPacketPayload { GunEventHandler.gunShoot(player, tag, spared); if (!InventoryTool.hasCreativeAmmoBox(player)) { cap.rifleAmmo = cap.rifleAmmo - 1; - cap.syncPlayerVariables(player); + player.setData(ModAttachments.PLAYER_VARIABLE, cap); + cap.sync(player); } } } diff --git a/src/main/java/com/atsuishio/superbwarfare/network/message/send/ZoomMessage.java b/src/main/java/com/atsuishio/superbwarfare/network/message/send/ZoomMessage.java index f8411226f..5c3e8ffed 100644 --- a/src/main/java/com/atsuishio/superbwarfare/network/message/send/ZoomMessage.java +++ b/src/main/java/com/atsuishio/superbwarfare/network/message/send/ZoomMessage.java @@ -1,9 +1,9 @@ package com.atsuishio.superbwarfare.network.message.send; import com.atsuishio.superbwarfare.Mod; -import com.atsuishio.superbwarfare.init.ModCapabilities; import com.atsuishio.superbwarfare.entity.vehicle.base.VehicleEntity; import com.atsuishio.superbwarfare.entity.vehicle.base.WeaponVehicleEntity; +import com.atsuishio.superbwarfare.init.ModAttachments; import com.atsuishio.superbwarfare.init.ModItems; import com.atsuishio.superbwarfare.init.ModSounds; import com.atsuishio.superbwarfare.item.gun.data.GunData; @@ -32,14 +32,13 @@ public record ZoomMessage(int msgType) implements CustomPacketPayload { var vehicle = player.getVehicle(); // 缩放音效播放条件: 载具是武器载具,且该位置有可用武器 - var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); + var cap = player.getData(ModAttachments.PLAYER_VARIABLE).watch(); if (message.msgType == 0) { - if (cap != null) { - cap.zoom = true; - cap.edit = false; - cap.syncPlayerVariables(player); - } + cap.zoom = true; + cap.edit = false; + player.setData(ModAttachments.PLAYER_VARIABLE, cap); + cap.sync(player); if (player.isPassenger() && vehicle instanceof WeaponVehicleEntity weaponEntity @@ -48,11 +47,10 @@ public record ZoomMessage(int msgType) implements CustomPacketPayload { ) SoundTool.playLocalSound(player, ModSounds.CANNON_ZOOM_IN.get(), 2, 1); } else if (message.msgType == 1) { - if (cap != null) { - cap.zoom = false; - cap.breath = false; - cap.syncPlayerVariables(player); - } + cap.zoom = false; + cap.breath = false; + player.setData(ModAttachments.PLAYER_VARIABLE, cap); + cap.sync(player); if (player.isPassenger() && vehicle instanceof WeaponVehicleEntity weaponEntity diff --git a/src/main/java/com/atsuishio/superbwarfare/tools/AmmoType.java b/src/main/java/com/atsuishio/superbwarfare/tools/AmmoType.java index 67657a8c3..2d3ce73a1 100644 --- a/src/main/java/com/atsuishio/superbwarfare/tools/AmmoType.java +++ b/src/main/java/com/atsuishio/superbwarfare/tools/AmmoType.java @@ -1,7 +1,7 @@ package com.atsuishio.superbwarfare.tools; -import com.atsuishio.superbwarfare.init.ModCapabilities; import com.atsuishio.superbwarfare.capability.player.PlayerVariable; +import com.atsuishio.superbwarfare.init.ModAttachments; import net.minecraft.core.component.DataComponentType; import net.minecraft.nbt.CompoundTag; import net.minecraft.world.entity.Entity; @@ -90,18 +90,17 @@ public enum AmmoType { // Entity public int get(Entity entity) { - var cap = entity.getCapability(ModCapabilities.PLAYER_VARIABLE); - if (cap == null) return 0; + var cap = entity.getData(ModAttachments.PLAYER_VARIABLE); return get(cap); } public void set(Entity entity, int count) { - var cap = entity.getCapability(ModCapabilities.PLAYER_VARIABLE); - if (cap == null) return; + var cap = entity.getData(ModAttachments.PLAYER_VARIABLE).watch(); set(cap, count); - cap.syncPlayerVariables(entity); + entity.setData(ModAttachments.PLAYER_VARIABLE, cap); + cap.sync(entity); } public void add(Entity entity, int count) { diff --git a/src/main/java/com/atsuishio/superbwarfare/tools/GunsTool.java b/src/main/java/com/atsuishio/superbwarfare/tools/GunsTool.java index faf7ea226..c16cec6ef 100644 --- a/src/main/java/com/atsuishio/superbwarfare/tools/GunsTool.java +++ b/src/main/java/com/atsuishio/superbwarfare/tools/GunsTool.java @@ -1,7 +1,7 @@ package com.atsuishio.superbwarfare.tools; import com.atsuishio.superbwarfare.Mod; -import com.atsuishio.superbwarfare.init.ModCapabilities; +import com.atsuishio.superbwarfare.init.ModAttachments; import com.atsuishio.superbwarfare.init.ModTags; import com.atsuishio.superbwarfare.item.gun.data.GunData; import com.atsuishio.superbwarfare.item.gun.data.ReloadState; @@ -88,15 +88,14 @@ public class GunsTool { data.bolt.markNeedless(); } - var capability = player.getCapability(ModCapabilities.PLAYER_VARIABLE); + var capability = player.getData(ModAttachments.PLAYER_VARIABLE).watch(); var playerAmmo = 0; - if (capability != null) { - playerAmmo = type.get(capability); - var newAmmoCount = Math.max(0, playerAmmo - ammoToAdd); - type.set(capability, newAmmoCount); - capability.syncPlayerVariables(player); - } + playerAmmo = type.get(capability); + var newAmmoCount = Math.max(0, playerAmmo - ammoToAdd); + type.set(capability, newAmmoCount); + player.setData(ModAttachments.PLAYER_VARIABLE, capability); + capability.sync(player); int needToAdd = ammo + Math.min(ammoToAdd, playerAmmo); @@ -117,18 +116,6 @@ public class GunsTool { return tag.getInt(name); } - public static void setPerkDoubleTag(final CompoundTag rootTag, String name, double num) { - CompoundTag tag = rootTag.getCompound("PerkData"); - if (!tag.contains(name) && num == 0) return; - tag.putDouble(name, num); - rootTag.put("PerkData", tag); - } - - public static double getPerkDoubleTag(final CompoundTag rootTag, String name) { - CompoundTag tag = rootTag.getCompound("PerkData"); - return tag.getDouble(name); - } - public static void setPerkBooleanTag(final CompoundTag rootTag, String name, boolean flag) { CompoundTag tag = rootTag.getCompound("PerkData"); if (!tag.contains(name) && !flag) return;