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

Properly implementing fluid tooltips #1581

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

Properly implementing fluid tooltips #1581

wants to merge 7 commits into from

Conversation

idcppl
Copy link
Contributor

@idcppl idcppl commented Apr 23, 2021

What:
This PR allows for people to implement their own tooltips, with more than one line.

How solved:
This is solved by allowing for the user to pass a list through the argument, and storing them as a list.

Additional Info:
GTCE formula tooltips should still be put at index of one and show up in machine slots, and tank tooltips.

@warjort
Copy link
Contributor

warjort commented Apr 23, 2021

I don't think you should be breaking something in api like this?

Why not:

// Old method for backwards comaptibilty (in case somebody else is using it)
@Deprecated
String getFluidToolTip() {
// return first in list
}
// New method that is actually used (note the "s" on the end)
List<String> getFluidToolTips() {}

@warjort
Copy link
Contributor

warjort commented Apr 23, 2021

You are also not doing the same validation when registering a list:

 public static boolean registerTooltip(Fluid fluid, List<String> tooltip) {
        if (fluid != null && tooltip != null && !tooltip.isEmpty()) {

 public static boolean registerTooltip(Fluid fluid, String tooltip) {
        if (fluid != null && tooltip != null && !tooltip.isEmpty()) {

The old method checks for an empty tooltip while you are checking for an empty list of tooltips.

Why not just make the new method do:

 public static boolean registerTooltip(Fluid fluid, List<String> tooltips) {
        if (fluid != null && tooltips != null && !tooltips.isEmpty()) {
            tooltips.forEach(tooltip -> register(fluid, tooltip);
        }
  }

I also think the original validation for an empty tooltip is wrong.
It should instead be doing:

tooltip.trim().isEmpty()

@idcppl
Copy link
Contributor Author

idcppl commented Apr 23, 2021

Thanks for the suggestions, fixed those area's. I didn't really think about how I was destroying backwards compatibility.

@warjort
Copy link
Contributor

warjort commented Apr 23, 2021

One more suggestion, I think the return value for the new method should be implemented something like:

 public static boolean registerTooltip(Fluid fluid, List<String> tooltips) {
        boolean result = false;
        if (fluid != null && tooltips != null && !tooltips.isEmpty()) {
            tooltips.forEach(tooltip -> {
                result = registerTooltip(fluid, tooltip) || result;
           });
        }
        return result;
    }

i.e. if any registration works it should return true.
I don't know how the result is actually used?

@idcppl
Copy link
Contributor Author

idcppl commented Apr 23, 2021

I don't know why it's there either, but shouldn't I be avoiding unnecessary mutability? I'm just carrying on the return type from before. @DStrand1 can possibly let us know what he was thinking.

@warjort
Copy link
Contributor

warjort commented Apr 23, 2021

mutablility of local variables/parameters is fine

mutability is an issue when it is shared state across threads.

@serenibyss
Copy link
Collaborator

There was no significant motivation for providing return values for registration, other than that I did not want to throw an exception like other registries do when bad parameters are passed, so I wanted some form of a return code for the user of the API to have if needed. If I had a mistake in returning the wrong value, then it should be fixed. True should be returned on a successful registry, otherwise false.

Additionally, I am not sure why we are using Java's Iterable.forEach() when it has worse performance and worse readability in stacktraces.

@serenibyss
Copy link
Collaborator

Since we are doing a more flexible system for adding tooltips, it may be wise to have a registry freeze to prevent the tooltip registry from being modified further past a certain point in the Forge loading cycle. If you need help implementing this, I can do it

@warjort
Copy link
Contributor

warjort commented Apr 23, 2021

Additionally, I am not sure why we are using Java's Iterable.forEach() when it has worse performance and worse readability in stacktraces.

It's swings and roundabouts.

Iterable.forEach() can be optimised by the implementation in ways for(x : Iterable) cannot.
The forEach() does introduce an extra function call to do the lamba versus for() when there is no optimisation and lambas
have additional restrictions on what they can do.

In the above example using a for() would probably be the better choice since we know the implementation
is defined in the same class as an ArrayList (which has no optimisations - at least for Oracle/OpenJDK).

@idcppl
Copy link
Contributor Author

idcppl commented Apr 23, 2021

Should we freeze the registry, and if so, should it be a GTControlledRegistry? And what would the index cap be on the registry if it were to be GTControlledRegistry? Mostly a question for Lag.

Quick edit: There also RegistrySimple we could use, doesn't have an max index requirement.

@warjort
Copy link
Contributor

warjort commented Apr 23, 2021

What issue does freezing the registry solve?

Is there something like block or color registration where you will "miss the bus" if you register too late?

Or the other use of registry freezing is to write the registry keys into the save file. So that forge can warn if they are missing in a future load of the save.

@LAGIdiot
Copy link
Member

LAGIdiot commented May 4, 2021

I don't feel like there is any reason for us to freeze this as there would not be "anything missing" if some one adds tooltip later.

@LAGIdiot LAGIdiot added rsr: revision Release size requirements: Revision type: feature New feature or request labels May 4, 2021
@@ -145,12 +146,12 @@ public static void init() {
int temperature = fluidMaterial.getFluidTemperature();
Fluid fluid = registerFluid(fluidMaterial, FluidType.NORMAL, temperature);
fluidMaterial.setMaterialFluid(fluid);
FluidTooltipUtil.registerTooltip(fluid, fluidMaterial.chemicalFormula);
FluidTooltipUtil.registerTooltip(fluid, ChatFormatting.GRAY + fluidMaterial.chemicalFormula);
Copy link
Collaborator

@serenibyss serenibyss May 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use TextFormatting as ChatFormatting only exists on the Client, and since we are now setting this from a shared class, this will cause a NoClassDefFoundError on dedicated servers

if (fluid == null)
return null;

return tooltips.get(fluid).get(1);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be .get(0)? From how we are registering it in registerTooltip(Fluid, String), it does not appear to be skipping the 0 index

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am too bit concerned why we are using sometimes 0, sometimes 1 when working with tooltips.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it is because when applying the tooltip to an ItemStack, the name of the Item is in index 0 in the list. For fluid tooltips, since I had to do it a bit more roundabout, index 0 of the registry will be put at index 1 of an ItemStack containing this fluid. Otherwise, when given to a FluidStack in JEI, it will just simply add to the end of the tooltip list rather than insert at an index.

@@ -154,33 +152,33 @@ public static void addMaterialFormulaHandler(ItemTooltipEvent event) {

// Handles Item tooltips
if (!(itemStack.getItem() instanceof ItemBlock)) {
String chemicalFormula = null;
List<String> chemicalFormula = null;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should change this to be 2 different methods of handling it, since our fluid tooltip is not just a chemical formula anymore, but a general fluid tooltip application

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rsr: revision Release size requirements: Revision type: feature New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants