Jump to content

[1.10.2] MalformedJsonException with valid JSON in lang file


Flawedspirit

Recommended Posts

I'm trying to print strings derived from some JSON in my en_US.lang file to notify the player about updates to a mod. However, when the event triggers, the game CTDs and prints a rather non-helpful error:

 

 

 

---- Minecraft Crash Report ----
// Shall we play a game?

Time: 12/24/16 5:17 PM
Description: Unexpected error

com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1
at com.google.gson.internal.Streams.parse(Streams.java:56)
at com.google.gson.TreeTypeAdapter.read(TreeTypeAdapter.java:54)
at net.minecraft.util.JsonUtils.gsonDeserialize(JsonUtils.java:453)
at net.minecraft.util.JsonUtils.gsonDeserialize(JsonUtils.java:468)
at net.minecraft.util.text.ITextComponent$Serializer.jsonToComponent(ITextComponent.java:279)
at com.flawedspirit.sandboxmod.util.UpdateChecker.onTick(UpdateChecker.java:66)
at net.minecraftforge.fml.common.eventhandler.ASMEventHandler_6_UpdateChecker_onTick_ClientTickEvent.invoke(.dynamic)
at net.minecraftforge.fml.common.eventhandler.ASMEventHandler.invoke(ASMEventHandler.java:90)
at net.minecraftforge.fml.common.eventhandler.EventBus.post(EventBus.java:185)
at net.minecraftforge.fml.common.FMLCommonHandler.onPostClientTick(FMLCommonHandler.java:344)
at net.minecraft.client.Minecraft.runTick(Minecraft.java:1948)
at net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:1118)
at net.minecraft.client.Minecraft.run(Minecraft.java:406)
at net.minecraft.client.main.Main.main(Main.java:118)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at net.minecraft.launchwrapper.Launch.launch(Launch.java:135)
at net.minecraft.launchwrapper.Launch.main(Launch.java:28)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at net.minecraftforge.gradle.GradleStartCommon.launch(GradleStartCommon.java:97)
at GradleStart.main(GradleStart.java:26)
Caused by: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1
at com.google.gson.stream.JsonReader.syntaxError(JsonReader.java:1505)
at com.google.gson.stream.JsonReader.checkLenient(JsonReader.java:1386)
at com.google.gson.stream.JsonReader.doPeek(JsonReader.java:572)
at com.google.gson.stream.JsonReader.peek(JsonReader.java:414)
at com.google.gson.internal.Streams.parse(Streams.java:42)
... 25 more


A detailed walkthrough of the error, its code path and all known details is as follows:
---------------------------------------------------------------------------------------

-- Head --
Thread: Client thread
Stacktrace:
at com.google.gson.internal.Streams.parse(Streams.java:56)
at com.google.gson.TreeTypeAdapter.read(TreeTypeAdapter.java:54)
at net.minecraft.util.JsonUtils.gsonDeserialize(JsonUtils.java:453)
at net.minecraft.util.JsonUtils.gsonDeserialize(JsonUtils.java:468)
at net.minecraft.util.text.ITextComponent$Serializer.jsonToComponent(ITextComponent.java:279)
at com.flawedspirit.sandboxmod.util.UpdateChecker.onTick(UpdateChecker.java:66)
at net.minecraftforge.fml.common.eventhandler.ASMEventHandler_6_UpdateChecker_onTick_ClientTickEvent.invoke(.dynamic)
at net.minecraftforge.fml.common.eventhandler.ASMEventHandler.invoke(ASMEventHandler.java:90)
at net.minecraftforge.fml.common.eventhandler.EventBus.post(EventBus.java:185)
at net.minecraftforge.fml.common.FMLCommonHandler.onPostClientTick(FMLCommonHandler.java:344)

-- Affected level --
Details:
Level name: MpServer
All players: 1 total; [EntityPlayerSP['Player437'/245, l='MpServer', x=-301.14, y=63.00, z=-597.66]]
Chunk stats: MultiplayerChunkCache: 625, 625
Level seed: 0
Level generator: ID 00 - default, ver 1. Features enabled: false
Level generator options: 
Level spawn location: World: (105,64,-40), Chunk: (at 9,4,8 in 6,-3; contains blocks 96,0,-48 to 111,255,-33), Region: (0,-1; contains chunks 0,-32 to 31,-1, blocks 0,0,-512 to 511,255,-1)
Level time: 1157961 game time, 13000 day time
Level dimension: 0
Level storage version: 0x00000 - Unknown?
Level weather: Rain time: 0 (now: false), thunder time: 0 (now: false)
Level game mode: Game mode: creative (ID 1). Hardcore: false. Cheats: false
Forced entities: 85 total; [EntitySheep['Sheep'/384, l='MpServer', x=-246.77, y=64.00, z=-519.50], EntityRabbit['Rabbit'/398, l='MpServer', x=-252.89, y=63.00, z=-674.95], EntityRabbit['Rabbit'/399, l='MpServer', x=-281.79, y=63.00, z=-653.79], EntityRabbit['Rabbit'/400, l='MpServer', x=-275.44, y=64.92, z=-643.50], EntityRabbit['Rabbit'/401, l='MpServer', x=-265.16, y=64.00, z=-663.97], EntityCreeper['Creeper'/403, l='MpServer', x=-298.23, y=24.00, z=-652.57], EntityRabbit['Rabbit'/404, l='MpServer', x=-297.99, y=66.00, z=-649.04], EntityRabbit['Rabbit'/405, l='MpServer', x=-291.55, y=66.00, z=-645.51], EntityRabbit['Rabbit'/406, l='MpServer', x=-248.43, y=64.00, z=-655.19], EntityZombie['Zombie'/410, l='MpServer', x=-250.16, y=45.00, z=-667.33], EntityRabbit['Rabbit'/411, l='MpServer', x=-314.51, y=63.00, z=-540.92], EntitySkeleton['Skeleton'/413, l='MpServer', x=-223.50, y=36.00, z=-644.50], EntityCreeper['Creeper'/416, l='MpServer', x=-229.46, y=26.00, z=-623.32], EntityBat['Bat'/417, l='MpServer', x=-276.75, y=41.05, z=-581.39], EntityBat['Bat'/418, l='MpServer', x=-276.75, y=41.03, z=-581.25], EntityZombie['Zombie'/419, l='MpServer', x=-265.50, y=33.00, z=-602.50], EntitySpider['Spider'/420, l='MpServer', x=-269.50, y=33.00, z=-601.50], EntitySpider['Spider'/421, l='MpServer', x=-271.99, y=33.00, z=-602.99], EntityPlayerSP['Player437'/245, l='MpServer', x=-301.14, y=63.00, z=-597.66], EntityZombie['Zombie'/422, l='MpServer', x=-269.50, y=33.00, z=-603.50], EntityCreeper['Creeper'/423, l='MpServer', x=-269.50, y=33.00, z=-604.50], EntityCreeper['Creeper'/424, l='MpServer', x=-266.50, y=33.00, z=-603.50], EntityCreeper['Creeper'/425, l='MpServer', x=-265.50, y=33.00, z=-602.50], EntityBat['Bat'/430, l='MpServer', x=-224.81, y=30.93, z=-659.97], EntitySkeleton['Skeleton'/431, l='MpServer', x=-245.50, y=48.00, z=-639.50], EntityZombie['Zombie'/432, l='MpServer', x=-233.50, y=36.00, z=-653.50], EntityBat['Bat'/305, l='MpServer', x=-367.53, y=44.33, z=-537.45], EntityCreeper['Creeper'/433, l='MpServer', x=-239.50, y=48.00, z=-646.50], EntitySkeleton['Skeleton'/434, l='MpServer', x=-281.30, y=45.00, z=-602.30], EntityCreeper['Creeper'/435, l='MpServer', x=-283.09, y=45.00, z=-603.04], EntityCreeper['Creeper'/436, l='MpServer', x=-279.93, y=45.00, z=-604.51], EntityBat['Bat'/309, l='MpServer', x=-360.50, y=27.10, z=-567.25], EntitySkeleton['Skeleton'/437, l='MpServer', x=-285.15, y=44.00, z=-596.19], EntityZombie['Zombie'/438, l='MpServer', x=-277.30, y=45.00, z=-605.08], EntityZombie['Zombie'/439, l='MpServer', x=-286.20, y=45.00, z=-595.49], EntityZombie['Zombie'/440, l='MpServer', x=-284.70, y=45.00, z=-604.32], EntityZombie['Zombie'/441, l='MpServer', x=-279.97, y=45.00, z=-605.49], EntityCreeper['Creeper'/442, l='MpServer', x=-285.77, y=45.00, z=-597.04], EntityRabbit['Rabbit'/315, l='MpServer', x=-336.48, y=65.28, z=-610.04], EntityCreeper['Creeper'/443, l='MpServer', x=-284.07, y=45.00, z=-604.57], EntityCreeper['Creeper'/444, l='MpServer', x=-279.23, y=47.00, z=-592.50], EntitySpider['Spider'/445, l='MpServer', x=-274.09, y=33.00, z=-605.09], EntityRabbit['Rabbit'/318, l='MpServer', x=-332.96, y=63.00, z=-554.64], EntitySpider['Spider'/446, l='MpServer', x=-272.50, y=33.00, z=-601.50], EntityCreeper['Creeper'/319, l='MpServer', x=-366.50, y=45.00, z=-522.50], EntitySkeleton['Skeleton'/447, l='MpServer', x=-277.70, y=48.00, z=-594.49], EntityCreeper['Creeper'/320, l='MpServer', x=-365.50, y=42.00, z=-538.50], EntitySkeleton['Skeleton'/448, l='MpServer', x=-258.50, y=34.00, z=-621.50], EntityZombie['Zombie'/321, l='MpServer', x=-343.49, y=36.00, z=-560.20], EntityZombie['Zombie'/449, l='MpServer', x=-268.50, y=35.00, z=-613.24], EntitySkeleton['Skeleton'/322, l='MpServer', x=-380.66, y=46.00, z=-526.85], EntitySkeleton['Skeleton'/450, l='MpServer', x=-271.50, y=34.00, z=-613.50], EntityZombie['Zombie'/323, l='MpServer', x=-380.50, y=43.00, z=-521.80], EntitySkeleton['Skeleton'/451, l='MpServer', x=-266.50, y=34.00, z=-613.50], EntityCreeper['Creeper'/324, l='MpServer', x=-370.50, y=47.00, z=-524.50], EntityRabbit['Rabbit'/452, l='MpServer', x=-259.99, y=63.00, z=-547.55], EntityBat['Bat'/326, l='MpServer', x=-368.49, y=43.05, z=-539.19], EntityBat['Bat'/459, l='MpServer', x=-267.25, y=46.20, z=-564.62], EntitySheep['Sheep'/460, l='MpServer', x=-262.28, y=64.00, z=-560.57], EntityBat['Bat'/461, l='MpServer', x=-252.75, y=19.99, z=-593.58], EntitySkeleton['Skeleton'/464, l='MpServer', x=-238.30, y=30.07, z=-614.30], EntityCreeper['Creeper'/465, l='MpServer', x=-229.50, y=27.00, z=-620.50], EntityRabbit['Rabbit'/466, l='MpServer', x=-228.49, y=70.00, z=-610.48], EntityRabbit['Rabbit'/468, l='MpServer', x=-258.79, y=65.24, z=-587.72], EntityRabbit['Rabbit'/469, l='MpServer', x=-261.81, y=64.15, z=-587.43], EntityRabbit['Rabbit'/355, l='MpServer', x=-305.95, y=65.00, z=-673.09], EntityZombie['Zombie'/357, l='MpServer', x=-362.65, y=30.00, z=-631.65], EntityBat['Bat'/358, l='MpServer', x=-349.53, y=67.10, z=-640.51], EntityCreeper['Creeper'/359, l='MpServer', x=-340.50, y=34.00, z=-665.50], EntityRabbit['Rabbit'/360, l='MpServer', x=-361.46, y=76.00, z=-655.03], EntitySpider['Spider'/361, l='MpServer', x=-354.70, y=61.00, z=-655.15], EntityRabbit['Rabbit'/362, l='MpServer', x=-307.82, y=66.00, z=-645.98], EntityRabbit['Rabbit'/364, l='MpServer', x=-375.16, y=69.00, z=-597.91], EntitySheep['Sheep'/492, l='MpServer', x=-233.98, y=62.37, z=-549.85], EntityCreeper['Creeper'/371, l='MpServer', x=-247.50, y=26.00, z=-554.50], EntityCreeper['Sir Charles Wingate III'/246, l='MpServer', x=-290.29, y=63.00, z=-593.67], EntityItemFrame['entity.ItemFrame.name'/247, l='MpServer', x=-294.50, y=63.50, z=-594.03], EntitySkeleton['Skeleton'/503, l='MpServer', x=-223.58, y=58.00, z=-608.50], EntityItemFrame['entity.ItemFrame.name'/248, l='MpServer', x=-293.97, y=63.50, z=-593.50], EntityItemFrame['entity.ItemFrame.name'/249, l='MpServer', x=-294.50, y=63.50, z=-592.97], EntitySheep['Sheep'/377, l='MpServer', x=-227.36, y=63.00, z=-525.82], EntityItemFrame['entity.ItemFrame.name'/250, l='MpServer', x=-295.03, y=63.50, z=-593.50], EntitySheep['Sheep'/381, l='MpServer', x=-230.68, y=63.00, z=-531.48], EntitySheep['Sheep'/382, l='MpServer', x=-226.62, y=64.00, z=-541.49], EntityCow['Cow'/383, l='MpServer', x=-235.93, y=63.00, z=-532.55]]
Retry entities: 0 total; []
Server brand: fml,forge
Server type: Integrated singleplayer server
Stacktrace:
at net.minecraft.client.multiplayer.WorldClient.addWorldInfoToCrashReport(WorldClient.java:451)
at net.minecraft.client.Minecraft.addGraphicsAndWorldToCrashReport(Minecraft.java:2779)
at net.minecraft.client.Minecraft.run(Minecraft.java:435)
at net.minecraft.client.main.Main.main(Main.java:118)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at net.minecraft.launchwrapper.Launch.launch(Launch.java:135)
at net.minecraft.launchwrapper.Launch.main(Launch.java:28)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at net.minecraftforge.gradle.GradleStartCommon.launch(GradleStartCommon.java:97)
at GradleStart.main(GradleStart.java:26)

-- System Details --
Details:
Minecraft Version: 1.10.2
Operating System: Windows 10 (amd64) version 10.0
Java Version: 1.8.0_111, Oracle Corporation
Java VM Version: Java HotSpot(TM) 64-Bit Server VM (mixed mode), Oracle Corporation
Memory: 654869312 bytes (624 MB) / 1921515520 bytes (1832 MB) up to 4761059328 bytes (4540 MB)
JVM Flags: 0 total; 
IntCache: cache: 0, tcache: 0, allocated: 13, tallocated: 95
FML: MCP 9.32 Powered by Forge 12.18.2.2099 7 mods loaded, 7 mods active
States: 'U' = Unloaded 'L' = Loaded 'C' = Constructed 'H' = Pre-initialized 'I' = Initialized 'J' = Post-initialized 'A' = Available 'D' = Disabled 'E' = Errored
UCHIJAAAA	mcp{9.19} [Minecraft Coder Pack] (minecraft.jar) 
UCHIJAAAA	FML{8.0.99.99} [Forge Mod Loader] (forgeSrc-1.10.2-12.18.2.2099.jar) 
UCHIJAAAA	Forge{12.18.2.2099} [Minecraft Forge] (forgeSrc-1.10.2-12.18.2.2099.jar) 
UCHIJAAAA	sandboxmod{1.0.0-alpha} [sandbox Mod] (bin) 
UCHIJAAAA	Waila{1.7.0} [Waila] (Waila-1.7.0-B3_1.9.4.jar) 
UCHIJAAAA	JEI{3.10.0.261} [Just Enough Items] (jei_1.10.2-3.10.0.261.jar) 
UCHIJAAAA	theoneprobe{1.3.3} [The One Probe] (TheOneProbe-1.10-1.3.3-41.jar) 
Loaded coremods (and transformers): 
GL info: ' Vendor: 'NVIDIA Corporation' Version: '4.5.0 NVIDIA 369.09' Renderer: 'GeForce GTX 760/PCIe/SSE2'
Launched Version: 1.10.2
LWJGL: 2.9.4
OpenGL: GeForce GTX 760/PCIe/SSE2 GL version 4.5.0 NVIDIA 369.09, NVIDIA Corporation
GL Caps: Using GL 1.3 multitexturing.
Using GL 1.3 texture combiners.
Using framebuffer objects because OpenGL 3.0 is supported and separate blending is supported.
Shaders are available because OpenGL 2.1 is supported.
VBOs are available because OpenGL 1.5 is supported.

Using VBOs: Yes
Is Modded: Definitely; Client brand changed to 'fml,forge'
Type: Client (map_client.txt)
Resource Packs: 
Current Language: English (US)
Profiler Position: N/A (disabled)
CPU: 4x Intel(R) Core(TM) i5-3570 CPU @ 3.40GHz

 

 

 

The JSON in question:

 

 

info.sandboxmod.update.banner=[{"text":"A new version of "},{"text":"Sandbox Mod","color":"blue"},{"text":" is available.","color":"none"}]
info.sandboxmod.update.compareRel=[{"text":"Current version: "},{"text":"%s","color":"dark_red"},{"text":", Newest version: ","color":"none"},{"text":"%s","color":"blue"}]
info.sandboxmod.update.compareBeta=[{"text":"Current version: "},{"text":"%s","color":"dark_red"},{"text":", Newest version: ","color":"none"},{"text":"%s","color":"gold"}]
info.sandboxmod.update.buttons=[{"text":"["},{"text":"Changelog","color":"blue","clickEvent":{"action":"open_url","value":"%s"}},{"text":"] [","color":"none"},{"text":"Download","color":"blue","clickEvent":{"action":"open_url","value":"%s"}},{"text":"]","color":"none"}]

 

 

 

It's all rather opaque-looking, but JSONLint confirms it's valid, so the code itself isn't the issue.

 

The update checker that actually requests the aforementioned JSON:

 

 

 

@SubscribeEvent(priority = EventPriority.NORMAL, receiveCanceled = true)
public void onTick(TickEvent.ClientTickEvent event) {

	if(!Reference.IS_DEV && ConfigHandler.enableUpdateChecking) {
		EntityPlayer player = Minecraft.getMinecraft().thePlayer;

		if(!notified && Minecraft.getMinecraft().thePlayer != null) {
			ticksBeforeNotify++;

			if(ticksBeforeNotify >= 200) {
				if(!notified) {
					player.addChatComponentMessage(ITextComponent.Serializer.jsonToComponent("info." + Reference.MODID + ".update.banner"));

					if(Reference.UPDATE_TYPE == VersionType.RELEASE) {
						player.addChatComponentMessage(ITextComponent.Serializer.jsonToComponent(String.format("info." + Reference.MODID + ".update.compareRel", Reference.VERSION, Reference.LATEST_VERSION)));
					} else {
						player.addChatComponentMessage(ITextComponent.Serializer.jsonToComponent(String.format("info." + Reference.MODID + ".update.compareBeta", Reference.VERSION, Reference.LATEST_VERSION)));
					}
					player.addChatComponentMessage(ITextComponent.Serializer.jsonToComponent(String.format("info." + Reference.MODID + ".update.buttons", Reference.CHANGELOG_URL, Reference.DOWNLOAD_URL)));

					notified = true;
					ticksBeforeNotify = 0;
				}
			}
		}
	}
}

 

 

 

After setting a breakpoint where the train goes off the rails (JsonUtils:468) and stepping through the process, it looks like somewhere in or around the gsonDeserialize method, it's returning JsonScope.EMPTY_DOCUMENT, and almost immediately after that, it leaves mainline processing and enters exception handling until finally reaching all the way to the top and telling Minecraft to end itself.

 

So my question is: is this implying that there's an issue reading the file itself, or is all this me misinterpreting it/a red herring? There is a LOT of stuff that goes on in the background to read this file, and it gets confusing.

 

Another note: I edit the lang files in Sublime Text 3 (or right in Eclipse if I feel like it), in case that helps determine if it's an encoding issue or something.

Link to comment
Share on other sites

I copied and pasted each line into http://jsonlint.com, and they all came up as "valid json" is bright green letters. Is that site wrong, then? I see it linked to all over this site.

 

Also, what do I need to do to achieve the same result then? Full disclosure: I looked to Actually Additions' 1.10.2 branch on Github to find out how that mod does the same thing (thank you/sorry Ellpeck!) and it's near identical to how I do it here.

Link to comment
Share on other sites

That's not part of the JSON that is being asked for. That's the tag in my en_US.lang.

 

So I'm working under incorrect assumptions about what the "ITextComponent.Serializer.jsonToComponent" does vis-a-vis the mod's lang files, so it looks like I'll go off and find out how to parse a particular entry in a lang file. Thanks.

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
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
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.



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • I haven't been having this issue recently until now, but after a minute or two, opening the inventory is giving me an immediate crash: https://pastebin.com/gwv7CeKV
    • rp.crazyheal.xyz mods  
    • I'm developing a dimension, but it's kinda resource intensive so some times during player teleporting it lags behind making the player phase down into the void, so im trying to implement some kind of pregeneration to force the game loading a small set of chunks in the are the player will teleport to. Some of the things i've tried like using ServerLevel and ServerChunkCache methods like getChunk() dont actually trigger chunk generation if the chunk isn't already on persistent storage (already generated) or placing tickets, but that doesn't work either. Ideally i should be able to check when the task has ended too. I've peeked around some pregen engines, but they're too complex for my current understanding of the system of which I have just a basic understanding (how ServerLevel ,ServerChunkCache  and ChunkMap work) of. Any tips or other classes I should be looking into to understand how to do this correctly?
    • https://mclo.gs/4UC49Ao
    • Way back in the Forge 1.17 days, work started for adding JPMS (Java Platform Module Support) to ModLauncher and ForgeModLoader. This has been used internally by Forge and some libraries for a while now, but mods (those with mods.toml specifically) have not been able to take advantage of it. As of Forge 1.21.1 and 1.21.3, this is now possible!   What is JPMS and what does it mean for modders? JPMS is the Java Platform Module System, introduced in Java 9. It allows you to define modules, which are collections of packages and resources that can be exported or hidden from other modules. This allows for much more fine-tuned control over visibility, cleaner syntax for service declarations and support for sealed types across packages. For example, you might have a mod with a module called `com.example.mod` that exports `com.example.mod.api` and `com.example.mod.impl` to other mods, but hides `com.example.mod.internal` from them. This would allow you to have a clean API for other mods to use, while keeping your internal implementation details hidden from IDE hints, helping prevent accidental usage of internals that might break without prior notice. This is particularly useful if you'd like to use public records with module-private constructors or partially module-private record components, as you can create a sealed interface that only your record implements, having the interface be exported and the record hidden. It's also nice for declaring and using services, as you'll get compile-time errors from the Java compiler for typos and the like, rather than deferring to runtime errors. In more advanced cases, you can also have public methods that are only accessible to specific other modules -- handy if you want internal interactions between multiple of your own mods.   How do I bypass it? We understand there may be drama in implementing a system that prevents mods from accessing each other's internals when necessary (like when a mod is abandoned or you need to fix a compat issue) -- after all, we are already modding a game that doesn't have explicit support for Java mods yet. We have already thought of this and are offering APIs from day one to selectively bypass module restrictions. Let me be clear: Forge mods are not required to use JPMS. If you don't want to use it, you don't have to. The default behaviour is to have fully open, fully exported automatic modules. In Java, you can use the `Add-Opens` and `Add-Exports` manifest attributes to selectively bypass module restrictions of other mods at launch time, and we've added explicit support for these when loading your Forge mods. At compile-time, you can use existing solutions such as the extra-java-module-info Gradle plugin to deal with non-modular dependencies and add extra opens and exports to other modules. Here's an example on how to make the internal package `com.example.examplemod.internal` open to your mod in your build.gradle: tasks.named('jar', Jar) { manifest { attributes([ 'Add-Opens' : 'com.example.examplemod/com.example.examplemod.internal' 'Specification-Title' : mod_id, 'Specification-Vendor' : mod_authors // (...) ]) } } With the above in your mod's jar manifest, you can now reflectively access the classes inside that internal package. Multiple entries are separated with a space, as per Java's official spec. You can also use Add-Exports to directly call without reflection, however you'd need to use the Gradle plugin mentioned earlier to be able to compile. The syntax for Add-Exports is the same as Add-Opens, and instructions for the compile-time step with the Gradle plugin are detailed later in this post. Remember to prefer the opens and exports keywords inside module-info.java for sources you control. The Add-Opens/Add-Exports attributes are only intended for forcing open other mods.   What else is new with module support? Previously, the runtime module name was always forced to the first mod ID in your `mods.toml` file and all packages were forced fully open and exported. Module names are now distinguished from mod IDs, meaning the module name in your module-info.java can be different from the mod ID in your `mods.toml`. This allows you to have a more descriptive module name that doesn't have to be the same as your mod ID, however we strongly recommend including your mod ID as part of your module name to aid troubleshooting. The `Automatic-Module-Name` manifest attribute is now also honoured, allowing you to specify a module name for your mod without needing to create a `module-info.java` file. This is particularly useful for mods that don't care about JPMS features but want to have a more descriptive module name and easier integration with other mods that do use JPMS.   How do I use it? The first step is to create a `module-info.java` file in your mod's source directory. This file should be in the same package as your main mod class, and should look something like this: open module com.example.examplemod { requires net.minecraftforge.eventbus; requires net.minecraftforge.fmlcore; requires net.minecraftforge.forge; requires net.minecraftforge.javafmlmod; requires net.minecraftforge.mergetool.api; requires org.slf4j; requires logging; } For now, we're leaving the whole module open to reflection, which is a good starting point. When we know we want to close something off, we can remove the open modifier from the module and open or export individual packages instead. Remember that you need to be open to Forge (module name net.minecraftforge.forge), otherwise it can't call your mod's constructor. Next is fixing modules in Gradle. While Forge and Java support modules properly, Gradle does not put automatic modules on the module path by default, meaning that the logging module (from com.mojang:logging) is not found. To fix this, add the Gradle plugin and add a compile-time module definition for that Mojang library: plugins { // (...) id 'org.gradlex.extra-java-module-info' version "1.9" } // (...) extraJavaModuleInfo { failOnMissingModuleInfo = false automaticModule("com.mojang:logging", "logging") } The automatic module override specified in your build.gradle should match the runtime one to avoid errors. You can do the same for any library or mod dependency that is missing either a module-info or explicit Automatic-Module-Name, however be aware that you may need to update your mod once said library adds one. That's all you need to get started with module support in your mods. You can learn more about modules and how to use them at dev.java.
  • Topics

  • Who's Online (See full list)

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.