Dependency on the plugin via Maven

Discussion in 'Plugin Development' started by qw_, Dec 11, 2021.

  1. Offline

    qw_

    Hello.
    I'm making a project that uses two dependencies (except Spigot): JDA and my plugin.
    The problem is that if I connect my plugin via Maven, then when accessing any method of its class (for example, the same getInstance of any class), it returns null.
    I could connect his jar directly to the project, but you can't connect JDA like that, and I need to use both.

    How to solve this problem?

    Example of a class from a plugin:
    Code:
    public class DatabaseEditor {
    
        static Connection connection;
        public static DatabaseEditor instance;
    
        public DatabaseEditor(Connection con) {
            connection = con;
            QChat.getInstance().getLogger().info("DB Connected");
            String query = "CREATE TABLE IF NOT EXISTS 'users' (" +
                    "'username' TEXT NOT NULL UNIQUE, " +
                    "'chat' TEXT NOT NULL " +
                    ")";
            try {
                Statement statement = connection.createStatement();
                statement.executeUpdate(query);
                String query_delete = "DELETE FROM users";
                statement.executeUpdate(query_delete);
                statement.close();
                QChat.getInstance().getLogger().info("DB Table created.");
            } catch (SQLException e) {
                e.printStackTrace();
            }
            instance = this;
        }
    
        public static void selectChat(Player player, Chat chat) {
            String name = player.getName();
            String query = String.format("INSERT INTO users VALUES('%s', '%s')", name, chat.getName());
            String query_delete = String.format("DELETE FROM 'users' WHERE username='%s'", name);
            try {
                Statement statement = connection.createStatement();
                statement.executeUpdate(query_delete);
                statement.executeUpdate(query);
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    
        public static String getSelectedChat(Player player) {
            String name = player.getName();
            String query = "SELECT chat FROM users WHERE username = '" + name + "'";
            String result = "none";
            try {
                Statement statement = connection.createStatement();
                ResultSet rs = statement.executeQuery(query);
                while(rs.next()) {
                    result = rs.getString("chat");
                }
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return result;
        }
    
        public static DatabaseEditor getInstance() {
            return instance;
        }
    
    }
    Code for accessing the plugin
    Code:
    if(DatabaseEditor.getSelectedChat(event.getPlayer()).equals(ChatGlobal.getInstance().getName())) {
    Error in the console
    Code:
    [16:55:30 ERROR]: Could not pass event AsyncPlayerChatEvent to (название плагина) v1.0                                        java.lang.NullPointerException: Cannot invoke "java.sql.Connection.createStatement()" because "quietw.qchat.Database.DatabaseEditor.connection" is null
    ps: spigot version - 1.18, tried with spigot and paper both

    add:

    "API" pom.xml:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>quietw</groupId> <artifactId>qchat</artifactId> <version>1.3.3</version> <packaging>jar</packaging> <name>QChat</name> <properties> <java.version>1.8</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>${java.version}</source> <target>${java.version}</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.2.4</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <createDependencyReducedPom>false</createDependencyReducedPom> </configuration> </execution> </executions> </plugin> </plugins> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> </build> <repositories> <repository> <id>spigotmc-repo</id> <url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url> </repository> <repository> <id>sonatype</id> <url>https://oss.sonatype.org/content/groups/public/</url> </repository> </repositories> <dependencies> <dependency> <groupId>org.spigotmc</groupId> <artifactId>spigot-api</artifactId> <version>1.18-R0.1-SNAPSHOT</version> <scope>provided</scope> </dependency> </dependencies></project>
    
    Plugin pom.xml:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>iqw</groupId>
        <artifactId>discordlogin</artifactId>
        <version>1.0</version>
        <packaging>jar</packaging>
    
        <name>DiscordLogin</name>
    
        <properties>
            <java.version>1.8</java.version>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        </properties>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.1</version>
                    <configuration>
                        <source>${java.version}</source>
                        <target>${java.version}</target>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-shade-plugin</artifactId>
                    <version>3.2.4</version>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>shade</goal>
                            </goals>
                            <configuration>
                                <createDependencyReducedPom>false</createDependencyReducedPom>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
            <resources>
                <resource>
                    <directory>src/main/resources</directory>
                    <filtering>true</filtering>
                </resource>
            </resources>
        </build>
    
        <repositories>
            <repository>
                <id>spigotmc-repo</id>
                <url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
            </repository>
            <repository>
                <id>sonatype</id>
                <url>https://oss.sonatype.org/content/groups/public/</url>
            </repository>
        </repositories>
    
        <dependencies>
            <dependency>
                <groupId>org.spigotmc</groupId>
                <artifactId>spigot-api</artifactId>
                <version>1.18-R0.1-SNAPSHOT</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>net.dv8tion</groupId>
                <artifactId>JDA</artifactId>
                <version>5.0.0-alpha.1</version>
            </dependency>
            <dependency>
                <groupId>quietw</groupId>
                <artifactId>qchat</artifactId>
                <version>1.3.3</version>
            </dependency>
        </dependencies>
    </project>
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Dec 12, 2021
  2. Offline

    Kars

    Your problem is with the dependency itself and not with Maven, otherwise you would get NoSuchMethod or something similar.
     
  3. Offline

    qw_

    And how I can fix it? (Dependency was created by me so I can edit it)
     

Share This Page