当前位置: 首页 > 知识库问答 >
问题:

Morphia-basicdBobject不能强制转换为MyClass

傅和璧
2023-03-14

完全错误:

[23:29:01 ERROR]: null
org.bukkit.command.CommandException: Unhandled exception executing command 'quest' in plugin ExploreRPG v1.0-SNAPSHOT
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:47) ~[patched_1.14.4.jar:git-Paper-176]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:159) ~[patched_1.14.4.jar:git-Paper-176]
        at org.bukkit.craftbukkit.v1_14_R1.CraftServer.dispatchCommand(CraftServer.java:739) ~[patched_1.14.4.jar:git-Paper-176]
        at net.minecraft.server.v1_14_R1.PlayerConnection.handleCommand(PlayerConnection.java:1800) ~[patched_1.14.4.jar:git-Paper-176]
        at net.minecraft.server.v1_14_R1.PlayerConnection.a(PlayerConnection.java:1608) ~[patched_1.14.4.jar:git-Paper-176]
        at net.minecraft.server.v1_14_R1.PacketPlayInChat.a(PacketPlayInChat.java:47) ~[patched_1.14.4.jar:git-Paper-176]
        at net.minecraft.server.v1_14_R1.PacketPlayInChat.a(PacketPlayInChat.java:5) ~[patched_1.14.4.jar:git-Paper-176]
        at net.minecraft.server.v1_14_R1.PlayerConnectionUtils.lambda$ensureMainThread$0(PlayerConnectionUtils.java:23) ~[patched_1.14.4.jar:git-Paper-176]
        at net.minecraft.server.v1_14_R1.TickTask.run(SourceFile:18) ~[patched_1.14.4.jar:git-Paper-176]
        at net.minecraft.server.v1_14_R1.IAsyncTaskHandler.executeTask(IAsyncTaskHandler.java:136) ~[patched_1.14.4.jar:git-Paper-176]
        at net.minecraft.server.v1_14_R1.IAsyncTaskHandlerReentrant.executeTask(SourceFile:23) ~[patched_1.14.4.jar:git-Paper-176]
        at net.minecraft.server.v1_14_R1.IAsyncTaskHandler.executeNext(IAsyncTaskHandler.java:109) ~[patched_1.14.4.jar:git-Paper-176]
        at net.minecraft.server.v1_14_R1.MinecraftServer.aX(MinecraftServer.java:1013) ~[patched_1.14.4.jar:git-Paper-176]
        at net.minecraft.server.v1_14_R1.MinecraftServer.executeNext(MinecraftServer.java:1006) ~[patched_1.14.4.jar:git-Paper-176]
        at net.minecraft.server.v1_14_R1.IAsyncTaskHandler.awaitTasks(IAsyncTaskHandler.java:119) ~[patched_1.14.4.jar:git-Paper-176]
        at net.minecraft.server.v1_14_R1.MinecraftServer.sleepForTick(MinecraftServer.java:990) ~[patched_1.14.4.jar:git-Paper-176]
        at net.minecraft.server.v1_14_R1.MinecraftServer.run(MinecraftServer.java:923) ~[patched_1.14.4.jar:git-Paper-176]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_221]
Caused by: java.lang.ClassCastException: com.mongodb.BasicDBObject cannot be cast to net.exploremc.explorerpg.controllers.user.QuestProgress
        at net.exploremc.explorerpg.controllers.User.getActiveQuests(User.java:148) ~[?:?]
        at net.exploremc.explorerpg.commands.QuestCMD.onCommand(QuestCMD.java:23) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:45) ~[patched_1.14.4.jar:git-Paper-176]
        ... 17 more

用户类:

@Entity(value = "users", noClassnameStored = true)
public class User {
    @Transient public static HashMap<UUID, User> usersList = new HashMap<>();
    @Transient private static Datastore datastore = ExploreRPG.getDatabase().getDatastore();

    @Id private String id;
    @Transient private Player player;
    @Indexed private String uuid;

    private List<String> ipHistory = new ArrayList<>();
    private List<String> nameHistory = new ArrayList<>();
    @Embedded @Property("quests") private HashMap<String, QuestProgress> quests = new HashMap<>();    // This is the problem class

    public User(){ }

    public User(Player player) {
        this.player = player;
        this.ipHistory.add(player.getAddress().getHostString());
        this.uuid = player.getUniqueId().toString();
        this.nameHistory.add(player.getName());
    }

    public void storeUser() {
        datastore.save(this);
    }

    public void updateUser(){
        Query<User> updateQuery = datastore.createQuery(User.class).field("uuid").equal(uuid);
        UpdateOperations<User> ops = datastore.createUpdateOperations(User.class)
                .set("ipHistory", ipHistory)
                .set("nameHistory", nameHistory)
                .set("quests", quests);
        datastore.update(updateQuery, ops);
    }

    public static boolean doesUserExist(Player player){

        Query<User> query = datastore.find(User.class).field("uuid").equal(player.getUniqueId().toString());
        User user = query.first();
        return user != null;
    }

    public static User getUserFromDB(Player player){
        Query<User> query = datastore.find(User.class).field("uuid").equal(player.getUniqueId().toString());
        User user = query.first();
        user.quests = query.first().quests;
        user.player = player;
        return user;
    }
}

导致此错误的实现:

public List<QuestProgress> getActiveQuests(){
        List<QuestProgress> activeQuests = new ArrayList<>();
        for (QuestProgress quest : quests.values()) {
            if(!quest.isCompleted())
                activeQuests.add(quest);
        }
        return activeQuests;
    }

暂时还没有答案

 类似资料: