Solved Kills.addKills(1); not working, more info here

Discussion in 'Plugin Development' started by TheManiacGamers, Apr 4, 2016.

Thread Status:
Not open for further replies.
  1. Offline

    TheManiacGamers

    Ignore the first part,

    I need help doing this

    Kills.addKills(1);

    Here's the onPlayerJoin

    Code:
    PlayerData Kills = PlayerData.pKills.get(p.getUniqueId());PlayerData.pKills.put(p.getUniqueId(), Kills);
    Here's the onPlayerDeath (it casts to k, which is the killer)

    Code:
                PlayerData Kills = PlayerData.pKills.get(k.getPlayer().getUniqueId());
    to then do Kills.addKills(1); to add 1 to the players kill count.

    Here's the hashmap in the PlayerData class;

    Code:
        public static HashMap<UUID, PlayerData> pKills = new HashMap<UUID, PlayerData>();
    Please help, thanks! :)
     
  2. Offline

    mine-care

    Does it really have to be public and static? I cant see why a listener requires a static variable.
    Please follow naming conventions,
    What you mean it "doesnt work"? Does it error? does it incrememt it? Explain the behaviour a bit.
    Also, we need to see the "PlayerData object"
     
  3. Offline

    TheManiacGamers

    Oops, I forgot to give the error it sends.

    Code:
    [13:26:14 WARN]: java.lang.NullPointerException
    [13:26:14 WARN]:        at me.themaniacgamers.HalfAHeart.Main.listeners.PlayerDe
    ath.onPlayerDeath(PlayerDeath.java:64)
    [13:26:14 WARN]:        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native M
    ethod)
    [13:26:14 WARN]:        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown S
    ource)
    [13:26:14 WARN]:        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unkno
    wn Source)
    [13:26:14 WARN]:        at java.lang.reflect.Method.invoke(Unknown Source)
    [13:26:14 WARN]:        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(Jav
    aPluginLoader.java:306)
    [13:26:14 WARN]:        at org.bukkit.plugin.RegisteredListener.callEvent(Regist
    eredListener.java:62)
    [13:26:14 WARN]:        at org.bukkit.plugin.SimplePluginManager.fireEvent(Simpl
    ePluginManager.java:502)
    [13:26:14 WARN]:        at org.bukkit.plugin.SimplePluginManager.callEvent(Simpl
    ePluginManager.java:487)
    [13:26:14 WARN]:        at org.bukkit.craftbukkit.v1_9_R1.event.CraftEventFactor
    y.callPlayerDeathEvent(CraftEventFactory.java:381)
    [13:26:14 WARN]:        at net.minecraft.server.v1_9_R1.EntityPlayer.die(EntityP
    layer.java:422)
    [13:26:14 WARN]:        at net.minecraft.server.v1_9_R1.EntityLiving.damageEntit
    y(EntityLiving.java:890)
    [13:26:14 WARN]:        at net.minecraft.server.v1_9_R1.EntityHuman.damageEntity
    (EntityHuman.java:760)
    [13:26:14 WARN]:        at net.minecraft.server.v1_9_R1.EntityPlayer.damageEntit
    y(EntityPlayer.java:513)
    [13:26:14 WARN]:        at net.minecraft.server.v1_9_R1.EntityHuman.attack(Entit
    yHuman.java:997)
    [13:26:14 WARN]:        at net.minecraft.server.v1_9_R1.EntityPlayer.attack(Enti
    tyPlayer.java:1141)
    [13:26:14 WARN]:        at net.minecraft.server.v1_9_R1.PlayerConnection.a(Playe
    rConnection.java:1529)
    [13:26:14 WARN]:        at net.minecraft.server.v1_9_R1.PacketPlayInUseEntity.a(
    SourceFile:68)
    [13:26:14 WARN]:        at net.minecraft.server.v1_9_R1.PacketPlayInUseEntity.a(
    SourceFile:12)
    [13:26:14 WARN]:        at net.minecraft.server.v1_9_R1.PlayerConnectionUtils$1.
    run(SourceFile:13)
    [13:26:14 WARN]:        at java.util.concurrent.Executors$RunnableAdapter.call(U
    nknown Source)
    [13:26:14 WARN]:        at java.util.concurrent.FutureTask.run(Unknown Source)
    [13:26:14 WARN]:        at net.minecraft.server.v1_9_R1.SystemUtils.a(SourceFile
    :45)
    [13:26:14 WARN]:        at net.minecraft.server.v1_9_R1.MinecraftServer.D(Minecr
    aftServer.java:716)
    [13:26:14 WARN]:        at net.minecraft.server.v1_9_R1.DedicatedServer.D(Dedica
    tedServer.java:400)
    [13:26:14 WARN]:        at net.minecraft.server.v1_9_R1.MinecraftServer.C(Minecr
    aftServer.java:655)
    [13:26:14 WARN]:        at net.minecraft.server.v1_9_R1.MinecraftServer.run(Mine
    craftServer.java:554)
    [13:26:14 WARN]:        at java.lang.Thread.run(Unknown Source)
    >
    [13:26:14 WARN]: at me.themaniacgamers.HalfAHeart.Main.listeners.PlayerDe
    ath.onPlayerDeath(PlayerDeath.java:64)

    on this line is the Kills.addKills(1);

    This is the PlayerData's entire class:

    Code:
    package me.themaniacgamers.HalfAHeart.Main.utils;
    
    import com.sk89q.minecraft.util.commands.ChatColor;
    import me.themaniacgamers.HalfAHeart.Main.Main;
    import me.themaniacgamers.HalfAHeart.Main.managers.ConfigsManager;
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    
    import java.io.File;
    import java.util.HashMap;
    import java.util.UUID;
    
    /**
    * Created by Corey on 4/3/2016.
    */
    public class PlayerData {
    
        Main plugin;
    
        public static HashMap<UUID, PlayerData> loadedPlayer = new HashMap<UUID, PlayerData>();
        public static HashMap<UUID, PlayerData> pKills = new HashMap<UUID, PlayerData>();
        public static HashMap<UUID, PlayerData> pDeaths = new HashMap<UUID, PlayerData>();
        public static HashMap<UUID, PlayerData> pBounty = new HashMap<UUID, PlayerData>();
    
        private int bounty;
        private int kills;
        private int deaths;
        //blah
    
        public ConfigsManager configs = ConfigsManager.getInstance();
    
        public PlayerData(UUID id) {
            //read all stats from file
            //bounty = whateverfromfile
            //configs manager to get from UUID.yml
            if (configs.getPlayersFile().getConfigurationSection("Stats").get("Kills") == null) {
                kills = configs.getPlayersFile().getConfigurationSection("Stats").getInt("Kills");
            } else {
                configs.getPlayersFile().set("Stats.Kills", 0);
            }
            if (configs.getPlayersFile().getConfigurationSection("Stats").get("Deaths") == null) {
                deaths = configs.getPlayersFile().getConfigurationSection("Stats").getInt("Deaths");
            } else {
                configs.getPlayersFile().set("Stats.Deaths", 0);
            }
            if (configs.getPlayersFile().getConfigurationSection("Stats").get("Bounty") == null) {
                bounty = configs.getPlayersFile().getConfigurationSection("Stats").getInt("Bounty");
            } else {
                configs.getPlayersFile().set("Stats.Bounty", 0);
            }
        }
    
        public int getBounty() {
            return bounty;
        }
    
        public void setBounty(int bounty) {
            this.bounty = bounty;
        }
    
        public int getKills() {
            return kills;
        }
    
        public int getDeaths() {
            return deaths;
        }
    
        public void setKills(int kills) {
            this.kills = kills;
        }
    
        public void addBounty(int amount) {
            bounty = bounty + amount;
        }
    
        public void addKills(int amount) {
            kills = kills + amount;
        }
    
        public void addDeaths(int amount) {
            deaths = deaths + amount;
        }
    
        public void saveData() {
            //stuff access configs Manager
            //export all current player stats to file through configs manager
            Player p = (Player) Bukkit.getOnlinePlayers();
            Player player = p.getServer().getPlayer(p.getName());
            UUID playerUUID = player.getUniqueId();
            File dataBase = new File(plugin.getDataFolder(), File.separator + "PlayerDatabase");
            File pFile = new File(dataBase, File.separator + playerUUID + ".yml");
            String pName = (ChatColor.BLUE + "" + ChatColor.BOLD + p.getName() + ChatColor.AQUA);
            Player pl = p.getPlayer();
            configs.getPlayersFile().getConfigurationSection("Stats").set("Kills", pKills);
            configs.getPlayersFile().getConfigurationSection("Stats").set("Deaths", pDeaths);
            configs.getPlayersFile().getConfigurationSection("Stats").set("Bounty", pBounty);
            configs.saveOnPlayersToConfig();
        }
    
    }
    

    EDIT:
    I made the HashMap just public, not public static.
     
  4. Offline

    mine-care

    Have you checked if the map contains the UUID's before accessing it?
    Also don't forget to tahg me :)
    Also in your "save data" code you cast a collection or array of players to a single player which obviously isn't going to work with
     
  5. Offline

    TheManiacGamers

    @mine-care

    Yeah, I know that, I don't know how to narrow it down to a single player instead, if you could help me on that, that'd be great!

    How do I check if the map contains the UUID? and if not adding the player?

    Code:
    if (PlayerData.pKills.containsKey(p.getUniqueId())) {
                    PlayerData Kills = PlayerData.pKills.get(k.getUniqueId());
                    try {
                        Kills.addKills(1);
                    } catch (Exception ex) {
                        ex.printStackTrace();
                        p.sendMessage(ChatColor.RED + "Kills Problem");
                        return;
                    }
                } else {
                    // what goes here?
                }
    Thanks :)

    Edit:
    How do I also ADD health to a player, not SET,
    Like,
    When the killer kills someone, they get an extra half heart, if they die it gets reset.

    I tried going killer.setMaxHealth(+0.5D); but it didn't work.
    then I tried going killer.setMaxHealth(k.getMaxHealth + 0.5D); and I don't think that worked, either.
     
    Last edited: Apr 5, 2016
  6. Offline

    mcdorli

    SetMaxHealt sets the MAX amount of health, not the cureent health.

    Please don't abuse static.

    Read this https://docs.oracle.com/javase/tutorial/ and don't come back before you're finished with it.
     
  7. Offline

    TheManiacGamers

    The aim of the "minigame" is to have half a heart (hence the name, if you noticed it in the package name).
    I'm attempting to create a donator perk, where, the more they kill (in a row) the more hearts they get, when they die, it goes back to normal

    Wouldn't k.setHealth(); set it to the health, that can regenerate?
    in which, I don't want happening.
     
  8. Offline

    mine-care

    @TheManiacGamers Take a look in Map docs, and you will find the solution :p Have you gone over the basics of java? They are required if you want to start with bukkit. |
    Also, i wouldn't recomend you start by a minigame straight away since it touches many many parts of Java and furthermore Bukkit that you need to have played before with.
    About your code above, i cant see where or why an exception is thrown, so i dont see the point of catching them all (Exception -> superclass of all). Thinks logically:
    if the map contains the player -> Do that
    Otherwise, -> Do that.
     
  9. Offline

    TheManiacGamers

    @mine-care

    Yes, I have learnt the basics of java, and this isn't a "start" thing,
    I've been doing bukkit for about a few months now, and this is something i never touched base on,
    I just need to get this part done, and it's pretty much complete.

    Would you be able to supply me a link to the Map docs?
    Thanks :)
     
  10. Offline

    mcdorli

    Collections are the part of basic java, the link I sended had a sub section for collections.
     
  11. Offline

    mine-care

Thread Status:
Not open for further replies.

Share This Page