提取所有弹药类型属性

This commit is contained in:
Light_Quanta 2025-04-15 22:06:10 +08:00
parent c912e271a6
commit b372ae9612
No known key found for this signature in database
GPG key ID: 11A39A1B8C890959
3 changed files with 20 additions and 34 deletions

View file

@ -16,16 +16,15 @@ import net.neoforged.neoforge.network.PacketDistributor;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import javax.annotation.ParametersAreNonnullByDefault; import javax.annotation.ParametersAreNonnullByDefault;
import java.util.HashMap;
import java.util.Map;
@EventBusSubscriber(modid = Mod.MODID) @EventBusSubscriber(modid = Mod.MODID)
public class PlayerVariable implements INBTSerializable<CompoundTag> { public class PlayerVariable implements INBTSerializable<CompoundTag> {
private PlayerVariable old = null; private PlayerVariable old = null;
public int rifleAmmo = 0;
public int handgunAmmo = 0; public Map<AmmoType, Integer> ammo = new HashMap<>();
public int shotgunAmmo = 0;
public int sniperAmmo = 0;
public int heavyAmmo = 0;
public boolean tacticalSprint = false; public boolean tacticalSprint = false;
public boolean edit = false; public boolean edit = false;
@ -79,15 +78,13 @@ public class PlayerVariable implements INBTSerializable<CompoundTag> {
public PlayerVariable copy() { public PlayerVariable copy() {
var clone = new PlayerVariable(); var clone = new PlayerVariable();
clone.rifleAmmo = this.rifleAmmo; for (var type : AmmoType.values()) {
clone.handgunAmmo = this.handgunAmmo; type.set(clone, type.get(this));
clone.shotgunAmmo = this.shotgunAmmo; }
clone.sniperAmmo = this.sniperAmmo;
clone.heavyAmmo = this.heavyAmmo;
clone.edit = this.edit; clone.edit = this.edit;
clone.tacticalSprint = this.tacticalSprint; clone.tacticalSprint = this.tacticalSprint;
return clone; return clone;
} }
@ -95,11 +92,11 @@ public class PlayerVariable implements INBTSerializable<CompoundTag> {
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (!(obj instanceof PlayerVariable other)) return false; if (!(obj instanceof PlayerVariable other)) return false;
return rifleAmmo == other.rifleAmmo for (var type : AmmoType.values()) {
&& handgunAmmo == other.handgunAmmo if (type.get(this) != type.get(other)) return false;
&& shotgunAmmo == other.shotgunAmmo }
&& sniperAmmo == other.sniperAmmo
&& heavyAmmo == other.heavyAmmo return tacticalSprint == other.tacticalSprint
&& edit == other.edit; && edit == other.edit;
} }

View file

@ -52,6 +52,7 @@ import net.neoforged.neoforge.network.PacketDistributor;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Objects; import java.util.Objects;
import java.util.stream.Stream;
@EventBusSubscriber @EventBusSubscriber
public class LivingEventHandler { public class LivingEventHandler {
@ -731,7 +732,9 @@ public class LivingEventHandler {
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.getData(ModAttachments.PLAYER_VARIABLE).watch(); var cap = player.getData(ModAttachments.PLAYER_VARIABLE).watch();
boolean drop = cap.rifleAmmo + cap.handgunAmmo + cap.shotgunAmmo + cap.sniperAmmo + cap.heavyAmmo > 0; boolean drop = Stream.of(AmmoType.values())
.mapToInt(type -> type.get(cap))
.sum() > 0;
if (drop) { if (drop) {
var stack = new ItemStack(ModItems.AMMO_BOX.get()); var stack = new ItemStack(ModItems.AMMO_BOX.get());

View file

@ -62,25 +62,13 @@ public enum AmmoType {
// PlayerVariables // PlayerVariables
public int get(PlayerVariable variable) { public int get(PlayerVariable variable) {
return switch (this) { return variable.ammo.get(this);
case HANDGUN -> variable.handgunAmmo;
case RIFLE -> variable.rifleAmmo;
case SHOTGUN -> variable.shotgunAmmo;
case SNIPER -> variable.sniperAmmo;
case HEAVY -> variable.heavyAmmo;
};
} }
public void set(PlayerVariable variable, int count) { public void set(PlayerVariable variable, int count) {
if (count < 0) count = 0; if (count < 0) count = 0;
switch (this) { variable.ammo.put(this, count);
case HANDGUN -> variable.handgunAmmo = count;
case RIFLE -> variable.rifleAmmo = count;
case SHOTGUN -> variable.shotgunAmmo = count;
case SNIPER -> variable.sniperAmmo = count;
case HEAVY -> variable.heavyAmmo = count;
}
} }
public void add(PlayerVariable variable, int count) { public void add(PlayerVariable variable, int count) {
@ -90,9 +78,7 @@ public enum AmmoType {
// Entity // Entity
public int get(Entity entity) { public int get(Entity entity) {
var cap = entity.getData(ModAttachments.PLAYER_VARIABLE); return get(entity.getData(ModAttachments.PLAYER_VARIABLE));
return get(cap);
} }
public void set(Entity entity, int count) { public void set(Entity entity, int count) {