Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I register a custom codec #2990

Open
TrueReassembly opened this issue Apr 15, 2024 · 1 comment
Open

How do I register a custom codec #2990

TrueReassembly opened this issue Apr 15, 2024 · 1 comment
Labels

Comments

@TrueReassembly
Copy link

** Please ask your question and provide whatever background or supporting information you think might be relevant: **

I am currently trying to register a codec that I have created for the Bukkit ItemStack class but I cannot figure out how to register it. I have already looked at both MongoDB's and Morphia's documentation but was unable to find anything. Here is my Mongo Registration class

object MongoDatabase {
    lateinit var datastore: Datastore
    var connect by Delegates.notNull<Boolean>()

    fun init() {
        connect = false
        try {
            val mongoUri = ClickerServer.getInstance().config.getString("mongo.uri")
            if (mongoUri == null) {
                ClickerServer.getInstance().logger.severe("MongoDB URI not found in config.yml")
                ClickerServer.getInstance().server.pluginManager.disablePlugin(ClickerServer.getInstance())
                throw IllegalStateException("MongoDB URI not found in config.yml")
            }
            datastore = Morphia.createDatastore(MongoClients.create(mongoUri), ClickerServer.getInstance().config.getString("mongo.db")!!)
            datastore.mapper.config.packages(listOf("dev.reassembly.clickerserver.models"))
            // TODO: Register Codec here
            connect = true
        } catch (e: Exception) {
            connect = false
            e.printStackTrace()
        }

    }
}

** Please complete the following information: **
Server Version: Unsure, I'm using mongo atlas if that's the question
Driver Version: Whichever one comes bundled in morphia
Morphia Version: 2.4.11

@rdlf0
Copy link

rdlf0 commented May 3, 2024

You can try this:

final MongoClientSettings mongoClientSettings =
        MongoClientSettings.builder()
                ...
                other client settings
                ...
                .codecRegistry(
                        CodecRegistries.fromRegistries(
                                CodecRegistries.fromCodecs(new YourCodecClass()),
                                MongoClientSettings.getDefaultCodecRegistry()))
                .build();

final Datastore ds = Morphia.createDatastore(MongoClients.create(mongoClientSettings));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants