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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/main/java/gregtech/api/gui/widgets/TankWidget.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@ public void drawInForeground(int mouseX, int mouseY) {
tooltips.add(fluid.getLocalizedName(lastFluidInTank));

// Add chemical formula tooltip
String formula = FluidTooltipUtil.getFluidTooltip(lastFluidInTank);
List<String> formula = FluidTooltipUtil.getFluidTooltips(lastFluidInTank);
if (formula != null && !formula.isEmpty())
tooltips.add(ChatFormatting.GRAY.toString() + formula);
tooltips.addAll(1, formula);

tooltips.add(I18n.format("gregtech.fluid.amount", lastFluidInTank.amount, lastTankCapacity));
tooltips.add(I18n.format("gregtech.fluid.temperature", fluid.getTemperature(lastFluidInTank)));
Expand Down
54 changes: 48 additions & 6 deletions src/main/java/gregtech/api/util/FluidTooltipUtil.java
Original file line number Diff line number Diff line change
@@ -1,57 +1,91 @@
package gregtech.api.util;

import com.google.common.collect.Lists;
import com.mojang.realmsclient.gui.ChatFormatting;
import gregtech.api.unification.material.Materials;
import gregtech.api.unification.stack.MaterialStack;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fluids.FluidStack;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FluidTooltipUtil {

/**
* Registry Mapping of <Fluid, Tooltip>
*/
private static final Map<Fluid, String> tooltips = new HashMap<>();
private static final Map<Fluid, List<String>> tooltips = new HashMap<>();

/**
* Used to register a tooltip to a Fluid. A Fluid can only have one tooltip, on one line.
*
* Ignores a tooltip applied for Water, so that it will be handled correctly for the chemical formula.
*
* @param fluid The fluid to register a tooltip for.
* @param tooltip The tooltip.
* @param tooltips The tooltip.
* @return False if either parameter is null or if tooltip is empty, true otherwise.
*/
public static boolean registerTooltip(Fluid fluid, List<String> tooltips) {
if (fluid != null && tooltips != null && !tooltips.isEmpty()) {
for (String tooltip : tooltips) {
registerTooltip(fluid, tooltip);
}
}
return false;
}

public static boolean registerTooltip(Fluid fluid, String tooltip) {
if (fluid != null && tooltip != null && !tooltip.isEmpty()) {
tooltips.put(fluid, tooltip);
if (fluid != null && tooltip != null && !tooltip.trim().isEmpty()) {
if(tooltips.containsKey(fluid)) {
tooltips.get(fluid).add(tooltip);
} else {
tooltips.put(fluid, Lists.newArrayList(tooltip));
}
return true;
}
return false;
}



/**
* Used to get a Fluid's tooltip.
*
* @param fluid The Fluid to get the tooltip of.
* @return The tooltip.
*/
public static String getFluidTooltip(Fluid fluid) {
public static List<String> getFluidTooltips(Fluid fluid) {
if (fluid == null)
return null;

return tooltips.get(fluid);
}

@Deprecated
public static String getFluidTooltip(Fluid fluid) {
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.

}

/**
* Used to get a Fluid's tooltip.
*
* @param stack A FluidStack, containing the Fluid to get the tooltip of.
* @return The tooltip.
*/
public static List<String> getFluidTooltips(FluidStack stack) {
if (stack == null)
return null;

return getFluidTooltips(stack.getFluid());
}

@Deprecated
public static String getFluidTooltip(FluidStack stack) {
if (stack == null)
return null;
Expand All @@ -65,6 +99,14 @@ public static String getFluidTooltip(FluidStack stack) {
* @param fluidName A String representing a Fluid to get the tooltip of.
* @return The tooltip.
*/
public static List<String> getFluidTooltips(String fluidName) {
if (fluidName == null || fluidName.isEmpty())
return null;

return getFluidTooltips(FluidRegistry.getFluid(fluidName));
}

@Deprecated
public static String getFluidTooltip(String fluidName) {
if (fluidName == null || fluidName.isEmpty())
return null;
Expand All @@ -79,6 +121,6 @@ public static String getFluidTooltip(String fluidName) {
*/
public static String getWaterTooltip() {
// Done like this to not return parenthesis around the tooltip
return (new MaterialStack(Materials.Hydrogen, 2)).toString() + "O";
return (ChatFormatting.GRAY + (new MaterialStack(Materials.Hydrogen, 2)).toString() + "O");
}
}
18 changes: 8 additions & 10 deletions src/main/java/gregtech/common/ClientProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import codechicken.lib.texture.TextureUtils;
import codechicken.lib.util.ItemNBTUtils;
import codechicken.lib.util.ResourceUtils;
import com.google.common.collect.Lists;
import com.mojang.authlib.minecraft.MinecraftProfileTexture.Type;
import com.mojang.realmsclient.gui.ChatFormatting;
import gregtech.api.GTValues;
Expand Down Expand Up @@ -56,10 +57,7 @@
import java.io.InputStreamReader;
import java.net.*;
import java.nio.charset.StandardCharsets;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.*;

@SideOnly(Side.CLIENT)
@Mod.EventBusSubscriber(Side.CLIENT)
Expand Down Expand Up @@ -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


// Test for Items
UnificationEntry unificationEntry = OreDictUnifier.getUnificationEntry(itemStack);
if (unificationEntry != null && unificationEntry.material != null) {
chemicalFormula = unificationEntry.material.chemicalFormula;
chemicalFormula = Lists.newArrayList(ChatFormatting.GRAY + unificationEntry.material.chemicalFormula);

// Test for Fluids
} else if (ItemNBTUtils.hasTag(itemStack)) {

// Vanilla bucket
chemicalFormula = FluidTooltipUtil.getFluidTooltip(ItemNBTUtils.getString(itemStack, "FluidName"));
chemicalFormula = FluidTooltipUtil.getFluidTooltips(ItemNBTUtils.getString(itemStack, "FluidName"));

// GTCE Cells, Forestry cans, some other containers
if (chemicalFormula == null) {
NBTTagCompound compound = itemStack.getTagCompound();
if (compound != null && compound.hasKey(FluidHandlerItemStack.FLUID_NBT_KEY, Constants.NBT.TAG_COMPOUND)) {
chemicalFormula = FluidTooltipUtil.getFluidTooltip(FluidStack.loadFluidStackFromNBT(compound.getCompoundTag(FluidHandlerItemStack.FLUID_NBT_KEY)));
chemicalFormula = FluidTooltipUtil.getFluidTooltips(FluidStack.loadFluidStackFromNBT(compound.getCompoundTag(FluidHandlerItemStack.FLUID_NBT_KEY)));
}
}

// Water buckets have a separate registry name from other buckets
} else if(itemStack.getItem().equals(Items.WATER_BUCKET)) {
chemicalFormula = FluidTooltipUtil.getWaterTooltip();
chemicalFormula = Lists.newArrayList(FluidTooltipUtil.getWaterTooltip());
}
if (chemicalFormula != null && !chemicalFormula.isEmpty())
event.getToolTip().add(1, ChatFormatting.GRAY.toString() + chemicalFormula);
event.getToolTip().addAll(1, chemicalFormula);
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/main/java/gregtech/common/MetaFluids.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package gregtech.common;

import com.mojang.realmsclient.gui.ChatFormatting;
import gregtech.api.GTValues;
import gregtech.api.unification.material.Materials;
import gregtech.api.unification.material.type.FluidMaterial;
Expand Down Expand Up @@ -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 (fluidMaterial.shouldGeneratePlasma() && fluidMaterial.getMaterialPlasma() == null) {
Fluid fluid = registerFluid(fluidMaterial, FluidType.PLASMA, 30000);
fluidMaterial.setMaterialPlasma(fluid);
FluidTooltipUtil.registerTooltip(fluid, fluidMaterial.chemicalFormula);
FluidTooltipUtil.registerTooltip(fluid, ChatFormatting.GRAY + fluidMaterial.chemicalFormula);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -682,9 +682,9 @@ public void addInformation(ItemStack stack, @Nullable World player, List<String>
if (fluidStack != null) {
tooltip.add(I18n.format("gregtech.machine.fluid_tank.fluid", fluidStack.amount, fluidStack.getLocalizedName()));
}
String formula = FluidTooltipUtil.getFluidTooltip(fluidStack);
if (formula != null)
tooltip.add(TextFormatting.GRAY + formula);
List<String> tooltips = FluidTooltipUtil.getFluidTooltips(fluidStack);
if (tooltips != null)
tooltip.addAll(1, tooltips);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/gregtech/integration/jei/utils/JEIHooks.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public class JEIHooks {
*/
public static void addFluidTooltip(List<String> tooltip, Object ingredient) {
if (ingredient instanceof FluidStack) {
String formula = FluidTooltipUtil.getFluidTooltip(((FluidStack) ingredient).getFluid());
List<String> formula = FluidTooltipUtil.getFluidTooltips(((FluidStack) ingredient).getFluid());
if (formula != null && !formula.isEmpty()) {
tooltip.add(1, ChatFormatting.GRAY + formula);
tooltip.addAll(1, formula);
}
}
}
Expand Down