Solved MySQL data efficiency?

Discussion in 'Plugin Development' started by WesJD, Nov 30, 2014.

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

    WesJD

    So right now I have a working MySQL system for a network. The problem is, a player with the highest rank(owner) has a chat delay of about 1.5 seconds due to the plugin having to retrieve the data, parse it, then check it on chat. I'm sure there's a way to make this more efficient. (By the way, this is my first attempt at using MySQL in a real interface.)

    Chat:

    Code:java
    1. @EventHandler
    2. public void chatHandler(AsyncPlayerChatEvent e) {
    3. Player p = e.getPlayer();
    4. if(Rank.getRank(p) == Rank.DEFAULT) {
    5. e.setFormat(ChatColor.GRAY + p.getDisplayName() + "" + ChatColor.DARK_GRAY + ": " + ChatColor.RESET + e.getMessage());
    6. } else if(Rank.getRank(p) == Rank.VIP) {
    7. e.setFormat(ChatColor.GOLD.toString() + ChatColor.BOLD + "VIP " + ChatColor.GRAY + p.getDisplayName() + "" + ChatColor.DARK_GRAY + ": " + ChatColor.RESET + e.getMessage() + "");
    8. } else if(Rank.getRank(p) == Rank.BUILDER) {
    9. e.setFormat(ChatColor.BLUE.toString() + ChatColor.BOLD + "BUILDER " + ChatColor.GRAY + p.getDisplayName() + "" + ChatColor.DARK_GRAY + ": " + ChatColor.RESET + e.getMessage() + "");
    10. } else if(Rank.getRank(p) == Rank.HELPER) {
    11. e.setFormat(ChatColor.DARK_GRAY.toString() + ChatColor.BOLD + "HELPER " + ChatColor.GRAY + p.getDisplayName() + "" + ChatColor.DARK_GRAY + ": " + ChatColor.RESET + e.getMessage());
    12. } else if(Rank.getRank(p) == Rank.MOD) {
    13. e.setFormat(ChatColor.DARK_GREEN.toString() + ChatColor.BOLD + "MOD " + ChatColor.GRAY + p.getDisplayName() + "" + ChatColor.DARK_GRAY + ": " + ChatColor.RESET + e.getMessage());
    14. } else if(Rank.getRank(p) == Rank.DEV) {
    15. e.setFormat(ChatColor.DARK_PURPLE.toString() + ChatColor.BOLD + "DEV " + ChatColor.GRAY + p.getDisplayName() + "" + ChatColor.DARK_GRAY + ": " + ChatColor.RESET + e.getMessage());
    16. } else if(Rank.getRank(p) == Rank.ADMIN){
    17. e.setFormat(ChatColor.DARK_RED.toString() + ChatColor.BOLD + "ADMIN " + ChatColor.GRAY + p.getDisplayName() + "" + ChatColor.DARK_GRAY + ": " + ChatColor.RESET + e.getMessage());
    18. } else if(Rank.getRank(p) == Rank.OWNER){
    19. e.setFormat(ChatColor.RED.toString() + ChatColor.BOLD + "OWNER " + ChatColor.GRAY + p.getDisplayName() + "" + ChatColor.DARK_GRAY + ": " + ChatColor.RESET + e.getMessage());
    20. }


    Getting the rank:

    Code:java
    1. public static Rank getRank(Player p) {
    2. ResultSet res = null;
    3. try {
    4. res = Main.statement.executeQuery("SELECT rank FROM test WHERE uuid='" + p.getUniqueId() + "';");
    5. res.next();
    6. } catch (SQLException e) {
    7. e.printStackTrace();
    8. }
    9. try {
    10. if(res.getString("rank").equals("DEFAULT")) {
    11. return DEFAULT;
    12. } else if(res.getString("rank").equals("VIP")) {
    13. return VIP;
    14. } else if(res.getString("rank").equals("BUILDER")) {
    15. return BUILDER;
    16. } else if(res.getString("rank").equals("HELPER")) {
    17. return HELPER;
    18. } else if(res.getString("rank").equals("MOD")) {
    19. return MOD;
    20. } else if(res.getString("rank").equals("DEV")) {
    21. return DEV;
    22. } else if(res.getString("rank").equals("ADMIN")) {
    23. return ADMIN;
    24. } else if(res.getString("rank").equals("OWNER")) {
    25. return OWNER;
    26. }
    27. } catch (SQLException e) {
    28. e.printStackTrace();
    29. }
    30. return null;
    31. }
     
  2. Offline

    aaomidi

    Why in Minecraft's sake aren't you caching this data?
     
    Rocoty and Skionz like this.
  3. Offline

    Skionz

    WesJD Load the data into some kind of Map.
     
    WesJD likes this.
  4. Offline

    PreFiXAUT

    *Not really relevant but it kinda bothers me*, Optimizing:
    Show Spoiler

    Code:java
    1. @EventHandler
    2. public void chatHandler(AsyncPlayerChatEvent e) {
    3. Player p = e.getPlayer();
    4. if(Rank.getRank(p) == Rank.DEFAULT) {
    5. e.setFormat(ChatColor.GRAY + p.getDisplayName() + "" + ChatColor.DARK_GRAY + ": " + ChatColor.RESET + e.getMessage());
    6. } else if(Rank.getRank(p) == Rank.VIP) {
    7. e.setFormat(ChatColor.GOLD.toString() + ChatColor.BOLD + "VIP " + ChatColor.GRAY + p.getDisplayName() + "" + ChatColor.DARK_GRAY + ": " + ChatColor.RESET + e.getMessage() + "");
    8. } else if(Rank.getRank(p) == Rank.BUILDER) {
    9. e.setFormat(ChatColor.BLUE.toString() + ChatColor.BOLD + "BUILDER " + ChatColor.GRAY + p.getDisplayName() + "" + ChatColor.DARK_GRAY + ": " + ChatColor.RESET + e.getMessage() + "");
    10. } else if(Rank.getRank(p) == Rank.HELPER) {
    11. e.setFormat(ChatColor.DARK_GRAY.toString() + ChatColor.BOLD + "HELPER " + ChatColor.GRAY + p.getDisplayName() + "" + ChatColor.DARK_GRAY + ": " + ChatColor.RESET + e.getMessage());
    12. } else if(Rank.getRank(p) == Rank.MOD) {
    13. e.setFormat(ChatColor.DARK_GREEN.toString() + ChatColor.BOLD + "MOD " + ChatColor.GRAY + p.getDisplayName() + "" + ChatColor.DARK_GRAY + ": " + ChatColor.RESET + e.getMessage());
    14. } else if(Rank.getRank(p) == Rank.DEV) {
    15. e.setFormat(ChatColor.DARK_PURPLE.toString() + ChatColor.BOLD + "DEV " + ChatColor.GRAY + p.getDisplayName() + "" + ChatColor.DARK_GRAY + ": " + ChatColor.RESET + e.getMessage());
    16. } else if(Rank.getRank(p) == Rank.ADMIN){
    17. e.setFormat(ChatColor.DARK_RED.toString() + ChatColor.BOLD + "ADMIN " + ChatColor.GRAY + p.getDisplayName() + "" + ChatColor.DARK_GRAY + ": " + ChatColor.RESET + e.getMessage());
    18. } else if(Rank.getRank(p) == Rank.OWNER){
    19. e.setFormat(ChatColor.RED.toString() + ChatColor.BOLD + "OWNER " + ChatColor.GRAY + p.getDisplayName() + "" + ChatColor.DARK_GRAY + ": " + ChatColor.RESET + e.getMessage());
    20. }
    to
    Code:java
    1. @EventHandler
    2. public void chatHandler(AsyncPlayerChatEvent e) {
    3. Player p = e.getPlayer();
    4. Rank r = Rank.getRank(p);
    5. if(r == Rank.DEFAULT) {
    6. e.setFormat(ChatColor.GRAY + p.getDisplayName() + "" + ChatColor.DARK_GRAY + ": " + ChatColor.RESET + e.getMessage());
    7. } else {
    8. e.setFormat(ChatColor.GOLD + ChatColor.BOLD + r.name().toUpperCase() + ChatColor.GRAY + p.getDisplayName() + ChatColor.DARK_GRAY + ": " + ChatColor.RESET + e.getMessage());
    9. }

    and (I know we shouldn't provide Code as complete thing, but I added a description to every Important part): [/spoiler]
    2nd:
    Show Spoiler
    Code:java
    1. public static Rank getRank(Player p) {
    2. Connection connection = null; // Get your Connection here.
    3. String r = "";
    4. try {
    5. PreparedStatement stmt = connection.prepareStatement("SELECT rank FROM test WHERE uuid=?;");
    6. // Use PreparedStatements because they are faster and more efficient than
    7. // when you're using Variables in your Request/Statement
    8. stmt.setString(1, p.getUniqueId().toString());
    9. // "Replaces" the first '?' in the Request with the UUID
    10. // This is made so it works better with Variables
    11. ResultSet res = stmt.executeQuery();
    12. if (!res.next()) { // Welp, there's no Data. Stop dis nao
    13. res.close();
    14. stmt.close();
    15. return null;
    16. }
    17. r = res.getString("rank");
    18. } catch (SQLException e) {
    19. e.printStackTrace();
    20. return null; // Stop here, you got a Error
    21. }
    22. try {
    23. return Rank.valueOf(r);
    24. } catch (Exception e) {
    25. e.printStackTrace();
    26. return null; // Couldn't parse it, because it's not a Rank.
    27. }
    28. }
     
    WesJD likes this.
  5. Offline

    fireblast709

    PreFiXAUT the first 'optimalisation' isn't actually correct, he has different ChatColors for different ranks (though, he could store that in the Rank enum, and make that work). Secondly, never catch Exception (unless some noob *cough* Essentials *cough* throws Exception and you have no other option) or when you just want to propagate the error in a wrapper
    Code:java
    1. try
    2. {
    3. // Some exceptional code
    4. }
    5. catch(Exception ex)
    6. {
    7. throw new SpecialExceptionWrapper(ex);
    8. }
     
  6. Offline

    PreFiXAUT

    Really? Oh sorry didn't see that :eek:

    Well catching Exception only, is nice IF you don't want/need to handle certain Exceptions differently. For example you have a Method which throws 5 Exceptions, it's nice to have different ones so you know what happend. But if you just want to stop the Command/(can't/don't want) to handle each Exception then catching only Exception is fine imo.

    But anyway, seems that this works since the Title sais Solved.
     
  7. Offline

    fireblast709

    PreFiXAUT quick side note though: it could hide exceptions which are harmful for the code (like a NullPointerException because you forgot to initialize a variable) ;3
     
Thread Status:
Not open for further replies.

Share This Page