Solved Annoying MySQL bug.

Discussion in 'Plugin Development' started by WRLD, Nov 30, 2015.

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

    WRLD

    Hey there, I've just joined the forums in need of some help. Here is my issue: I have a class called SQLS.java and I have a MySQL database to fetch all the ranks, kills, etc... in another class I am trying to fetch this data and display a chat prefix. This doesn't want to work.

    SQLS.java class:
    Show Spoiler

    Code:
    package com.wrld.chamber.sql;
    
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    import com.wong.chamber.Chamber;
    
    public class SQLS {
       
       public static boolean playerExists(String uuid) {
         try {
           ResultSet res = Chamber.c.createStatement().executeQuery("SELECT * FROM users WHERE uuid='" + uuid + "';");
           if (!res.next()) {
             return res.getString("uuid") != null;
           }
           return false;
         } catch (SQLException sql) {
           sql.printStackTrace();
         }
         return false;
       }
       
       public static Integer getScore(String uuid) {
         Integer i = 0;
         if (playerExists(uuid)) {
           try {
             ResultSet res = Chamber.c.createStatement().executeQuery("SELECT * FROM users WHERE uuid='" + uuid + "';");
             if ((!res.next()) || (Integer.valueOf(res.getInt("score")) == null));
             
             i = res.getInt("score");
           } catch (SQLException sql) {
             sql.printStackTrace();
           }
         }
         
         return i;
       }
       
       public static Integer getKills(String uuid) {
         Integer i = 0;
         if (playerExists(uuid)) {
           try {
             ResultSet res = Chamber.c.createStatement().executeQuery("SELECT * FROM users WHERE uuid='" + uuid + "';");
             if ((!res.next()) || (Integer.valueOf(res.getInt("kills")) == null));
             
             i = res.getInt("kills");
           } catch (SQLException sql) {
             sql.printStackTrace();
           }
         }
         
         return i;
       }
       
       public static Integer getDeaths(String uuid) {
         Integer i = 0;
         if (playerExists(uuid)) {
           try {
             ResultSet res = Chamber.c.createStatement().executeQuery("SELECT * FROM users WHERE uuid='" + uuid + "';");
             if ((!res.next()) || (Integer.valueOf(res.getInt("deaths")) == null));
             
             i = res.getInt("deaths");
           } catch (SQLException sql) {
             sql.printStackTrace();
           }
         }
         
         return i;
       }
       
       public static Integer getWins(String uuid) {
         Integer i = 0;
         if (playerExists(uuid)) {
           try {
             ResultSet res = Chamber.c.createStatement().executeQuery("SELECT * FROM users WHERE uuid='" + uuid + "';");
             if ((!res.next()) || (Integer.valueOf(res.getInt("wins")) == null));
             
             i = res.getInt("wins");
           } catch (SQLException sql) {
             sql.printStackTrace();
           }
         }
         
         return i;
       }
       
       public static String getRank(String uuid) {
         String r = null;
         
         try {
           ResultSet res = Chamber.c.createStatement().executeQuery("SELECT * FROM users WHERE uuid='" + uuid + "';");
           if ((!res.next()) || (String.valueOf(res.getString("rank")) == null));
         
           r = res.getString("rank");
         } catch (SQLException sql) {
           sql.printStackTrace();
         }
         
         return r.toString();
       }
       
    }
    


    and.. the ChatManager.java class:
    Show Spoiler

    Code:
    package com.wrld.chamber.utils;
    
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    
    import com.wrld.chamber.Chamber;
    import com.wrld.chamber.listeners.ChamberListener;
    import com.wrld.chamber.sql.SQLS;
    
    import net.md_5.bungee.api.ChatColor;
    
    public class ChatManager extends ChamberListener {
       
       public ChatManager(Chamber pl) {
         super(pl);
       }
    
       @EventHandler
       public void onPlayerChat(AsyncPlayerChatEvent e) {
         Player p = e.getPlayer();
         String format = null;
         
         if (!p.isOp()) {
           if (SQLS.getRank(p.getUniqueId().toString()) == "rank.user") {
             format = ChatColor.GRAY + "%s: %s";
           } else if (SQLS.getRank(p.getUniqueId().toString()) == "rank.vip") {
             format = ChatColor.GREEN + "[VIP] %s" + ChatColor.WHITE + ": %s";
           } else if (SQLS.getRank(p.getUniqueId().toString()) == "rank.yt") {
             format = ChatColor.GOLD + "[YOUTUBE] %s" + ChatColor.WHITE + ": %s";
           } else if (SQLS.getRank(p.getUniqueId().toString()) == "rank.helper") {
             format = ChatColor.BLUE + "[HELPER] %s" + ChatColor.WHITE + ": %s";
           } else if (SQLS.getRank(p.getUniqueId().toString()) == "rank.mod") {
             format = ChatColor.DARK_GREEN + "[MOD] %s" + ChatColor.WHITE + ": %s";
           } else if (SQLS.getRank(p.getUniqueId().toString()) == "rank.admin") {
             format = ChatColor.RED + "[ADMIN] %s" + ChatColor.WHITE + ": %s";
           } else {
             format = ChatColor.GRAY + "%s: %s";
           }
         } else {
           format = ChatColor.RED + "[ADMIN] %s" + ChatColor.WHITE + ": %s";
         }
         
         e.setFormat(format);
       }
       
    }
    
    

    I've been at this for over 2 hours now. I may have just been stupid somewhere, but I hope I can find a fix.
     
  2. Offline

    Scimiguy

    We aren't going to spend ages pawing through your code.
    What debugging have you tried?
    Have you registered your event handler?
    Does your plugin even start?
    Is there any errors in console?
    Have you checked to make sure your SQL lines are correct?
     
  3. Offline

    WRLD

    Everything works, I used to do it with permissions. I have tested the function I wrote and it works just fine, but when I use it inside an if statement it doesn't want to output the prefix.

    getRank() method:

    Code:
    public static String getRank(String uuid) {
         String r = null;
         
         try {
           ResultSet res = Chamber.c.createStatement().executeQuery("SELECT * FROM users WHERE uuid='" + uuid + "';");
           if ((!res.next()) || (String.valueOf(res.getString("rank")) == null));
         
           r = res.getString("rank");
         } catch (SQLException sql) {
           sql.printStackTrace();
         }
         
         return r.toString();
       }
      
    and the AsyncPlayerChat event:

    Code:
    @EventHandler
       public void onPlayerChat(AsyncPlayerChatEvent e) {
         Player p = e.getPlayer();
         String format = null;
         
         if (!p.isOp()) {
           if (SQLS.getRank(p.getUniqueId().toString()) == "rank.user") {
             format = ChatColor.GRAY + "%s: %s";
           } else if (SQLS.getRank(p.getUniqueId().toString()) == "rank.vip") {
             format = ChatColor.GREEN + "[VIP] %s" + ChatColor.WHITE + ": %s";
           } else if (SQLS.getRank(p.getUniqueId().toString()) == "rank.yt") {
             format = ChatColor.GOLD + "[YOUTUBE] %s" + ChatColor.WHITE + ": %s";
           } else if (SQLS.getRank(p.getUniqueId().toString()) == "rank.helper") {
             format = ChatColor.BLUE + "[HELPER] %s" + ChatColor.WHITE + ": %s";
           } else if (SQLS.getRank(p.getUniqueId().toString()) == "rank.mod") {
             format = ChatColor.DARK_GREEN + "[MOD] %s" + ChatColor.WHITE + ": %s";
           } else if (SQLS.getRank(p.getUniqueId().toString()) == "rank.admin") {
             format = ChatColor.RED + "[ADMIN] %s" + ChatColor.WHITE + ": %s";
           } else {
             format = ChatColor.GRAY + "%s: %s";
           }
         } else {
           format = ChatColor.RED + "[ADMIN] %s" + ChatColor.WHITE + ": %s";
         }
         
         e.setFormat(format);
       }
    
     
  4. Offline

    hexaan

    @WRLD Without any errors we can't help much with your problem.

    However looking at your ChatManager you are making a lot of calls to the database every time you want to check the rank of the same player. "SQLS.getRank(p.getUniqueId().toString())" is being called six times every time somebody chats.

    You might want to find a way to load this data in at some point and only unload it when the server shuts down.

    -- Edit

    Have you tried comparing with String.equals instead of ==?
     
    Last edited: Nov 30, 2015
    WRLD likes this.
  5. Offline

    WRLD

    I'll try that now, thanks. And regarding your other answer, I don't think that would work since I'd have to restart the server for everyone's ranks to update. Thanks for your help though.

    Wow, thanks that worked. For any other users looking at this thread for help for the same issue. What I did was:
    replace
    Code:
    if (SQLS.getRank(p.getUniqueId().toString()) == "rank.user") {
    with
    Code:
    if (SQLS.getRank(p.getUniqueId().toString()).equals("rank.user")) {
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Nov 30, 2015
  6. Offline

    Scimiguy

    Edit the title of this thread and mark it as solved
    @WRLD
     
Thread Status:
Not open for further replies.

Share This Page