JnCrMx / discord-game-sdk4j

Java bindings for Discord's Game SDK
MIT License
121 stars 23 forks source link

ResposneError when trying to initialize DiscordGameSDK #40

Closed simaxme closed 2 years ago

simaxme commented 2 years ago

Hey, So I am wanna make a simple disord presence application, but when i try to start my application, i get this weird message saying [ERROR] Failed to configure networking: ResponseError { code: TransactionAborted, message: "Transaction was aborted" } I am currently working on a Linux 64x system and do not know what to do. Any idea?

Here is my complete code:


    private static final long clientID = 931843550801449011L;
    private static final boolean enabled = true;

    private ModClient modClient;
    private int tickCount = 0;
    private Core core;

    public DiscordRPCManager(ModClient modClient) {
        this.modClient = modClient;
    }

    public void onTick(MinecraftClient minecraftClient) {
        if (!enabled) return;
        tickCount++;
        if (tickCount >= 200) {
            tickCount = 0;
            this.onTenSeconds();
        }
    }
    public void onTenSeconds() {
        if (core == null) return;
        core.runCallbacks();
    }

    public void init() throws IOException {
        if (!enabled) return;
        File discordLibrary = DownloadNativeLibrary.downloadDiscordLibrary();
        if (discordLibrary == null) {
            System.out.println("Failed to download discord library");
            return;
        }
        Core.init(discordLibrary);

        try (CreateParams params = new CreateParams()) {
            params.setClientID(clientID);
            params.setFlags(CreateParams.Flags.NO_REQUIRE_DISCORD);
            try(Core core = new Core(params)) {
                this.core = core;
                this.createPresence();
                this.onTenSeconds();
            }

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Failed to create discord rpc");
        }
    }

    private void createPresence() {
        try(Activity activity = new Activity())
            {
                activity.setDetails("Spielt auf JaHolLDE");
                activity.setState("der RP-Name");

                // Setting a start time causes an "elapsed" field to appear
                activity.timestamps().setStart(Instant.now());

                // We are in a party with 10 out of 100 people.
                    /*
                    activity.party().size().setMaxSize(100);
                    activity.party().size().setCurrentSize(10);*/

                // Make a "cool" image show up
                //activity.assets().setLargeImage("test");

                // Setting a join secret and a party ID causes an "Ask to Join" button to appear
                //activity.party().setID("Party!");
                //activity.secrets().setJoinSecret("Join!");

                // Finally, update the current activity to our activity
                core.activityManager().updateActivity(activity);
            } catch (Exception e) {
                System.out.println("Failed to create discord presence");
            }
    }

}```
JnCrMx commented 2 years ago

The Core gets destroyed as soon as you leave the try-with-resources block:

            try(Core core = new Core(params)) {
                this.core = core;
                this.createPresence();
                this.onTenSeconds();
            }

Just change it to

            this.core = new Core(params);
            this.createPresence();
            this.onTenSeconds();

and it should work.

You only want to use a try-with-resources statement if you don't need the resource later anymore.

EDIT: You also should store the CreateParams somewhere (e.g. as a field of your class) and also not use a try-with-resources for them either, they need to be alive as long as the core is alive.