Kill and deaths

Discussion in 'Plugin Development' started by Threehundredcc2, Jan 25, 2013.

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

    Threehundredcc2

    This is a question I was wondering for a while and can't seam to figure out.

    How can I make a simple kill and death counter. Such as if a player1 kills player2 another player it saves that player1 got +1 kill and player2 got +1 death, This would save in some sort of Database file.

    And this would stack up

    Also it would be great if someone could also tell me how they could view thier Kills and Deaths with like /kdr or some command
     
  2. Offline

    chasechocolate

    PlayerDeathEvent, and either save the player's deaths + 1/kills + 1 to a MySQL database (which if there is a lot of player's dying, then it could potentially cause lag) or a Yaml configuration.
     
  3. Offline

    RealDope

    I would suggest, saving it to a HashMap or some sort, then onDisable() push it all to MySQL.
     
  4. Offline

    Threehundredcc2

    @chasechocolate

    Sounds perfect

    How can I push this into a mySQL? Also, how can players check there own kills and deaths?
     
  5. Offline

    RealDope

    Well. Lets see. You do it similar to:
    Code:Java
    1.  
    2. public void whenPlayerSays("/Kills") {
    3. tellHim.hisKills();
    4. }
    5. public void whenPlayerSays("/Deaths") {
    6. tellHim.hisDeaths();
    7. }
    8. public void onDisable() {
    9. pushStats.toMySQL();
    10. }
    11.  

    Yep. That looks about what you're looking for. Now go out and learn basic Java!
     
  6. Offline

    JjPwN1

    This is giving me errors what do I do now?
     
  7. Offline

    RealDope

    Congratulations JjPwN1 you have earned my first ever "like" given to a post on Bukkit Forums hahahahahaha
     
  8. Offline

    JjPwN1

    Threehundredcc2
    A rough example of how you might go about doing this if you want to save this to Bukkit's built-in config API
    Code:
        @EventHandler
        public void killsAndDeaths(PlayerDeathEvent event){
            Player player = event.getEntity(); //The person who died
            if(player.getKiller() instanceof Player){ //Check if the entity who killed the player is a player
                Player killer = player.getKiller(); //The killer of the person
                this.reloadConfig();
                getConfig().set("players." + player.getName() + ".deaths", getConfig().getInt("players." + player.getName() + ".deaths") + 1); //Add a death to the player
                getConfig().set("players." + killer.getName() + ".kills", getConfig().getInt("players." + killer.getName() + ".kills") + 1); //Add a kill to the killer
                this.saveConfig();
            }
        }
     
  9. Offline

    ImDeJay

    here's what i did in my killstats plugin.

    All information is saved within a YAML configuration.

    inside the PlayerDeathEvent i grab each players kills and deaths. then increase them as needed.

    Code:java
    1. @EventHandler
    2. public void playerDeath(PlayerDeathEvent event){
    3.  
    4. Player player = event.getEntity();
    5. Player killer = player.getKiller();
    6.  
    7. if(player instanceof Player && killer instanceof Player){
    8.  
    9. int playerDeath = this.playersFile.getInt("Players." + player.getName() + ".deaths");
    10. int killerKills = this.playersFile.getInt("Players." + killer.getName() + ".kills");
    11. int playerStreak = this.playersFile.getInt("Players." + player.getName() + ".streak");
    12. int killerStreak = this.playersFile.getInt("Players." + killer.getName() + ".streak");
    13.  
    14. if(playerStreak > 0){
    15. this.playersFile.set("Players." + player.getName() + ".streak", 0);
    16. } //playerstreak > 0
    17.  
    18. killerStreak++;
    19. playerDeath++;
    20. killerKills++;
    21.  
    22. this.playersFile.set("Players." + killer.getName() + ".streak", killerStreak);
    23. this.playersFile.set("Players." + player.getName() + ".deaths", playerDeath);
    24. this.playersFile.set("Players." + killer.getName() + ".kills", killerKills);
    25.  
    26. this.savePlayerYML();
    27.  
    28. }
    29.  
    30.  
    31. }
     
  10. Offline

    Threehundredcc2


    I know basic java, I was wondering about MySQL and how to retrieve data on command.
     
  11. Offline

    Threehundredcc2

    But seriously, does anyone know how to get the kills and deaths and push them to MySQL, not config.
     
  12. Offline

    d33k40

  13. Offline

    JjPwN1

    You need a MySQL database. You can buy one for low prices at many places, just search "mysql databases" on Google and I bet you'll find some sellers. Although, if you have a website hosting, you can most likely go onto your website's control panel and create free, unlimited MySQL databases, and edit them via phpMyAdmin.

    I'm assuming you have/can do one of the above. Here's how I use MySQL in my plugin that tracks kills, deaths, and many other things:

    I added MySQL Connecter/J to my project's class-path. Then, in my project's main class, I created some variables, that will be used later.
    Code:
    Connection con;
    Statement statement;
    After doing this, in the onEnable method in my main class, I gave the variables some values.
    Code:
            try{
                Class.forName("com.mysql.jdbc.Driver");
                con = (Connection) DriverManager.getConnection("jdbc:mysql://pwncraftpvp.com/pwncraft_mctdm","pwncraft_jjpwn",passwordstring);
                statement = con.createStatement();
                System.out.println("CONNECTED SUCCESSFULLY!");
            }catch (SQLException e){
                e.printStackTrace();
            }
    Then, I made a new class, named "MySQL". Inside this new class, I added methods that could easily execute queries to the database.
    Code:
    import java.sql.ResultSet;
    import java.sql.SQLException;
     
    import com.pwncraftpvp.MCTDM.MCTDMMain;
     
    public class MySQL {
     
        public static YourMainClass plugin;
        public MySQL(YourMainClass instance){
            plugin = instance;
        }
     
        public int databaseIntSelect(String query, String column) throws SQLException{
            plugin.statement.executeQuery(query);
            ResultSet rs = plugin.statement.getResultSet();
            int intResult = 0;
            while(rs.next()){
                intResult = rs.getInt(column);
            }
            return intResult;
        }
        public String databaseStringSelect(String query, String column) throws SQLException{
            plugin.statement.executeQuery(query);
            ResultSet rs = plugin.statement.getResultSet();
            String stringResult = "";
            while(rs.next()){
                stringResult = rs.getString(column);
            }
            return stringResult;
        }
        public void databaseQuery(String query) throws SQLException{
            plugin.statement.execute(query);
        }
    }
    This code may be confusing, but it makes it much easier to execute queries to, for example, get the player's kills and deaths or set their kills and deaths as something.
    So, I then imported the new class into the main class.
    Code:
    import your.Package.Name.MySQL;
    public MySQL mysql = new MySQL(this);
    From there, you could start tracking kills and deaths. So, on a PlayerDeathEvent, you'd get the player, and run this query to increase their kills
    Code:
        @EventHandler
        public void deathEvent(PlayerDeathEvent event) throws SQLException, ClassNotFoundException{
            int kills = mysql.databaseIntSelect("SELECT kills FROM players WHERE username='" + event.getEntity().getName() + "'", "kills") + 1;
            mysql.databaseQuery("UPDATE players SET kills=" + kills + " WHERE username='" + event.getEntity().getName() + "'");
     
    maxmar628 and Threehundredcc2 like this.
  14. Offline

    Threehundredcc2

    Thank you so much you helped alot.
     
  15. Offline

    Doodledew


    I know it's old, but is this still usable? And can you use this plugin to make webstats? :3
     
  16. Offline

    JjPwN1

    This should still work, yes.

    Also, you can use MySQL to make webstats very easily. In PHP, there's built-in MySQL methods that will allow you to grab information from your MySQL tables and display them on a web page.
     
  17. Offline

    Doodledew


    Do you have a tutorial for that anywhere? I'm new to html and php and I'm trying to put up my website xD
     
Thread Status:
Not open for further replies.

Share This Page