montoyo / mcef

Minecraft ChromiumEF (based on JCEF)
Other
191 stars 85 forks source link

Crash on registerJSQueryHandler #81

Closed Raraph84 closed 2 years ago

Raraph84 commented 2 years ago

Hi, I would like to display a web interface in a gui but my game crashes on startup. This is the error :

[18:24:52] [Client thread/INFO] [STDOUT]: [net.minecraft.init.Bootstrap:printToSYSOUT:553]: ---- Minecraft Crash Report ----
// Uh... Did I do that?

Time: 12/26/21 6:24 PM
Description: There was a severe problem during mod loading that has caused the game to fail

net.minecraftforge.fml.common.LoaderExceptionModCrash: Caught exception from DoYourLife Mod (doyourlifemod)
Caused by: java.lang.NullPointerException
    at net.montoyo.mcef.client.ClientProxy.registerJSQueryHandler(ClientProxy.java:308)
    at fr.raraph84.doyourlifemod.DoYourLifeModClient.init(DoYourLifeModClient.java:56)
    at fr.raraph84.doyourlifemod.DoYourLifeMod.init(DoYourLifeMod.java:32)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at net.minecraftforge.fml.common.FMLModContainer.handleModStateEvent(FMLModContainer.java:639)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.google.common.eventbus.Subscriber.invokeSubscriberMethod(Subscriber.java:91)
    at com.google.common.eventbus.Subscriber$SynchronizedSubscriber.invokeSubscriberMethod(Subscriber.java:150)
    at com.google.common.eventbus.Subscriber$1.run(Subscriber.java:76)
    at com.google.common.util.concurrent.MoreExecutors$DirectExecutor.execute(MoreExecutors.java:399)
    at com.google.common.eventbus.Subscriber.dispatchEvent(Subscriber.java:71)
    at com.google.common.eventbus.Dispatcher$PerThreadQueuedDispatcher.dispatch(Dispatcher.java:116)
    at com.google.common.eventbus.EventBus.post(EventBus.java:217)
    at net.minecraftforge.fml.common.LoadController.sendEventToModContainer(LoadController.java:219)
    at net.minecraftforge.fml.common.LoadController.propogateStateMessage(LoadController.java:197)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.google.common.eventbus.Subscriber.invokeSubscriberMethod(Subscriber.java:91)
    at com.google.common.eventbus.Subscriber$SynchronizedSubscriber.invokeSubscriberMethod(Subscriber.java:150)
    at com.google.common.eventbus.Subscriber$1.run(Subscriber.java:76)
    at com.google.common.util.concurrent.MoreExecutors$DirectExecutor.execute(MoreExecutors.java:399)
    at com.google.common.eventbus.Subscriber.dispatchEvent(Subscriber.java:71)
    at com.google.common.eventbus.Dispatcher$PerThreadQueuedDispatcher.dispatch(Dispatcher.java:116)
    at com.google.common.eventbus.EventBus.post(EventBus.java:217)
    at net.minecraftforge.fml.common.LoadController.distributeStateMessage(LoadController.java:136)
    at net.minecraftforge.fml.common.Loader.initializeMods(Loader.java:749)
    at net.minecraftforge.fml.client.FMLClientHandler.finishMinecraftLoading(FMLClientHandler.java:336)
    at net.minecraft.client.Minecraft.init(Minecraft.java:535)
    at net.minecraft.client.Minecraft.run(Minecraft.java:378)
    at net.minecraft.client.main.Main.main(Main.java:118)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at net.minecraft.launchwrapper.Launch.launch(Launch.java:135)
    at net.minecraft.launchwrapper.Launch.main(Launch.java:28)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at net.minecraftforge.legacydev.Main.start(Main.java:86)
    at net.minecraftforge.legacydev.MainClient.main(MainClient.java:29)

A detailed walkthrough of the error, its code path and all known details is as follows:
---------------------------------------------------------------------------------------

-- System Details --
Details:
    Minecraft Version: 1.12.2
    Operating System: Windows 10 (amd64) version 10.0
    Java Version: 1.8.0_311, Oracle Corporation
    Java VM Version: Java HotSpot(TM) 64-Bit Server VM (mixed mode), Oracle Corporation
    Memory: 448560112 bytes (427 MB) / 843055104 bytes (804 MB) up to 1858600960 bytes (1772 MB)
    JVM Flags: 0 total; 
    IntCache: cache: 0, tcache: 0, allocated: 0, tallocated: 0
    FML: MCP 9.42 Powered by Forge 14.23.5.2860 6 mods loaded, 6 mods active
    States: 'U' = Unloaded 'L' = Loaded 'C' = Constructed 'H' = Pre-initialized 'I' = Initialized 'J' = Post-initialized 'A' = Available 'D' = Disabled 'E' = Errored

    | State | ID            | Version      | Source                                                             | Signature |
    |:----- |:------------- |:------------ |:------------------------------------------------------------------ |:--------- |
    | LCHI  | minecraft     | 1.12.2       | minecraft.jar                                                      | None      |
    | LCHI  | mcp           | 9.42         | minecraft.jar                                                      | None      |
    | LCHI  | FML           | 8.0.99.99    | forge-1.12.2-14.23.5.2860_mapped_snapshot_20171003-1.12-recomp.jar | None      |
    | LCHI  | forge         | 14.23.5.2860 | forge-1.12.2-14.23.5.2860_mapped_snapshot_20171003-1.12-recomp.jar | None      |
    | LCHE  | doyourlifemod | 1.0          | main                                                               | None      |
    | LCH   | mcef          | 1.20         | mcef-1.12.2-1.20-api.jar                                           | None      |

    Loaded coremods (and transformers): 
    GL info: ' Vendor: 'Intel' Version: '4.6.0 - Build 27.20.100.8439' Renderer: 'Intel(R) Iris(R) Xe Graphics'
[18:24:52] [Client thread/INFO] [STDOUT]: [net.minecraft.init.Bootstrap:printToSYSOUT:553]: #@!@# Game crashed! Crash report saved to: #@!@# C:\Users\Raraph84\Documents\Eclipsia-Workspace\DoYourLife_Mod\run\.\crash-reports\crash-2021-12-26_18.24.52-client.txt
AL lib: (EE) alc_cleanup: 1 device not closed

My code :

package fr.raraph84.doyourlifemod;

import java.io.File;

import fr.raraph84.doyourlifemod.guis.BrowserScreen;
import fr.raraph84.doyourlifemod.guis.GuiCustomIngame;
import fr.raraph84.doyourlifemod.guis.GuiCustomIngameMenu;
import fr.raraph84.doyourlifemod.guis.ScreenCfg;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiIngameMenu;
import net.minecraft.client.gui.GuiMainMenu;
import net.minecraft.client.gui.GuiOptions;
import net.minecraftforge.client.GuiIngameForge;
import net.minecraftforge.client.event.GuiOpenEvent;
import net.minecraftforge.client.event.RenderGameOverlayEvent;
import net.minecraftforge.client.event.RenderGameOverlayEvent.ElementType;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.montoyo.mcef.api.API;
import net.montoyo.mcef.api.IBrowser;
import net.montoyo.mcef.api.IDisplayHandler;
import net.montoyo.mcef.api.IJSQueryCallback;
import net.montoyo.mcef.api.IJSQueryHandler;
import net.montoyo.mcef.api.MCEFApi;
import net.montoyo.mcef.utilities.Log;

public class DoYourLifeModClient extends DoYourLifeModCommon implements IDisplayHandler, IJSQueryHandler {

    public static DoYourLifeModClient instance;

    public ScreenCfg hudBrowser = null;
    private Minecraft mc = Minecraft.getMinecraft();
    private BrowserScreen backup = null;
    private API api;

    @Override
    public void preInit(File configFile) {

        super.preInit(configFile);

        api = MCEFApi.getAPI();
    }

    @Override
    public void init() {

        super.init();

        instance = this;

        GuiIngameForge.renderObjective = false;

        MinecraftForge.EVENT_BUS.register(this);

        api.registerDisplayHandler(this);
        api.registerJSQueryHandler(this);
    }

    @SubscribeEvent
    public void onOpenGui(GuiOpenEvent event) {

        if (event.getGui() == null)
            return;

        if (event.getGui().getClass() == GuiIngameMenu.class)
            event.setGui(new GuiCustomIngameMenu());
        else if (event.getGui().getClass() == GuiMainMenu.class)
            event.setGui(new GuiMainMenu());

        if (event.getGui().getClass() == GuiOptions.class)
            event.setGui(new BrowserScreen("https://google.com"));
    }

    @SubscribeEvent
    public void renderGameOverlayPre(RenderGameOverlayEvent.Pre event) {

        if (event.getType().equals(ElementType.HEALTH) || event.getType().equals(ElementType.FOOD)
                || event.getType().equals(ElementType.EXPERIENCE) || event.getType().equals(ElementType.POTION_ICONS))
            event.setCanceled(true);
        else if (event.getType().equals(ElementType.CHAT)) {
            event.setCanceled(true);
            GuiCustomIngame.renderChat(0, event.getResolution().getScaledHeight() - 65);
        }
    }

    @SubscribeEvent
    public void renderGameOverlayPost(RenderGameOverlayEvent.Post event) {

        if (event.getType().equals(ElementType.ALL))
            GuiCustomIngame.renderAll(event.getResolution());
    }

    public API getAPI() {
        return api;
    }

    public void setBackup(BrowserScreen bu) {
        backup = bu;
    }

    public boolean hasBackup() {
        return (backup != null);
    }

    public void showScreen(String url) {
        if (mc.currentScreen instanceof BrowserScreen)
            ((BrowserScreen) mc.currentScreen).loadURL(url);
        else if (hasBackup()) {
            mc.displayGuiScreen(backup);
            backup.loadURL(url);
            backup = null;
        } else
            mc.displayGuiScreen(new BrowserScreen(url));
    }

    public IBrowser getBrowser() {
        if (mc.currentScreen instanceof BrowserScreen)
            return ((BrowserScreen) mc.currentScreen).browser;
        else if (backup != null)
            return backup.browser;
        else
            return null;
    }

    @Override
    public void onAddressChange(IBrowser browser, String url) {
        // Called by MCEF if a browser's URL changes. Forward this event to the screen.
        if (mc.currentScreen instanceof BrowserScreen)
            ((BrowserScreen) mc.currentScreen).onUrlChanged(browser, url);
        else if (hasBackup())
            backup.onUrlChanged(browser, url);
    }

    @Override
    public void onTitleChange(IBrowser browser, String title) {
    }

    @Override
    public void onTooltip(IBrowser browser, String text) {
    }

    @Override
    public void onStatusMessage(IBrowser browser, String value) {
    }

    @Override
    public boolean handleQuery(IBrowser b, long queryId, String query, boolean persistent, IJSQueryCallback cb) {
        if (b != null && query.equalsIgnoreCase("username")) {
            if (b.getURL().startsWith("mod://")) {
                // Only allow MCEF URLs to get the player's username to keep his identity secret

                mc.addScheduledTask(() -> {
                    // Add this to a scheduled task because this is NOT called from the main
                    // Minecraft thread...

                    try {
                        String name = mc.getSession().getUsername();
                        cb.success(name);
                    } catch (Throwable t) {
                        cb.failure(500, "Internal error.");
                        Log.warning("Could not get username from JavaScript:");
                        t.printStackTrace();
                    }
                });
            } else
                cb.failure(403, "Can't access username from external page");

            return true;
        }

        return false;
    }

    @Override
    public void cancelQuery(IBrowser b, long queryId) {
    }

    @SubscribeEvent
    public void onDrawHUD(RenderGameOverlayEvent.Post ev) {
        if (hudBrowser != null)
            hudBrowser.drawScreen(0, 0, 0.f);
    }
}

Have a good day !

GaoXanSheng commented 2 years ago

If you want to use the command to open webui, I have implemented a mod If you want to refer to Look => https://github.com/GaoXanSheng/WebUI

Raraph84 commented 2 years ago

Its good ! Thanks !