Skip to content

Commit

Permalink
Clear tooltip but leave name & remove tooltip by line (#1622)
Browse files Browse the repository at this point in the history
* Fix clear tooltip also clear display name

* add remove tooltip by line

* leave name operation

* fix breaking change
  • Loading branch information
friendlyhj committed Feb 19, 2023
1 parent 4911fde commit 7e3b463
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ public class IngredientTooltips {

private static final IngredientMap<Pair<IFormattedText, IFormattedText>> TOOLTIPS = new IngredientMap<>();
private static final IngredientMap<Pair<IFormattedText, IFormattedText>> SHIFT_TOOLTIPS = new IngredientMap<>();
private static final List<IIngredient> CLEARED_TOOLTIPS = new LinkedList<>();
private static final IngredientMap<Boolean> CLEARED_TOOLTIPS = new IngredientMap<>();
private static final IngredientMap<Pair<ITooltipFunction, ITooltipFunction>> TOOLTIP_FUNCTIONS = new IngredientMap<>();
private static final IngredientMap<Pair<ITooltipFunction, ITooltipFunction>> SHIFT_TOOLTIP_FUNCTIONS = new IngredientMap<>();
private static final IngredientMap<Pattern> REMOVED_TOOLTIPS = new IngredientMap<>();
private static final IngredientMap<Integer> REMOVED_TOOLTIPS_LINE = new IngredientMap<>();

@ZenMethod
public static void addTooltip(IIngredient ingredient, IFormattedText tooltip) {
Expand All @@ -45,16 +46,26 @@ public static void addShiftTooltip(IIngredient ingredient, IFormattedText toolti
public static void addShiftTooltip(IIngredient ingredient, ITooltipFunction function, @Optional ITooltipFunction showMessage) {
CraftTweakerAPI.apply(new AddAdvancedTooltipAction(ingredient, function, true, showMessage));
}

@ZenMethod
public static void clearTooltip(IIngredient ingredient) {
CraftTweakerAPI.apply(new ClearTooltipAction(ingredient));
clearTooltip(ingredient, false);
}

@ZenMethod
public static void clearTooltip(IIngredient ingredient, boolean leaveName) {
CraftTweakerAPI.apply(new ClearTooltipAction(ingredient, leaveName));
}

@ZenMethod
public static void removeTooltip(IIngredient ingredient, String regex) {
CraftTweakerAPI.apply(new RemoveTooltipAction(ingredient, regex));
}

@ZenMethod
public static void removeTooltip(IIngredient ingredient, int line) {
CraftTweakerAPI.apply(new RemoveTooltipLineAction(ingredient, line));
}

public static List<Pair<IFormattedText, IFormattedText>> getTooltips(IItemStack item) {
return TOOLTIPS.getEntries(item);
Expand All @@ -75,14 +86,17 @@ public static List<Pair<ITooltipFunction, ITooltipFunction>> getAdvancedShiftToo
public static List<Pattern> getTooltipsToRemove(IItemStack item) {
return REMOVED_TOOLTIPS.getEntries(item);
}

public static boolean shouldClearToolTip(IItemStack item) {
for(IIngredient cleared : CLEARED_TOOLTIPS) {
if(cleared.matches(item)) {
return true;
}
}
return false;

public static List<Integer> getTooltipLinesToRemove(IItemStack item) {
return REMOVED_TOOLTIPS_LINE.getEntries(item);
}

// Boolean wrapper as 3 status flag
// null -> don't clear tooltip, FALSE -> clear tooltip, TRUE -> clear tooltip but leave name
// a little dirty, but making an enum for the small thing is annoying
public static Boolean shouldClearToolTip(IItemStack item) {
List<Boolean> entries = CLEARED_TOOLTIPS.getEntries(item);
return entries.isEmpty() ? null : entries.get(0);
}

// ######################
Expand Down Expand Up @@ -169,14 +183,20 @@ public String describe() {
private static class ClearTooltipAction implements IAction {

private final IIngredient ingredient;

private final boolean leaveName;

public ClearTooltipAction(IIngredient ingredient) {
this(ingredient, false);
}

public ClearTooltipAction(IIngredient ingredient, boolean leaveItemName) {
this.ingredient = ingredient;
this.leaveName = leaveItemName;
}

@Override
public void apply() {
ingredient.getItems().forEach(item -> CLEARED_TOOLTIPS.add(item));
CLEARED_TOOLTIPS.register(ingredient, leaveName);
}


Expand All @@ -186,4 +206,25 @@ public String describe() {
}

}

private static class RemoveTooltipLineAction implements IAction {

private final IIngredient ingredient;
private final int line;

public RemoveTooltipLineAction(IIngredient ingredient, int line) {
this.ingredient = ingredient;
this.line = line;
}

@Override
public void apply() {
REMOVED_TOOLTIPS_LINE.register(ingredient, line);
}

@Override
public String describe() {
return "Removing tooltip for " + ingredient + " at line " + line;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,42 +20,49 @@ public class ClientEventHandler {

@SubscribeEvent(priority = EventPriority.LOW)
public void onItemTooltip(ItemTooltipEvent ev) {
List<String> tooltipList = ev.getToolTip();
if(!ev.getItemStack().isEmpty()) {
IItemStack itemStack = CraftTweakerMC.getIItemStack(ev.getItemStack());
if(IngredientTooltips.shouldClearToolTip(itemStack)) {
ev.getToolTip().clear();
IItemStack itemStack = CraftTweakerMC.getIItemStackForMatching(ev.getItemStack());
Boolean clearTooltipFlag = IngredientTooltips.shouldClearToolTip(itemStack);
if(clearTooltipFlag != null) {
(clearTooltipFlag ? tooltipList.subList(1, tooltipList.size()) : tooltipList).clear();
}

List<String> toRemove = new ArrayList<>();
for (Integer line : IngredientTooltips.getTooltipLinesToRemove(itemStack)) {
if (line > 0 && line < tooltipList.size()) {
toRemove.add(tooltipList.get(line));
}
}
for(Pattern regex : IngredientTooltips.getTooltipsToRemove(itemStack)) {
for(String s : ev.getToolTip()) {
for(String s : tooltipList) {
if(regex.matcher(s).find()) {
toRemove.add(s);
}
}
}
ev.getToolTip().removeAll(toRemove);
tooltipList.removeAll(toRemove);

for(Pair<IFormattedText, IFormattedText> tooltip : IngredientTooltips.getTooltips(itemStack)) {
ev.getToolTip().add(((IMCFormattedString) tooltip.getKey()).getTooltipString());
tooltipList.add(((IMCFormattedString) tooltip.getKey()).getTooltipString());
}
for(Pair<ITooltipFunction, ITooltipFunction> tooltip : IngredientTooltips.getAdvancedTooltips(itemStack)) {
ev.getToolTip().add(tooltip.getKey().process(itemStack));
tooltipList.add(tooltip.getKey().process(itemStack));
}

boolean pressed = Keyboard.isKeyDown(Keyboard.KEY_LSHIFT) || Keyboard.isKeyDown(Keyboard.KEY_RSHIFT);
for(Pair<IFormattedText, IFormattedText> tooltip : IngredientTooltips.getShiftTooltips(itemStack)) {
if(pressed) {
ev.getToolTip().add(((IMCFormattedString) tooltip.getKey()).getTooltipString());
tooltipList.add(((IMCFormattedString) tooltip.getKey()).getTooltipString());
} else if(tooltip.getValue() != null) {
ev.getToolTip().add(((IMCFormattedString) tooltip.getValue()).getTooltipString());
tooltipList.add(((IMCFormattedString) tooltip.getValue()).getTooltipString());
}
}
for(Pair<ITooltipFunction, ITooltipFunction> tooltip : IngredientTooltips.getAdvancedShiftTooltips(itemStack)) {
if(pressed) {
ev.getToolTip().add(tooltip.getKey().process(itemStack));
tooltipList.add(tooltip.getKey().process(itemStack));
} else if(tooltip.getValue() != null) {
ev.getToolTip().add(tooltip.getValue().process(itemStack));
tooltipList.add(tooltip.getValue().process(itemStack));
}
}
}
Expand Down

0 comments on commit 7e3b463

Please sign in to comment.