Jump to content

NoSuchMethodError: Minecraft.getMinecraft()


Jimmeh

Recommended Posts

Hi there!

 

I'm jumping into Forge modding for the second time (I didn't last long the first after coming from using Spigot for a few years). All I'm trying to do right now is for the server to send a packet to the player that runs a command. This packet will tell the client to open a GUI. Now, I have the network part of this down, or at least, there's no errors coming from that. The error pops up when I'm trying to call 

Minecraft.getMinecraft().displayGuiScreen(new myGui());

I call this in my implementation of IMessageHandler, making sure that I have registered this handler to be receiving packets on the client side.I have no idea as to why the above code is throwing the NoSuchMethodError when it's running clientside. My only guess is that it has to do with run-time classpaths being different than compile-time, but I have a shadow plugin I'm using with Gradle to shade in my dependencies. Any help is greatly appreciated!

 

Here's where I'm registering my IMessage and IMessageHandler classes:

public class AureliosPacketHandler {

    public static final SimpleNetworkWrapper INSTANCE = NetworkRegistry.INSTANCE.newSimpleChannel("aurelios");
    private static int id = -1;

    public static void init() {
        INSTANCE.registerMessage(PacketOpenGui.GuiPacketHandler.class, PacketOpenGui.class, id++, Side.CLIENT);
    }
}

 

And here's the classes themselves:

public class PacketOpenGui implements IMessage {

    private GuiTypes guiType;

    public PacketOpenGui(){}

    public PacketOpenGui(GuiTypes type){
        this.guiType = type;
    }

    @Override
    public void fromBytes(ByteBuf buf) {
        guiType = GuiTypes.getGuiType(buf.readInt()).get();
    }

    @Override
    public void toBytes(ByteBuf buf) {
        buf.writeInt(guiType.getID());
    }

    public static class GuiPacketHandler implements IMessageHandler<PacketOpenGui, IMessage> {

        @Override
        public IMessage onMessage(PacketOpenGui message, MessageContext ctx) {
            Minecraft.getMinecraft().displayGuiScreen(message.guiType.getGui());
            return null;
        }
    }
}

For reference, GuiTypes is simply an Enum to make things easier on me down the road. Again, thanks for any help! :D

Link to comment
Share on other sites

Yes, basically your suspicion about run-time class loading is correct. The Minecraft class is not loaded on the server side, but your PackeOpenGUI class is loaded on both sides.

 

What you need to do is use your proxy. You create a method called something like openGui() in your proxy but in the server proxy class you have it do nothing and in the client proxy class you have it call the Minecraft.getMinecraft.displayGuiScreen().

 

Basically you need to collect all your code that is affected by sided loading into the proxy.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

Or use EntityPlayer.openGui

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

2 minutes ago, diesieben07 said:

Only relevant for GUIs that have a server-side component (Container).

Ok, fair point

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

13 hours ago, jabelar said:

Yes, basically your suspicion about run-time class loading is correct. The Minecraft class is not loaded on the server side, but your PackeOpenGUI class is loaded on both sides.

 

What you need to do is use your proxy. You create a method called something like openGui() in your proxy but in the server proxy class you have it do nothing and in the client proxy class you have it call the Minecraft.getMinecraft.displayGuiScreen().

 

Basically you need to collect all your code that is affected by sided loading into the proxy.

Okay, this makes sense! However, it's looking like my proxies aren't getting instantiated even though I'm using the annotation. I believe this to be so because I get a NullPointerException when trying to call "proxy.preInit()". I've watched a few tutorials for this and I'm not doing anything different from them. If you have a moment, could you take a look and see if there's something wrong that catches your eye? Thanks!

 

@SidedProxy(clientSide = "com.aurelios.client.ClientProxy", serverSide = "com.aurelios.server.ServerProxy")
    public static IProxy proxy;

    @Listener
    public void onGamePreInit(GamePreInitializationEvent event){
        AureliosPacketHandler.init();

        proxy.preInit();
    }

This is a SpongeForge mod (hence why I'm using GamePreInitializationEvent). For clarity, I did cast GamePreInitializationEvent to FMLPreInitializationEvent and the cast didn't fail, so I think it's safe to assume they are the same thing (or rather an extension of the FML event). And attached is my project structure, so that you can see I have the annotation pointing to the correct classes.

 

Also, I just noticed your name. If you're the Jabelar that wrote the tutorials on Blogspot, thanks for doing that! I have everyone of those bookmarked. They have and definitely will continue to come in handy!

structure.png

Edited by Jimmeh
Link to comment
Share on other sites

I don't really think the GamePreIntializationEvent is an extention of FMLPreInitializationEvent because the source for GamePreIntializationEvent shows that it extends GameEvent which extends Event (and this Event class is a Sponge-specific one).

 

I'm guessing that somehow this event is posted before the proxy annotation is processed, because otherwise the value of proxy should have been properly injected. Or do you get any warning in your console log like a class not found related to the proxy?

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

9 hours ago, jabelar said:

I don't really think the GamePreIntializationEvent is an extention of FMLPreInitializationEvent because the source for GamePreIntializationEvent shows that it extends GameEvent which extends Event (and this Event class is a Sponge-specific one).

 

I'm guessing that somehow this event is posted before the proxy annotation is processed, because otherwise the value of proxy should have been properly injected. Or do you get any warning in your console log like a class not found related to the proxy?

Your assumption was correct. I should've noticed that about the Sponge event. It works just fine using the FML events. Well, that'll do for my questions right now. Thanks for all the help! It's appreciated!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.