Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve (Multiblock) Server Side Number Formatting #1670

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

warjort
Copy link
Contributor

@warjort warjort commented Jun 13, 2021

What:
In #1467 the client side number formatting was improved by adding %,d and %,f to the language file for numbers.
This was not possible for the serverside because TextComponentTranslatable doesn't support anything except the %s format.
This was recently mentioned here: Shadows-of-Fire/Shadows-of-Greg#151

How solved:
Implement an EnhancedTextComponentTranslatable that knows how to do all the formatting supported by java.util.Formatter

This enhanced version is not used directly, instead a subclass of the ITextComponent.Serializer automatically swaps the
normal TextComponentTranslatable for the enhanced version during deserialization on the client.
Additionally, it also tries to parse the arguments deserialzed as integers/floats - the default serializer serializes them using String.valueof() and on the client side it does a dance where it effectively does

String replacementObject = new TextComponentString(primitiveJson.asString()).getText();

The AdvancedTextWidget has been changed to use this new enhanced serializer.
This is used by Multiblock controllers amongst other things.

Outcome:
Multiblock displays can now use formats like %,d
2021-06-13_22 18 50

Additional info:
To demonstrate the capability, I have changed the multiblock EU output in the lang file to use %,d see image.

I changed both the serialize and deserialize to use the enhanced version in AdvancedTextWidget.
This is just for symmetry. The change in behaviour is entirely in deserialize on the client.

Possible compatibility issue:
None expected. To enable this feature, the lang file will need to be updated to use something other than %s.

@warjort
Copy link
Contributor Author

warjort commented Jun 13, 2021

Issues:

  1. The language file needs to be gone through again to see what other server side numbers can benefit from this change.

  2. This is still not a complete solution to the problem. There are other uses of TextComponentTranslatable where we don't control the deserialization (e.g. TOP or chat messages) that can't take advantage of this change.

  3. Issue (2) above could potentially be solved by using a CoreMod to swap all uses of ITextComponent.Serializer with this new Enhanced version.

  4. This mechanism could be extended to other information. e.g. you can imagine a special escape character that lets you do something like:

new TextComponentTranslation("some.translation.key", itemStack);
some.translation.key=item: %q

to display an item description on the client. I haven't checked if %q is already used by java.util.Formatter

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant