BuiltBrokenModding / SBM-SheepMetal

Sheep that produce metal wool
MIT License
0 stars 1 forks source link

Mod compat issues with AWT (java.awt.Color) in 1.14.x #2

Open comp500 opened 5 years ago

comp500 commented 5 years ago

I've found that a mod I recently ported to 1.14, Screenshot to Clipboard, doesn't work properly when run with your mod.

This is because in order to copy a BufferedImage to the clipboard in my mod, I have to use the AWT toolkit. However, due to the switch to LWJGL3 (and GLFW), Minecraft disables the parts of AWT that interact with native code (by setting the property java.awt.headless to true), described in more detail here. In order to use the system clipboard, as GLFW only supports copying text to the clipboard, my mod sets the java.awt.headless property back to false in it's mod class constructor, if the system is not macOS (as the issue that caused it to be set to true only occurs on macOS). On macOS, my mod has a seperate method for copying the image that is specific to macOS.

For this modification to work, my mod must set the java.awt.headless property before anything touches AWT, even the parts that work when the property is enabled, because AWT classes statically load the native libraries. Your mod is using AWT for java.awt.Color, which causes the AWT classes to be loaded before the property has been changed, as this class is used in SheepTypes which is used by your mod's configuration, loaded in your mod's constructor. Due to the parallel loading introduced in 1.13/1.14 Forge, this is not consistent, and sometimes my mod will load first and set the property.

I don't know of any way to run code in my mod any earlier than the class constructor, apart from a coremod, so this will have to be fixed in your mod. I am willing to submit a PR if needed.

There are several ways I think this could be solved:

Of these, I think the last method would be the easiest and most compatible way to accomplish this. I would like your opinion on this before submitting a PR, if you don't want to do this yourself.

DarkGuardsman commented 5 years ago

I'll take a look later this week, but this seems like its going to be a problem down the road with other mods. From what I read in the link you included java swing is also an issue. We use this a lot for test and support systems that are shipped with the mods.

comp500 commented 5 years ago

If you're only using Windows or Linux, or if you're only using them on the server, Swing and AWT should work fine, but you will need to ensure java.awt.headless is set to false before you use them. Just make sure not to call those classes, or change java.awt.headless, on macOS - although that will be an issue if you want to run your test/support systems on macOS. I suspect this will be an issue for a lot of mods, as AWT is very commonly used.

DarkGuardsman commented 5 years ago

Ya very likely would be, might want to look at different ways to build out your mod to avoid the problem. Since even if I fix this issue it looks like there will be no ensured way to avoid compatibility problems long term. You might be able to ASM an injection point in before the modloading phase.

comp500 commented 5 years ago

Yeah that's a good idea, I'll probably do that.

comp500 commented 5 years ago

I've added a coremod that injects my setProperty call, and it has fixed the issue. I'm going to close this issue now, but if you want to use it to remember to change from using AWT/Swing you can reopen it.

DarkGuardsman commented 5 years ago

Ya I'm going to remove awt support either way since its no longer supported by MC or forge