Mystical Customization

Mystical Customization

23M Downloads

Gibberish text when displaying Crop Tier

SihenZhang opened this issue · 2 comments

commented

Describe the bug
I was playing ATM 6 - To the Sky and I found the crop tier of the custom crops is displayed as gibberish text.
Gibberish Text

Expected behavior
After reading the source code, I found that the script is read via FileReader that uses Java's platform default encoding.

for (File file : files) {
JsonObject json;
FileReader reader = null;
ResourceLocation id = null;
try {
JsonParser parser = new JsonParser();
reader = new FileReader(file);
json = parser.parse(reader).getAsJsonObject();
String name = file.getName().replace(".json", "");
id = new ResourceLocation(MysticalCustomization.MOD_ID, name);
CropTierCreator.create(id, json);
reader.close();
} catch (Exception e) {
LOGGER.error("An error occurred while creating crop tier with id {}", id, e);
} finally {
IOUtils.closeQuietly(reader);
}
}

My operating system is Windows 11 in Simplified Chinese, and its default encoding is GBK, while the encoding of the script file is UTF-8. Therefore, it displays the gibberish text.
Since Java 11 FileReader has gained constructors that accept an encoding: new FileReader(file, charset) and new FileReader(fileName, charset).
In earlier versions of java, you need to use new InputStreamReader(new FileInputStream(pathToFile), <encoding>).

Screenshots / Scripts / Logs
magical.json

{
    "name": "§bMagical",
    "value": 8,
    "farmland": "mysticalagradditions:insanium_farmland",
    "essence": "mysticalagradditions:insanium_block",
    "fertilizable": false,
    "secondarySeedDrop": false
}

Versions (please complete the following information):

  • Minecraft: 1.16.5
  • Forge: 36.1.66
  • Cucumber: 4.1.10
  • Mystical Agriculture: 4.2.1
  • Mystical Customization: 2.1.5
commented

You should be able to encode the file in UTF-8. I remember back in the day I had this issue with minecraft translation files and having to re-encode them using notepad++.

commented

Did you really read what he said?

The language file is encoded in UTF-8, but you have not specified to use UTF-8 to read the file, which in the Chinese area defaults to using GBK encoding to decode.

Related code:

for (File file : files) {
JsonObject json;
FileReader reader = null;
ResourceLocation id = null;
try {
JsonParser parser = new JsonParser();
reader = new FileReader(file);
json = parser.parse(reader).getAsJsonObject();
String name = file.getName().replace(".json", "");
id = new ResourceLocation(MysticalCustomization.MOD_ID, name);
CropTierCreator.create(id, json);
reader.close();
} catch (Exception e) {
LOGGER.error("An error occurred while creating crop tier with id {}", id, e);
} finally {
IOUtils.closeQuietly(reader);
}
}