MySQL Storage Error (Kills and Deaths)?

Discussion in 'Plugin Development' started by MrGG4ming, Apr 6, 2017.

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

    MrGG4ming

    Hello, I'm having a problem that I've never had and in which I find it very strange to be occurring .. I'm doing a kills and deaths count in mysql with an Integer, when the player dies it picks up the current amounts of its kills and adds + 1 that is if I had already died 10 times would add 1 = 11. But in this system I did in which I clearly defined to add +1 he is adding +2 .. If anyone knows what may be causing this help me, Thank you.

    Code:
    @EventHandler
        public void onDeath(PlayerDeathEvent event) {
            Player Dead = event.getEntity().getPlayer();
            Player Killer = event.getEntity().getKiller();
    
            event.setDeathMessage(null);
           
            Integer kills = KDRApi.getKills(Killer.getName());
            Integer Deaths = KDRApi.getDeaths(Dead.getName());
    
    //Here it defines (Current Quantity) + 1
            KDRApi.setKills(Killer.getName(), kills + 1);
            KDRApi.setDeaths(Dead.getName(), Deaths + 1);
            
    //Here he also sets +1 if he has a gang (Clan) ..
            if (GangueAPI.getGangue(Killer.getName()) != null) {
                GangueAPI.addKills(Killer.getName(), GangueAPI.getKills(Killer.getName()) + 1);
            }
            if (GangueAPI.getGangue(Dead.getName()) != null) {
                GangueAPI.addDeaths(Dead.getName(), GangueAPI.getDeaths(Dead.getName()) + 1);
            }
        }
    My API to list in MySql the information ..

    Code:
    public static Integer getDeaths(String UUID) {
                try {
                    PreparedStatement st = MySql.con.prepareStatement("SELECT * FROM KDR_data WHERE UUID = ?");
                    st.setString(1, UUID);
                    ResultSet rs = st.executeQuery();
                    while (rs.next()) {
                        return rs.getInt("Deaths");
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                return 0;
            }
        
         public static Integer getKills(String UUID) {
                try {
                    PreparedStatement st = MySql.con.prepareStatement("SELECT * FROM KDR_data WHERE UUID = ?");
                    st.setString(1, UUID);
                    ResultSet rs = st.executeQuery();
                    while (rs.next()) {
                        return rs.getInt("Kills");
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                return 0;
            }
        
         public static Integer getUUID(String UUID) {
                try {
                    PreparedStatement st = MySql.con.prepareStatement("SELECT * FROM KDR_data WHERE UUID = ?");
                    st.setString(1, UUID);
                    ResultSet rs = st.executeQuery();
                    while (rs.next()) {
                        return rs.getInt("UUID");
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                return null;
            }
        
        public static void setDeaths(String UUID, Integer Contagem) {
            try {
                PreparedStatement st = MySql.con.prepareStatement("UPDATE KDR_data SET Deaths = ? WHERE UUID = ?");
                st.setString(2, UUID);
                st.setInt(1, Contagem);
                st.executeUpdate();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
       
        public static void setKills(String UUID, Integer Contagem) {
            try {
                PreparedStatement st = MySql.con.prepareStatement("UPDATE KDR_data SET Kills = ? WHERE UUID = ?");
                st.setString(2, UUID);
                st.setInt(1, Contagem);
                st.executeUpdate();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
       
        public static void addKills(String UUID, Integer Kills) {
            setKills(UUID, Kills + getKills(UUID));
        }
       
        public static void addDeaths(String UUID, Integer Deaths) {
            setDeaths(UUID, Deaths + getDeaths(UUID));
        }
    }
     
  2. Offline

    martian3333

    @MrGG4ming
    Are Gang and player Kills/Deaths all stored in the KDR_data table? It looks to me like if the player is in a Gang it adds +1 kill for the player for themselves then adds +1 to the player instead of to the gang.
     
  3. Offline

    MrGG4ming

    @martian3333 I used two separate APIs one to set for one gang and one for his KDR ..
     
  4. Offline

    martian3333

    @MrGG4ming
    If both APIs use the same code shown above (I'm assuming they do since you only posted one), it appears that all the writes to the database are applied to the player's UUID and nothing to the Gang (unless the Gang has a UUID and I'm missing this somewhere). I apologize if I'm misinterpreting the code you provided.
     
  5. Offline

    MrGG4ming

    @martian3333 Not an API is different from a KDR_data list and another uses Gangue_data..

    It is defining in the KDR database and the gang, the only problem is that it is considered that "1" is "2" ..
     
  6. Offline

    martian3333

    My whole theory here was way off. Removed

    I was worried about having in your code:

    KDRApi.setKills(Killer.getName(), kills + 1);

    And then in your KDRApi:

    public static void addKills(String UUID, Integer Kills) {
    setKills(UUID, Kills + getKills(UUID));
    }


    but I can't see that addKills is ever called so I've got nothing.
     
    Last edited: Apr 6, 2017
  7. Offline

    N00BHUN73R

    @MrGG4ming
    Is the same thing happening with the gang mysql?
     
  8. Offline

    MrGG4ming

    @N00BHUN73R Yes, the same is happening with the gang. As I said I hedge both in different lists (KDRApi [KDR_data] / GangAPI [Gang_data]) Each one with his list, however he defined +2 instead of +1, being that I added +1.

    @martian3333 I created the setKills method for (KDR_data) and (Gang_data), but soon after I created an addKills like you saw ..
     
  9. Offline

    N00BHUN73R

    Hm that is very odd... The only ways I could see this being possible is if you accidently call the function twice, 2 listeners or something with the MySQL code.. I'll try and investigate.

    -- PixeLInc --
     
  10. Offline

    MrGG4ming

    @N00BHUN73R Exact .. Even make a test with a Double number and it is when 1 when I define +0.5 (By death)

    Every number I use doubles for some reason.

    I do not know if it might be that java considers who died and who killed, which are two different people and define in mysql 2 times for that reason ..
     
  11. Offline

    martian3333

    @MrGG4ming
    Would you mind posting your Gang API also?
     
  12. Offline

    MrGG4ming

    @martian3333 Its here:
    Code:
    package com.finalelite;
    
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    public class GangueAPI {
    
        public static Integer getDeaths(String UUID) {
            try {
                PreparedStatement st = MySql.con.prepareStatement("SELECT * FROM gangue_data WHERE Sigla = ?");
                st.setString(1, UUID);
                ResultSet rs = st.executeQuery();
                while (rs.next()) {
                    return rs.getInt("Deaths");
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return 0;
        }
      
        public static Integer getKills(String UUID) {
            try {
                PreparedStatement st = MySql.con.prepareStatement("SELECT * FROM gangue_data WHERE Sigla = ?");
                st.setString(1, UUID);
                ResultSet rs = st.executeQuery();
                while (rs.next()) {
                    return rs.getInt("Kills");
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return 0;
        }
      
        public static void setDeaths(String Sigla, Integer Contagem) {
            try {
                PreparedStatement st = MySql.con.prepareStatement("UPDATE gangue_data SET Deaths = ? WHERE Sigla = ?");
                st.setString(2, Sigla);
                st.setInt(1, Contagem);
                st.executeUpdate();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
      
        public static void setKills(String Sigla, Integer Contagem) {
            try {
                PreparedStatement st = MySql.con.prepareStatement("UPDATE gangue_data SET Kills = ? WHERE Sigla = ?");
                st.setString(2, Sigla);
                st.setInt(1, Contagem);
                st.executeUpdate();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    
         public static void addKills(String UUID, Integer Kills) {
            setKills(UUID, Kills + getKills(UUID));
        }
      
         public static void addDeaths(String UUID, Integer Deaths) {
            setDeaths(UUID, Deaths + getDeaths(UUID));
        }
    }
     
  13. Offline

    martian3333

    Code:
    Code
    
    
    ** Okay let's say the player is a member of a gang and currently has 3 kills. **
    ** Here you call the addKills function of the API and send it the value 4 **
    
    
    //Here he also sets +1 if he has a gang (Clan) ..
            if (GangueAPI.getGangue(Killer.getName()) != null) {
                GangueAPI.addKills(Killer.getName(), GangueAPI.getKills(Killer.getName()) + 1);
            }
    
    
    
    
    
    Gang API
    
    ** Here the API receives the player info and the number 4 **
    ** It then calls setKills and sends the player name and Kills which = 4 + **
    ** the value from getKills(UUID) **
    
    
         public static void addKills(String UUID, Integer Kills) {
            setKills(UUID, Kills + getKills(UUID));
        }
    
    
    
    ** Here getKills pulls the player's current kills count which for this scenario **
    ** was 3 kills and that value is added back at the setKills call **
    
        public static Integer getKills(String UUID) {
            try {
                PreparedStatement st = MySql.con.prepareStatement("SELECT * FROM gangue_data WHERE Sigla = ?");
                st.setString(1, UUID);
                ResultSet rs = st.executeQuery();
                while (rs.next()) {
                    return rs.getInt("Kills");
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return 0;
        }
    @MrGG4ming
     
  14. Offline

    MrGG4ming

    @martian3333 Exact.. It is doing exactly that, but when I kill it it adds 2 in place of 1 ..
     
  15. Offline

    martian3333

    @MrGG4ming
    With what I'm following (in the Gang API only) a player with 1 kill who gets another kill would then show 3 kills (an increase of 2)
    A player with 0 kills would have 1
    A player with 2 kills would end up with 5
    And a player with 3 kills would end up with 7
     
  16. Offline

    MrGG4ming

    @martian3333 In fact if it has 0 kills it appears as 0, if it has 1 kill it appears as 2 kills, if it has 2 kills it will appear as 4 kills, if it has 3 kills it will appear as 6 kills ...
     
  17. Offline

    N00BHUN73R

    Alright so it multiplies by 2.. that's even weirder..

    -- PixeLInc --
     
  18. Offline

    martian3333

    @MrGG4ming

    It looks like you should either get rid of ...


    public static void addKills(String UUID, Integer Kills) {
    setKills(UUID, Kills + getKills(UUID));
    }


    or...

    if (GangueAPI.getGangue(Killer.getName()) != null) {
    GangueAPI.addKills(Killer.getName(), GangueAPI.getKills(Killer.getName()) + 1);
    }
     
  19. Offline

    MrGG4ming

    @N00BHUN73R Yes, and still this happens with a double in the same way.

    @martian3333 I had withdrawn the one from the gang to see if it was he who made the mistake, but nothing changed. And before I used add I used the met set direct, but the same error occurred.

    I have to get the current amount of kills or deaths from the player otherwise it will always set 1 ..

    The first information had not killed anyone, already on the second was when I killed 1 player ..
    https://imgur.com/E7JrWKA
     
    Last edited: Apr 7, 2017
Thread Status:
Not open for further replies.

Share This Page