Saving 3 different Infos (player, player, float) - Not a HashMap but how?

Discussion in 'Plugin Development' started by anonym110, Sep 17, 2012.

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

    anonym110

    Hey,

    for a plugin I am making I am trying to record the damage a player made and then when the target dies it should print the percentage of damage he did.

    So I would need to record the Attacker, Target and the Damage and then once the target dies it would check the total damage he recieved in a certain period and the amount of damage the attacker did to him. Basically then calculate (AttackerDamage/TotalDamage)*100 = Percentage of Damage

    Now I am having the problem saving those 3 infos as I only know how to use hashmaps with 2 items how would I go on about saving all this information?
     
  2. Offline

    skore87

    Often people use a custom class to hold multiple pieces of data. I would suggest doing that as it allows you to expand on it at a later time and you completely control how you access it.

    I would set it up so the HashMap contains the player being attacked as the key with the value being a custom class that contains a list of attack data class objects.

    So... HashMap<String,IncommingAttackData> where IncommingAttackData would contain an ArrayList<AttackData> and methods to calculate percentages and other things. AttackData would simply hold two things, the player attacking and the damage caused.
     
  3. Offline

    anonym110

    Hmm thank you :) Though I saw that before in some other thread and I dont understand it at all... Mind helping me out a bit?
     
  4. Offline

    skore87

    Sorry for the late reply, didn't see you wrote and I left for class. Here is a simple example of those two classes. I wrote them up in notepad so there is the possibility of some syntax errors.

    Code:
    public class IncommingAttackData{
        private ArrayList<AttackData> attackData;
        private String playerName;
        private int totalDamage;
       
        public IncommingAttackData(Player player){
            this.playerName = player.getName();
            this.attackData = new ArrayList<AttackData>();
        }
       
        public IncommingAttackData(AttackData attack){
            this(Bukkit.getPlayerExact(attack.getAttackerName()));
            this.attackData.add(attack);
        }
       
        public int getDamageDone(String playerName){
            int damage = 0;
            for (AttackData attack : attackData)
                if (attack.getDefenderName.equals(playerName)) damage += attack.getDamage();
            return damage;
        }
       
        public int getTotalDamage(){
            if (totalDamage == null) return totalDamage;
            else{
                totalDamage = 0;
                for (AttackData attack: attackData)
                    totalDamage += attack.getDamage();
                return totalDamage;
            }
        }
     
        public Double getDamagePercent(String playerName){
            return getDamageDone(playerName) / getTotalDamage();
        }
    }
     
    public class AttackData{
        private final String attacker;
        private final String defender;
        private final int damage;
     
        public AttackData(EntityDamageByEntityEvent e){
            if (e.getEntity() instanceof Player &&
                e.getDamager() instanceof Player){
                this.attacker = ((Player) e.getEntity()).getName();
                this.defender = ((Player) e.getDamager()).getName();
                this.damage = e.getDamage();
                this.cause = e.getCause();
            }else throw new Exception("Entity is not a player!");
        }
       
        public int getDamage(){
            return damage;
        }
       
        public String getAttackerName(){
            return attacker;
        }
       
        public String getDefenderName(){
            return defender;
        }
    }
     
    anonym110 likes this.
  5. Offline

    anonym110

    Thank you so much :)
     
  6. Offline

    skore87

    I goofed... at getTotalDamage() in IncommingAttackData, change "totalDamage == null" to "totalDamage != null".
     
Thread Status:
Not open for further replies.

Share This Page