Solved Error - PlayerJoinEvent : org.bukkit.event.EventException

Discussion in 'Spigot Plugin Development' started by Simsimpicpic, Dec 4, 2019 at 2:44 PM.

Thread Status:
Not open for further replies.
  1. Hello...
    I have a problem with my listener...

    Code (Java):

    package fr.simon.francoranks;

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.util.Properties;

    import org.bukkit.plugin.java.JavaPlugin;

    public class FrancoRanks extends JavaPlugin {

        private SQLConnection SQL;
     
        static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
     
        @Override
        public void onEnable() {
            getServer().getPluginManager().registerEvents(new FrancoRanksListener(SQL), this);
            Connection dbConnection = null;
            try {
                String url = "jdbc:mysql://*:3306/*";
                Properties info = new Properties();
                info.put("user", "*"); info.put("password", "*");
                dbConnection = DriverManager.getConnection(url, info);
                if (dbConnection != null) {
                    System.out.println("Successfully connected to MySQL database test");
                }
            } catch (SQLException ex) {
                System.out.println("An error occurred while connecting MySQL databse");
                ex.printStackTrace();
            }
        }
    }

     

    Code (Java):

    package fr.simon.francoranks;

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;

    import org.bukkit.ChatColor;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;

    public class SQLConnection {

        private FrancoRanks main;
        public static FileConfiguration config;
        private Connection connection;
        private String urlbase, host, database, user, pass;
     
        public SQLConnection(String urlbase, String host, String database, String user, String pass) {
            this.urlbase = urlbase;
            this.host = host;
            this.database = database;
            this.user = user;
            this.pass = pass;
        }
     
        public void connection(){
            if(!isConnected()){
                try {
                    connection = DriverManager.getConnection(urlbase + host + "/" + database, user, pass);
                    main.getServer().getConsoleSender().sendMessage(String.format("[FrancoLand] " + ChatColor.AQUA + "Connection a la base de données effectuée avec succès !"));
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
     
        public void disconnect(){
            if(isConnected()){
                try {
                    connection.close();
                    main.getServer().getConsoleSender().sendMessage(String.format("[FrancoLand] " + ChatColor.AQUA + "Déconnection a la base de données effectuée avec succès !"));
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
     
        public boolean isConnected(){
            return connection != null;
        }

        public void setRank(Player player, Rank rank){
         
            try {
                PreparedStatement rs = connection.prepareStatement("UPDATE joueurs SET grade = ? WHERE uuid = ?");
                rs.setInt(1, rank.getPower());
                rs.setString(2, player.getUniqueId().toString());
                rs.executeUpdate();
                rs.close();

            } catch (SQLException e) {
                e.printStackTrace();
            }
         
        }
     
        public Rank getRank(Player player){
         
            try {
                PreparedStatement q = connection.prepareStatement("SELECT grade FROM joueurs WHERE uuid = ?");
                q.setString(1, player.getUniqueId().toString());

                int power = 0;
                ResultSet rs = q.executeQuery();
             
                while(rs.next()){
                    power = rs.getInt("grade");
                }
             
                q.close();
             
                return Rank.idToRank(power);
             
            } catch (SQLException e) {
                e.printStackTrace();
            }
         
            return Rank.JOUEUR;
        }
     
        public void createAccount(Player player){
            if(!hasAccount(player)){
                try {
                    PreparedStatement q = connection.prepareStatement("INSERT INTO joueurs(uuid,grade) VALUES (?,?)");
                    q.setString(0, player.getUniqueId().toString());
                    q.setInt(1, 1);
                    q.execute();
                    q.close();
                    player.sendMessage(config.getString("messages.auth.prefix") + config.getString("messages.auth.message-registered"));
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
     
        public boolean hasAccount(Player player){
            try {
                PreparedStatement q = connection.prepareStatement("SELECT uuid FROM joueurs WHERE uuid = ?");
                q.setString(1, player.getUniqueId().toString());
                ResultSet resultat = q.executeQuery();
                boolean hasAccount = resultat.next();
                q.close();
                return hasAccount;
            } catch (SQLException e) {
                e.printStackTrace();
            }
         
            return false;
        }

        public void connect(Player player) {
            if(hasAccount(player)) {
                player.sendMessage(config.getString("messages.auth.message-connected"));
            } else createAccount(player);
        }
    }
     

    Code (Java):

    package fr.simon.francoranks;

    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    import org.bukkit.event.player.PlayerJoinEvent;

    public class FrancoRanksListener implements Listener {

        private SQLConnection SQL;

        public FrancoRanksListener (SQLConnection SQL) {
            this.SQL = SQL;
        }
     
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e) {
            SQL.createAccount(e.getPlayer());
        }

        @EventHandler
        public void onPlayerChat(AsyncPlayerChatEvent e) {
            Player p = e.getPlayer();
            Rank rank = SQL.getRank(p);
         
            e.setFormat(rank.getName() + " " + p.getName() + " » " + rank.getTag() + e.getMessage());
        }
    }
     

    Thanks you,
    Simon.
     
    #1 Simsimpicpic, Dec 4, 2019 at 2:44 PM
    Last edited: Dec 5, 2019 at 9:19 AM
  2. Are we supposed to guess the error? (EventException is for every exception that happens in an event handler)
    Post the full stacktrace.
     
    • Informative Informative x 1
  3. Like @robertlit said, we would need the full stack trace to help you. It is worth noting that it may be because the values in the database may not be fetched fast enough in the login event, and thus the data is null
     
  4. I’m just going to guess that Rank is null
     
  5. [21:27:58 ERROR]: Could not pass event PlayerJoinEvent to FrancoRanks v1.0
    org.bukkit.event.EventException
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:305) ~[server.jar:git-Spigot-c3c767f-33d5de3]
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[server.jar:git-Spigot-c3c767f-33d5de3]
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [server.jar:git-Spigot-c3c767f-33d5de3]
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [server.jar:git-Spigot-c3c767f-33d5de3]
    at net.minecraft.server.v1_8_R1.PlayerList.onPlayerJoin(PlayerList.java:272) [server.jar:git-Spigot-c3c767f-33d5de3]
    at net.minecraft.server.v1_8_R1.PlayerList.a(PlayerList.java:156) [server.jar:git-Spigot-c3c767f-33d5de3]
    at net.minecraft.server.v1_8_R1.LoginListener.b(LoginListener.java:109) [server.jar:git-Spigot-c3c767f-33d5de3]
    at net.minecraft.server.v1_8_R1.LoginListener.c(LoginListener.java:41) [server.jar:git-Spigot-c3c767f-33d5de3]
    at net.minecraft.server.v1_8_R1.NetworkManager.a(NetworkManager.java:159) [server.jar:git-Spigot-c3c767f-33d5de3]
    at net.minecraft.server.v1_8_R1.ServerConnection.c(ServerConnection.java:82) [server.jar:git-Spigot-c3c767f-33d5de3]
    at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:800) [server.jar:git-Spigot-c3c767f-33d5de3]
    at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:316) [server.jar:git-Spigot-c3c767f-33d5de3]
    at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:634) [server.jar:git-Spigot-c3c767f-33d5de3]
    at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java:537) [server.jar:git-Spigot-c3c767f-33d5de3]
    at java.lang.Thread.run(Thread.java:748) [?:1.8.0_222]
    Caused by: java.lang.NullPointerException
    at fr.simon.francoranks.FrancoRanksListener.onPlayerJoin(FrancoRanksListener.java:19) ~[?:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_222]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_222]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_222]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_222]
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:301) ~[server.jar:git-Spigot-c3c767f-33d5de3]
    ... 14 more
     
  6. Initialize your SQL varible
    It return npe
     
  7. Thanks everyone ^^
    It's resolved.
     
Thread Status:
Not open for further replies.