1.14.4 Toplist

Discussion in 'Spigot Plugin Development' started by GodCipher, Jan 13, 2020.

  1. Hello, little question.
    How can I get the top 10 players or the position of a player in the leaderboard via MySQL? I have tried many different ways on the net but each one gave me a NullPointer or other errors.
     
  2. Ordne deine Kills aus deiner MySQL mit ORDER BY. Dann setze sie in eine HashMap

    Code (Java):
    static HashMap<Integer, String> rang = new HashMap<Integer, String>();
     
        public static void set() {
         
            ResultSet rs = MySQL.mysql.query("SELECT UUID FROM Stats ORDER BY Kills DESC LIMIT 10");
         
            int i = 0;
         
            try {
                while(rs.next()) {
                    i++;
                    rang.put(i, rs.getString("UUID"));
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
    }
    .
    .
    .
           

    Der hat das ganz gut erklärt:

     
  3. Das ist genau das was ich probiert habe.. Kommt nh NullPointerException.
     
  4. Joshb_

    Supporter

    This is how I got my top (Anything) in my pvpkits plugin


    Code (Java):
        public synchronized static HashMap<Integer, String> getTop(String KillsDeathsKDR, int page) {
            openConnection();
            String split = "#####";
            try {
                PreparedStatement sql = connection.prepareStatement(
                        "SELECT * FROM `" + tableName + "` ORDER BY " + KillsDeathsKDR + " DESC LIMIT 10 OFFSET ?");
                sql.setInt(1, page * 10);
                ResultSet rs = sql.executeQuery();

                HashMap<Integer, String> top = new HashMap<Integer, String>();

                int place = 1;

                int pages = page * 10 + 1;

                while (rs.next()) {
                    if (KillsDeathsKDR.equals("kdr")) {
                        top.put(place, rs.getString("username") + split + pages + split + rs.getDouble(KillsDeathsKDR));
                    } else {

                        top.put(place, rs.getString("username") + split + pages + split + rs.getInt(KillsDeathsKDR));
                    }
                    place++;
                    pages++;
                }

                sql.close();
                rs.close();
                return top;

            } catch (Exception ex) {
                ex.printStackTrace();
                return null;
            }
        }
     
  5. Joshb_

    Supporter

  6. Yep. Any list I get is null. Try yours tomorrow @Joshb_2, thank y'all for the answers. I'll show up tomorrow again if it doesn't work out.
     
  7. FrostedSnowman

    Resource Staff

    You should note that your query will block any operations on the main thread until your query is complete, in other words, the server will eventually freeze until it times out. You’ll need to run the query off the main thread and either manipulate the code through a callback or return a CompletableFuture for you to do work on.

    Also use try-with-resources to clean up your connections automatically or at least close them in a finally block, in case something goes wrong within the main operations block and they’re unable to be closed.