r/badcode • u/[deleted] • Feb 04 '21
java Saw this one while editing Minecraft Classic's code
3
354
u/SIGSTACKFAULT Ba'al, the soul-eater Feb 04 '21
Maybe it's a jank way of implementing "if this.focus is null, Halt And Catch Fire"
113
u/TheSchred Feb 04 '21
*
this.canvas56
45
42
u/onthefence928 Feb 04 '21
annoyingly this codepath doesnt throw an error, so i divided by zero just to make sure
16
2
252
u/RavynousHunter Feb 04 '21
Minecraft is full of weirdness like that. I think a lot of it stems from the fact so much of it was made back when Notch was a wee babby developer and Minecraft was more of a hobby thing he just happened to put out there. Still, some of the shit I've seen Reika pull from Minecraft's code just leaves me utterly bewildered.
166
u/kodicraft4 Feb 04 '21
Minecraft is, from a technical standpoint, a hot steaming pile of garbage that barely works. I'm still surprised Mojang hasn't remade the entire game from scratch yet especially considering the fact that Microsoft bothered recreating Minecraft Bedrock from scratch.
141
u/SanianCreations Feb 04 '21
The technical minecraft community has very much grown attached to some of the bugs/quirks of the game that weren't really intentional in the first place. Take quasi connectivity for example - a bug where pistons can be powered diagonally even though redstone power isn't actually supposed to transfer diagonally. Currently some of those bugs are intentionally left in because the technical community would throw a fit (myself included) if they were removed.
Rewriting the game would probably get rid of those bugs unless purposefully reimplemented as features, and it would bring a whole new load of bugs along with it as well.
Bedrock is a prime example of this, it has arguably more bugs than Java edition (which is where it gets the nickname Bugrock), and for technical purposes it varies quite a bit from the main game. It doesn't have any of the bug/features java has (no quasi connectivity :( ), and even the way standard redstone works is in some cases randomised instead of consistent. For example: using pistons to pull a block in two directions at the same time, in Java the game will always prefer one side over the other which is conistent, in Bedrock the block will randomly choose a direction - this makes some contraptions completely unreliable. So in the case of Bedrock "rewriting the game from scratch" has pretty much failed. (it probably runs better though, I'll give it that)
It is known that Mojang strives to bring Java and Bedrock closer together so that they eventually share all features, the Java community is just not having that.
40
u/kodicraft4 Feb 04 '21
Oh, certainly. I wouldn't be happy myself if they were to iron out the cool bugs and I despise Bedrock for it, but the game technically doesn't even compare with games released years before it. I think Mojang needs to severely update the game's technical capabilities before focusing too much on content, though it's hard making money doing that.
1
u/Furry_69 Apr 27 '23
They've been doing that in the background, but I agree that it isn't enough. They need to devote an entire update to just refactoring massive parts of the game code, and then release a content update building off of that rewrite. (making sure that the good bugs/features stay in, which is easier said than done, of course)
47
u/Golden_Flame0 Feb 04 '21
They've done bits at a time, I know they've done stuff to the rendering pipeline, to the way the game saves and loads both blocks and items, and to bits of the animation.
31
u/kodicraft4 Feb 04 '21
As I see it, it doesn't seem like it helps the game that much. A lot of parts are reworked and are less compatible with their peers, so they're better individually but worse all together. For instance, the new rendering displays chunks better but at major FPS costs because of the way data is loaded.
40
u/Golden_Flame0 Feb 04 '21
In software engineering, there's a concept of a "Strangler Pattern" that you can use when you're upgrading things, especially critical infrastructure. You don't just trash the whole thing, you do bits at a time until you "strangle" out the old codebase. That way you can still use the system mid-upgrade, and you slowly start shedding technical debt (except for the stuff you need to translate between the new and the old, but it should be well marked and easy to clean up once you're done).
17
u/zesterer Feb 05 '21
Problem is, that's very hard to do when half of your codebase runs using a fundamental different execution model (Garbage-collected, running on top of the JVM and compiled, manually managed memory)
27
u/DenkJu Feb 04 '21
You answered your own question. For Microsoft, Bedrock edition is the future (it's also where they make by far the most profit). Java edition only still exists to satisfy the fans.
17
u/RavynousHunter Feb 05 '21
I can kinda see it from their perspective, at least nowadays, because Minecraft's over a decade old. That's an enormous amount of code to just up and give a complete redo from square one.
Also, if you think modders aren't annoyed enough with the current release cadence (and the fact most versions break at least some part of any mod worth a shit), then having to deal with code that's completely foreign to what they know? Oh heeeell no. MCP would basically be shot for a long time (weeks to months, at least) and Forge? Shit, it'd prolly take as long as MCP, if not longer.
Though, a massive refactor could go a long way towards making things both more performant and stable.
10
u/LegateLaurie Feb 05 '21
maybe they could add a modding API while they're at it, pretty please.
5
u/RavynousHunter Feb 05 '21
Dude, you know they'd only let you write mods in some bullshit like PHP or Javascript, lol.
2
u/LegateLaurie Feb 05 '21
Yeah, but even that would be a step forward
2
u/RavynousHunter Feb 05 '21
This is true. One would hope that, if they were to do so, they'd choose a language that doesn't suck, assuming they (for whatever reason) don't go with Java.
On a somewhat related note, I really wish ComputerCraft didn't use Lua...
2
u/LegateLaurie Feb 05 '21
I enjoy computercraft, because I did learn a new language out of it, (quite half arsedly, but still), but I do really wish it used a bit more complex a language
2
u/RavynousHunter Feb 06 '21
Likewise, though I'll admit most of my Lua knowledge fell out of my head since it just started rubbing me the wrong way. Like, why Lua? Why not something nice like Python? Something with some real object orientation that just looks nicer and is less aggravating with weird, special use operators that only serve as pointless, illegible line noise.
...Yeah, trying to remember all that crap kinda irritated me, can ya tell? :P
3
13
u/fanz0 Feb 04 '21
I think developers have said this before. It is no surprise when Notch was not expecting his game to attract a large audience and he made stuff that would barely work because "it just werks"
6
u/danbulant Feb 04 '21
Microsoft doesn't bother rewriting code from scratch to improve it. At least not faster than once per 20 years.
4
u/LoneFoxKK Feb 05 '21
Have you ever heard of minecraft bedrock? That shit runs on infinite render distance like it's nothing while java edition can barely handle 8 chunks on a gtx 1050 ti
2
u/tcilffAx Feb 11 '21
what minecraft are you running? I can get 16 chunk render distance on a 660 ti with 60+ frames. not that that matters...minecraft is way more reliant on the cpu unless you using shaders..
36
u/GamerNebulae Feb 04 '21
Do you maybe have a link where Reika talks about it?
4
u/RavynousHunter Feb 05 '21
Alas, it was mostly on his Teamspeak and/or Discord; neither of which I'm at liberty to give links. Or, at least, I ain't gonna take that liberty.
11
u/JonnyBoy522 Feb 04 '21
Yeah, that's why the key feature of damage camera tilting is still not fixed to today
2
u/FuryOfficial Feb 04 '21
Minecraft is open source? Where?
8
Feb 04 '21
Minecraft Classic's code has been published on GitHub
3
u/JamieMansfield Feb 05 '21
Well some deobfuscated and decompiled output has been published on GitHub yes, but it is not open-source.
96
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Feb 04 '21
Image Transcription: Code
this.hasMouse = true;
if(this.levelLoaded) {
try {
Mouse.setNativeCursor(this.cursor);
Mouse.setCursorPosition(this.width / 2, this.height / 2);
} catch (LWJGLException exception) {
exception.printStackTrace();
}
if(this.canvas == null) {
// How can you request focus from null?
// Notch is crazy
this.canvas.requestFocus();
}
} else {
Mouse.setGrabbed(true);
}
this.setCurrentScreen(null);
this.lastClick = this.ticks + 10000;I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!
29
11
u/slushie31 Feb 04 '21
Do you use OCR at all or write everything from scratch? You transcribers are amazing!
21
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Feb 04 '21
Thanks! <3
Wrote it all from scratch. We do have an OCR that attempts to transcribe the posts, but code's actually one of the things it's worst at. I don't know whether it's the indentation, the odd colours, or the jumble of symbols, but it just messes it up so often.
Some people use the OCR as a guide to their transcriptions (code or not); I personally prefer not to, though. I feel it's not much quicker for me in most circumstances, especially not with code.
8
u/slushie31 Feb 04 '21
Very interesting! I was thinking about using OCR as a guide and then making adjustments manually but if it's not quicker that obviously doesn't help. I wonder if converting the image to greyscale first would help OCR.
Anyways, thanks again for the info and the work you do!
21
21
Feb 04 '21
One of the funniest things I've seen in minecraft Bukkit code (An API used for making plugins on servers) is Bukkit.getOfflinePlayer() this function is used to get a player by their UUID however this function will always return an object even if there is no such thing as that player,
Directly from the javadoc
"This will return an object even if the player does not exist. To this method, all players will exist"
17
u/Squidy7 Feb 04 '21
Makes sense to me. I assume a player "doesn't exist" if they haven't been active in that server before, so there's no playerdata to read. However, you might still want to perform operations on a player with a given UUID (such as preemptively banning abusive players).
4
Feb 04 '21
Idk the class already has a function to get banned players it would be so much more useful it if just returned the player if the UUID is valid and null if it's not.
But I just have a work around that keeps track of a hashmap of a players name and their UUID if they have ever played on the server which just works better for me
2
u/FredL2 Feb 05 '21
This is most likely it. In some cases, returning some default values may be preferable, such as a Player with the given UUID and the name "Unknown Player" or the like.
56
u/MatthewRPG576 Feb 04 '21
I'm more interested in wtf LWJGLException stands for
163
u/goodoldwhoami Feb 04 '21
Lightweight Java Game Library Exception
88
u/MatthewRPG576 Feb 04 '21
Wait, did I just get a genuinely right answer? ON REDDIT?
69
u/botle Feb 04 '21
If you want to get a proper answer, don't ask a question, post the wrong answer.
And that's where r/badcode shines.
46
Feb 04 '21
Whats the difference between stack overflow and integer overflow??
"What a dumb question, google it." or "I recommend reading this book so you find out for yourself"
Buffer overflow and integer overflow are the same.
"Uhm... actually, they are different..." Proceeds to explain why.
34
u/ZedTT Feb 04 '21
Yes, this is called Poe's law.
Saying the wrong thing on the internet is more likely to get you the right answer than asking the question.
44
u/ZedTT Feb 04 '21
No it's not you idiot. That's Cunningham's law.
Poe's law says that it's impossible to tell extremism from parody once they reach a certain level of absurdity.
25
u/5p4n911 Feb 04 '21
Looks like it worked
ritual finished noises
21
u/ZedTT Feb 04 '21
Now look at the usernames
10
u/5p4n911 Feb 04 '21
How did you two glitched Reddit? /s
But seriously, I'm not sure if this makes it better or worse.
2
2
11
u/lppedd Feb 04 '21
You don't ask, just catch and shhhh
8
u/MatthewRPG576 Feb 04 '21
I think languages should implement an alternative catch for when you don't want to do anything with the Exception. Perhaps "try...ignore"
9
u/lppedd Feb 04 '21 edited Feb 04 '21
Well the JVM bytecode doesn't force catching checked exception, it's just the compiler. So, edit the compiler and take out the throws part of the method. You're done.
4
7
u/CommenceTheConfusion Feb 04 '21
You can just catch it and then not do anything in the catch block. I'm my opinion, having to ignore it explicitly like that is a good thing.
4
u/MatthewRPG576 Feb 04 '21 edited Feb 04 '21
Perhaps, but I was thinking like so:
try: [code that might throw] ignore (ExceptionTypeA) ignore (ExceptionTybeB) catch (ExceptionTypeC): [deal with that] finally: [whatever]Effectively just syntactical sugar
3
u/larsmaehlum Feb 04 '21
Why not just try? If there’s no catch, just keep going.
On error resume next?
18
u/Archiive Feb 04 '21
At least it's not, a single php file with over 40k lines of code, running inside a `while` loop, with any and all logic consisted of if/else trees, which then led to either more if/else trees or more loops. No function calls, no external libraries included.
12
6
Feb 05 '21
It's very easy. There's a catch block somewhere on the outside that catches the NullPointerException and initializes canvas on demand only when you signal the need by calling the missing method and then re-enters the method. I swear it's totally an optimization, bro. CPU go BRRR.
2
u/Radfordhound Feb 05 '21
Could it have been a typo? As in, he meant to write “if (canvas != null)”?
1
u/snzzare Feb 04 '21
What color theme is this?
18
u/dpelego Feb 04 '21
Looks like IntelliJ dark mode.
2
7
2
-3
u/Co0perat0r Feb 04 '21
If you look up different themes for IntelliJ there are so many better ones after looking at the default dark for hours it makes me wanna puke
0
u/TheRealAstroOrbis Feb 04 '21 edited Feb 10 '21
Not one to reopen old wounds, but isn’t that illegal? I know mods are a thing, and Forge/fabric is a mod loader, idk I’m confused about this someone enlighten me pls
Edit: I'm sorry if this offended anyone or some stuff I'm just actually curious and I saw debates kinda everywhere so I wanted to know what exactly was going on, why downvote :p
5
Feb 04 '21
Minecraft Classic's code has been published on GitHub
3
u/autisticCatnip Feb 05 '21
Looks more like someone just published a polished version of decompiler output.
2
7
u/TheGodOfThunder-THOR Feb 04 '21
Notch is a neonazi
3
u/NF-MIP Feb 05 '21
Wait, is he?
8
u/LegateLaurie Feb 05 '21
He's really bigoted and he's into Q shit. Mojang pulled all references to him in Minecraft recently.
4
1
10
Feb 04 '21
[removed] — view removed comment
6
3
u/Speedswiper Feb 05 '21
How would that be a static function call?
6
Feb 05 '21
[removed] — view removed comment
4
u/Speedswiper Feb 05 '21
Seriously? I can't believe I never knew that. Is there any actual use for it, or would it just be bad code anyways? Thanks for the new information :)
2
u/ZiggyTheHamster Feb 05 '21
null is a scalar, though, so I don't think you can add static members to it
6
u/EishLekker Feb 05 '21
A call to a static method is evaluated (is that the right word?) at compile time.
2
u/ZiggyTheHamster Feb 06 '21
Right, but you can't add static methods to sealed classes or scalars. So it would fail to compile if you tried. It's not like Ruby where you can just:
class Object def lol puts 'lol' end end 12345.lol 'test'.lol nil.lolSince this seems do be decompiled, I think what's actually happened is that the real code has a type expression which evaluates to null at compile/type erasure time and thus the expression is simplified and stupid.
1
u/EishLekker Feb 08 '21
I have no idea what you talk about now. No one is trying to add a static method to anything. The static method is already there. Here we are only talking about if it is possible to call that method on an null instance of that class. And that is possible, at least in Java (the language discussed in this thread).
1
u/ZiggyTheHamster Feb 09 '21
You can't have a null instance of a class though. You can have a method which was supposed to return
Thingbut returnednullinstead, which is then passed to a method expecting an instance ofThingbut that doesn't make it aThing, so a method call on it wouldn't work. This code would need a cast like((Thing)this.canvas).requestFocus()before it tried to call a static method onThing. Maybe the cast is implicit because of the property being declared as aThing, but in my experience it isn't.2
u/EishLekker Feb 12 '21
I still don't really understand what you are trying to say. What do you even mean with "You can't have a null instance of a class"? I never claimed that you can. But you can call a static method using a object reference that is null.
public class HelloWorld { public static void main(String []args) { HelloWorld helloWord = null; System.out.println(helloWord.test()); } public static String test() { return "IT'S WORKING!"; } }You can test that code online, here:
53
u/bobbyboys301 Feb 04 '21
where is this codebase?