HalestormXV Posted February 20, 2018 Posted February 20, 2018 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. Quote
Terrails Posted February 20, 2018 Posted February 20, 2018 (edited) 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 February 20, 2018 by Terrails Quote
HalestormXV Posted February 20, 2018 Author Posted February 20, 2018 Alright, yeah that fixed the Lambda version, thank you for that. But I'm still curious as to why. Any thoughts on why that is needed for Lambda but not for Run? Quote
Terrails Posted February 20, 2018 Posted February 20, 2018 (edited) I'm not sure too. It looks like it doesn't care if the data is received on client when lamda is used and it tries to launch the code on server when registering even though its a client side packet. Edited February 20, 2018 by Terrails Quote
jabelar Posted February 21, 2018 Posted February 21, 2018 (edited) 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 February 21, 2018 by jabelar Quote Check out my tutorials here: http://jabelarminecraft.blogspot.com/
HalestormXV Posted February 22, 2018 Author Posted February 22, 2018 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. Quote
Recommended Posts
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.