Inconsistent Player UUIDs

Discussion in 'Plugin Development' started by Napkin222, May 17, 2014.

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

    Napkin222

    I am storing player UUIDs in a database when they connect for the first time. However, if they then run a command that finds them via UUID it is unable to find them. Any help?
     
  2. Offline

    Bammerbom

    Napkin222 If you are using offline mode they will be inconsistent.
    Switch to online mode.
     
  3. Offline

    FerusGrim

    When you say 'Database', do you mean MySQL?

    MySQL is finicky when it comes to a query which stores values. If you do not surround the UUID with ''s in the query, for example, it will not store correctly. This was an odd issue I found by chance, while working on a SQL heavy plugin.

    If you could, paste your code which connects to the database, as well as the query you're using.
     
  4. Offline

    Napkin222

    Jhtzb The server is in online mode.

    FerusGrim
    Code:java
    1. //To add a player:
    2. statement.execute("REPLACE INTO squibblygoo SET uuid='" + UUIDFetcher.getUUIDOf(e.getPlayer().getName()).toString() + "', username='" + e.getPlayer().getName() + "'");
    3.  
    4. //To find a player:
    5. ResultSet set = statement.executeQuery("SELECT * FROM squibblygoo WHERE uuid='" + UUIDFetcher.getUUIDOf(p.getName()).toString() + "'");


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  5. Offline

    FerusGrim

    Napkin222

    I would recommend, for the sake of human-readability, that you stop using statement(), and switch to using preparedStatement().

    Code:java
    1. PreparedStatement ps = conn.prepareStatement("REPLACE INTO sqibblygoo SET uuid=?, username=?");
    2. ps.setString(1, UUIDFetcher.getUUIDOf(e.getPlayer().getName()).toString();
    3. ps.setString(2, e.getPlayer().getName());


    prepareStatement will automatically prepend and append ''s to your query, so do NOT surround the ?s with them.
     
  6. Offline

    Azubuso

    Napkin222 One thing I recommend doing though is saving the UUID as a string without "-"s, saves a bit of space and all you need to do is add back the "-"s when fetching it from the database.

    Code sniplet:
    PHP:
    public String addBackDashes(String uuid) {
        return 
    uuid.replaceAll("(\w{8})(\w{4})(\w{4})(\w{4})(\w{12})""$1-$2-$3-$4-$5");
    }
     
  7. Offline

    Napkin222

    Thank you, I've replaced them all with PreparedStatements, will test now.
     
  8. Offline

    Napkin222

    The server is in online-mode, but these are the UUIDs from the same player:
    Code:
       
     
        5d02033c-3c66-3de2-bf68-5f13fa0f9e95    => UUID From Server
        3e9b7245-0741-4904-a7db-b1fee6115e51    => UUID In Database
     
    
    Anyone? This will break all my plugins

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  9. Offline

    Azubuso

  10. Offline

    Napkin222

    That's what I'm using.

    Azubuso

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  11. Offline

    xize

    also make sure your case is correct as how the case is with the orginal player.

    if you are getting the UUID's from a fetcher which connects to the site of mojang uppercase and lowercase has a nasty side effect because it returns a other UUID rather than it should return the same, I don't know if it suposed to work like that since ive seen this close when they launched the Mojang accounts client.
     
  12. Offline

    Azubuso

    xize Actually the request is case insensitive. (Click)

    Napkin222 That's really strange...hmm
     
  13. Offline

    Napkin222

    Here is my entire class if that will help:

    Code:java
    1. package me.llamositopia.kunal.squibblygoo;
    2.  
    3. import com.evilmidget38.UUIDFetcher;
    4. import com.vexsoftware.votifier.model.Vote;
    5. import com.vexsoftware.votifier.model.VotifierEvent;
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.ChatColor;
    8. import org.bukkit.Material;
    9. import org.bukkit.command.Command;
    10. import org.bukkit.command.CommandSender;
    11. import org.bukkit.entity.Player;
    12. import org.bukkit.event.EventHandler;
    13. import org.bukkit.event.Listener;
    14. import org.bukkit.event.inventory.InventoryInteractEvent;
    15. import org.bukkit.event.player.PlayerJoinEvent;
    16. import org.bukkit.inventory.Inventory;
    17. import org.bukkit.inventory.ItemStack;
    18. import org.bukkit.inventory.meta.ItemMeta;
    19. import org.bukkit.plugin.java.JavaPlugin;
    20.  
    21. import java.sql.*;
    22. import java.util.ArrayList;
    23. import java.util.Calendar;
    24. import java.util.logging.Logger;
    25.  
    26. public class Main extends JavaPlugin implements Listener, Runnable{
    27.  
    28. Logger log;
    29. private Connection connection;
    30. private Statement statement;
    31.  
    32. ArrayList<String> thisMonth_l = new ArrayList<>(), lastMonth_l = new ArrayList<>();
    33.  
    34. public void onEnable() {
    35. log = this.getLogger();
    36. saveDefaultConfig();
    37. {
    38. log.info("Connecting to MySQL database....");
    39. String ip = getConfig().getString("connection.ip", "localhost");
    40. String username = getConfig().getString("connection.username", "squibblygoo");
    41. String password = getConfig().getString("connection.password", "12345678");
    42. String database = getConfig().getString("connection.database", "squibblygoo");
    43. int port = getConfig().getInt("connection.port", 3306);
    44. String connectionFull = "jdbc:mysql://" + ip + ":" + port + "/" + database;
    45. log.info("Using connection protocol: " + connectionFull);
    46. try {
    47. connection = DriverManager.getConnection(connectionFull, username, password);
    48. log.info("Connected to database. Creating statement....");
    49. statement = connection.createStatement();
    50. log.info("Statement created. Creating friends table if not already existing....");
    51. statement.execute("CREATE TABLE IF NOT EXISTS squibblygoo (uuid VARCHAR(50) NOT NULL PRIMARY KEY, username VARCHAR(16) NOT NULL, votes INT NOT NULL DEFAULT 0)");
    52. statement.execute("CREATE TABLE IF NOT EXISTS squibblygoo_old SELECT uuid, username, votes FROM squibblygoo");
    53. } catch (SQLException e) {
    54. log.info("Could not set up interactions with database.... Disabling....");
    55. Bukkit.getPluginManager().disablePlugin(this);
    56. return;
    57. }
    58. }
    59. log.info("Tables created. Registering listeners and timers....");
    60. Bukkit.getPluginManager().registerEvents(this, this);
    61. Bukkit.getScheduler().scheduleSyncRepeatingTask(this, this, 200, 200);
    62. log.info("Registered. Creating inventory items....");
    63. log.info("SquibblyGoo enabled.");
    64. }
    65.  
    66. public void onDisable() {
    67. if (connection!=null){
    68. try {
    69. connection.close();
    70. } catch (SQLException e) {
    71. e.printStackTrace();
    72. }
    73. }
    74. log.info("SquibblyGoo has been disabled.");
    75. }
    76.  
    77. public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    78. System.out.println("Executed command: " + command.getName());
    79. if (command.getName().equalsIgnoreCase("vote")){
    80. System.out.println("Executed command vote: sender instanceof Player: " + (sender instanceof Player));
    81. if (!(sender instanceof Player)){
    82. sender.sendMessage(getConfig().getString("vote.link"));
    83. return true;
    84. }
    85. System.out.println("Executing showPlayerStats((Player) sender);");
    86. showPlayerStats((Player) sender);
    87. return true;
    88. }
    89. return false;
    90. }
    91.  
    92. public void run() {
    93. if (getConfig().getInt("automated.oldMonth")!=Calendar.getInstance().get(Calendar.MONTH)){
    94. try {
    95. statement.execute("DROP TABLE squibblygoo_old");
    96. statement.execute("CREATE TABLE IF NOT EXISTS squibblygoo_old SELECT uuid, username, votes FROM squibblygoo");
    97. statement.execute("DROP TABLE squibblygoo");
    98. statement.execute("CREATE TABLE IF NOT EXISTS squibblygoo (uuid VARCHAR(50) NOT NULL PRIMARY KEY, username VARCHAR(16) NOT NULL, votes INT NOT NULL DEFAULT 0)");
    99. for (Player a : Bukkit.getOnlinePlayers()){
    100. PreparedStatement ps = connection.prepareStatement("REPLACE INTO squibblygoo SET uuid=?, username=?");
    101. ps.setString(1, UUIDFetcher.getUUIDOf(a.getName()).toString());
    102. ps.setString(2, a.getName());
    103. ps.execute();
    104. }
    105. getConfig().set("automated.oldMonth", Calendar.getInstance().get(Calendar.MONTH));
    106. } catch (Exception e) {
    107. e.printStackTrace();
    108. }
    109. }
    110. }
    111.  
    112. @EventHandler
    113. public void onPlayerConnect(PlayerJoinEvent e){
    114. try {
    115. PreparedStatement ps = connection.prepareStatement("UPDATE squibblygoo SET username=? WHERE uuid=?");
    116. ps.setString(1, e.getPlayer().getName());
    117. ps.setString(2, UUIDFetcher.getUUIDOf(e.getPlayer().getName()).toString());
    118. ps.execute();
    119. PreparedStatement ps_old = connection.prepareStatement("UPDATE squibblygoo_old SET username=? WHERE uuid=?");
    120. ps_old.setString(1, e.getPlayer().getName());
    121. ps_old.setString(2, UUIDFetcher.getUUIDOf(e.getPlayer().getName()).toString());
    122. ps_old.execute();
    123. PreparedStatement ps_new = connection.prepareStatement("REPLACE INTO squibblygoo_old SET username=?, uuid=?");
    124. ps_new.setString(1, e.getPlayer().getName());
    125. ps_new.setString(2, UUIDFetcher.getUUIDOf(e.getPlayer().getName()).toString());
    126. ps_new.execute();
    127. } catch (Exception e1) {
    128. e1.printStackTrace();
    129. }
    130. }
    131.  
    132. @EventHandler
    133. public void onInventoryEvent(InventoryInteractEvent e){
    134. e.setCancelled(e.getInventory().getName().equals(ChatColor.DARK_AQUA + "Vote Menu"));
    135. }
    136.  
    137. @EventHandler
    138. public void onVote(VotifierEvent e){
    139. Vote vote = e.getVote();
    140. Player p = Bukkit.getPlayer(vote.getUsername());
    141. if (p==null){
    142. return;
    143. }
    144. try {
    145. PreparedStatement ps = connection.prepareStatement("SELECT * FROM squibblygoo WHERE uuid=?");
    146. ps.setString(1, UUIDFetcher.getUUIDOf(p.getName()).toString());
    147. ResultSet set = ps.executeQuery();
    148. int votes = set.getInt("votes");
    149. votes++;
    150. PreparedStatement ps2 = connection.prepareStatement("UPDATE squibblygoo SET votes=? WHERE uuid=?");
    151. ps2.setInt(1, votes);
    152. ps2.setString(2, UUIDFetcher.getUUIDOf(p.getName()).toString());
    153. ps2.execute();
    154. } catch (Exception e1) {
    155. e1.printStackTrace();
    156. }
    157. }
    158.  
    159. private void loadVoteStats() {
    160. try {
    161. thisMonth_l.clear();
    162. lastMonth_l.clear();
    163. ResultSet set = statement.executeQuery("SELECT * FROM squibblygoo ORDER BY votes DESC");
    164. for (int j = 0; j < 10; j++) {
    165. set.next();
    166. thisMonth_l.add(ChatColor.GOLD + set.getString("username") + ": " + ChatColor.YELLOW + set.getInt("votes"));
    167. }
    168. set = statement.executeQuery("SELECT * FROM squibblygoo_old ORDER BY votes DESC");
    169. for (int j = 0; j < 10; j++) {
    170. set.next();
    171. lastMonth_l.add(ChatColor.GOLD + set.getString("username") + ": " + ChatColor.YELLOW + set.getInt("votes"));
    172. }
    173. } catch (SQLException e) {
    174. e.printStackTrace();
    175. }
    176. }
    177.  
    178. private void showPlayerStats(Player p){
    179. try {
    180. System.out.println("Retrieving player data from database....");
    181. PreparedStatement ps = connection.prepareStatement("SELECT * FROM squibblygoo WHERE uuid=?");
    182. ps.setString(1, UUIDFetcher.getUUIDOf(p.getName()).toString());
    183. ResultSet set = ps.executeQuery();
    184. set.next();
    185. int votesThisMonth = set.getInt("votes");
    186. System.out.println("Retrieving old player data from database....");
    187. PreparedStatement ps_old = connection.prepareStatement("SELECT * FROM squibblygoo WHERE uuid=?");
    188. ps_old.setString(1, UUIDFetcher.getUUIDOf(p.getName()).toString());
    189. set = ps_old.executeQuery();
    190. set.next();
    191. int votesLastMonth = set.getInt("votes");
    192. ArrayList<String> personal_l = new ArrayList<>();
    193. personal_l.add(ChatColor.GRAY + "Your votes this month: " + ChatColor.DARK_AQUA + votesThisMonth);
    194. personal_l.add(ChatColor.GRAY + "Your votes last month: " + ChatColor.DARK_AQUA + votesLastMonth);
    195. System.out.println("Creating inventory....");
    196. Inventory i = Bukkit.createInventory(null, 9, "Vote Menu");
    197. System.out.println("Adding items....");
    198. ItemStack personal = new ItemStack(Material.EYE_OF_ENDER, 1);
    199. ItemStack thisMonth = new ItemStack(Material.GOLD_HELMET, 1);
    200. ItemStack lastMonth = new ItemStack(Material.DIAMOND_HELMET, 1);
    201. ItemStack link = new ItemStack(Material.EMERALD, 1);
    202. ItemMeta pe = personal.getItemMeta();
    203. ItemMeta tm = thisMonth.getItemMeta();
    204. ItemMeta lm = lastMonth.getItemMeta();
    205. ItemMeta li = link.getItemMeta();
    206. pe.setDisplayName(ChatColor.DARK_AQUA + "Your Votes");
    207. tm.setDisplayName(ChatColor.DARK_AQUA + "Top Voters - This Month");
    208. lm.setDisplayName(ChatColor.DARK_AQUA + "Top Voters - Last Month");
    209. li.setDisplayName(ChatColor.GREEN + "Click here to vote!");
    210. pe.setLore(personal_l);
    211. tm.setLore(thisMonth_l);
    212. lm.setLore(lastMonth_l);
    213. personal.setItemMeta(pe);
    214. thisMonth.setItemMeta(tm);
    215. lastMonth.setItemMeta(lm);
    216. link.setItemMeta(li);
    217. i.setItem(1, personal);
    218. i.setItem(3, thisMonth);
    219. i.setItem(5, lastMonth);
    220. i.setItem(7, link);
    221. System.out.println("Opening inventory for player....");
    222. p.openInventory(i);
    223. } catch (Exception e) {
    224. e.printStackTrace();
    225. }
    226. }
    227.  
    228. }
    229.  

    Azubuso
    FerusGrim
    xize
     
  14. Offline

    xize

    Azubuso
    Yea they probably changed that, I'm not up to date anymore since ive made my own system around it I only know it was a cause at the beginning I think when 1.7.2 was released shortly, but I never knew if case sensitivy was actually a bug or intended behaviour where it returned the wrong uuid based on the wrong case.

    Napkin222
    Do what FerusGrim said, he actually made a topic about it where he explains if you make a table with the name uuid it simply transforms every entry on that table to a UUID which means that you are double converting a uuid..., strange enough I can't believe it myself because I would think the table won't be stored like a type maybe only with the exception of a _ which means a auto increment atleast in sqlite, but else the type would/should be defined on the second collom like:

    table|type|can be null or is primary

    It goes against my whole theory but it seems its true if you read it here:
    http://forums.bukkit.org/threads/no...ingly-random-uuids-mysql.269562/#post-2500594
     
    Azubuso likes this.
Thread Status:
Not open for further replies.

Share This Page