提取所有弹药类型属性
This commit is contained in:
parent
c912e271a6
commit
b372ae9612
3 changed files with 20 additions and 34 deletions
|
@ -16,16 +16,15 @@ import net.neoforged.neoforge.network.PacketDistributor;
|
|||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import javax.annotation.ParametersAreNonnullByDefault;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
@EventBusSubscriber(modid = Mod.MODID)
|
||||
public class PlayerVariable implements INBTSerializable<CompoundTag> {
|
||||
private PlayerVariable old = null;
|
||||
public int rifleAmmo = 0;
|
||||
public int handgunAmmo = 0;
|
||||
public int shotgunAmmo = 0;
|
||||
public int sniperAmmo = 0;
|
||||
public int heavyAmmo = 0;
|
||||
|
||||
public Map<AmmoType, Integer> ammo = new HashMap<>();
|
||||
public boolean tacticalSprint = false;
|
||||
public boolean edit = false;
|
||||
|
||||
|
@ -79,15 +78,13 @@ public class PlayerVariable implements INBTSerializable<CompoundTag> {
|
|||
public PlayerVariable copy() {
|
||||
var clone = new PlayerVariable();
|
||||
|
||||
clone.rifleAmmo = this.rifleAmmo;
|
||||
clone.handgunAmmo = this.handgunAmmo;
|
||||
clone.shotgunAmmo = this.shotgunAmmo;
|
||||
clone.sniperAmmo = this.sniperAmmo;
|
||||
clone.heavyAmmo = this.heavyAmmo;
|
||||
for (var type : AmmoType.values()) {
|
||||
type.set(clone, type.get(this));
|
||||
}
|
||||
|
||||
clone.edit = this.edit;
|
||||
clone.tacticalSprint = this.tacticalSprint;
|
||||
|
||||
|
||||
return clone;
|
||||
}
|
||||
|
||||
|
@ -95,11 +92,11 @@ public class PlayerVariable implements INBTSerializable<CompoundTag> {
|
|||
public boolean equals(Object obj) {
|
||||
if (!(obj instanceof PlayerVariable other)) return false;
|
||||
|
||||
return rifleAmmo == other.rifleAmmo
|
||||
&& handgunAmmo == other.handgunAmmo
|
||||
&& shotgunAmmo == other.shotgunAmmo
|
||||
&& sniperAmmo == other.sniperAmmo
|
||||
&& heavyAmmo == other.heavyAmmo
|
||||
for (var type : AmmoType.values()) {
|
||||
if (type.get(this) != type.get(other)) return false;
|
||||
}
|
||||
|
||||
return tacticalSprint == other.tacticalSprint
|
||||
&& edit == other.edit;
|
||||
}
|
||||
|
||||
|
|
|
@ -52,6 +52,7 @@ import net.neoforged.neoforge.network.PacketDistributor;
|
|||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@EventBusSubscriber
|
||||
public class LivingEventHandler {
|
||||
|
@ -731,7 +732,9 @@ public class LivingEventHandler {
|
|||
if (event.getEntity() instanceof Player player && !player.level().getLevelData().getGameRules().getBoolean(GameRules.RULE_KEEPINVENTORY)) {
|
||||
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) {
|
||||
var stack = new ItemStack(ModItems.AMMO_BOX.get());
|
||||
|
|
|
@ -62,25 +62,13 @@ public enum AmmoType {
|
|||
|
||||
// PlayerVariables
|
||||
public int get(PlayerVariable variable) {
|
||||
return switch (this) {
|
||||
case HANDGUN -> variable.handgunAmmo;
|
||||
case RIFLE -> variable.rifleAmmo;
|
||||
case SHOTGUN -> variable.shotgunAmmo;
|
||||
case SNIPER -> variable.sniperAmmo;
|
||||
case HEAVY -> variable.heavyAmmo;
|
||||
};
|
||||
return variable.ammo.get(this);
|
||||
}
|
||||
|
||||
public void set(PlayerVariable variable, int count) {
|
||||
if (count < 0) count = 0;
|
||||
|
||||
switch (this) {
|
||||
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;
|
||||
}
|
||||
variable.ammo.put(this, count);
|
||||
}
|
||||
|
||||
public void add(PlayerVariable variable, int count) {
|
||||
|
@ -90,9 +78,7 @@ public enum AmmoType {
|
|||
|
||||
// Entity
|
||||
public int get(Entity entity) {
|
||||
var cap = entity.getData(ModAttachments.PLAYER_VARIABLE);
|
||||
|
||||
return get(cap);
|
||||
return get(entity.getData(ModAttachments.PLAYER_VARIABLE));
|
||||
}
|
||||
|
||||
public void set(Entity entity, int count) {
|
||||
|
|
Loading…
Add table
Reference in a new issue