连接 mongodb 到我的世界插件

Connect mongodb to minecraft plugin

我正在为我的 minecraft 服务器制作一个 minecraft 插件,但出现错误,我找不到解决方案。上下文:我想存储 level/xp/rank 等播放器的数据......你能帮我做这部分插件吗,我是 Java.[=16= 的初学者]

这是我的代码:

import com.mongodb.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import me.codexis.velocitylobbygeneral.commands.Lobby;
import me.codexis.velocitylobbygeneral.commands.MoveBot;
import me.codexis.velocitylobbygeneral.commands.Test;
import me.codexis.velocitylobbygeneral.event.*;
import org.bson.Document;
import org.bukkit.ChatColor;
import org.bukkit.plugin.java.JavaPlugin;

public final class VelocityLobbyGeneral extends JavaPlugin {

    private static VelocityLobbyGeneral instance;

    @Override
    public void onEnable() {
        setInstance(this);

        // Listeners
        getServer().getPluginManager().registerEvents(new OnJoinQuit(), this);
        getServer().getPluginManager().registerEvents(new FormatChat(), this);
        getServer().getPluginManager().registerEvents(new Scoreboard(), this);

        // Channels
        getServer().getMessenger().registerOutgoingPluginChannel(this, "BungeeCord");

        // Commands
        new Lobby();
        new MoveBot();
        new Test();

        // Connection to database
        MongoClient mongoClient = (MongoClient) MongoClients.create("mongodb+srv://myusername:@databasemc.ehssc.mongodb.net/VelocityMC?retryWrites=true&w=majority");
        MongoDatabase mongoDatabase = mongoClient.getDatabase("VelocityMC");
        MongoCollection<Document> mongoCollection = mongoDatabase.getCollection("Vide");
        getLogger().info(ChatColor.GREEN + "Connected to Database");

        getLogger().info("=============================================");
        getLogger().info("   >>>> Velocity Lobby General Loaded <<<<   ");
        getLogger().info("=============================================");
   }

   @Override
   public void onDisable() {

       getLogger().info("===============================================");
       getLogger().info("   >>>> Velocity Lobby General disabled <<<<   ");
       getLogger().info("===============================================");

   }

   public static VelocityLobbyGeneral getInstance(){
       return instance;
   }

   private static void setInstance(VelocityLobbyGeneral instance){
      velocityLobbyGeneral.instance = instance;
   }

}

这是我的错误:

[12:32:49 WARN]: java.lang.NoClassDefFoundError: com/mongodb/client/MongoClients
[12:32:49 WARN]:        at VelocityLobbyGeneral.jar//me.codexis.velocitylobbygeneral.VelocityLobbyGeneral.onEnable(VelocityLo 
bbyGeneral.java:38)
[12:32:49 WARN]:        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:264)
[12:32:49 WARN]:        at 
org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:370)
[12:32:49 WARN]:        at 
org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:500)
[12:32:49 WARN]:        at 
org.bukkit.craftbukkit.v1_17_R1.CraftServer.enablePlugin(CraftServer.java:535)
[12:32:49 WARN]:        at 
org.bukkit.craftbukkit.v1_17_R1.CraftServer.enablePlugins(CraftServer.java:449)
[12:32:49 WARN]:        at 
org.bukkit.craftbukkit.v1_17_R1.CraftServer.reload(CraftServer.java:970)
[12:32:49 WARN]:        at org.bukkit.Bukkit.reload(Bukkit.java:769)
[12:32:49 WARN]:        at         org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:54)
[12:32:49 WARN]:        at     org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:159)
[12:32:49 WARN]:        at org.bukkit.craftbukkit.v1_17_R1.CraftServer.dispatchCommand(CraftServer.java:838)
[12:32:49 WARN]:        at     org.bukkit.craftbukkit.v1_17_R1.CraftServer.dispatchServerCommand(CraftServer.java:801)
[12:32:49 WARN]:        at net.minecraft.server.dedicated.DedicatedServer.handleCommandQueue(DedicatedServer.java:518)
[12:32:49 WARN]:        at     net.minecraft.server.dedicated.DedicatedServer.b(DedicatedServer.java:480)
[12:32:49 WARN]:        at net.minecraft.server.MinecraftServer.a(MinecraftServer.java:1475)
[12:32:49 WARN]:        at net.minecraft.server.MinecraftServer.x(MinecraftServer.java:1274)
[12:32:49 WARN]:        at net.minecraft.server.MinecraftServer.lambda$spin[=12=](MinecraftServer.java:319)
[12:32:49 WARN]:        at java.base/java.lang.Thread.run(Thread.java:831)
[12:32:49 WARN]: Caused by: java.lang.ClassNotFoundException: com.mongodb.client.MongoClients
[12:32:49 WARN]:        at org.bukkit.plugin.java.PluginClassLoader.loadClass0(PluginClassLoader.java:146)
[12:32:49 WARN]:        at org.bukkit.plugin.java.PluginClassLoader.loadClass(PluginClassLoader.java:103)
[12:32:49 WARN]:        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:519)
[12:32:49 WARN]:        ... 18 more

谁能帮帮我

Pom.xml :

<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>me.codexisphantom</groupId>
<artifactId>VelocityLobby</artifactId>
<version>1.0</version>
<packaging>jar</packaging>

<name>VelocityLobby</name>

<description>Official VelocityMC Plugin</description>
<properties>
    <java.version>1.8</java.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<url>www.velocity-net.com</url>

<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>papermc-repo</id>
        <url>https://papermc.io/repo/repository/maven-public/</url>
    </repository>
    <repository>
        <id>sonatype</id>
        <url>https://oss.sonatype.org/content/groups/public/</url>
    </repository>
</repositories>

<dependencies>
    <dependency>
        <groupId>io.papermc.paper</groupId>
        <artifactId>paper-api</artifactId>
        <version>1.17.1-R0.1-SNAPSHOT</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

Mongodb 依赖:

    <dependency>
        <groupId>org.mongodb</groupId>
        <artifactId>mongo-java-driver</artifactId>
        <version>3.12.10</version>
        <scope>compile</scope>
    </dependency>

已添加插件:

    <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-assembly-plugin</artifactId>
         <executions>
             <execution>
                 <phase>package</phase>
                 <goals>
                     <goal>single</goal>
                 </goals>
                 <configuration>
                     <descriptorRefs>
                         <descriptorRef>jar-with-dependencies</descriptorRef>
                     </descriptorRefs>
                 </configuration>
             </execution>
         </executions>
     </plugin>

在您的 Maven 配置中,您正在导入 MongoDB。所以,它会编译,你将能够在开发时使用它。

但是,当你编译时,它不会包含在最终的jar 中。并且例如 MongoDB 不在龙头中,它会造成您的错误(找不到 MongoDB class)。

要修复它,有多个教程 1, 2, , 4 ...我相信我们可以找到更多。

我建议你像那样导入 mongo 数据库 :

<dependency>
    <groupId>org.mongodb</groupId>
    <artifactId>mongo-java-driver</artifactId>
    <version>3.12.10</version> <!-- The version that you want -->
    <scope>compile</scope> <!-- In my maven project, it include this project in builded jar -->
</dependency>

您可以找到 MongoDB here

的所有版本

我的完整配置有效:

<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>
        <plugin>
             <groupId>org.apache.maven.plugins</groupId>
             <artifactId>maven-assembly-plugin</artifactId>
             <executions>
                 <execution>
                     <phase>package</phase>
                     <goals>
                         <goal>single</goal>
                     </goals>
                     <configuration>
                         <descriptorRefs>
                             <descriptorRef>jar-with-dependencies</descriptorRef>
                         </descriptorRefs>
                     </configuration>
                 </execution>
             </executions>
         </plugin>
    </plugins>
   </build>

 <repositories>
   <repository>
       <id>sonatype</id>
       <url>https://oss.sonatype.org/content/groups/public/</url>
   </repository>
 </repositories>

 <dependencies>
     <dependency>
       <groupId>org.mongodb</groupId>
       <artifactId>mongo-java-driver</artifactId>
       <version>3.12.10</version>
       <scope>compile</scope>
   </dependency>
 </dependencies>