Creating a user(account) upon join via mysql problem

Discussion in 'Plugin Development' started by Eos, Jul 14, 2015.

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

    Eos

    I'm trying to create a user upon joining the server I first check if there uuid exists if not it creates there account for the server.
    The connection between the server and the database are successful i'm doing the accountcreation on the
    PlayerJoinEvent!

    Full Error:
    Code:
    [20:39:15 ERROR]: Could not pass event PlayerJoinEvent to CryptLibrary v1.0
    
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:310) ~[spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:502) [spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:487) [spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
            at net.minecraft.server.v1_8_R3.PlayerList.onPlayerJoin(PlayerList.java:
    296) [spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
            at net.minecraft.server.v1_8_R3.PlayerList.a(PlayerList.java:156) [spigo
    t-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
            at net.minecraft.server.v1_8_R3.LoginListener.b(LoginListener.java:144)
    [spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
            at net.minecraft.server.v1_8_R3.LoginListener.c(LoginListener.java:54) [
    spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
            at net.minecraft.server.v1_8_R3.NetworkManager.a(NetworkManager.java:231
    ) [spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
            at net.minecraft.server.v1_8_R3.ServerConnection.c(ServerConnection.java
    :148) [spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:8
    17) [spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:3
    67) [spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:6
    57) [spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java
    :560) [spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_45]
    Caused by: java.lang.ClassCastException: org.bukkit.event.player.PlayerJoinEvent
    cannot be cast to org.bukkit.entity.Player
            at crypted.cryptlibrary.Events.AccountCreation.AccountCreation(AccountCr
    eation.java:28) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0
    _45]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0
    _45]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .8.0_45]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_45]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:306) ~[spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
            ... 14 more
    Shorten Down
    Code:
    [20:39:15 ERROR]: Could not pass event PlayerJoinEvent to CryptLibrary v1.0
    org.bukkit.event.EventException
    

    AccountCreation.Java
    Code:
    public class AccountCreation implements Listener {
    
    
        MySQLManager MYSQL = new MySQLManager();
    
    
        @EventHandler
        public boolean AccountCreation(PlayerJoinEvent event){
    
    
            Player player = (Player)event;
    
            ResultSet query1 = MYSQL.query("SELECT * FROM players WHERE uuid = '" + player.getUniqueId() + "'" );
    
           /*ResultSet query2 = MYSQL.query("INSERT INTO players(`uuid`) VALUES ('" + player.getUniqueId() + "')");*/
    
            try {
                if (query1.getString("uuid") == null){
    
                    MYSQL.update("INSERT INTO players(`uuid`, `username`) VALUES ('" + player.getUniqueId() + player.getName() + "')");
                    Bukkit.getConsoleSender().sendMessage(ChatColor.YELLOW + "Created a new user.");
                }
    
            } catch(Exception error) {
    
                Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "Account Creation was unsuccessful" + error);
    
            }
            return false;
        }
    Core.Java
    Code:
        MySQLManager MYSQL = new MySQLManager();
    
        @Override
        public void onEnable(){
    
            //Commands
            
             //Events
            Bukkit.getServer().getPluginManager().registerEvents(new AccountCreation(), this);
            //Config
            configbot.loadConfig();
            //sql
            MYSQL.connect();
        }
    
    }[/code=java]
    [/B][/B]
     
  2. Offline

    Totom3

    @Eos You're looking at the wrong part of the stacktrace. Here's what's relevant:
    Code:
    Caused by: java.lang.ClassCastException: org.bukkit.event.player.PlayerJoinEvent cannot be cast to org.bukkit.entity.Player
            at crypted.cryptlibrary.Events.AccountCreation.AccountCreation(AccountCreation.java:28) ~[?:?]
    It is pointing to this line:
    Code:
    Player player = (Player)event;
    You are casting a PlayerJoinEvent to a Player. Use event.getPlayer() instead.
     
  3. Offline

    Eos

    Well that error got fixed, the main problem is that the PlayerJoinEvent cannot pass.
     
  4. Offline

    Totom3

    @Eos Cannot pass? What do you mean?
     
  5. Offline

    boysnnoco

    doing WHERE uuid = ' player.getUniqueId() ' is bad practice for SQL related things.
     
  6. Offline

    Zombie_Striker

    @Eos
    Code:
     @EventHandler
        public boolean AccountCreation(PlayerJoinEvent event){
            Player player = (Player)event;
            ResultSet query1 = MYSQL.query("SELECT * FROM players WHERE uuid = '" + player.getUniqueId() + "'" );
           /*ResultSet query2 = MYSQL.query("INSERT INTO players(`uuid`) VALUES ('" + player.getUniqueId() + "')");*/
            try {
                if (query1.getString("uuid") == null){
                    MYSQL.update("INSERT INTO players(`uuid`, `username`) VALUES ('" + player.getUniqueId() + player.getName() + "')");
                    Bukkit.getConsoleSender().sendMessage(ChatColor.YELLOW + "Created a new user.");
                }
            } catch(Exception error) {
                Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "Account Creation was unsuccessful" + error);
            }
            return false;
        }
    So lets see..
    1. You fixed the Player = PlayerJoinEvent error.
    2. I'm trusting the MYSQL class you are referencing is working, and that there is nothing wrong with that class.
    3. Why are you ending that statement with +"". That adds nothing.
    4. If query2 is not being used, why is it still there
    5. if (query1.getString("uuid") == null){ What is this line doing?
    6. And finally, Why is a method that should be a void a boolean?
     
  7. Offline

    Eos

    3. How else am I suppose to seperate the code?
    2. It certainly does work.
    5. I'm trying to check if the player does not exist in the mysql it creates a new account for them.
    6. That was met to be void not boolean. I didn't realize it was still boolean.
     
  8. Offline

    guitargun

    @Eos your mysql.update still has a syntax error in it. you mis the seperation between UUID and playernam. now you send it as UUIDplayername.
     
  9. Offline

    Eos

    Thanks for pointing that out I just fixed that syntax problem
    Code:
    MYSQL.update("INSERT INTO players(uuid, username) VALUES ('" + event.getPlayer().getUniqueId() + "', '" + event.getPlayer().getName() + "');");
     
Thread Status:
Not open for further replies.

Share This Page