Yes, yesterday I found an optimization for a function which caught me off guard with the indexing. I had to write a ridiculous one-liner to map the indexing but I felt it was necessary to explain why the mapping was the way it was.
I see f.setAccessible(true)above so it’s a Java Field accessed reflectively - which matches f.get(packet) (Field#get(ObjToGetTheFieldValieFrom) - and that returns Object, the generic superclass for everything in Java.
And probably ending up changing it and absolutely braking it 😂😂😂 because it was forgotten the first reason why you did a quirk thing in the first place
That's what I thought. Might also be octal. I've had to fight that on shell scripts, avoiding a month like 08 from being interpreted as octal, and crashing
I would guess it’s a weird way of removing leading zeroes
EDIT: Never mind, thought the toString() was outside of the parentheses. I have no explanation for this unless the result of "f.get(packet)" doesn't return an integer already lol
Almost certainly returns Object - I see f.setAccessible(true)above so it’s a Java Field accessed reflectively - which matches f.get(packet)Field#get(ObjToGetTheFieldValieFrom - and that returns Object, the generic superclass for everything in Java.
npc.getId() is an integer. They're parsing a value they get from a Java Reflections Field instance and compare the result to npc.getId(). So no, that would not work.
it seems like they're using reflections in their code with the f.get(), reflections always have the return type object no matter the type of the reflected field. One could just type cast it to int tho
If it seem stupid but it works it aint stupid, take time if you can rework it and test it at some point in the future, maybe add a comment in the code for your colleagues and everthing is fine ^^
Almost certainly returns Object - I see f.setAccessible(true)above so it’s a Java Field accessed reflectively - which matches f.get(packet)Field#get(ObjToGetTheFieldValieFrom - and that returns Object, the generic superclass for everything in Java.
Almost certainly returns Object - I see f.setAccessible(true)above so it’s a Java Field accessed reflectively - which matches f.get(packet) (Field#get(ObjToGetTheFieldValieFrom) - and that returns Object, the generic superclass for everything in Java.
All these webdevs think this is bad code but this looks like backend for a game server. A packet of binary data was sent from the player which was serialized as a string which is an unknown length of bytes(as opposed to an int which is 4bytes). Then after the binary data has been converted into a string he can then finally turn it into an int. Op probably just got hired and thinks he is a leet coder… but tbh I don’t know why the person programming the packets on the other end is sending numbers as string bytes???
Oddly enough my teacher at university told us to use exclusively standardized text based protocols because binary encoding are just to easy to mess up and create vulnerabilities.
Yeah, this might work for the simplest of multiplayer games but this idea quickly falls apart when latency and bandwidth become an issue. Especially when you need to send a lot of very small messages very fast, or encode data extremely densely using bit packing.
The „setAccessible(true)“ looks like the worst thing here for me. Looks like you’re setting private properties to public to directly access their values. Your software architect will thank you a lot
it's not the only way!!! use paperweight userdev and you can code against mojang-mapped NMS code, and you can even set it up to work cross-version like I do in https://github.com/Auxilor/eco
I really don't care, if you want to use whataboutism to justify it, you do you.
I'm just saying there are probably a lot of players that would like to use your stuff, but don't want to downgrade, especially since other mods they like only exist for newer versions.
I use viaversion and viabackward plugins, means people can join from any minecraft version to 1.12.2 server - no need for them to downgrade their mc version
What is going on there? And why there are double parenthesis lol. Also the f.get() method, looking at that comparison implies it is returning a numerical ID value, why parsing it to a string to than an integer again. That’s just orribile and confusing. that getter method should have a name that self explains what is returning, and why a getter is also getting a parameter or TCP packet type I assume. OP explain what is going on there 😂
I get it returns a object. The point is to name the getter to something meaningful. Like getID() or getIpPacket() etc. The toString() is actually pretty straightforward, but not in that context. Because if that f is instance of some class of yours, you could simple add a method called getID() Than it would ended up with something straight forward as if (f.getID() == ncp.getID) { ….. }
I once inherited a codebase in C#, where all parameters to all methods were always passed as strings. Converted to strings when calling, then parsed back inside each method. That was, uhm, curious
Must have been a conversion of some python php Javascript or other text based code to c# and they did it that way to be able to copy paste the code with minimal changes or considerations for how a method uses another
There's even another layer to this; they're using Java Reflections to access a private field on an object, and then use Object get(Object) instead of Integer getInt(Object).
The only difference is the type of exception thrown if the underlying type is not integer - and this may be the reason, doing it like in OPs picture will give a NumberFormatException while doing it with the getInt() will give an IllegalAccessException. The latter is less readable in logs.
198
u/lets_eat_bees Mar 20 '23
It might be a way to achieve normalization. It may remove '+' sign from the front, for example.
47
u/[deleted] Mar 20 '23
There's always a reason. The reason may be ignorance, but could be to attempt validation or to normalise the input before comparing to npc.getId()?
If you scroll up a bit, is there a comment // LEAVE THIS ALONE IT WORKS THIS WAY (dunno why)? 😀
7
u/Shower_Handel Mar 20 '23
Why do I feel like this was written at 3 am
3
u/AeolinFerjuennoz Mar 20 '23
it seems like they're using reflections in their code with the f.get(), reflections always have the return type object no matter the type of the reflected field. One could just type cast it to int tho
101
u/indentuum Mar 20 '23
Since get() is used instead of getInt(), maybe it’s not an int field and toString() returns string with needed value
98
u/shizzy0 Mar 20 '23
“This Integer class, what do you think it parses?”
“Dunno. Could be anything?”
“Good point. Better name it parseInt so no one gets confused.”
102
u/NikZM Mar 20 '23
Honestly im more annoyed by the extra parentheses
13
u/FLUX51 Mar 20 '23
“Just to be sure. ”
9
u/TallBoyPirateSqaud Mar 20 '23
All these webdevs think this is bad code but this looks like backend for a game server. A packet of binary data was sent from the player which was serialized as a string which is an unknown length of bytes(as opposed to an int which is 4bytes). Then after the binary data has been converted into a string he can then finally turn it into an int. Op probably just got hired and thinks he is a leet coder… but tbh I don’t know why the person programming the packets on the other end is sending numbers as string bytes???
1
u/[deleted] Mar 20 '23
"if it works, it works"
1
u/TorbenKoehn Mar 20 '23
The „setAccessible(true)“ looks like the worst thing here for me. Looks like you’re setting private properties to public to directly access their values. Your software architect will thank you a lot
1
u/NickSicilianu Mar 20 '23
What is going on there? And why there are double parenthesis lol. Also the f.get() method, looking at that comparison implies it is returning a numerical ID value, why parsing it to a string to than an integer again. That’s just orribile and confusing. that getter method should have a name that self explains what is returning, and why a getter is also getting a parameter or TCP packet type I assume. OP explain what is going on there 😂
3
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Mar 20 '23
Image Transcription: Code
[In the following code, both
Integer.parseIntand.toString()are underlined in red:]I'm a human volunteer content transcriber and you could be too! If you'd like more information on what we do and why we do it, click here!
3
u/trutheality Mar 20 '23
Maybe useless, or maybe
f.get(packet)doesn't return anint.2
u/LavenderDay3544 Mar 20 '23
Typical Java
1
u/brjukva Mar 20 '23
I once inherited a codebase in C#, where all parameters to all methods were always passed as strings. Converted to strings when calling, then parsed back inside each method. That was, uhm, curious
1
u/SirFireball Mar 20 '23
I have written
if x == str(int(x))before, this might be similar. Bad still, but similar1
u/Synonysis Mar 20 '23
Parkour
1
u/Rasta_Dev Mar 21 '23
Isn't it all about ID? Should've been a custom type for that. And that's why folks you need to use typescript.
-2
u/TheZipCreator Mar 20 '23
wouldn't this not work anyway since it's
==on a string?