Skip to content

Commit 547dfa3

Browse files
Backport to 1.21.1: [1.21.x] Preserve original iteration order in Attribute Formatting API (#2031)
This ensures that attributes follow the order set by the ItemAttributes component. It also minimizes the advanced tooltip info for base modifiers.
1 parent 472c782 commit 547dfa3

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

src/main/java/net/neoforged/neoforge/common/extensions/IAttributeExtension.java

+8-2
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ default Component getDebugInfo(AttributeModifier modif, TooltipFlag flag) {
102102
* Gets the specific ID that represents a "base" (green) modifier for this attribute.
103103
*
104104
* @return The ID of the "base" modifier, or null, if no such modifier may exist.
105+
*
106+
* @apiNote Base modifiers always operate as if they were using {@link Operation#ADD_VALUE}.
105107
*/
106108
@Nullable
107109
default ResourceLocation getBaseId() {
@@ -121,6 +123,8 @@ default ResourceLocation getBaseId() {
121123
* @param merged If we are displaying a merged base component (which will have a non-merged base component as a child).
122124
* @param flag The tooltip flag.
123125
* @return The component representation of the passed attribute modifier.
126+
*
127+
* @apiNote Base modifiers always operate as if they were using {@link Operation#ADD_VALUE}.
124128
*/
125129
default MutableComponent toBaseComponent(double value, double entityBase, boolean merged, TooltipFlag flag) {
126130
Attribute attr = self();
@@ -129,8 +133,10 @@ default MutableComponent toBaseComponent(double value, double entityBase, boolea
129133
// Emit both the value of the modifier, and the entity's base value as debug information, since both are flattened into the modifier.
130134
// Skip showing debug information here when displaying a merged modifier, since it will be shown if the user holds shift to display the un-merged modifier.
131135
if (flag.isAdvanced() && !merged && NeoForgeConfig.COMMON.attributeAdvancedTooltipDebugInfo.get()) {
132-
Component debugInfo = Component.literal(" ").append(Component.translatable("neoforge.attribute.debug.base", FORMAT.format(entityBase), FORMAT.format(value - entityBase)).withStyle(ChatFormatting.GRAY));
133-
comp.append(debugInfo);
136+
double baseBonus = value - entityBase;
137+
String baseBonusText = String.format(Locale.ROOT, baseBonus > 0 ? " + %s" : " - %s", FORMAT.format(Math.abs(baseBonus)));
138+
Component debugInfo = Component.translatable("neoforge.attribute.debug.base", FORMAT.format(entityBase), baseBonusText).withStyle(ChatFormatting.GRAY);
139+
comp.append(CommonComponents.SPACE).append(debugInfo);
134140
}
135141

136142
return comp;

src/main/java/net/neoforged/neoforge/common/util/AttributeUtil.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55

66
package net.neoforged.neoforge.common.util;
77

8+
import com.google.common.collect.LinkedListMultimap;
89
import com.google.common.collect.Multimap;
910
import com.google.common.collect.TreeMultimap;
1011
import com.mojang.datafixers.util.Pair;
12+
import it.unimi.dsi.fastutil.objects.Reference2ReferenceLinkedOpenHashMap;
1113
import java.util.ArrayList;
1214
import java.util.Collection;
1315
import java.util.Comparator;
1416
import java.util.EnumMap;
15-
import java.util.IdentityHashMap;
1617
import java.util.LinkedList;
1718
import java.util.List;
1819
import java.util.Map;
@@ -153,7 +154,7 @@ public static void applyTextFor(ItemStack stack, Consumer<Component> tooltip, Mu
153154
}
154155

155156
// Collect all the base modifiers
156-
Map<Holder<Attribute>, BaseModifier> baseModifs = new IdentityHashMap<>();
157+
Map<Holder<Attribute>, BaseModifier> baseModifs = new Reference2ReferenceLinkedOpenHashMap<>();
157158

158159
var it = modifierMap.entries().iterator();
159160
while (it.hasNext()) {
@@ -299,7 +300,7 @@ public static Multimap<Holder<Attribute>, AttributeModifier> sortedMap() {
299300
* @param slot The slot group to query modifiers for.
300301
*/
301302
public static Multimap<Holder<Attribute>, AttributeModifier> getSortedModifiers(ItemStack stack, EquipmentSlotGroup slot) {
302-
Multimap<Holder<Attribute>, AttributeModifier> map = sortedMap();
303+
Multimap<Holder<Attribute>, AttributeModifier> map = LinkedListMultimap.create();
303304
stack.forEachModifier(slot, (attr, modif) -> {
304305
if (attr != null && modif != null) {
305306
map.put(attr, modif);

src/main/resources/assets/neoforge/lang/en_us.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@
272272
"neoforge.network.feature_flags.entry_mismatch": "The server and client have different sets of custom FeatureFlags. Make sure you are using the same mod and NeoForge versions as the server. See the log for more details",
273273
"neoforge.network.feature_flags.no_vanilla_server": "This client does not support vanilla servers as it has custom FeatureFlags",
274274

275-
"neoforge.attribute.debug.base": "[Entity: %s | Item: %s]",
275+
"neoforge.attribute.debug.base": "[%s%s]",
276276

277277
"neoforge.value.flat": "%s",
278278
"neoforge.value.percent": "%s%%",

0 commit comments

Comments
 (0)