Jump to content

[1.12.2] Lambda Expression Crashes - Overriding Run Doesn't?


HalestormXV

Recommended Posts

So I am using 1.8 and fiddling with Lambda where and when I can as it is cleaner and I happen to like it more. So I have a nice little packet that works fine when doing it this way:

 

https://pastebin.com/Q0u1JmLJ (Yes this is just a snip of the code, as the code isn't really at issue)

https://pastebin.com/sEezYdRu (This is the Lambda Conversion)

 

Now I should note that I am using IntelliJ which I love and does that conversion for me and I've never once had an issue with it converting anything to Lambda. So the question is really, when it converts to Lambda is it getting rid of something that is needed or is this just something else? Forgive the ignorance as I am just starting to use Lambda and also just starting to realize just how powerful IntelliJ is, as I've always used Eclipse. 

 

Here is the crash that generates when it is in Lambda form: https://pastebin.com/WcR6316z

 

For now I am keeping it in its original format because like I said, it works beautifully when it is in the original format.

Link to comment
Share on other sites

Had this issue too. I fixed it by adding a method to my proxy called getPlayer() with which I return null in server proxy and Minecraft.getMinecraft.player in client proxy. After that do something like this:

            FMLCommonHandler.instance().getWorldThread(ctx.netHandler).addScheduledTask(() -> {
                EntityPlayer player = MainClass.proxy.getPlayer();
                if (player != null) {
                    World world = player.getEntityWorld();
                }
            });

 

Edited by Terrails
Link to comment
Share on other sites

Yeah, this has been an issue for a long time with packet handling. I'm also not sure the explanation but isn't really a problem with your Lamba but can affect people doing non-Lambda as well.

 

The code I actually use (I think it may be more comprehensive than Terrails example) in client proxy is:

    public EntityPlayer getPlayerEntityFromContext(MessageContext ctx)
    {
        // Note that if you simply return 'Minecraft.getMinecraft().thePlayer',
        // your packets will not work because you will be getting a client
        // player even when you are on the server! Sounds absurd, but it's true.

        // Solution is to double-check side before returning the player:
        return (ctx.side.isClient() ? Minecraft.getMinecraft().player : ctx.getServerHandler().player);
    }

 

And in my server proxy it is:

    public EntityPlayer getPlayerEntityFromContext(MessageContext ctx)
    {
        return ctx.getServerHandler().player;
    }
 

 

By the way, I know it is cool to have your IDE convert code to lamba, but in my opinion lamba only makes sense if you "think natively" in lamba and code that way from the start. If you're coding non-lamba and then getting your IDE to convert you're not really doing lamba (yet). But it is good for learning I guess.


Also Eclipse can convert to lambda too, although maybe IntelliJ does something more with it. Like you can use the quick assist "migrate anonymous creations to lamba expressions" or you can have it do code cleanup with the "convert functional instances".

Edited by jabelar

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

Link to comment
Share on other sites

Spoiler

 

 

Gotcha and in the server proxy return ctx.getServerHandler().player; will return what exactly? Will it still be null? Will it return EntityPlayerMP? Sorry if it sounds dumb but I want to actually learn it lol not just get answers. I get what the client side one will do.

 

And yes, although it is really cool to have your IDE auto-convert things to lambda you still need to do the legwork to learn it. I am one who learns by example, so when I see the code in front of me and see what it spits out I can take it apart and understand it.

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.