Skip to content

Commit

Permalink
Added modifier key states to TooltipFlag and Level to TooltipContext (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
HenryLoenwind authored Aug 5, 2024
1 parent 682a618 commit c556779
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 3 deletions.
9 changes: 9 additions & 0 deletions patches/net/minecraft/client/gui/screens/Screen.java.patch
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@
}

protected <T extends GuiEventListener & Renderable & NarratableEntry> T addRenderableWidget(T p_169406_) {
@@ -242,7 +_,7 @@
return p_282833_.getTooltipLines(
Item.TooltipContext.of(p_281881_.level),
p_281881_.player,
- p_281881_.options.advancedItemTooltips ? TooltipFlag.Default.ADVANCED : TooltipFlag.Default.NORMAL
+ net.neoforged.neoforge.client.ClientTooltipFlag.of(p_281881_.options.advancedItemTooltips ? TooltipFlag.Default.ADVANCED : TooltipFlag.Default.NORMAL)
);
}

@@ -312,8 +_,11 @@
this.width = p_96608_;
this.height = p_96609_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,15 +185,19 @@
this.renderTooltip(p_283000_, p_281317_, p_282770_);
}

@@ -684,7 +_,7 @@
@@ -684,10 +_,10 @@
public List<Component> getTooltipFromContainerItem(ItemStack p_281769_) {
boolean flag = this.hoveredSlot != null && this.hoveredSlot instanceof CreativeModeInventoryScreen.CustomCreativeSlot;
boolean flag1 = selectedTab.getType() == CreativeModeTab.Type.CATEGORY;
- boolean flag2 = selectedTab.getType() == CreativeModeTab.Type.SEARCH;
+ boolean flag2 = selectedTab.hasSearchBar();
TooltipFlag.Default tooltipflag$default = this.minecraft.options.advancedItemTooltips ? TooltipFlag.Default.ADVANCED : TooltipFlag.Default.NORMAL;
TooltipFlag tooltipflag = flag ? tooltipflag$default.asCreative() : tooltipflag$default;
List<Component> list = p_281769_.getTooltipLines(Item.TooltipContext.of(this.minecraft.level), this.minecraft.player, tooltipflag);
- List<Component> list = p_281769_.getTooltipLines(Item.TooltipContext.of(this.minecraft.level), this.minecraft.player, tooltipflag);
+ List<Component> list = p_281769_.getTooltipLines(Item.TooltipContext.of(this.minecraft.level), this.minecraft.player, net.neoforged.neoforge.client.ClientTooltipFlag.of(tooltipflag));
if (flag1 && flag) {
return list;
} else {
@@ -703,7 +_,7 @@
int i = 1;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
--- a/net/minecraft/client/multiplayer/SessionSearchTrees.java
+++ b/net/minecraft/client/multiplayer/SessionSearchTrees.java
@@ -60,7 +_,7 @@
List<RecipeCollection> list = p_346233_.getCollections();
Registry<Item> registry = p_345600_.registryOrThrow(Registries.ITEM);
Item.TooltipContext item$tooltipcontext = Item.TooltipContext.of(p_345600_);
- TooltipFlag tooltipflag = TooltipFlag.Default.NORMAL;
+ TooltipFlag tooltipflag = net.neoforged.neoforge.client.ClientTooltipFlag.of(TooltipFlag.Default.NORMAL);
CompletableFuture<?> completablefuture = this.recipeSearch;
this.recipeSearch = CompletableFuture.supplyAsync(
() -> new FullTextSearchTree<>(
@@ -86,44 +_,60 @@
}

Expand Down Expand Up @@ -43,9 +52,10 @@
+ key,
() -> {
Item.TooltipContext item$tooltipcontext = Item.TooltipContext.of(p_345391_);
TooltipFlag tooltipflag = TooltipFlag.Default.NORMAL.asCreative();
- TooltipFlag tooltipflag = TooltipFlag.Default.NORMAL.asCreative();
- CompletableFuture<?> completablefuture = this.creativeByNameSearch;
- this.creativeByNameSearch = CompletableFuture.supplyAsync(
+ TooltipFlag tooltipflag = net.neoforged.neoforge.client.ClientTooltipFlag.of(TooltipFlag.Default.NORMAL.asCreative());
+ CompletableFuture<?> completablefuture = net.neoforged.neoforge.client.CreativeModeTabSearchRegistry.getNameSearchTree(key);
+ net.neoforged.neoforge.client.CreativeModeTabSearchRegistry.putNameSearchTree(key, CompletableFuture.supplyAsync(
() -> new FullTextSearchTree<>(
Expand Down
27 changes: 27 additions & 0 deletions patches/net/minecraft/world/item/Item.java.patch
Original file line number Diff line number Diff line change
Expand Up @@ -196,3 +196,30 @@
if (datacomponentmap.has(DataComponents.DAMAGE) && datacomponentmap.getOrDefault(DataComponents.MAX_STACK_SIZE, 1) > 1) {
throw new IllegalStateException("Item cannot have both durability and be stackable");
} else {
@@ -441,6 +_,14 @@
@Nullable
MapItemSavedData mapData(MapId p_339670_);

+ /**
+ * Neo: Returns the level if it's available.
+ */
+ @Nullable
+ default Level level() {
+ return null;
+ }
+
static Item.TooltipContext of(@Nullable final Level p_339599_) {
return p_339599_ == null ? EMPTY : new Item.TooltipContext() {
@Override
@@ -456,6 +_,11 @@
@Override
public MapItemSavedData mapData(MapId p_339628_) {
return p_339599_.getMapData(p_339628_);
+ }
+
+ @Override
+ public Level level() {
+ return p_339599_;
}
};
}
30 changes: 30 additions & 0 deletions patches/net/minecraft/world/item/TooltipFlag.java.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
--- a/net/minecraft/world/item/TooltipFlag.java
+++ b/net/minecraft/world/item/TooltipFlag.java
@@ -8,6 +_,27 @@

boolean isCreative();

+ /**
+ * Neo: Returns the state of the Control key (as reported by Screen) on the client, or {@code false} on the server.
+ */
+ default boolean hasControlDown() {
+ return false;
+ }
+
+ /**
+ * Neo: Returns the state of the Shift key (as reported by Screen) on the client, or {@code false} on the server.
+ */
+ default boolean hasShiftDown() {
+ return false;
+ }
+
+ /**
+ * Neo: Returns the state of the Alt key (as reported by Screen) on the client, or {@code false} on the server.
+ */
+ default boolean hasAltDown() {
+ return false;
+ }
+
public static record Default(boolean advanced, boolean creative) implements TooltipFlag {
@Override
public boolean isAdvanced() {
49 changes: 49 additions & 0 deletions src/main/java/net/neoforged/neoforge/client/ClientTooltipFlag.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (c) NeoForged and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/

package net.neoforged.neoforge.client;

import net.minecraft.client.gui.screens.Screen;
import net.minecraft.world.item.TooltipFlag;
import org.jetbrains.annotations.ApiStatus;

/**
* A version of {@link TooltipFlag} that knows about Screen and can provide modifier key states. It is patched into all vanilla uses of TooltipFlags in client classes.
* <p>
* When calling any tooltip method that needs a TooltipFlag yourself, use either this (by calling {@link #of(TooltipFlag)}) or {@link TooltipFlag.Default} depending on the <em>logical</em> side you're on.
*/
public record ClientTooltipFlag(boolean advanced, boolean creative, boolean shiftDown, boolean controlDown, boolean altDown) implements TooltipFlag {
@ApiStatus.Internal
public ClientTooltipFlag {}

@Override
public boolean isAdvanced() {
return this.advanced;
}

@Override
public boolean isCreative() {
return this.creative;
}

@Override
public boolean hasControlDown() {
return controlDown;
}

@Override
public boolean hasShiftDown() {
return shiftDown;
}

@Override
public boolean hasAltDown() {
return altDown;
}

public static TooltipFlag of(TooltipFlag other) {
return new ClientTooltipFlag(other.isAdvanced(), other.isCreative(), Screen.hasShiftDown(), Screen.hasControlDown(), Screen.hasAltDown());
}
}

0 comments on commit c556779

Please sign in to comment.