正确实现PlayerVariables,优化同步机制

This commit is contained in:
Light_Quanta 2025-04-08 10:50:39 +08:00
parent 392a72aa93
commit 6910646584
No known key found for this signature in database
GPG key ID: 11A39A1B8C890959
50 changed files with 333 additions and 413 deletions

View file

@ -56,6 +56,7 @@ public class Mod {
ModArmorMaterials.MATERIALS.register(bus); ModArmorMaterials.MATERIALS.register(bus);
ModAttributes.ATTRIBUTES.register(bus); ModAttributes.ATTRIBUTES.register(bus);
ModCriteriaTriggers.REGISTRY.register(bus); ModCriteriaTriggers.REGISTRY.register(bus);
ModAttachments.ATTACHMENT_TYPES.register(bus);
// bus.addListener(this::onCommonSetup); // bus.addListener(this::onCommonSetup);
bus.addListener(this::onClientSetup); bus.addListener(this::onClientSetup);

View file

@ -1,8 +1,8 @@
package com.atsuishio.superbwarfare.block; package com.atsuishio.superbwarfare.block;
import com.atsuishio.superbwarfare.init.ModCapabilities;
import com.atsuishio.superbwarfare.entity.TargetEntity; import com.atsuishio.superbwarfare.entity.TargetEntity;
import com.atsuishio.superbwarfare.entity.vehicle.base.CannonEntity; import com.atsuishio.superbwarfare.entity.vehicle.base.CannonEntity;
import com.atsuishio.superbwarfare.init.ModAttachments;
import com.atsuishio.superbwarfare.init.ModSounds; import com.atsuishio.superbwarfare.init.ModSounds;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction; 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); level.playLocalSound(pos.getX(), pos.getY(), pos.getZ(), ModSounds.JUMP.get(), SoundSource.BLOCKS, 1, 1, false);
} }
var capability = entity.getCapability(ModCapabilities.PLAYER_VARIABLE); var capability = entity.getData(ModAttachments.PLAYER_VARIABLE).watch();
if (capability != null) { capability.playerDoubleJump = true;
capability.playerDoubleJump = true;
capability.syncPlayerVariables(entity); entity.setData(ModAttachments.PLAYER_VARIABLE, capability);
} capability.sync(entity);
} }
} }

View file

@ -1,21 +1,27 @@
package com.atsuishio.superbwarfare.capability.player; package com.atsuishio.superbwarfare.capability.player;
import com.atsuishio.superbwarfare.Mod; 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.network.message.receive.PlayerVariablesSyncMessage;
import com.atsuishio.superbwarfare.tools.AmmoType; import com.atsuishio.superbwarfare.tools.AmmoType;
import net.minecraft.core.HolderLookup;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.Tag;
import net.minecraft.server.level.ServerPlayer; import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.Entity;
import net.neoforged.bus.api.SubscribeEvent; import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.EventBusSubscriber; import net.neoforged.fml.common.EventBusSubscriber;
import net.neoforged.neoforge.common.util.INBTSerializable;
import net.neoforged.neoforge.event.entity.player.PlayerEvent; import net.neoforged.neoforge.event.entity.player.PlayerEvent;
import net.neoforged.neoforge.network.PacketDistributor; import net.neoforged.neoforge.network.PacketDistributor;
import org.jetbrains.annotations.NotNull;
import javax.annotation.ParametersAreNonnullByDefault;
// TODO 在退出世界时正确持久化弹药数量
@EventBusSubscriber(modid = Mod.MODID) @EventBusSubscriber(modid = Mod.MODID)
public class PlayerVariable { public class PlayerVariable implements INBTSerializable<CompoundTag> {
private PlayerVariable old = null;
public boolean zoom = false; public boolean zoom = false;
public boolean holdFire = false; public boolean holdFire = false;
public int rifleAmmo = 0; public int rifleAmmo = 0;
@ -34,12 +40,29 @@ public class PlayerVariable {
public boolean breathExhaustion = false; public boolean breathExhaustion = false;
public boolean edit = 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) { 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() { public CompoundTag writeToNBT() {
CompoundTag nbt = new CompoundTag(); CompoundTag nbt = new CompoundTag();
nbt.putBoolean("Zoom", zoom); nbt.putBoolean("Zoom", zoom);
@ -63,86 +86,92 @@ public class PlayerVariable {
return nbt; return nbt;
} }
public PlayerVariable readFromNBT(Tag tag) { public PlayerVariable readFromNBT(CompoundTag tag) {
CompoundTag nbt = (CompoundTag) tag; zoom = tag.getBoolean("Zoom");
holdFire = tag.getBoolean("HoldFire");
zoom = nbt.getBoolean("Zoom");
holdFire = nbt.getBoolean("HoldFire");
for (var type : AmmoType.values()) { for (var type : AmmoType.values()) {
type.set(this, type.get(nbt)); type.set(this, type.get(tag));
} }
bowPullHold = nbt.getBoolean("BowPullHold"); bowPullHold = tag.getBoolean("BowPullHold");
bowPull = nbt.getBoolean("BowPull"); bowPull = tag.getBoolean("BowPull");
playerDoubleJump = nbt.getBoolean("DoubleJump"); playerDoubleJump = tag.getBoolean("DoubleJump");
tacticalSprint = nbt.getBoolean("TacticalSprint"); tacticalSprint = tag.getBoolean("TacticalSprint");
tacticalSprintTime = nbt.getInt("TacticalSprintTime"); tacticalSprintTime = tag.getInt("TacticalSprintTime");
tacticalSprintExhaustion = nbt.getBoolean("TacticalSprintExhaustion"); tacticalSprintExhaustion = tag.getBoolean("TacticalSprintExhaustion");
breath = nbt.getBoolean("Breath"); breath = tag.getBoolean("Breath");
breathTime = nbt.getInt("BreathTime"); breathTime = tag.getInt("BreathTime");
breathExhaustion = nbt.getBoolean("BreathExhaustion"); breathExhaustion = tag.getBoolean("BreathExhaustion");
edit = nbt.getBoolean("EditMode"); edit = tag.getBoolean("EditMode");
return this; return this;
} }
@SubscribeEvent public PlayerVariable copy() {
public static void onPlayerLoggedInSyncPlayerVariables(PlayerEvent.PlayerLoggedInEvent event) { var clone = new PlayerVariable();
if (event.getEntity().level().isClientSide()) return;
var player = event.getEntity(); clone.zoom = this.zoom;
var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE, null); clone.holdFire = this.holdFire;
if (cap != null) cap.syncPlayerVariables(player); 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 @Override
public static void onPlayerRespawnedSyncPlayerVariables(PlayerEvent.PlayerRespawnEvent event) { public boolean equals(Object obj) {
if (event.getEntity().level().isClientSide()) return; if (!(obj instanceof PlayerVariable other)) return false;
var player = event.getEntity(); return zoom == other.zoom
var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE, null); && holdFire == other.holdFire
if (cap != null) cap.syncPlayerVariables(player); && rifleAmmo == other.rifleAmmo
} && handgunAmmo == other.handgunAmmo
&& shotgunAmmo == other.shotgunAmmo
@SubscribeEvent && sniperAmmo == other.sniperAmmo
public static void onPlayerChangedDimensionSyncPlayerVariables(PlayerEvent.PlayerChangedDimensionEvent event) { && heavyAmmo == other.heavyAmmo
if (event.getEntity().level().isClientSide()) return; && bowPullHold == other.bowPullHold
&& bowPull == other.bowPull
var player = event.getEntity(); && playerDoubleJump == other.playerDoubleJump
var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE, null); && tacticalSprint == other.tacticalSprint
if (cap != null) cap.syncPlayerVariables(player); // && tacticalSprintTime == other.tacticalSprintTime
&& tacticalSprintExhaustion == other.tacticalSprintExhaustion
&& breath == other.breath
// && breathTime == other.breathTime
&& breathExhaustion == other.breathExhaustion
&& edit == other.edit;
} }
@SubscribeEvent @SubscribeEvent
public static void clonePlayer(PlayerEvent.Clone event) { public static void clonePlayer(PlayerEvent.Clone event) {
event.getOriginal().revive(); event.getOriginal().revive();
var original = event.getOriginal().getCapability(ModCapabilities.PLAYER_VARIABLE, null); var original = event.getOriginal().getData(ModAttachments.PLAYER_VARIABLE);
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;
if (event.getEntity().level().isClientSide()) return; if (event.getEntity().level().isClientSide()) return;
var player = event.getEntity(); event.getEntity().setData(ModAttachments.PLAYER_VARIABLE, original.copy());
var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE, null); }
if (cap != null) cap.syncPlayerVariables(player);
@Override
public CompoundTag serializeNBT(HolderLookup.@NotNull Provider provider) {
return writeToNBT();
}
@Override
@ParametersAreNonnullByDefault
public void deserializeNBT(HolderLookup.Provider provider, CompoundTag nbt) {
readFromNBT(nbt);
} }
} }

View file

@ -1,6 +1,5 @@
package com.atsuishio.superbwarfare.client; package com.atsuishio.superbwarfare.client;
import com.atsuishio.superbwarfare.init.ModCapabilities;
import com.atsuishio.superbwarfare.config.client.ReloadConfig; import com.atsuishio.superbwarfare.config.client.ReloadConfig;
import com.atsuishio.superbwarfare.entity.MortarEntity; import com.atsuishio.superbwarfare.entity.MortarEntity;
import com.atsuishio.superbwarfare.entity.vehicle.base.ArmedVehicleEntity; import com.atsuishio.superbwarfare.entity.vehicle.base.ArmedVehicleEntity;
@ -224,8 +223,7 @@ public class ClickHandler {
PacketDistributor.sendToServer(new EditModeMessage(0)); PacketDistributor.sendToServer(new EditModeMessage(0));
} }
var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); if (player.getData(ModAttachments.PLAYER_VARIABLE).edit) {
if (cap != null && cap.edit) {
if (!(stack.getItem() instanceof GunItem gunItem)) return; if (!(stack.getItem() instanceof GunItem gunItem)) return;
if (ModKeyMappings.EDIT_GRIP.getKeyModifier().isActive(KeyConflictContext.IN_GAME)) { if (ModKeyMappings.EDIT_GRIP.getKeyModifier().isActive(KeyConflictContext.IN_GAME)) {
if (key == ModKeyMappings.EDIT_GRIP.getKey().getValue() && gunItem.hasCustomGrip(stack)) { if (key == ModKeyMappings.EDIT_GRIP.getKey().getValue() && gunItem.hasCustomGrip(stack)) {
@ -402,8 +400,7 @@ public class ClickHandler {
return; return;
} }
var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); if (player.getData(ModAttachments.PLAYER_VARIABLE).playerDoubleJump) {
if (cap != null && cap.playerDoubleJump) {
player.setDeltaMovement(new Vec3(player.getLookAngle().x, 0.8, player.getLookAngle().z)); 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); level.playLocalSound(x, y, z, ModSounds.DOUBLE_JUMP.get(), SoundSource.BLOCKS, 1, 1, false);

View file

@ -1,10 +1,9 @@
package com.atsuishio.superbwarfare.client.overlay; package com.atsuishio.superbwarfare.client.overlay;
import com.atsuishio.superbwarfare.Mod; 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.config.client.DisplayConfig;
import com.atsuishio.superbwarfare.entity.vehicle.base.ArmedVehicleEntity; 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.ModItems;
import com.atsuishio.superbwarfare.init.ModKeyMappings; import com.atsuishio.superbwarfare.init.ModKeyMappings;
import com.atsuishio.superbwarfare.init.ModTags; import com.atsuishio.superbwarfare.init.ModTags;
@ -47,8 +46,7 @@ public class AmmoBarOverlay implements LayeredDraw.Layer {
ItemStack stack = player.getMainHandItem(); ItemStack stack = player.getMainHandItem();
if (stack.getItem() == ModItems.MINIGUN.get()) { if (stack.getItem() == ModItems.MINIGUN.get()) {
var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); return player.getData(ModAttachments.PLAYER_VARIABLE).rifleAmmo;
return cap != null ? cap.rifleAmmo : 0;
} }
if (stack.getItem() == ModItems.BOCEK.get()) { if (stack.getItem() == ModItems.BOCEK.get()) {
@ -65,8 +63,7 @@ public class AmmoBarOverlay implements LayeredDraw.Layer {
return ""; return "";
} }
var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); var cap = player.getData(ModAttachments.PLAYER_VARIABLE);
if (cap == null) cap = new PlayerVariable();
if (!hasCreativeAmmo()) { if (!hasCreativeAmmo()) {
var data = GunData.from(stack); var data = GunData.from(stack);
if (stack.is(ModTags.Items.LAUNCHER) || stack.getItem() == ModItems.TASER.get()) { if (stack.is(ModTags.Items.LAUNCHER) || stack.getItem() == ModItems.TASER.get()) {

View file

@ -1,10 +1,9 @@
package com.atsuishio.superbwarfare.client.overlay; package com.atsuishio.superbwarfare.client.overlay;
import com.atsuishio.superbwarfare.Mod; 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.component.ModDataComponents;
import com.atsuishio.superbwarfare.entity.vehicle.base.ArmedVehicleEntity; 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.ModItems;
import com.atsuishio.superbwarfare.item.common.ammo.AmmoSupplierItem; import com.atsuishio.superbwarfare.item.common.ammo.AmmoSupplierItem;
import com.atsuishio.superbwarfare.tools.AmmoType; 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 yOffset = (-h - AmmoType.values().length * fontHeight) / 2f;
// 渲染总弹药数量 // 渲染总弹药数量
var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); var cap = player.getData(ModAttachments.PLAYER_VARIABLE);
if (cap == null) cap = new PlayerVariable();
var font = Minecraft.getInstance().font; var font = Minecraft.getInstance().font;
for (var type : AmmoType.values()) { for (var type : AmmoType.values()) {

View file

@ -1,9 +1,9 @@
package com.atsuishio.superbwarfare.client.overlay; package com.atsuishio.superbwarfare.client.overlay;
import com.atsuishio.superbwarfare.Mod; import com.atsuishio.superbwarfare.Mod;
import com.atsuishio.superbwarfare.init.ModCapabilities;
import com.atsuishio.superbwarfare.component.ModDataComponents; import com.atsuishio.superbwarfare.component.ModDataComponents;
import com.atsuishio.superbwarfare.entity.vehicle.base.ArmedVehicleEntity; 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.ModItems;
import com.atsuishio.superbwarfare.item.common.ammo.AmmoSupplierItem; import com.atsuishio.superbwarfare.item.common.ammo.AmmoSupplierItem;
import com.atsuishio.superbwarfare.item.common.ammo.box.AmmoBoxInfo; 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 yOffset = (-h - AmmoType.values().length * fontHeight) / 2f;
// 渲染总弹药数量 // 渲染总弹药数量
var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE, null); var cap = player.getData(ModAttachments.PLAYER_VARIABLE);
if (cap == null) return;
var font = Minecraft.getInstance().font; var font = Minecraft.getInstance().font;
for (var type : AmmoType.values()) { for (var type : AmmoType.values()) {

View file

@ -1,11 +1,11 @@
package com.atsuishio.superbwarfare.client.overlay; package com.atsuishio.superbwarfare.client.overlay;
import com.atsuishio.superbwarfare.Mod; import com.atsuishio.superbwarfare.Mod;
import com.atsuishio.superbwarfare.init.ModCapabilities;
import com.atsuishio.superbwarfare.config.client.DisplayConfig; import com.atsuishio.superbwarfare.config.client.DisplayConfig;
import com.atsuishio.superbwarfare.entity.vehicle.Ah6Entity; import com.atsuishio.superbwarfare.entity.vehicle.Ah6Entity;
import com.atsuishio.superbwarfare.entity.vehicle.base.ArmedVehicleEntity; import com.atsuishio.superbwarfare.entity.vehicle.base.ArmedVehicleEntity;
import com.atsuishio.superbwarfare.event.ClientEventHandler; import com.atsuishio.superbwarfare.event.ClientEventHandler;
import com.atsuishio.superbwarfare.init.ModAttachments;
import com.atsuishio.superbwarfare.init.ModItems; import com.atsuishio.superbwarfare.init.ModItems;
import com.atsuishio.superbwarfare.init.ModTags; import com.atsuishio.superbwarfare.init.ModTags;
import com.atsuishio.superbwarfare.item.gun.data.GunData; import com.atsuishio.superbwarfare.item.gun.data.GunData;
@ -54,8 +54,7 @@ public class CrossHairOverlay {
return; return;
} }
var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); if (player.getData(ModAttachments.PLAYER_VARIABLE).edit) return;
if (cap != null && cap.edit) return;
if (!player.getMainHandItem().is(ModTags.Items.GUN) || (player.getVehicle() instanceof ArmedVehicleEntity iArmedVehicle && iArmedVehicle.banHand(player))) if (!player.getMainHandItem().is(ModTags.Items.GUN) || (player.getVehicle() instanceof ArmedVehicleEntity iArmedVehicle && iArmedVehicle.banHand(player)))
return; return;

View file

@ -1,10 +1,10 @@
package com.atsuishio.superbwarfare.client.overlay; package com.atsuishio.superbwarfare.client.overlay;
import com.atsuishio.superbwarfare.Mod; 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.ArmedVehicleEntity;
import com.atsuishio.superbwarfare.entity.vehicle.base.CannonEntity; import com.atsuishio.superbwarfare.entity.vehicle.base.CannonEntity;
import com.atsuishio.superbwarfare.event.ClientEventHandler; import com.atsuishio.superbwarfare.event.ClientEventHandler;
import com.atsuishio.superbwarfare.init.ModAttachments;
import com.atsuishio.superbwarfare.init.ModItems; import com.atsuishio.superbwarfare.init.ModItems;
import com.mojang.blaze3d.platform.GlStateManager; import com.mojang.blaze3d.platform.GlStateManager;
import com.mojang.blaze3d.systems.RenderSystem; import com.mojang.blaze3d.systems.RenderSystem;
@ -28,8 +28,8 @@ public class GrenadeLauncherOverlay {
Player player = Minecraft.getInstance().player; Player player = Minecraft.getInstance().player;
if (player == null) return; if (player == null) return;
var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); var cap = player.getData(ModAttachments.PLAYER_VARIABLE);
if (cap == null || cap.edit) return; if (cap.edit) return;
if (player.getVehicle() instanceof ArmedVehicleEntity iArmedVehicle && iArmedVehicle.banHand(player)) if (player.getVehicle() instanceof ArmedVehicleEntity iArmedVehicle && iArmedVehicle.banHand(player))
return; return;

View file

@ -1,10 +1,10 @@
package com.atsuishio.superbwarfare.client.overlay; package com.atsuishio.superbwarfare.client.overlay;
import com.atsuishio.superbwarfare.Mod; import com.atsuishio.superbwarfare.Mod;
import com.atsuishio.superbwarfare.init.ModCapabilities;
import com.atsuishio.superbwarfare.client.RenderHelper; import com.atsuishio.superbwarfare.client.RenderHelper;
import com.atsuishio.superbwarfare.entity.vehicle.base.ArmedVehicleEntity; import com.atsuishio.superbwarfare.entity.vehicle.base.ArmedVehicleEntity;
import com.atsuishio.superbwarfare.event.ClientEventHandler; import com.atsuishio.superbwarfare.event.ClientEventHandler;
import com.atsuishio.superbwarfare.init.ModAttachments;
import com.atsuishio.superbwarfare.init.ModPerks; import com.atsuishio.superbwarfare.init.ModPerks;
import com.atsuishio.superbwarfare.item.gun.GunItem; import com.atsuishio.superbwarfare.item.gun.GunItem;
import com.atsuishio.superbwarfare.item.gun.data.GunData; import com.atsuishio.superbwarfare.item.gun.data.GunData;
@ -43,8 +43,7 @@ public class HandsomeFrameOverlay {
if (player != null) { if (player != null) {
ItemStack stack = player.getMainHandItem(); ItemStack stack = player.getMainHandItem();
var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); if (player.getData(ModAttachments.PLAYER_VARIABLE).edit) return;
if (cap == null || cap.edit) return;
if (player.getVehicle() instanceof ArmedVehicleEntity iArmedVehicle && iArmedVehicle.banHand(player)) if (player.getVehicle() instanceof ArmedVehicleEntity iArmedVehicle && iArmedVehicle.banHand(player))
return; return;

View file

@ -1,7 +1,6 @@
package com.atsuishio.superbwarfare.client.overlay; package com.atsuishio.superbwarfare.client.overlay;
import com.atsuishio.superbwarfare.Mod; import com.atsuishio.superbwarfare.Mod;
import com.atsuishio.superbwarfare.init.ModCapabilities;
import com.atsuishio.superbwarfare.client.RenderHelper; import com.atsuishio.superbwarfare.client.RenderHelper;
import com.atsuishio.superbwarfare.entity.vehicle.Ah6Entity; import com.atsuishio.superbwarfare.entity.vehicle.Ah6Entity;
import com.atsuishio.superbwarfare.entity.vehicle.base.HelicopterEntity; 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.VehicleEntity;
import com.atsuishio.superbwarfare.entity.vehicle.base.WeaponVehicleEntity; import com.atsuishio.superbwarfare.entity.vehicle.base.WeaponVehicleEntity;
import com.atsuishio.superbwarfare.event.ClientEventHandler; import com.atsuishio.superbwarfare.event.ClientEventHandler;
import com.atsuishio.superbwarfare.init.ModAttachments;
import com.atsuishio.superbwarfare.tools.FormatTool; import com.atsuishio.superbwarfare.tools.FormatTool;
import com.atsuishio.superbwarfare.tools.InventoryTool; import com.atsuishio.superbwarfare.tools.InventoryTool;
import com.mojang.blaze3d.platform.GlStateManager; import com.mojang.blaze3d.platform.GlStateManager;
@ -59,8 +59,7 @@ public class HelicopterHudOverlay {
if (player == null) return; if (player == null) return;
var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); if (player.getData(ModAttachments.PLAYER_VARIABLE).edit) return;
if (cap == null || cap.edit) return;
if (player.getVehicle() instanceof HelicopterEntity iHelicopterEntity && player.getVehicle() instanceof MobileVehicleEntity mobileVehicle && iHelicopterEntity.isDriver(player) && player.getVehicle() instanceof WeaponVehicleEntity weaponVehicle) { if (player.getVehicle() instanceof HelicopterEntity iHelicopterEntity && player.getVehicle() instanceof MobileVehicleEntity mobileVehicle && iHelicopterEntity.isDriver(player) && player.getVehicle() instanceof WeaponVehicleEntity weaponVehicle) {
poseStack.pushPose(); poseStack.pushPose();

View file

@ -1,10 +1,10 @@
package com.atsuishio.superbwarfare.client.overlay; package com.atsuishio.superbwarfare.client.overlay;
import com.atsuishio.superbwarfare.Mod; import com.atsuishio.superbwarfare.Mod;
import com.atsuishio.superbwarfare.init.ModCapabilities;
import com.atsuishio.superbwarfare.client.RenderHelper; import com.atsuishio.superbwarfare.client.RenderHelper;
import com.atsuishio.superbwarfare.entity.vehicle.base.ArmedVehicleEntity; import com.atsuishio.superbwarfare.entity.vehicle.base.ArmedVehicleEntity;
import com.atsuishio.superbwarfare.event.ClientEventHandler; import com.atsuishio.superbwarfare.event.ClientEventHandler;
import com.atsuishio.superbwarfare.init.ModAttachments;
import com.atsuishio.superbwarfare.init.ModItems; import com.atsuishio.superbwarfare.init.ModItems;
import com.atsuishio.superbwarfare.item.gun.data.GunData; import com.atsuishio.superbwarfare.item.gun.data.GunData;
import com.atsuishio.superbwarfare.tools.EntityFindUtil; import com.atsuishio.superbwarfare.tools.EntityFindUtil;
@ -49,8 +49,7 @@ public class JavelinHudOverlay {
if (player != null) { if (player != null) {
ItemStack stack = player.getMainHandItem(); ItemStack stack = player.getMainHandItem();
var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); if (player.getData(ModAttachments.PLAYER_VARIABLE).edit) return;
if (cap == null || cap.edit) return;
if (player.getVehicle() instanceof ArmedVehicleEntity iArmedVehicle && iArmedVehicle.banHand(player)) if (player.getVehicle() instanceof ArmedVehicleEntity iArmedVehicle && iArmedVehicle.banHand(player))
return; return;

View file

@ -1,7 +1,7 @@
package com.atsuishio.superbwarfare.entity; package com.atsuishio.superbwarfare.entity;
import com.atsuishio.superbwarfare.Mod; 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.ModItems;
import com.atsuishio.superbwarfare.init.ModSounds; import com.atsuishio.superbwarfare.init.ModSounds;
import com.atsuishio.superbwarfare.tools.FormatTool; 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())); player.addItem(new ItemStack(ModItems.TARGET_DEPLOYER.get()));
} }
} else { } else {
var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE, null); if (!player.getData(ModAttachments.PLAYER_VARIABLE).zoom) {
if (cap != null && !cap.zoom) {
this.lookAt(EntityAnchorArgument.Anchor.EYES, new Vec3((player.getX()), this.getY(), (player.getZ()))); this.lookAt(EntityAnchorArgument.Anchor.EYES, new Vec3((player.getX()), this.getY(), (player.getZ())));
this.setXRot(0); this.setXRot(0);
this.xRotO = this.getXRot(); this.xRotO = this.getXRot();

View file

@ -1,7 +1,6 @@
package com.atsuishio.superbwarfare.event; package com.atsuishio.superbwarfare.event;
import com.atsuishio.superbwarfare.Mod; import com.atsuishio.superbwarfare.Mod;
import com.atsuishio.superbwarfare.init.ModCapabilities;
import com.atsuishio.superbwarfare.client.ClickHandler; import com.atsuishio.superbwarfare.client.ClickHandler;
import com.atsuishio.superbwarfare.config.client.DisplayConfig; import com.atsuishio.superbwarfare.config.client.DisplayConfig;
import com.atsuishio.superbwarfare.entity.vehicle.*; import com.atsuishio.superbwarfare.entity.vehicle.*;
@ -305,17 +304,18 @@ public class ClientEventHandler {
public static void handleGunMelee(Player player, ItemStack stack) { public static void handleGunMelee(Player player, ItemStack stack) {
if (stack.getItem() instanceof GunItem gunItem) { if (stack.getItem() instanceof GunItem gunItem) {
var data = GunData.from(stack); var data = GunData.from(stack);
var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); var cap = player.getData(ModAttachments.PLAYER_VARIABLE);
if (gunItem.hasMeleeAttack(stack) && gunMelee == 0 && drawTime < 0.01 if (gunItem.hasMeleeAttack(stack)
&& gunMelee == 0
&& drawTime < 0.01
&& ModKeyMappings.MELEE.isDown() && ModKeyMappings.MELEE.isDown()
&& !(player.getVehicle() instanceof ArmedVehicleEntity iArmedVehicle && iArmedVehicle.banHand(player)) && !(player.getVehicle() instanceof ArmedVehicleEntity iArmedVehicle && iArmedVehicle.banHand(player))
&& !holdFireVehicle && !holdFireVehicle
&& !notInGame() && !notInGame()
&& cap != null && !cap.edit && !cap.edit
&& !(data.reload.normal() || data.reload.empty()) && !(data.reload.normal() || data.reload.empty())
&& !data.reloading() && !data.reloading()
&& !data.charging() && !data.charging() && !player.getCooldowns().isOnCooldown(stack.getItem())
&& !player.getCooldowns().isOnCooldown(stack.getItem())
) { ) {
gunMelee = 36; gunMelee = 36;
cantFireTime = 40; cantFireTime = 40;
@ -464,7 +464,7 @@ public class ClientEventHandler {
revolverPreTime = Mth.clamp(revolverPreTime - 1.2 * times, 0, 1); 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) if ((holdFire || burstFireAmount > 0)
&& !(player.getVehicle() instanceof ArmedVehicleEntity iArmedVehicle && iArmedVehicle.banHand(player)) && !(player.getVehicle() instanceof ArmedVehicleEntity iArmedVehicle && iArmedVehicle.banHand(player))
@ -472,7 +472,7 @@ public class ClientEventHandler {
&& (stack.is(ModTags.Items.NORMAL_GUN) && (stack.is(ModTags.Items.NORMAL_GUN)
&& cantFireTime == 0 && cantFireTime == 0
&& drawTime < 0.01 && drawTime < 0.01
&& cap != null && !cap.edit && !cap.edit
&& !notInGame() && !notInGame()
&& (!(data.reload.normal() || data.reload.empty()) && (!(data.reload.normal() || data.reload.empty())
&& !data.reloading() && !data.reloading()
@ -485,7 +485,7 @@ public class ClientEventHandler {
&& !player.isSprinting() && !player.isSprinting()
&& tag.getDouble("overheat") == 0 && tag.getDouble("overheat") == 0
&& !player.getCooldowns().isOnCooldown(stack.getItem()) && miniGunRot >= 20 && !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 (mode == 0) {
if (clientTimer.getProgress() == 0) { if (clientTimer.getProgress() == 0) {
@ -594,12 +594,11 @@ public class ClientEventHandler {
revolverPreTime = 0; revolverPreTime = 0;
revolverWheelPreTime = 0; revolverWheelPreTime = 0;
playGunClientSounds(player, tag); playGunClientSounds(player);
handleClientShoot(); handleClientShoot();
} }
} else if (stack.is(ModItems.MINIGUN.get())) { } else if (stack.is(ModItems.MINIGUN.get())) {
var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); if (player.getData(ModAttachments.PLAYER_VARIABLE).rifleAmmo > 0 || InventoryTool.hasCreativeAmmoBox(player)) {
if (cap != null && cap.rifleAmmo > 0 || InventoryTool.hasCreativeAmmoBox(player)) {
var perk = data.perk.get(Perk.Type.AMMO); 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"))); 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); 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(); ItemStack stack = player.getMainHandItem();
if (!(stack.getItem() instanceof GunItem gunItem)) return; if (!(stack.getItem() instanceof GunItem gunItem)) return;
@ -849,9 +848,9 @@ public class ClientEventHandler {
double customWeight = data.customWeight(); 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)); 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.setXRot(newPitch);
player.xRotO = player.getXRot(); player.xRotO = player.getXRot();
@ -1017,8 +1016,7 @@ public class ClientEventHandler {
onGround = 0.001; onGround = 0.001;
} }
var cap = entity.getCapability(ModCapabilities.PLAYER_VARIABLE); if (!entity.getData(ModAttachments.PLAYER_VARIABLE).edit) {
if (cap != null && !cap.edit) {
if (Minecraft.getInstance().options.keyUp.isDown() && firePosTimer == 0) { if (Minecraft.getInstance().options.keyUp.isDown() && firePosTimer == 0) {
moveRotZ = Mth.lerp(0.2f * times, moveRotZ, 0.14) * (1 - zoomTime); moveRotZ = Mth.lerp(0.2f * times, moveRotZ, 0.14) * (1 - zoomTime);
} else { } else {
@ -1073,12 +1071,12 @@ public class ClientEventHandler {
double weight = data.weight(); double weight = data.weight();
double speed = 1.5 - (0.07 * weight); double speed = 1.5 - (0.07 * weight);
var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); var cap = player.getData(ModAttachments.PLAYER_VARIABLE);
if (zoom if (zoom
&& !(player.getVehicle() instanceof ArmedVehicleEntity iArmedVehicle && iArmedVehicle.banHand(player)) && !(player.getVehicle() instanceof ArmedVehicleEntity iArmedVehicle && iArmedVehicle.banHand(player))
&& !notInGame() && !notInGame()
&& drawTime < 0.01 && drawTime < 0.01
&& cap != null && !cap.edit) { && !cap.edit) {
if (Minecraft.getInstance().player != null) { if (Minecraft.getInstance().player != null) {
Minecraft.getInstance().player.getPersistentData().putDouble("noRun", 5); Minecraft.getInstance().player.getPersistentData().putDouble("noRun", 5);
} }
@ -1281,8 +1279,7 @@ public class ClientEventHandler {
private static void handlePlayerBreath(LivingEntity entity) { private static void handlePlayerBreath(LivingEntity entity) {
float times = (float) Math.min(Minecraft.getInstance().getTimer().getRealtimeDeltaTicks(), 0.8); float times = (float) Math.min(Minecraft.getInstance().getTimer().getRealtimeDeltaTicks(), 0.8);
var cap = entity.getCapability(ModCapabilities.PLAYER_VARIABLE); boolean breath = entity.getData(ModAttachments.PLAYER_VARIABLE).breath;
boolean breath = cap != null && cap.breath;
breathTime = Mth.lerp(0.2f * times, breathTime, breath ? 1 : 0); breathTime = Mth.lerp(0.2f * times, breathTime, breath ? 1 : 0);
} }
@ -1353,8 +1350,7 @@ public class ClientEventHandler {
private static void handleBowPullAnimation(LivingEntity entity) { private static void handleBowPullAnimation(LivingEntity entity) {
float times = 4 * (float) Math.min(Minecraft.getInstance().getTimer().getRealtimeDeltaTicks(), 0.8); float times = 4 * (float) Math.min(Minecraft.getInstance().getTimer().getRealtimeDeltaTicks(), 0.8);
var cap = entity.getCapability(ModCapabilities.PLAYER_VARIABLE); if (entity.getData(ModAttachments.PLAYER_VARIABLE).bowPull) {
if (cap != null && cap.bowPull) {
pullTimer = Math.min(pullTimer + 0.024 * times, 1.4); pullTimer = Math.min(pullTimer + 0.024 * times, 1.4);
bowTimer = Math.min(bowTimer + 0.018 * times, 1); bowTimer = Math.min(bowTimer + 0.018 * times, 1);
handTimer = Math.min(handTimer + 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)); event.setFOV(event.getFOV() / (1.0 + p * 0.01) * (1 - 0.4 * breathTime));
fov = event.getFOV(); fov = event.getFOV();
var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE);
// 智慧芯片 // 智慧芯片
if (zoom if (zoom
&& !notInGame() && !notInGame()
&& drawTime < 0.01 && drawTime < 0.01
&& cap != null && !player.getData(ModAttachments.PLAYER_VARIABLE).edit) {
&& !cap.edit) {
if (!player.isShiftKeyDown()) { if (!player.isShiftKeyDown()) {
int intelligentChipLevel = data.perk.getLevel(ModPerks.INTELLIGENT_CHIP); int intelligentChipLevel = data.perk.getLevel(ModPerks.INTELLIGENT_CHIP);
@ -1549,8 +1542,7 @@ public class ClientEventHandler {
public static void aimAtVillager(Player player) { public static void aimAtVillager(Player player) {
if (aimVillagerCountdown > 0) return; if (aimVillagerCountdown > 0) return;
var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); if (player.getData(ModAttachments.PLAYER_VARIABLE).zoom) {
if (cap != null && cap.zoom) {
Entity entity = TraceTool.findLookingEntity(player, 10); Entity entity = TraceTool.findLookingEntity(player, 10);
if (entity instanceof AbstractVillager villager) { if (entity instanceof AbstractVillager villager) {
List<Entity> entities = SeekTool.seekLivingEntities(villager, villager.level(), 16, 120); List<Entity> entities = SeekTool.seekLivingEntities(villager, villager.level(), 16, 120);

View file

@ -1,14 +1,9 @@
package com.atsuishio.superbwarfare.event; package com.atsuishio.superbwarfare.event;
import com.atsuishio.superbwarfare.Mod; 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.entity.projectile.ProjectileEntity;
import com.atsuishio.superbwarfare.event.events.ReloadEvent; import com.atsuishio.superbwarfare.event.events.ReloadEvent;
import com.atsuishio.superbwarfare.init.ModItems; import com.atsuishio.superbwarfare.init.*;
import com.atsuishio.superbwarfare.init.ModPerks;
import com.atsuishio.superbwarfare.init.ModSounds;
import com.atsuishio.superbwarfare.init.ModTags;
import com.atsuishio.superbwarfare.item.gun.GunItem; import com.atsuishio.superbwarfare.item.gun.GunItem;
import com.atsuishio.superbwarfare.item.gun.data.AttachmentType; import com.atsuishio.superbwarfare.item.gun.data.AttachmentType;
import com.atsuishio.superbwarfare.item.gun.data.GunData; 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)); float velocity = (float) ((data.velocity() + GunsTool.getGunDoubleTag(tag, "CustomVelocity")) * perkSpeed(data));
int projectileAmount = data.projectileAmount(); int projectileAmount = data.projectileAmount();
float bypassArmorRate = (float) data.bypassArmor(); float bypassArmorRate = (float) data.bypassArmor();
var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); boolean zoom = player.getData(ModAttachments.PLAYER_VARIABLE).zoom;
boolean zoom = cap != null && cap.zoom;
var perkInstance = data.perk.getInstance(Perk.Type.AMMO); var perkInstance = data.perk.getInstance(Perk.Type.AMMO);
var perk = perkInstance != null ? perkInstance.perk() : null; var perk = perkInstance != null ? perkInstance.perk() : null;
@ -557,8 +551,7 @@ public class GunEventHandler {
// 一阶段结束检查备弹如果有则二阶段启动无则直接跳到三阶段 // 一阶段结束检查备弹如果有则二阶段启动无则直接跳到三阶段
if ((tag.getDouble("PrepareTime") == 1 || tag.getDouble("PrepareLoadTime") == 1)) { if ((tag.getDouble("PrepareTime") == 1 || tag.getDouble("PrepareLoadTime") == 1)) {
if (!InventoryTool.hasCreativeAmmoBox(player)) { if (!InventoryTool.hasCreativeAmmoBox(player)) {
var capability = player.getCapability(ModCapabilities.PLAYER_VARIABLE); var capability = player.getData(ModAttachments.PLAYER_VARIABLE);
if (capability == null) capability = new PlayerVariable();
if (stack.is(ModTags.Items.USE_SHOTGUN_AMMO) && capability.shotgunAmmo == 0) { if (stack.is(ModTags.Items.USE_SHOTGUN_AMMO) && capability.shotgunAmmo == 0) {
tag.putBoolean("ForceStartStage3", true); tag.putBoolean("ForceStartStage3", true);
@ -640,8 +633,7 @@ public class GunEventHandler {
// 备弹耗尽结束 // 备弹耗尽结束
if (!InventoryTool.hasCreativeAmmoBox(player)) { if (!InventoryTool.hasCreativeAmmoBox(player)) {
var capability = player.getCapability(ModCapabilities.PLAYER_VARIABLE); var capability = player.getData(ModAttachments.PLAYER_VARIABLE);
if (capability == null) capability = new PlayerVariable();
if (stack.is(ModTags.Items.USE_SHOTGUN_AMMO) && capability.shotgunAmmo == 0) { if (stack.is(ModTags.Items.USE_SHOTGUN_AMMO) && capability.shotgunAmmo == 0) {
gunData.reload.setStage(3); gunData.reload.setStage(3);
@ -695,8 +687,7 @@ public class GunEventHandler {
data.setAmmo(data.ammo() + 1); data.setAmmo(data.ammo() + 1);
if (!InventoryTool.hasCreativeAmmoBox(player)) { if (!InventoryTool.hasCreativeAmmoBox(player)) {
var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); var cap = player.getData(ModAttachments.PLAYER_VARIABLE);
if (cap == null) return;
ItemStack stack = player.getMainHandItem(); ItemStack stack = player.getMainHandItem();
if (stack.is(ModTags.Items.USE_SHOTGUN_AMMO)) { 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()); player.getInventory().clearOrCountMatchingItems(p -> p.getItem() == ModItems.GRENADE_40MM.get(), 1, player.inventoryMenu.getCraftSlots());
} }
cap.syncPlayerVariables(player); player.setData(ModAttachments.PLAYER_VARIABLE, cap);
} }
} }

View file

@ -1,7 +1,5 @@
package com.atsuishio.superbwarfare.event; 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.component.ModDataComponents;
import com.atsuishio.superbwarfare.config.common.GameplayConfig; import com.atsuishio.superbwarfare.config.common.GameplayConfig;
import com.atsuishio.superbwarfare.config.server.MiscConfig; import com.atsuishio.superbwarfare.config.server.MiscConfig;
@ -216,7 +214,6 @@ public class LivingEventHandler {
var data = GunData.from(stack); var data = GunData.from(stack);
double amount = Math.min(0.125 * event.getAmount(), event.getEntity().getMaxHealth()); double amount = Math.min(0.125 * event.getAmount(), event.getEntity().getMaxHealth());
final var tag = NBTTool.getTag(stack);
// 先处理发射器类武器或高爆弹的爆炸伤害 // 先处理发射器类武器或高爆弹的爆炸伤害
if (source.is(ModDamageTypes.PROJECTILE_BOOM)) { if (source.is(ModDamageTypes.PROJECTILE_BOOM)) {
@ -385,11 +382,10 @@ public class LivingEventHandler {
oldData.charge.reset(); oldData.charge.reset();
} }
var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); var cap = player.getData(ModAttachments.PLAYER_VARIABLE).watch();
if (cap != null) { cap.edit = false;
cap.edit = false; player.setData(ModAttachments.PLAYER_VARIABLE, cap);
cap.syncPlayerVariables(player); cap.sync(player);
}
oldData.save(); oldData.save();
} }
@ -433,11 +429,10 @@ public class LivingEventHandler {
PacketDistributor.sendToPlayer(serverPlayer, new DrawClientMessage(true)); PacketDistributor.sendToPlayer(serverPlayer, new DrawClientMessage(true));
} }
var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); var cap = player.getData(ModAttachments.PLAYER_VARIABLE).watch();
if (cap != null) { cap.tacticalSprint = false;
cap.tacticalSprint = false; player.setData(ModAttachments.PLAYER_VARIABLE, cap);
cap.syncPlayerVariables(player); cap.sync(player);
}
newData.save(); newData.save();
} }
@ -608,7 +603,6 @@ public class LivingEventHandler {
private static void handleKillingTallyDamage(ItemStack stack, LivingIncomingDamageEvent event) { private static void handleKillingTallyDamage(ItemStack stack, LivingIncomingDamageEvent event) {
var data = GunData.from(stack); var data = GunData.from(stack);
final var tag = data.tag();
int level = data.perk.getLevel(ModPerks.KILLING_TALLY); int level = data.perk.getLevel(ModPerks.KILLING_TALLY);
if (level == 0) return; 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); 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); var cap = player.getData(ModAttachments.PLAYER_VARIABLE).watch();
if (cap == null) return;
int mag = data.magazine(); int mag = data.magazine();
int ammo = data.ammo(); int ammo = data.ammo();
@ -685,7 +678,8 @@ public class LivingEventHandler {
data.setAmmo(Math.min(mag, ammo + ammoFinal)); data.setAmmo(Math.min(mag, ammo + ammoFinal));
} }
data.save(); 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) { public static void onLivingDrops(LivingDropsEvent event) {
// 死亡掉落弹药盒 // 死亡掉落弹药盒
if (event.getEntity() instanceof Player player && !player.level().getLevelData().getGameRules().getBoolean(GameRules.RULE_KEEPINVENTORY)) { if (event.getEntity() instanceof Player player && !player.level().getLevelData().getGameRules().getBoolean(GameRules.RULE_KEEPINVENTORY)) {
var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); var cap = player.getData(ModAttachments.PLAYER_VARIABLE).watch();
if (cap == null) cap = new PlayerVariable();
boolean drop = cap.rifleAmmo + cap.handgunAmmo + cap.shotgunAmmo + cap.sniperAmmo + cap.heavyAmmo > 0; 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); var info = new AmmoBoxInfo("All", true);
stack.set(ModDataComponents.AMMO_BOX_INFO, info); 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)); event.getDrops().add(new ItemEntity(player.level(), player.getX(), player.getY() + 1, player.getZ(), stack));
} }

View file

@ -1,8 +1,8 @@
package com.atsuishio.superbwarfare.event; package com.atsuishio.superbwarfare.event;
import com.atsuishio.superbwarfare.init.ModCapabilities;
import com.atsuishio.superbwarfare.config.common.GameplayConfig; import com.atsuishio.superbwarfare.config.common.GameplayConfig;
import com.atsuishio.superbwarfare.config.server.MiscConfig; import com.atsuishio.superbwarfare.config.server.MiscConfig;
import com.atsuishio.superbwarfare.init.ModAttachments;
import com.atsuishio.superbwarfare.init.ModItems; import com.atsuishio.superbwarfare.init.ModItems;
import com.atsuishio.superbwarfare.init.ModSounds; import com.atsuishio.superbwarfare.init.ModSounds;
import com.atsuishio.superbwarfare.init.ModTags; import com.atsuishio.superbwarfare.init.ModTags;
@ -52,13 +52,12 @@ public class PlayerEventHandler {
public static void onPlayerRespawned(PlayerEvent.PlayerRespawnEvent event) { public static void onPlayerRespawned(PlayerEvent.PlayerRespawnEvent event) {
Player player = event.getEntity(); Player player = event.getEntity();
var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); var cap = player.getData(ModAttachments.PLAYER_VARIABLE).watch();
if (cap != null) { cap.zoom = false;
cap.zoom = false; cap.tacticalSprintExhaustion = false;
cap.tacticalSprintExhaustion = false; cap.tacticalSprintTime = 600;
cap.tacticalSprintTime = 600; player.setData(ModAttachments.PLAYER_VARIABLE, cap);
cap.syncPlayerVariables(player); cap.sync(player);
}
handleRespawnReload(player); handleRespawnReload(player);
handleRespawnAutoArmor(player); handleRespawnAutoArmor(player);
@ -79,6 +78,7 @@ public class PlayerEventHandler {
public static void onPlayerTick(PlayerTickEvent.Post event) { public static void onPlayerTick(PlayerTickEvent.Post event) {
Player player = event.getEntity(); Player player = event.getEntity();
ItemStack stack = player.getMainHandItem(); ItemStack stack = player.getMainHandItem();
var variable = player.getData(ModAttachments.PLAYER_VARIABLE).watch();
if (stack.is(ModTags.Items.GUN)) { if (stack.is(ModTags.Items.GUN)) {
handlePlayerSprint(player); handlePlayerSprint(player);
@ -90,11 +90,12 @@ public class PlayerEventHandler {
handleSimulationDistance(player); handleSimulationDistance(player);
handleTacticalSprint(player); handleTacticalSprint(player);
handleBreath(player); handleBreath(player);
variable.sync(player);
} }
private static void handleBreath(Player player) { private static void handleBreath(Player player) {
var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); var cap = player.getData(ModAttachments.PLAYER_VARIABLE).watch();
if (cap == null) return;
if (cap.breath) { if (cap.breath) {
cap.breathTime = Mth.clamp(cap.breathTime - 1, 0, 100); cap.breathTime = Mth.clamp(cap.breathTime - 1, 0, 100);
@ -111,12 +112,11 @@ public class PlayerEventHandler {
cap.breathExhaustion = false; cap.breathExhaustion = false;
} }
cap.syncPlayerVariables(player); player.setData(ModAttachments.PLAYER_VARIABLE, cap);
} }
private static void handleTacticalSprint(Player player) { private static void handleTacticalSprint(Player player) {
var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); var cap = player.getData(ModAttachments.PLAYER_VARIABLE).watch();
if (cap == null) return;
ItemStack stack = player.getMainHandItem(); ItemStack stack = player.getMainHandItem();
int sprintCost; int sprintCost;
@ -161,15 +161,14 @@ public class PlayerEventHandler {
player.getPersistentData().putBoolean("canTacticalSprint", true); player.getPersistentData().putBoolean("canTacticalSprint", true);
} }
cap.syncPlayerVariables(player); player.setData(ModAttachments.PLAYER_VARIABLE, cap);
} }
/** /**
* 判断玩家是否在奔跑 * 判断玩家是否在奔跑
*/ */
private static void handlePlayerSprint(Player player) { private static void handlePlayerSprint(Player player) {
var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); var cap = player.getData(ModAttachments.PLAYER_VARIABLE);
if (cap == null) return;
if (cap.holdFire) { if (cap.holdFire) {
player.getPersistentData().putDouble("noRun", 10); player.getPersistentData().putDouble("noRun", 10);
@ -191,12 +190,11 @@ public class PlayerEventHandler {
} }
private static void handleGround(Player player) { private static void handleGround(Player player) {
var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); var cap = player.getData(ModAttachments.PLAYER_VARIABLE);
if (cap == null) return;
if (player.onGround()) { if (player.onGround()) {
cap.playerDoubleJump = false; 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) { if ((stack.is(ModItems.RPG.get()) || stack.is(ModItems.BOCEK.get())) && data.ammo() == 1) {
tag.remove("IsEmpty"); tag.remove("IsEmpty");
data.save();
} }
} }
private static void handleBocekPulling(Player player) { private static void handleBocekPulling(Player player) {
ItemStack stack = player.getMainHandItem(); ItemStack stack = player.getMainHandItem();
var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); var cap = player.getData(ModAttachments.PLAYER_VARIABLE).watch();
if (cap == null) return;
var data = GunData.from(stack); var data = GunData.from(stack);
final var tag = data.tag(); final var tag = data.tag();
@ -231,7 +227,7 @@ public class PlayerEventHandler {
cap.bowPull = true; cap.bowPull = true;
cap.tacticalSprint = false; cap.tacticalSprint = false;
cap.syncPlayerVariables(player); player.setData(ModAttachments.PLAYER_VARIABLE, cap);
player.setSprinting(false); player.setSprinting(false);
} }
if (GunsTool.getGunDoubleTag(tag, "Power") == 1) { if (GunsTool.getGunDoubleTag(tag, "Power") == 1) {
@ -245,15 +241,16 @@ public class PlayerEventHandler {
GunsTool.setGunDoubleTag(tag, "Power", 0); GunsTool.setGunDoubleTag(tag, "Power", 0);
} }
cap.bowPull = false; cap.bowPull = false;
cap.syncPlayerVariables(player); player.setData(ModAttachments.PLAYER_VARIABLE, cap);
} }
if (GunsTool.getGunDoubleTag(tag, "Power") > 0) { if (GunsTool.getGunDoubleTag(tag, "Power") > 0) {
cap.tacticalSprint = false; cap.tacticalSprint = false;
cap.syncPlayerVariables(player); player.setData(ModAttachments.PLAYER_VARIABLE, cap);
player.setSprinting(false); player.setSprinting(false);
} }
cap.sync(player);
data.save(); data.save();
} }
@ -277,8 +274,7 @@ public class PlayerEventHandler {
var tag = data.tag(); var tag = data.tag();
if (!InventoryTool.hasCreativeAmmoBox(player)) { if (!InventoryTool.hasCreativeAmmoBox(player)) {
var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); var cap = player.getData(ModAttachments.PLAYER_VARIABLE);
if (cap == null) return;
if (stack.is(ModTags.Items.USE_SHOTGUN_AMMO) && cap.shotgunAmmo > 0) { if (stack.is(ModTags.Items.USE_SHOTGUN_AMMO) && cap.shotgunAmmo > 0) {
GunsTool.reload(player, stack, data, AmmoType.SHOTGUN); GunsTool.reload(player, stack, data, AmmoType.SHOTGUN);

View file

@ -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<AttachmentType<?>> ATTACHMENT_TYPES = DeferredRegister.create(NeoForgeRegistries.ATTACHMENT_TYPES, Mod.MODID);
public static final Supplier<AttachmentType<PlayerVariable>> PLAYER_VARIABLE = ATTACHMENT_TYPES.register(
"player_variable", () -> AttachmentType.serializable(PlayerVariable::new).build()
);
}

View file

@ -7,8 +7,6 @@ import com.atsuishio.superbwarfare.block.entity.FuMO25BlockEntity;
import com.atsuishio.superbwarfare.capability.energy.ItemEnergyStorage; import com.atsuishio.superbwarfare.capability.energy.ItemEnergyStorage;
import com.atsuishio.superbwarfare.capability.laser.LaserCapability; import com.atsuishio.superbwarfare.capability.laser.LaserCapability;
import com.atsuishio.superbwarfare.capability.laser.LaserCapabilityProvider; 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.ContainerMobileVehicleEntity;
import com.atsuishio.superbwarfare.entity.vehicle.base.EnergyVehicleEntity; import com.atsuishio.superbwarfare.entity.vehicle.base.EnergyVehicleEntity;
import com.atsuishio.superbwarfare.item.CreativeChargingStationBlockItem; import com.atsuishio.superbwarfare.item.CreativeChargingStationBlockItem;
@ -32,13 +30,11 @@ import java.util.ArrayList;
public class ModCapabilities { public class ModCapabilities {
public static final EntityCapability<LaserCapability, Void> LASER_CAPABILITY = EntityCapability.createVoid(Mod.loc("laser_capability"), LaserCapability.class); public static final EntityCapability<LaserCapability, Void> LASER_CAPABILITY = EntityCapability.createVoid(Mod.loc("laser_capability"), LaserCapability.class);
public static final EntityCapability<PlayerVariable, Void> PLAYER_VARIABLE = EntityCapability.createVoid(Mod.loc("player_variable"), PlayerVariable.class);
@SubscribeEvent @SubscribeEvent
public static void registerCapabilities(RegisterCapabilitiesEvent event) { public static void registerCapabilities(RegisterCapabilitiesEvent event) {
// 玩家变量和激光 // 激光
event.registerEntity(ModCapabilities.LASER_CAPABILITY, EntityType.PLAYER, new LaserCapabilityProvider()); 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); event.registerBlockEntity(Capabilities.EnergyStorage.BLOCK, ModBlockEntities.CHARGING_STATION.value(), ChargingStationBlockEntity::getEnergyStorage);

View file

@ -1,9 +1,9 @@
package com.atsuishio.superbwarfare.item; package com.atsuishio.superbwarfare.item;
import com.atsuishio.superbwarfare.init.ModCapabilities;
import com.atsuishio.superbwarfare.capability.laser.LaserHandler; import com.atsuishio.superbwarfare.capability.laser.LaserHandler;
import com.atsuishio.superbwarfare.client.TooltipTool; import com.atsuishio.superbwarfare.client.TooltipTool;
import com.atsuishio.superbwarfare.entity.projectile.LaserEntity; import com.atsuishio.superbwarfare.entity.projectile.LaserEntity;
import com.atsuishio.superbwarfare.init.ModCapabilities;
import com.atsuishio.superbwarfare.init.ModSounds; import com.atsuishio.superbwarfare.init.ModSounds;
import com.atsuishio.superbwarfare.network.message.receive.ShakeClientMessage; import com.atsuishio.superbwarfare.network.message.receive.ShakeClientMessage;
import com.atsuishio.superbwarfare.network.message.send.LaserShootMessage; import com.atsuishio.superbwarfare.network.message.send.LaserShootMessage;

View file

@ -1,6 +1,6 @@
package com.atsuishio.superbwarfare.item.common.ammo; 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.ModItems;
import com.atsuishio.superbwarfare.init.ModSounds; import com.atsuishio.superbwarfare.init.ModSounds;
import com.atsuishio.superbwarfare.tools.AmmoType; import com.atsuishio.superbwarfare.tools.AmmoType;
@ -49,12 +49,11 @@ public class AmmoSupplierItem extends Item {
if (offhandItem.is(ModItems.AMMO_BOX.get())) { if (offhandItem.is(ModItems.AMMO_BOX.get())) {
this.type.add(offhandItem, ammoToAdd * count); this.type.add(offhandItem, ammoToAdd * count);
} else { } 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);
this.type.add(capability, ammoToAdd * count); player.setData(ModAttachments.PLAYER_VARIABLE, capability);
capability.syncPlayerVariables(player); capability.sync(player);
}
} }
if (!level.isClientSide()) { if (!level.isClientSide()) {

View file

@ -1,7 +1,7 @@
package com.atsuishio.superbwarfare.item.common.ammo.box; package com.atsuishio.superbwarfare.item.common.ammo.box;
import com.atsuishio.superbwarfare.init.ModCapabilities;
import com.atsuishio.superbwarfare.component.ModDataComponents; import com.atsuishio.superbwarfare.component.ModDataComponents;
import com.atsuishio.superbwarfare.init.ModAttachments;
import com.atsuishio.superbwarfare.init.ModSounds; import com.atsuishio.superbwarfare.init.ModSounds;
import com.atsuishio.superbwarfare.tools.AmmoType; import com.atsuishio.superbwarfare.tools.AmmoType;
import com.atsuishio.superbwarfare.tools.FormatTool; import com.atsuishio.superbwarfare.tools.FormatTool;
@ -41,8 +41,8 @@ public class AmmoBox extends Item {
if (info == null) info = new AmmoBoxInfo("All", false); if (info == null) info = new AmmoBoxInfo("All", false);
String selectedType = info.type(); String selectedType = info.type();
var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE, null); var cap = player.getData(ModAttachments.PLAYER_VARIABLE).watch();
if (cap != null && !level.isClientSide()) { if (!level.isClientSide()) {
var types = selectedType.equals("All") ? AmmoType.values() : new AmmoType[]{AmmoType.getType(selectedType)}; var types = selectedType.equals("All") ? AmmoType.values() : new AmmoType[]{AmmoType.getType(selectedType)};
for (var type : types) { for (var type : types) {
@ -58,7 +58,8 @@ public class AmmoBox extends Item {
type.set(stack, 0); 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); level.playSound(null, player.blockPosition(), SoundEvents.ARROW_HIT_PLAYER, SoundSource.PLAYERS, 1, 1);
// 取出弹药时若弹药盒为掉落物版本则移除弹药盒物品 // 取出弹药时若弹药盒为掉落物版本则移除弹药盒物品

View file

@ -1,9 +1,9 @@
package com.atsuishio.superbwarfare.item.gun; package com.atsuishio.superbwarfare.item.gun;
import com.atsuishio.superbwarfare.Mod; import com.atsuishio.superbwarfare.Mod;
import com.atsuishio.superbwarfare.init.ModCapabilities;
import com.atsuishio.superbwarfare.client.PoseTool; import com.atsuishio.superbwarfare.client.PoseTool;
import com.atsuishio.superbwarfare.client.tooltip.component.GunImageComponent; 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.ModItems;
import com.atsuishio.superbwarfare.init.ModPerks; import com.atsuishio.superbwarfare.init.ModPerks;
import com.atsuishio.superbwarfare.init.ModTags; 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)) { if ((hasBulletInBarrel && ammoCount > magazine + 1) || (!hasBulletInBarrel && ammoCount > magazine)) {
int count = ammoCount - magazine - (hasBulletInBarrel ? 1 : 0); int count = ammoCount - magazine - (hasBulletInBarrel ? 1 : 0);
var capability = entity.getCapability(ModCapabilities.PLAYER_VARIABLE); var capability = entity.getData(ModAttachments.PLAYER_VARIABLE).watch();
if (capability != null) { if (stack.is(ModTags.Items.USE_SHOTGUN_AMMO)) {
if (stack.is(ModTags.Items.USE_SHOTGUN_AMMO)) { AmmoType.SHOTGUN.add(capability, count);
AmmoType.SHOTGUN.add(capability, count); } else if (stack.is(ModTags.Items.USE_SNIPER_AMMO)) {
} else if (stack.is(ModTags.Items.USE_SNIPER_AMMO)) { AmmoType.SNIPER.add(capability, count);
AmmoType.SNIPER.add(capability, count); } else if (stack.is(ModTags.Items.USE_HANDGUN_AMMO)) {
} else if (stack.is(ModTags.Items.USE_HANDGUN_AMMO)) { AmmoType.HANDGUN.add(capability, count);
AmmoType.HANDGUN.add(capability, count); } else if (stack.is(ModTags.Items.USE_RIFLE_AMMO)) {
} else if (stack.is(ModTags.Items.USE_RIFLE_AMMO)) { AmmoType.RIFLE.add(capability, count);
AmmoType.RIFLE.add(capability, count); } else if (stack.is(ModTags.Items.USE_HEAVY_AMMO)) {
} else if (stack.is(ModTags.Items.USE_HEAVY_AMMO)) { AmmoType.HEAVY.add(capability, count);
AmmoType.HEAVY.add(capability, count);
}
capability.syncPlayerVariables(entity);
} }
entity.setData(ModAttachments.PLAYER_VARIABLE, capability);
capability.sync(entity);
data.setAmmo(magazine + (hasBulletInBarrel ? 1 : 0)); data.setAmmo(magazine + (hasBulletInBarrel ? 1 : 0));
} }
data.save(); data.save();

View file

@ -1,9 +1,9 @@
package com.atsuishio.superbwarfare.item.gun.handgun; package com.atsuishio.superbwarfare.item.gun.handgun;
import com.atsuishio.superbwarfare.init.ModCapabilities;
import com.atsuishio.superbwarfare.client.TooltipTool; import com.atsuishio.superbwarfare.client.TooltipTool;
import com.atsuishio.superbwarfare.client.renderer.item.TracheliumItemRenderer; import com.atsuishio.superbwarfare.client.renderer.item.TracheliumItemRenderer;
import com.atsuishio.superbwarfare.event.ClientEventHandler; import com.atsuishio.superbwarfare.event.ClientEventHandler;
import com.atsuishio.superbwarfare.init.ModAttachments;
import com.atsuishio.superbwarfare.init.ModSounds; import com.atsuishio.superbwarfare.init.ModSounds;
import com.atsuishio.superbwarfare.init.ModTags; import com.atsuishio.superbwarfare.init.ModTags;
import com.atsuishio.superbwarfare.item.gun.GunItem; import com.atsuishio.superbwarfare.item.gun.GunItem;
@ -194,8 +194,7 @@ public class Trachelium extends GunItem implements GeoItem {
ItemStack stack = player.getMainHandItem(); ItemStack stack = player.getMainHandItem();
if (!stack.is(ModTags.Items.GUN)) return PlayState.STOP; if (!stack.is(ModTags.Items.GUN)) return PlayState.STOP;
var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); if (player.getData(ModAttachments.PLAYER_VARIABLE).edit) {
if (cap != null && cap.edit) {
return event.setAndContinue(RawAnimation.begin().thenPlay("animation.trachelium.edit")); return event.setAndContinue(RawAnimation.begin().thenPlay("animation.trachelium.edit"));
} }

View file

@ -1,9 +1,9 @@
package com.atsuishio.superbwarfare.item.gun.heavy; package com.atsuishio.superbwarfare.item.gun.heavy;
import com.atsuishio.superbwarfare.Mod; import com.atsuishio.superbwarfare.Mod;
import com.atsuishio.superbwarfare.init.ModCapabilities;
import com.atsuishio.superbwarfare.client.renderer.item.Ntw20Renderer; import com.atsuishio.superbwarfare.client.renderer.item.Ntw20Renderer;
import com.atsuishio.superbwarfare.event.ClientEventHandler; import com.atsuishio.superbwarfare.event.ClientEventHandler;
import com.atsuishio.superbwarfare.init.ModAttachments;
import com.atsuishio.superbwarfare.init.ModRarity; import com.atsuishio.superbwarfare.init.ModRarity;
import com.atsuishio.superbwarfare.init.ModSounds; import com.atsuishio.superbwarfare.init.ModSounds;
import com.atsuishio.superbwarfare.init.ModTags; import com.atsuishio.superbwarfare.init.ModTags;
@ -101,8 +101,7 @@ public class Ntw20Item extends GunItem implements GeoItem {
ItemStack stack = player.getMainHandItem(); ItemStack stack = player.getMainHandItem();
if (!stack.is(ModTags.Items.GUN)) return PlayState.STOP; if (!stack.is(ModTags.Items.GUN)) return PlayState.STOP;
var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); if (player.getData(ModAttachments.PLAYER_VARIABLE).edit) {
if (cap != null && cap.edit) {
return event.setAndContinue(RawAnimation.begin().thenPlay("animation.ntw_20.edit")); return event.setAndContinue(RawAnimation.begin().thenPlay("animation.ntw_20.edit"));
} }

View file

@ -1,7 +1,6 @@
package com.atsuishio.superbwarfare.item.gun.launcher; package com.atsuishio.superbwarfare.item.gun.launcher;
import com.atsuishio.superbwarfare.Mod; import com.atsuishio.superbwarfare.Mod;
import com.atsuishio.superbwarfare.init.ModCapabilities;
import com.atsuishio.superbwarfare.client.renderer.item.JavelinItemRenderer; import com.atsuishio.superbwarfare.client.renderer.item.JavelinItemRenderer;
import com.atsuishio.superbwarfare.client.tooltip.component.LauncherImageComponent; import com.atsuishio.superbwarfare.client.tooltip.component.LauncherImageComponent;
import com.atsuishio.superbwarfare.entity.projectile.FlareDecoyEntity; 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) { public void fireOnPress(Player player, final GunData data) {
var tag = data.tag(); var tag = data.tag();
var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); if (!player.getData(ModAttachments.PLAYER_VARIABLE).zoom || data.ammo() <= 0) return;
if (cap != null && !cap.zoom || data.ammo() <= 0) return;
Entity seekingEntity = SeekTool.seekEntity(player, player.level(), 512, 8); Entity seekingEntity = SeekTool.seekEntity(player, player.level(), 512, 8);

View file

@ -1,15 +1,11 @@
package com.atsuishio.superbwarfare.item.gun.launcher; package com.atsuishio.superbwarfare.item.gun.launcher;
import com.atsuishio.superbwarfare.Mod; import com.atsuishio.superbwarfare.Mod;
import com.atsuishio.superbwarfare.init.ModCapabilities;
import com.atsuishio.superbwarfare.client.renderer.item.M79ItemRenderer; import com.atsuishio.superbwarfare.client.renderer.item.M79ItemRenderer;
import com.atsuishio.superbwarfare.client.tooltip.component.LauncherImageComponent; import com.atsuishio.superbwarfare.client.tooltip.component.LauncherImageComponent;
import com.atsuishio.superbwarfare.entity.projectile.GunGrenadeEntity; import com.atsuishio.superbwarfare.entity.projectile.GunGrenadeEntity;
import com.atsuishio.superbwarfare.event.ClientEventHandler; import com.atsuishio.superbwarfare.event.ClientEventHandler;
import com.atsuishio.superbwarfare.init.ModItems; import com.atsuishio.superbwarfare.init.*;
import com.atsuishio.superbwarfare.init.ModPerks;
import com.atsuishio.superbwarfare.init.ModSounds;
import com.atsuishio.superbwarfare.init.ModTags;
import com.atsuishio.superbwarfare.item.gun.GunItem; import com.atsuishio.superbwarfare.item.gun.GunItem;
import com.atsuishio.superbwarfare.item.gun.SpecialFireWeapon; import com.atsuishio.superbwarfare.item.gun.SpecialFireWeapon;
import com.atsuishio.superbwarfare.item.gun.data.GunData; import com.atsuishio.superbwarfare.item.gun.data.GunData;
@ -175,9 +171,7 @@ public class M79Item extends GunItem implements GeoItem, SpecialFireWeapon {
ItemStack stack = data.stack(); ItemStack stack = data.stack();
if (player.getCooldowns().isOnCooldown(stack.getItem()) || data.ammo() <= 0) return; if (player.getCooldowns().isOnCooldown(stack.getItem()) || data.ammo() <= 0) return;
var tag = data.tag(); boolean zooming = player.getData(ModAttachments.PLAYER_VARIABLE).zoom;
var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE);
boolean zooming = cap != null && cap.zoom;
double spread = data.spread(); double spread = data.spread();
if (player.level() instanceof ServerLevel serverLevel) { if (player.level() instanceof ServerLevel serverLevel) {

View file

@ -1,15 +1,11 @@
package com.atsuishio.superbwarfare.item.gun.launcher; package com.atsuishio.superbwarfare.item.gun.launcher;
import com.atsuishio.superbwarfare.Mod; import com.atsuishio.superbwarfare.Mod;
import com.atsuishio.superbwarfare.init.ModCapabilities;
import com.atsuishio.superbwarfare.client.renderer.item.RpgItemRenderer; import com.atsuishio.superbwarfare.client.renderer.item.RpgItemRenderer;
import com.atsuishio.superbwarfare.client.tooltip.component.LauncherImageComponent; import com.atsuishio.superbwarfare.client.tooltip.component.LauncherImageComponent;
import com.atsuishio.superbwarfare.entity.projectile.RpgRocketEntity; import com.atsuishio.superbwarfare.entity.projectile.RpgRocketEntity;
import com.atsuishio.superbwarfare.event.ClientEventHandler; import com.atsuishio.superbwarfare.event.ClientEventHandler;
import com.atsuishio.superbwarfare.init.ModItems; import com.atsuishio.superbwarfare.init.*;
import com.atsuishio.superbwarfare.init.ModPerks;
import com.atsuishio.superbwarfare.init.ModSounds;
import com.atsuishio.superbwarfare.init.ModTags;
import com.atsuishio.superbwarfare.item.gun.GunItem; import com.atsuishio.superbwarfare.item.gun.GunItem;
import com.atsuishio.superbwarfare.item.gun.SpecialFireWeapon; import com.atsuishio.superbwarfare.item.gun.SpecialFireWeapon;
import com.atsuishio.superbwarfare.item.gun.data.GunData; import com.atsuishio.superbwarfare.item.gun.data.GunData;
@ -190,8 +186,7 @@ public class RpgItem extends GunItem implements GeoItem, SpecialFireWeapon {
|| data.ammo() <= 0 || data.ammo() <= 0
) return; ) return;
var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); boolean zoom = player.getData(ModAttachments.PLAYER_VARIABLE).zoom;
boolean zoom = cap != null && cap.zoom;
double spread = data.spread(); double spread = data.spread();
if (player.level() instanceof ServerLevel serverLevel) { if (player.level() instanceof ServerLevel serverLevel) {

View file

@ -1,7 +1,6 @@
package com.atsuishio.superbwarfare.item.gun.launcher; package com.atsuishio.superbwarfare.item.gun.launcher;
import com.atsuishio.superbwarfare.Mod; import com.atsuishio.superbwarfare.Mod;
import com.atsuishio.superbwarfare.init.ModCapabilities;
import com.atsuishio.superbwarfare.client.renderer.item.SecondaryCataclysmRenderer; import com.atsuishio.superbwarfare.client.renderer.item.SecondaryCataclysmRenderer;
import com.atsuishio.superbwarfare.client.tooltip.component.SecondaryCataclysmImageComponent; import com.atsuishio.superbwarfare.client.tooltip.component.SecondaryCataclysmImageComponent;
import com.atsuishio.superbwarfare.entity.projectile.GunGrenadeEntity; import com.atsuishio.superbwarfare.entity.projectile.GunGrenadeEntity;
@ -274,9 +273,7 @@ public class SecondaryCataclysm extends GunItem implements GeoItem, SpecialFireW
ItemStack stack = data.stack(); ItemStack stack = data.stack();
if (player.getCooldowns().isOnCooldown(stack.getItem()) || data.ammo() <= 0) return; if (player.getCooldowns().isOnCooldown(stack.getItem()) || data.ammo() <= 0) return;
var tag = data.tag(); boolean zooming = player.getData(ModAttachments.PLAYER_VARIABLE).zoom;
var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE);
boolean zooming = cap != null && cap.zoom;
double spread = data.spread(); double spread = data.spread();
var stackCap = stack.getCapability(Capabilities.EnergyStorage.ITEM); var stackCap = stack.getCapability(Capabilities.EnergyStorage.ITEM);

View file

@ -1,9 +1,9 @@
package com.atsuishio.superbwarfare.item.gun.machinegun; package com.atsuishio.superbwarfare.item.gun.machinegun;
import com.atsuishio.superbwarfare.Mod; import com.atsuishio.superbwarfare.Mod;
import com.atsuishio.superbwarfare.init.ModCapabilities;
import com.atsuishio.superbwarfare.client.renderer.item.RpkItemRenderer; import com.atsuishio.superbwarfare.client.renderer.item.RpkItemRenderer;
import com.atsuishio.superbwarfare.event.ClientEventHandler; import com.atsuishio.superbwarfare.event.ClientEventHandler;
import com.atsuishio.superbwarfare.init.ModAttachments;
import com.atsuishio.superbwarfare.init.ModPerks; import com.atsuishio.superbwarfare.init.ModPerks;
import com.atsuishio.superbwarfare.init.ModSounds; import com.atsuishio.superbwarfare.init.ModSounds;
import com.atsuishio.superbwarfare.init.ModTags; import com.atsuishio.superbwarfare.init.ModTags;
@ -118,8 +118,7 @@ public class RpkItem extends GunItem implements GeoItem {
ItemStack stack = player.getMainHandItem(); ItemStack stack = player.getMainHandItem();
if (!stack.is(ModTags.Items.GUN)) return PlayState.STOP; if (!stack.is(ModTags.Items.GUN)) return PlayState.STOP;
var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); if (player.getData(ModAttachments.PLAYER_VARIABLE).edit) {
if (cap != null && cap.edit) {
return event.setAndContinue(RawAnimation.begin().thenPlay("animation.ak47.edit")); return event.setAndContinue(RawAnimation.begin().thenPlay("animation.ak47.edit"));
} }

View file

@ -1,9 +1,9 @@
package com.atsuishio.superbwarfare.item.gun.rifle; package com.atsuishio.superbwarfare.item.gun.rifle;
import com.atsuishio.superbwarfare.Mod; import com.atsuishio.superbwarfare.Mod;
import com.atsuishio.superbwarfare.init.ModCapabilities;
import com.atsuishio.superbwarfare.client.renderer.item.AK12ItemRenderer; import com.atsuishio.superbwarfare.client.renderer.item.AK12ItemRenderer;
import com.atsuishio.superbwarfare.event.ClientEventHandler; import com.atsuishio.superbwarfare.event.ClientEventHandler;
import com.atsuishio.superbwarfare.init.ModAttachments;
import com.atsuishio.superbwarfare.init.ModSounds; import com.atsuishio.superbwarfare.init.ModSounds;
import com.atsuishio.superbwarfare.init.ModTags; import com.atsuishio.superbwarfare.init.ModTags;
import com.atsuishio.superbwarfare.item.gun.GunItem; import com.atsuishio.superbwarfare.item.gun.GunItem;
@ -111,8 +111,7 @@ public class AK12Item extends GunItem implements GeoItem {
ItemStack stack = player.getMainHandItem(); ItemStack stack = player.getMainHandItem();
if (!stack.is(ModTags.Items.GUN)) return PlayState.STOP; if (!stack.is(ModTags.Items.GUN)) return PlayState.STOP;
var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); if (player.getData(ModAttachments.PLAYER_VARIABLE).edit) {
if (cap != null && cap.edit) {
return event.setAndContinue(RawAnimation.begin().thenPlay("animation.ak12.edit")); return event.setAndContinue(RawAnimation.begin().thenPlay("animation.ak12.edit"));
} }

View file

@ -1,9 +1,9 @@
package com.atsuishio.superbwarfare.item.gun.rifle; package com.atsuishio.superbwarfare.item.gun.rifle;
import com.atsuishio.superbwarfare.Mod; import com.atsuishio.superbwarfare.Mod;
import com.atsuishio.superbwarfare.init.ModCapabilities;
import com.atsuishio.superbwarfare.client.renderer.item.AK47ItemRenderer; import com.atsuishio.superbwarfare.client.renderer.item.AK47ItemRenderer;
import com.atsuishio.superbwarfare.event.ClientEventHandler; import com.atsuishio.superbwarfare.event.ClientEventHandler;
import com.atsuishio.superbwarfare.init.ModAttachments;
import com.atsuishio.superbwarfare.init.ModSounds; import com.atsuishio.superbwarfare.init.ModSounds;
import com.atsuishio.superbwarfare.init.ModTags; import com.atsuishio.superbwarfare.init.ModTags;
import com.atsuishio.superbwarfare.item.gun.GunItem; import com.atsuishio.superbwarfare.item.gun.GunItem;
@ -119,8 +119,7 @@ public class AK47Item extends GunItem implements GeoItem {
ItemStack stack = player.getMainHandItem(); ItemStack stack = player.getMainHandItem();
if (!stack.is(ModTags.Items.GUN)) return PlayState.STOP; if (!stack.is(ModTags.Items.GUN)) return PlayState.STOP;
var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); if (player.getData(ModAttachments.PLAYER_VARIABLE).edit) {
if (cap != null && cap.edit) {
return event.setAndContinue(RawAnimation.begin().thenPlay("animation.ak47.edit")); return event.setAndContinue(RawAnimation.begin().thenPlay("animation.ak47.edit"));
} }

View file

@ -1,9 +1,9 @@
package com.atsuishio.superbwarfare.item.gun.rifle; package com.atsuishio.superbwarfare.item.gun.rifle;
import com.atsuishio.superbwarfare.Mod; import com.atsuishio.superbwarfare.Mod;
import com.atsuishio.superbwarfare.init.ModCapabilities;
import com.atsuishio.superbwarfare.client.renderer.item.Hk416ItemRenderer; import com.atsuishio.superbwarfare.client.renderer.item.Hk416ItemRenderer;
import com.atsuishio.superbwarfare.event.ClientEventHandler; import com.atsuishio.superbwarfare.event.ClientEventHandler;
import com.atsuishio.superbwarfare.init.ModAttachments;
import com.atsuishio.superbwarfare.init.ModSounds; import com.atsuishio.superbwarfare.init.ModSounds;
import com.atsuishio.superbwarfare.init.ModTags; import com.atsuishio.superbwarfare.init.ModTags;
import com.atsuishio.superbwarfare.item.gun.GunItem; import com.atsuishio.superbwarfare.item.gun.GunItem;
@ -114,8 +114,7 @@ public class Hk416Item extends GunItem implements GeoItem {
ItemStack stack = player.getMainHandItem(); ItemStack stack = player.getMainHandItem();
if (!stack.is(ModTags.Items.GUN)) return PlayState.STOP; if (!stack.is(ModTags.Items.GUN)) return PlayState.STOP;
var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); if (player.getData(ModAttachments.PLAYER_VARIABLE).edit) {
if (cap != null && cap.edit) {
return event.setAndContinue(RawAnimation.begin().thenPlay("animation.m4.edit")); return event.setAndContinue(RawAnimation.begin().thenPlay("animation.m4.edit"));
} }

View file

@ -1,9 +1,9 @@
package com.atsuishio.superbwarfare.item.gun.rifle; package com.atsuishio.superbwarfare.item.gun.rifle;
import com.atsuishio.superbwarfare.Mod; import com.atsuishio.superbwarfare.Mod;
import com.atsuishio.superbwarfare.init.ModCapabilities;
import com.atsuishio.superbwarfare.client.renderer.item.M4ItemRenderer; import com.atsuishio.superbwarfare.client.renderer.item.M4ItemRenderer;
import com.atsuishio.superbwarfare.event.ClientEventHandler; import com.atsuishio.superbwarfare.event.ClientEventHandler;
import com.atsuishio.superbwarfare.init.ModAttachments;
import com.atsuishio.superbwarfare.init.ModSounds; import com.atsuishio.superbwarfare.init.ModSounds;
import com.atsuishio.superbwarfare.init.ModTags; import com.atsuishio.superbwarfare.init.ModTags;
import com.atsuishio.superbwarfare.item.gun.GunItem; import com.atsuishio.superbwarfare.item.gun.GunItem;
@ -115,8 +115,7 @@ public class M4Item extends GunItem implements GeoItem {
ItemStack stack = player.getMainHandItem(); ItemStack stack = player.getMainHandItem();
if (!stack.is(ModTags.Items.GUN)) return PlayState.STOP; if (!stack.is(ModTags.Items.GUN)) return PlayState.STOP;
var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); if (player.getData(ModAttachments.PLAYER_VARIABLE).edit) {
if (cap != null && cap.edit) {
return event.setAndContinue(RawAnimation.begin().thenPlay("animation.m4.edit")); return event.setAndContinue(RawAnimation.begin().thenPlay("animation.m4.edit"));
} }

View file

@ -1,9 +1,9 @@
package com.atsuishio.superbwarfare.item.gun.rifle; package com.atsuishio.superbwarfare.item.gun.rifle;
import com.atsuishio.superbwarfare.Mod; import com.atsuishio.superbwarfare.Mod;
import com.atsuishio.superbwarfare.init.ModCapabilities;
import com.atsuishio.superbwarfare.client.renderer.item.Mk14ItemRenderer; import com.atsuishio.superbwarfare.client.renderer.item.Mk14ItemRenderer;
import com.atsuishio.superbwarfare.event.ClientEventHandler; import com.atsuishio.superbwarfare.event.ClientEventHandler;
import com.atsuishio.superbwarfare.init.ModAttachments;
import com.atsuishio.superbwarfare.init.ModSounds; import com.atsuishio.superbwarfare.init.ModSounds;
import com.atsuishio.superbwarfare.init.ModTags; import com.atsuishio.superbwarfare.init.ModTags;
import com.atsuishio.superbwarfare.item.gun.GunItem; import com.atsuishio.superbwarfare.item.gun.GunItem;
@ -114,8 +114,7 @@ public class Mk14Item extends GunItem implements GeoItem {
ItemStack stack = player.getMainHandItem(); ItemStack stack = player.getMainHandItem();
if (!stack.is(ModTags.Items.GUN)) return PlayState.STOP; if (!stack.is(ModTags.Items.GUN)) return PlayState.STOP;
var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); if (player.getData(ModAttachments.PLAYER_VARIABLE).edit) {
if (cap != null && cap.edit) {
return event.setAndContinue(RawAnimation.begin().thenPlay("animation.m14.edit")); return event.setAndContinue(RawAnimation.begin().thenPlay("animation.m14.edit"));
} }

View file

@ -1,9 +1,9 @@
package com.atsuishio.superbwarfare.item.gun.rifle; package com.atsuishio.superbwarfare.item.gun.rifle;
import com.atsuishio.superbwarfare.Mod; import com.atsuishio.superbwarfare.Mod;
import com.atsuishio.superbwarfare.init.ModCapabilities;
import com.atsuishio.superbwarfare.client.renderer.item.Qbz95ItemRenderer; import com.atsuishio.superbwarfare.client.renderer.item.Qbz95ItemRenderer;
import com.atsuishio.superbwarfare.event.ClientEventHandler; import com.atsuishio.superbwarfare.event.ClientEventHandler;
import com.atsuishio.superbwarfare.init.ModAttachments;
import com.atsuishio.superbwarfare.init.ModSounds; import com.atsuishio.superbwarfare.init.ModSounds;
import com.atsuishio.superbwarfare.init.ModTags; import com.atsuishio.superbwarfare.init.ModTags;
import com.atsuishio.superbwarfare.item.gun.GunItem; import com.atsuishio.superbwarfare.item.gun.GunItem;
@ -117,8 +117,7 @@ public class Qbz95Item extends GunItem implements GeoItem {
ItemStack stack = player.getMainHandItem(); ItemStack stack = player.getMainHandItem();
if (!stack.is(ModTags.Items.GUN)) return PlayState.STOP; if (!stack.is(ModTags.Items.GUN)) return PlayState.STOP;
var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); if (player.getData(ModAttachments.PLAYER_VARIABLE).edit) {
if (cap != null && cap.edit) {
return event.setAndContinue(RawAnimation.begin().thenPlay("animation.qbz95.edit")); return event.setAndContinue(RawAnimation.begin().thenPlay("animation.qbz95.edit"));
} }

View file

@ -1,9 +1,9 @@
package com.atsuishio.superbwarfare.item.gun.smg; package com.atsuishio.superbwarfare.item.gun.smg;
import com.atsuishio.superbwarfare.Mod; import com.atsuishio.superbwarfare.Mod;
import com.atsuishio.superbwarfare.init.ModCapabilities;
import com.atsuishio.superbwarfare.client.renderer.item.VectorItemRenderer; import com.atsuishio.superbwarfare.client.renderer.item.VectorItemRenderer;
import com.atsuishio.superbwarfare.event.ClientEventHandler; import com.atsuishio.superbwarfare.event.ClientEventHandler;
import com.atsuishio.superbwarfare.init.ModAttachments;
import com.atsuishio.superbwarfare.init.ModSounds; import com.atsuishio.superbwarfare.init.ModSounds;
import com.atsuishio.superbwarfare.init.ModTags; import com.atsuishio.superbwarfare.init.ModTags;
import com.atsuishio.superbwarfare.item.gun.GunItem; import com.atsuishio.superbwarfare.item.gun.GunItem;
@ -87,8 +87,7 @@ public class VectorItem extends GunItem implements GeoItem {
ItemStack stack = player.getMainHandItem(); ItemStack stack = player.getMainHandItem();
if (!stack.is(ModTags.Items.GUN)) return PlayState.STOP; if (!stack.is(ModTags.Items.GUN)) return PlayState.STOP;
var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); if (player.getData(ModAttachments.PLAYER_VARIABLE).edit) {
if (cap != null && cap.edit) {
return event.setAndContinue(RawAnimation.begin().thenPlay("animation.vector.edit")); return event.setAndContinue(RawAnimation.begin().thenPlay("animation.vector.edit"));
} }

View file

@ -1,9 +1,9 @@
package com.atsuishio.superbwarfare.item.gun.sniper; package com.atsuishio.superbwarfare.item.gun.sniper;
import com.atsuishio.superbwarfare.Mod; import com.atsuishio.superbwarfare.Mod;
import com.atsuishio.superbwarfare.init.ModCapabilities;
import com.atsuishio.superbwarfare.client.renderer.item.SvdItemRenderer; import com.atsuishio.superbwarfare.client.renderer.item.SvdItemRenderer;
import com.atsuishio.superbwarfare.event.ClientEventHandler; import com.atsuishio.superbwarfare.event.ClientEventHandler;
import com.atsuishio.superbwarfare.init.ModAttachments;
import com.atsuishio.superbwarfare.init.ModSounds; import com.atsuishio.superbwarfare.init.ModSounds;
import com.atsuishio.superbwarfare.init.ModTags; import com.atsuishio.superbwarfare.init.ModTags;
import com.atsuishio.superbwarfare.item.gun.GunItem; import com.atsuishio.superbwarfare.item.gun.GunItem;
@ -78,9 +78,7 @@ public class SvdItem extends GunItem implements GeoItem {
ItemStack stack = player.getMainHandItem(); ItemStack stack = player.getMainHandItem();
if (!stack.is(ModTags.Items.GUN)) return PlayState.STOP; if (!stack.is(ModTags.Items.GUN)) return PlayState.STOP;
var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); if (player.getData(ModAttachments.PLAYER_VARIABLE).edit) {
if (cap != null && cap.edit) {
return event.setAndContinue(RawAnimation.begin().thenPlay("animation.svd.edit")); return event.setAndContinue(RawAnimation.begin().thenPlay("animation.svd.edit"));
} }

View file

@ -1,14 +1,10 @@
package com.atsuishio.superbwarfare.item.gun.special; package com.atsuishio.superbwarfare.item.gun.special;
import com.atsuishio.superbwarfare.Mod; import com.atsuishio.superbwarfare.Mod;
import com.atsuishio.superbwarfare.init.ModCapabilities;
import com.atsuishio.superbwarfare.client.renderer.item.BocekItemRenderer; import com.atsuishio.superbwarfare.client.renderer.item.BocekItemRenderer;
import com.atsuishio.superbwarfare.client.tooltip.component.BocekImageComponent; import com.atsuishio.superbwarfare.client.tooltip.component.BocekImageComponent;
import com.atsuishio.superbwarfare.event.ClientEventHandler; import com.atsuishio.superbwarfare.event.ClientEventHandler;
import com.atsuishio.superbwarfare.init.ModItems; import com.atsuishio.superbwarfare.init.*;
import com.atsuishio.superbwarfare.init.ModPerks;
import com.atsuishio.superbwarfare.init.ModSounds;
import com.atsuishio.superbwarfare.init.ModTags;
import com.atsuishio.superbwarfare.item.gun.GunItem; import com.atsuishio.superbwarfare.item.gun.GunItem;
import com.atsuishio.superbwarfare.item.gun.SpecialFireWeapon; import com.atsuishio.superbwarfare.item.gun.SpecialFireWeapon;
import com.atsuishio.superbwarfare.item.gun.data.GunData; 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) { if (GunsTool.getGunDoubleTag(tag, "Power") >= 6) {
var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); if (player.getData(ModAttachments.PLAYER_VARIABLE).zoom) {
if (cap != null && cap.zoom) {
spawnBullet(player, tag); spawnBullet(player, tag);
SoundTool.playLocalSound(player, ModSounds.BOCEK_ZOOM_FIRE_1P.get(), 10, 1); SoundTool.playLocalSound(player, ModSounds.BOCEK_ZOOM_FIRE_1P.get(), 10, 1);
@ -218,10 +213,9 @@ public class BocekItem extends GunItem implements GeoItem, SpecialFireWeapon {
@Override @Override
public void fireOnPress(Player player, final GunData data) { public void fireOnPress(Player player, final GunData data) {
var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); var cap = player.getData(ModAttachments.PLAYER_VARIABLE).watch();
if (cap != null) { cap.bowPullHold = true;
cap.bowPullHold = true; player.setData(ModAttachments.PLAYER_VARIABLE, cap);
cap.syncPlayerVariables(player); cap.sync(player);
}
} }
} }

View file

@ -1,15 +1,11 @@
package com.atsuishio.superbwarfare.item.gun.special; package com.atsuishio.superbwarfare.item.gun.special;
import com.atsuishio.superbwarfare.Mod; import com.atsuishio.superbwarfare.Mod;
import com.atsuishio.superbwarfare.init.ModCapabilities;
import com.atsuishio.superbwarfare.client.renderer.item.TaserItemRenderer; import com.atsuishio.superbwarfare.client.renderer.item.TaserItemRenderer;
import com.atsuishio.superbwarfare.client.tooltip.component.EnergyImageComponent; import com.atsuishio.superbwarfare.client.tooltip.component.EnergyImageComponent;
import com.atsuishio.superbwarfare.entity.projectile.TaserBulletEntity; import com.atsuishio.superbwarfare.entity.projectile.TaserBulletEntity;
import com.atsuishio.superbwarfare.event.ClientEventHandler; import com.atsuishio.superbwarfare.event.ClientEventHandler;
import com.atsuishio.superbwarfare.init.ModItems; import com.atsuishio.superbwarfare.init.*;
import com.atsuishio.superbwarfare.init.ModPerks;
import com.atsuishio.superbwarfare.init.ModSounds;
import com.atsuishio.superbwarfare.init.ModTags;
import com.atsuishio.superbwarfare.item.EnergyStorageItem; import com.atsuishio.superbwarfare.item.EnergyStorageItem;
import com.atsuishio.superbwarfare.item.gun.GunItem; import com.atsuishio.superbwarfare.item.gun.GunItem;
import com.atsuishio.superbwarfare.item.gun.SpecialFireWeapon; 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) { public void fireOnPress(Player player, final GunData data) {
if (data.reloading()) return; if (data.reloading()) return;
ItemStack stack = data.stack(); ItemStack stack = data.stack();
var tag = data.tag();
int perkLevel = data.perk.getLevel(ModPerks.VOLT_OVERLOAD); int perkLevel = data.perk.getLevel(ModPerks.VOLT_OVERLOAD);
var energyStorage = stack.getCapability(Capabilities.EnergyStorage.ITEM); 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); player.getCooldowns().addCooldown(stack.getItem(), 5);
if (player instanceof ServerPlayer serverPlayer) { if (player instanceof ServerPlayer serverPlayer) {
var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); boolean zoom = player.getData(ModAttachments.PLAYER_VARIABLE).zoom;
boolean zoom = cap != null && cap.zoom;
double spread = data.spread(); double spread = data.spread();
int volt = data.perk.getLevel(ModPerks.VOLT_OVERLOAD); int volt = data.perk.getLevel(ModPerks.VOLT_OVERLOAD);

View file

@ -1,8 +1,8 @@
package com.atsuishio.superbwarfare.network.message.receive; package com.atsuishio.superbwarfare.network.message.receive;
import com.atsuishio.superbwarfare.Mod; import com.atsuishio.superbwarfare.Mod;
import com.atsuishio.superbwarfare.init.ModCapabilities;
import com.atsuishio.superbwarfare.capability.player.PlayerVariable; import com.atsuishio.superbwarfare.capability.player.PlayerVariable;
import com.atsuishio.superbwarfare.init.ModAttachments;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
import net.minecraft.nbt.CompoundTag; 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()); var entity = Minecraft.getInstance().player.level().getEntity(message.target());
if (entity == null) return; if (entity == null) return;
var variables = entity.getCapability(ModCapabilities.PLAYER_VARIABLE, null); entity.setData(ModAttachments.PLAYER_VARIABLE, data);
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;
} }
@Override @Override

View file

@ -1,7 +1,7 @@
package com.atsuishio.superbwarfare.network.message.send; package com.atsuishio.superbwarfare.network.message.send;
import com.atsuishio.superbwarfare.Mod; import com.atsuishio.superbwarfare.Mod;
import com.atsuishio.superbwarfare.init.ModCapabilities; import com.atsuishio.superbwarfare.init.ModAttachments;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import net.minecraft.network.codec.ByteBufCodecs; import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec; 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) { public static void handler(final BreathMessage message, final IPayloadContext context) {
ServerPlayer player = (ServerPlayer) context.player(); ServerPlayer player = (ServerPlayer) context.player();
var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); var cap = player.getData(ModAttachments.PLAYER_VARIABLE).watch();
if (cap == null) return;
if (message.msgType if (message.msgType
&& !cap.breathExhaustion && !cap.breathExhaustion
&& cap.zoom && cap.zoom
&& player.getPersistentData().getDouble("NoBreath") == 0 && player.getPersistentData().getDouble("NoBreath") == 0
) { ) {
cap.breath = true; cap.breath = true;
cap.syncPlayerVariables(player); player.setData(ModAttachments.PLAYER_VARIABLE, cap);
cap.sync(player);
} }
if (!message.msgType) { if (!message.msgType) {
cap.breath = false; cap.breath = false;
cap.syncPlayerVariables(player); player.setData(ModAttachments.PLAYER_VARIABLE, cap);
cap.sync(player);
} }
} }

View file

@ -1,7 +1,7 @@
package com.atsuishio.superbwarfare.network.message.send; package com.atsuishio.superbwarfare.network.message.send;
import com.atsuishio.superbwarfare.Mod; 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.init.ModSounds;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import net.minecraft.core.BlockPos; 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); level.playSound(null, BlockPos.containing(x, y, z), ModSounds.DOUBLE_JUMP.get(), SoundSource.BLOCKS, 1, 1);
var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); var cap = player.getData(ModAttachments.PLAYER_VARIABLE).watch();
if (cap == null) return;
cap.playerDoubleJump = message.canDoubleJump; cap.playerDoubleJump = message.canDoubleJump;
cap.syncPlayerVariables(player); player.setData(ModAttachments.PLAYER_VARIABLE, cap);
cap.sync(player);
} }
@Override @Override

View file

@ -1,7 +1,7 @@
package com.atsuishio.superbwarfare.network.message.send; package com.atsuishio.superbwarfare.network.message.send;
import com.atsuishio.superbwarfare.Mod; 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.init.ModSounds;
import com.atsuishio.superbwarfare.item.gun.GunItem; import com.atsuishio.superbwarfare.item.gun.GunItem;
import com.atsuishio.superbwarfare.tools.SoundTool; import com.atsuishio.superbwarfare.tools.SoundTool;
@ -33,14 +33,15 @@ public record EditModeMessage(int msgType) implements CustomPacketPayload {
ItemStack mainHandItem = player.getMainHandItem(); ItemStack mainHandItem = player.getMainHandItem();
if (!(mainHandItem.getItem() instanceof GunItem gunItem)) return; 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) { if (!cap.edit) {
SoundTool.playLocalSound(player, ModSounds.EDIT_MODE.get(), 1f, 1f); SoundTool.playLocalSound(player, ModSounds.EDIT_MODE.get(), 1f, 1f);
} }
cap.edit = !cap.edit; cap.edit = !cap.edit;
cap.syncPlayerVariables(player); player.setData(ModAttachments.PLAYER_VARIABLE, cap);
cap.sync(player);
} }
} }

View file

@ -1,9 +1,9 @@
package com.atsuishio.superbwarfare.network.message.send; package com.atsuishio.superbwarfare.network.message.send;
import com.atsuishio.superbwarfare.Mod; import com.atsuishio.superbwarfare.Mod;
import com.atsuishio.superbwarfare.init.ModCapabilities;
import com.atsuishio.superbwarfare.entity.projectile.ProjectileEntity; import com.atsuishio.superbwarfare.entity.projectile.ProjectileEntity;
import com.atsuishio.superbwarfare.event.GunEventHandler; import com.atsuishio.superbwarfare.event.GunEventHandler;
import com.atsuishio.superbwarfare.init.ModAttachments;
import com.atsuishio.superbwarfare.init.ModPerks; import com.atsuishio.superbwarfare.init.ModPerks;
import com.atsuishio.superbwarfare.init.ModTags; import com.atsuishio.superbwarfare.init.ModTags;
import com.atsuishio.superbwarfare.item.gun.SpecialFireWeapon; import com.atsuishio.superbwarfare.item.gun.SpecialFireWeapon;
@ -49,39 +49,36 @@ public record FireMessage(int msgType) implements CustomPacketPayload {
handleGunBolt(player, stack); handleGunBolt(player, stack);
var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); var cap = player.getData(ModAttachments.PLAYER_VARIABLE).watch();
if (type == 0) { if (type == 0) {
if (tag.getDouble("PrepareTime") == 0 && data.reloading() && data.ammo() > 0) { if (tag.getDouble("PrepareTime") == 0 && data.reloading() && data.ammo() > 0) {
tag.putBoolean("ForceStop", true); tag.putBoolean("ForceStop", true);
} }
if (cap != null) { cap.edit = false;
cap.edit = false;
}
// 按下开火 // 按下开火
if (!(stack.getItem() instanceof SpecialFireWeapon specialFireWeapon)) { if (!(stack.getItem() instanceof SpecialFireWeapon specialFireWeapon)) {
if (cap != null) cap.syncPlayerVariables(player); player.setData(ModAttachments.PLAYER_VARIABLE, cap);
cap.sync(player);
return; return;
} }
specialFireWeapon.fireOnPress(player, data); specialFireWeapon.fireOnPress(player, data);
if (cap != null) { cap.holdFire = true;
cap.holdFire = true; player.setData(ModAttachments.PLAYER_VARIABLE, cap);
cap.syncPlayerVariables(player);
}
} else if (type == 1) { } else if (type == 1) {
if (cap != null) { cap.bowPullHold = false;
cap.bowPullHold = false; cap.holdFire = false;
cap.holdFire = false; player.setData(ModAttachments.PLAYER_VARIABLE, cap);
cap.syncPlayerVariables(player);
}
// 松开开火 // 松开开火
if (stack.getItem() instanceof SpecialFireWeapon specialFireWeapon) { if (stack.getItem() instanceof SpecialFireWeapon specialFireWeapon) {
specialFireWeapon.fireOnRelease(player, data); specialFireWeapon.fireOnRelease(player, data);
} }
} }
cap.sync(player);
data.save(); data.save();
} }
@ -122,8 +119,7 @@ public record FireMessage(int msgType) implements CustomPacketPayload {
float bypassArmorRate = (float) data.bypassArmor(); float bypassArmorRate = (float) data.bypassArmor();
double damage; double damage;
var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); boolean zoom = player.getData(ModAttachments.PLAYER_VARIABLE).zoom;
boolean zoom = cap != null && cap.zoom;
float spread; float spread;
if (zoom) { if (zoom) {

View file

@ -1,7 +1,7 @@
package com.atsuishio.superbwarfare.network.message.send; package com.atsuishio.superbwarfare.network.message.send;
import com.atsuishio.superbwarfare.Mod; 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.ModItems;
import com.atsuishio.superbwarfare.init.ModTags; import com.atsuishio.superbwarfare.init.ModTags;
import com.atsuishio.superbwarfare.item.gun.GunItem; 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) { public static void pressAction(Player player, int type) {
if (type != 0) return; if (type != 0) return;
var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); var cap = player.getData(ModAttachments.PLAYER_VARIABLE).watch();
if (cap != null) { cap.edit = false;
cap.edit = false; player.setData(ModAttachments.PLAYER_VARIABLE, cap);
cap.syncPlayerVariables(player); cap.sync(player);
}
ItemStack stack = player.getMainHandItem(); ItemStack stack = player.getMainHandItem();
if (!(stack.getItem() instanceof GunItem gunItem)) return; 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())); 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) { if (stack.is(ModTags.Items.USE_SHOTGUN_AMMO) && cap.shotgunAmmo == 0) {
return; return;
} else if (stack.is(ModTags.Items.USE_SNIPER_AMMO) && cap.sniperAmmo == 0) { } else if (stack.is(ModTags.Items.USE_SNIPER_AMMO) && cap.sniperAmmo == 0) {

View file

@ -1,12 +1,8 @@
package com.atsuishio.superbwarfare.network.message.send; package com.atsuishio.superbwarfare.network.message.send;
import com.atsuishio.superbwarfare.Mod; import com.atsuishio.superbwarfare.Mod;
import com.atsuishio.superbwarfare.init.ModCapabilities;
import com.atsuishio.superbwarfare.event.GunEventHandler; import com.atsuishio.superbwarfare.event.GunEventHandler;
import com.atsuishio.superbwarfare.init.ModItems; import com.atsuishio.superbwarfare.init.*;
import com.atsuishio.superbwarfare.init.ModPerks;
import com.atsuishio.superbwarfare.init.ModSounds;
import com.atsuishio.superbwarfare.init.ModTags;
import com.atsuishio.superbwarfare.item.gun.data.GunData; import com.atsuishio.superbwarfare.item.gun.data.GunData;
import com.atsuishio.superbwarfare.perk.AmmoPerk; import com.atsuishio.superbwarfare.perk.AmmoPerk;
import com.atsuishio.superbwarfare.perk.Perk; import com.atsuishio.superbwarfare.perk.Perk;
@ -96,8 +92,7 @@ public record ShootMessage(double spread) implements CustomPacketPayload {
GunEventHandler.playGunSounds(player); GunEventHandler.playGunSounds(player);
} }
} else if (stack.is(ModItems.MINIGUN.get())) { } else if (stack.is(ModItems.MINIGUN.get())) {
var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); var cap = player.getData(ModAttachments.PLAYER_VARIABLE).watch();
if (cap == null) return;
if (cap.rifleAmmo > 0 || InventoryTool.hasCreativeAmmoBox(player)) { if (cap.rifleAmmo > 0 || InventoryTool.hasCreativeAmmoBox(player)) {
tag.putDouble("heat", (tag.getDouble("heat") + 0.1)); tag.putDouble("heat", (tag.getDouble("heat") + 0.1));
@ -127,7 +122,8 @@ public record ShootMessage(double spread) implements CustomPacketPayload {
GunEventHandler.gunShoot(player, tag, spared); GunEventHandler.gunShoot(player, tag, spared);
if (!InventoryTool.hasCreativeAmmoBox(player)) { if (!InventoryTool.hasCreativeAmmoBox(player)) {
cap.rifleAmmo = cap.rifleAmmo - 1; cap.rifleAmmo = cap.rifleAmmo - 1;
cap.syncPlayerVariables(player); player.setData(ModAttachments.PLAYER_VARIABLE, cap);
cap.sync(player);
} }
} }
} }

View file

@ -1,9 +1,9 @@
package com.atsuishio.superbwarfare.network.message.send; package com.atsuishio.superbwarfare.network.message.send;
import com.atsuishio.superbwarfare.Mod; 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.VehicleEntity;
import com.atsuishio.superbwarfare.entity.vehicle.base.WeaponVehicleEntity; 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.ModItems;
import com.atsuishio.superbwarfare.init.ModSounds; import com.atsuishio.superbwarfare.init.ModSounds;
import com.atsuishio.superbwarfare.item.gun.data.GunData; import com.atsuishio.superbwarfare.item.gun.data.GunData;
@ -32,14 +32,13 @@ public record ZoomMessage(int msgType) implements CustomPacketPayload {
var vehicle = player.getVehicle(); var vehicle = player.getVehicle();
// 缩放音效播放条件: 载具是武器载具且该位置有可用武器 // 缩放音效播放条件: 载具是武器载具且该位置有可用武器
var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE); var cap = player.getData(ModAttachments.PLAYER_VARIABLE).watch();
if (message.msgType == 0) { if (message.msgType == 0) {
if (cap != null) { cap.zoom = true;
cap.zoom = true; cap.edit = false;
cap.edit = false; player.setData(ModAttachments.PLAYER_VARIABLE, cap);
cap.syncPlayerVariables(player); cap.sync(player);
}
if (player.isPassenger() if (player.isPassenger()
&& vehicle instanceof WeaponVehicleEntity weaponEntity && 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); ) SoundTool.playLocalSound(player, ModSounds.CANNON_ZOOM_IN.get(), 2, 1);
} else if (message.msgType == 1) { } else if (message.msgType == 1) {
if (cap != null) { cap.zoom = false;
cap.zoom = false; cap.breath = false;
cap.breath = false; player.setData(ModAttachments.PLAYER_VARIABLE, cap);
cap.syncPlayerVariables(player); cap.sync(player);
}
if (player.isPassenger() if (player.isPassenger()
&& vehicle instanceof WeaponVehicleEntity weaponEntity && vehicle instanceof WeaponVehicleEntity weaponEntity

View file

@ -1,7 +1,7 @@
package com.atsuishio.superbwarfare.tools; package com.atsuishio.superbwarfare.tools;
import com.atsuishio.superbwarfare.init.ModCapabilities;
import com.atsuishio.superbwarfare.capability.player.PlayerVariable; import com.atsuishio.superbwarfare.capability.player.PlayerVariable;
import com.atsuishio.superbwarfare.init.ModAttachments;
import net.minecraft.core.component.DataComponentType; import net.minecraft.core.component.DataComponentType;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.Entity;
@ -90,18 +90,17 @@ public enum AmmoType {
// Entity // Entity
public int get(Entity entity) { public int get(Entity entity) {
var cap = entity.getCapability(ModCapabilities.PLAYER_VARIABLE); var cap = entity.getData(ModAttachments.PLAYER_VARIABLE);
if (cap == null) return 0;
return get(cap); return get(cap);
} }
public void set(Entity entity, int count) { public void set(Entity entity, int count) {
var cap = entity.getCapability(ModCapabilities.PLAYER_VARIABLE); var cap = entity.getData(ModAttachments.PLAYER_VARIABLE).watch();
if (cap == null) return;
set(cap, count); set(cap, count);
cap.syncPlayerVariables(entity); entity.setData(ModAttachments.PLAYER_VARIABLE, cap);
cap.sync(entity);
} }
public void add(Entity entity, int count) { public void add(Entity entity, int count) {

View file

@ -1,7 +1,7 @@
package com.atsuishio.superbwarfare.tools; package com.atsuishio.superbwarfare.tools;
import com.atsuishio.superbwarfare.Mod; 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.init.ModTags;
import com.atsuishio.superbwarfare.item.gun.data.GunData; import com.atsuishio.superbwarfare.item.gun.data.GunData;
import com.atsuishio.superbwarfare.item.gun.data.ReloadState; import com.atsuishio.superbwarfare.item.gun.data.ReloadState;
@ -88,15 +88,14 @@ public class GunsTool {
data.bolt.markNeedless(); data.bolt.markNeedless();
} }
var capability = player.getCapability(ModCapabilities.PLAYER_VARIABLE); var capability = player.getData(ModAttachments.PLAYER_VARIABLE).watch();
var playerAmmo = 0; var playerAmmo = 0;
if (capability != null) { playerAmmo = type.get(capability);
playerAmmo = type.get(capability); var newAmmoCount = Math.max(0, playerAmmo - ammoToAdd);
var newAmmoCount = Math.max(0, playerAmmo - ammoToAdd); type.set(capability, newAmmoCount);
type.set(capability, newAmmoCount); player.setData(ModAttachments.PLAYER_VARIABLE, capability);
capability.syncPlayerVariables(player); capability.sync(player);
}
int needToAdd = ammo + Math.min(ammoToAdd, playerAmmo); int needToAdd = ammo + Math.min(ammoToAdd, playerAmmo);
@ -117,18 +116,6 @@ public class GunsTool {
return tag.getInt(name); 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) { public static void setPerkBooleanTag(final CompoundTag rootTag, String name, boolean flag) {
CompoundTag tag = rootTag.getCompound("PerkData"); CompoundTag tag = rootTag.getCompound("PerkData");
if (!tag.contains(name) && !flag) return; if (!tag.contains(name) && !flag) return;