Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[1.15.1] Questions regarding class loading


DavidM
 Share

Recommended Posts

I previously thought that class loading only occurs when a class is referenced anywhere as long as the class it is referenced is loaded.

However, I came across this post today:

and if I understood correctly, casting would trigger class loading.

However, I am slightly confused by this:

1 hour ago, diesieben07 said:

Because Minecraft#player is of a different type (ClientPlayerEntity) than PlayerEntity this triggers classloading (the JVM needs to load both classes to verify they are compatible). To make the code work you need to adjust your variable player to be the same type.

Even if OP changed the type of player to ClientPlayerEntity, wouldn't that still cause the loading of the class?

Similarly, the OP in the linked post called Minecraft::getMinecraft in his packet code, which is loaded on both sides. Despite the method being called in enqueued work, would this still cause a crash when loaded on a server?

Some tips:

Spoiler

Modder Support:

Spoiler

1. Do not follow tutorials on YouTube, especially TechnoVision (previously called Loremaster) and HarryTalks, due to their promotion of bad practice and usage of outdated code.

2. Always post your code.

3. Never copy and paste code. You won't learn anything from doing that.

4. 

Quote

Programming via Eclipse's hotfixes will get you nowhere

5. Learn to use your IDE, especially the debugger.

6.

Quote

The "picture that's worth 1000 words" only works if there's an obvious problem or a freehand red circle around it.

Support & Bug Reports:

Spoiler

1. Read the EAQ before asking for help. Remember to provide the appropriate log(s).

2. Versions below 1.11 are no longer supported due to their age. Update to a modern version of Minecraft to receive support.

 

 

Link to comment
Share on other sites

Take this example:

class Foo {
   SomeClass methodInFoo()
}


class Bar {
    void methodInBar() {
        SomeClass value = new Foo().methodInFoo();
    }
}

 

If Bar is loaded, Foo and SomeClass are not immediately loaded, because they are only referenced in method bodies.

However, the JVM needs to verify that Bar's bytecode is correct. This includes verifying that all assignments are correct.

In the example this is trivial, because the return type for methodInFoo is SomeClass and it's being assigned to SomeClass. This is known without loading Foo or SomeClass, because methodInFoo's signature (return type and parameters) are part of the call bytecode in Bar. Therefor loading of Bar does not trigger loading of Foo or SomeClass.

 

If however we change methodInBar as follows:

void methodInBar() {
    SomeOtherClass value = new Foo().methodInFoo();
}

Now the verification that the assignment is correct is no longer trivial. We need to check if SomeClass (return type of methodInFoo) is assignable to SomeOtherClass. To do this, we need to load both SomeClass and SomeOtherClass. Therefor loading of Bar now triggers loading of SomeClass and SomeOtherClass.

 

How does this relate to DistExecutor? DistExecutor relies on lambdas to isolate the code. Lambdas are however desugared into static methods of their containing class. So, if you have an assignment in the lambda which assignes one type to another, verification of the containing class will trigger loading of the classes mentioned in that assignment (even if it happens inside the lambda, inside the bytecode a lambda is just a static method).

This is why DistExecutor is not sufficient to properly isolate client-only code.

  • Thanks 1
Link to comment
Share on other sites

  • Thanks 1

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Link to comment
Share on other sites

Thank you for the explaination. I was previously confused about the difference between a reference to a class and the loading of a class.

 

Does this mean that the only appropiate place to load client-only classes in modern versions of Minecraft is client-only event bus subscribers?

Some tips:

Spoiler

Modder Support:

Spoiler

1. Do not follow tutorials on YouTube, especially TechnoVision (previously called Loremaster) and HarryTalks, due to their promotion of bad practice and usage of outdated code.

2. Always post your code.

3. Never copy and paste code. You won't learn anything from doing that.

4. 

Quote

Programming via Eclipse's hotfixes will get you nowhere

5. Learn to use your IDE, especially the debugger.

6.

Quote

The "picture that's worth 1000 words" only works if there's an obvious problem or a freehand red circle around it.

Support & Bug Reports:

Spoiler

1. Read the EAQ before asking for help. Remember to provide the appropriate log(s).

2. Versions below 1.11 are no longer supported due to their age. Update to a modern version of Minecraft to receive support.

 

 

Link to comment
Share on other sites

Just now, Cadiboo said:

Yay, even more stupid ASM hacks, just so we don't need to admit that it was a bad idea and bring back @SidedProxy.

Link to comment
Share on other sites

Just now, DavidM said:

Does this mean that the only appropiate place to load client-only classes in modern versions of Minecraft is client-only event bus subscribers?

In all versions client only code should be accessed exclusively from other client only code. In most circumstances though it will not crash.

  • Thanks 1

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Link to comment
Share on other sites

4 minutes ago, Cadiboo said:

Thanks for the tip. However I am concerned that this might be concerned hacky (although I am not aware of the direct consequences) due to the direct deletion of part of the class on certain side to avoid class loading.

Edited by DavidM

Some tips:

Spoiler

Modder Support:

Spoiler

1. Do not follow tutorials on YouTube, especially TechnoVision (previously called Loremaster) and HarryTalks, due to their promotion of bad practice and usage of outdated code.

2. Always post your code.

3. Never copy and paste code. You won't learn anything from doing that.

4. 

Quote

Programming via Eclipse's hotfixes will get you nowhere

5. Learn to use your IDE, especially the debugger.

6.

Quote

The "picture that's worth 1000 words" only works if there's an obvious problem or a freehand red circle around it.

Support & Bug Reports:

Spoiler

1. Read the EAQ before asking for help. Remember to provide the appropriate log(s).

2. Versions below 1.11 are no longer supported due to their age. Update to a modern version of Minecraft to receive support.

 

 

Link to comment
Share on other sites

3 minutes ago, diesieben07 said:

Yay, even more stupid ASM hacks, just so we don't need to admit that it was a bad idea and bring back @SidedProxy.

@SidedProxy had its own issues. It really is best to not stringly type your code and let the compiler do its work. I agree that it is a bit of a dirty hack though.

Edited by Cadiboo

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Link to comment
Share on other sites

6 minutes ago, Cadiboo said:

I agree that it is a bit of a dirty hack though.

It's hacks upon more dirty hacks.

  • It relies on javac internals about how lambdas are desugared.
  • It relies on internals about how lambdas are turned into real objects at runtime.
  • It relies on internals about how the JVM loads and verifies classes (lord behold someone doesn't do the "same class" optimization during verification that I described above... suddenly this stuff no longer works).

This is much more hacky than @SidedProxy, which relies on clearly defined things. Yes, of course it's stringly typed (not even that, you can omit the class names and follow a naming convention), but thats the only way to properly prevent the JVM from maybe loading your class.

In 90% of cases @EventBusSubscriber works fine. But @SidedProxy is needed as an escape hatch.

Link to comment
Share on other sites

1 hour ago, diesieben07 said:

(lord behold someone doesn't do the "same class" optimization during verification that I described above... suddenly this stuff no longer works).

Quote

...remember to check the hundreds of things you know to see if they’ve been updated or broken and make sure they all still work together and that nobody fixed the bug in one of them that you exploited to do something you thought was really clever one weekend when you were drunk. You’re all up to date, so that’s cool, then everything breaks.

“Double you tee eff?” you say, and start hunting for the problem. You discover that one day, some idiot decided that since another idiot decided that 1/0 should equal infinity, they could just use that as a shorthand for “Infinity” when simplifying their code. Then a non-idiot rightly decided that this was idiotic, which is what the original idiot should have decided, but since he didn’t, the non-idiot decided to be a dick and make this a failing error in his new compiler. Then he decided he wasn’t going to tell anyone that this was an error, because he’s a dick, and now all your snowflakes are urine and you can’t even find the cat.

Still Drinking

  • Like 2

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

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
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.

 Share



×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.