我的Minecraft插件中的武器系统有问题。

package me.feist2007.loopcityscript.weapons;

import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.entity.Arrow;
import org.bukkit.entity.Player;

import me.feist2007.loopcityscript.main;
import me.feist2007.loopcityscript.utils.Utils;

public class Pistole extends Weapon{
    static int maxAmmoInClip = 12;
    static int Ammo = 12;
    static int AmmoInClip = 12;
    public Pistole(main plugin, Material material, long reloadTime, double damage) {
        super(plugin, material, reloadTime, damage);

    }


    @Override
    public void shootEffects(Player player) {
        if(this.AmmoInClip > 0) {
        Arrow projectile = player.launchProjectile(Arrow.class);
        projectile.setVelocity(player.getEyeLocation().getDirection().multiply(18));
        projectile.setShooter(player);
    player.getWorld().playSound(player.getLocation(), Sound.ENTITY_IRONGOLEM_HURT, 30, 1);
        this.AmmoInClip = this.AmmoInClip - 1;
    }}


    @Override
    protected void reload(Player player) {
        if(this.Ammo >= this.AmmoInClip) {
            this.AmmoInClip = 12;
            this.Ammo = this.Ammo - 12;
        }

    }


}


问题是弹药和重新加载不仅针对一种武器,而且还针对服务器上的所有玩家进行计数。任何想法我怎么能编码,以便重新加载和弹药只算一枚武器

最佳答案

您将AmmoAmmoInClip设置为班级的变量,因此任何播放器都可以更改值。我建议您使用Map<UUID, Integer>来跟踪每个玩家自己的唯一值。 Also, in Java, variable names are supposed to be lowerCamelCase。我在下面的代码段中对此进行了更正,并向您展示了如何使用地图解决问题。

    private Map<UUID, Integer> ammoInClipMap = new HashMap<>();

    @Override
    public void shootEffects(Player player) {
        int ammoInClip = ammoInClipMap.getOrDefault(player.getUniqueId(), 12);

        if (ammoInClip > 0) {
            Arrow projectile = player.launchProjectile(Arrow.class);
            projectile.setVelocity(player.getEyeLocation().getDirection().multiply(18));
            projectile.setShooter(player);
            player.getWorld().playSound(player.getLocation(), Sound.ENTITY_IRONGOLEM_HURT, 30, 1);
            ammoInClipMap.put(player.getUniqueId(), ammoInClip - 1);
        }
    }

07-26 07:56