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

[1.21.x] Preserve original iteration order in Attribute Formatting API #1831

Open
wants to merge 2 commits into
base: 1.21.x
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
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ default Component getDebugInfo(AttributeModifier modif, TooltipFlag flag) {
* Gets the specific ID that represents a "base" (green) modifier for this attribute.
*
* @return The ID of the "base" modifier, or null, if no such modifier may exist.
*
* @apiNote Base modifiers always operate as if they were using {@link Operation#ADD_VALUE}.
*/
@Nullable
default ResourceLocation getBaseId() {
Expand All @@ -121,6 +123,8 @@ default ResourceLocation getBaseId() {
* @param merged If we are displaying a merged base component (which will have a non-merged base component as a child).
* @param flag The tooltip flag.
* @return The component representation of the passed attribute modifier.
*
* @apiNote Base modifiers always operate as if they were using {@link Operation#ADD_VALUE}.
*/
default MutableComponent toBaseComponent(double value, double entityBase, boolean merged, TooltipFlag flag) {
Attribute attr = self();
Expand All @@ -129,8 +133,10 @@ default MutableComponent toBaseComponent(double value, double entityBase, boolea
// Emit both the value of the modifier, and the entity's base value as debug information, since both are flattened into the modifier.
// 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.
if (flag.isAdvanced() && !merged) {
Component debugInfo = Component.literal(" ").append(Component.translatable("neoforge.attribute.debug.base", FORMAT.format(entityBase), FORMAT.format(value - entityBase)).withStyle(ChatFormatting.GRAY));
comp.append(debugInfo);
double baseBonus = value - entityBase;
String baseBonusText = String.format(Locale.ROOT, baseBonus > 0 ? " + %s" : " - %s", FORMAT.format(Math.abs(baseBonus)));
Component debugInfo = Component.translatable("neoforge.attribute.debug.base", FORMAT.format(entityBase), baseBonusText).withStyle(ChatFormatting.GRAY);
comp.append(CommonComponents.SPACE).append(debugInfo);
}

return comp;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@

package net.neoforged.neoforge.common.util;

import com.google.common.collect.LinkedListMultimap;
import com.google.common.collect.Multimap;
import com.google.common.collect.TreeMultimap;
import com.mojang.datafixers.util.Pair;
import it.unimi.dsi.fastutil.objects.Reference2ReferenceLinkedOpenHashMap;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.EnumMap;
import java.util.IdentityHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -153,7 +154,7 @@ public static void applyTextFor(ItemStack stack, Consumer<Component> tooltip, Mu
}

// Collect all the base modifiers
Map<Holder<Attribute>, BaseModifier> baseModifs = new IdentityHashMap<>();
Map<Holder<Attribute>, BaseModifier> baseModifs = new Reference2ReferenceLinkedOpenHashMap<>();

var it = modifierMap.entries().iterator();
while (it.hasNext()) {
Expand Down Expand Up @@ -299,7 +300,7 @@ public static Multimap<Holder<Attribute>, AttributeModifier> sortedMap() {
* @param slot The slot group to query modifiers for.
*/
public static Multimap<Holder<Attribute>, AttributeModifier> getSortedModifiers(ItemStack stack, EquipmentSlotGroup slot) {
Multimap<Holder<Attribute>, AttributeModifier> map = sortedMap();
Multimap<Holder<Attribute>, AttributeModifier> map = LinkedListMultimap.create();
stack.forEachModifier(slot, (attr, modif) -> {
if (attr != null && modif != null) {
map.put(attr, modif);
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/assets/neoforge/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@
"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",
"neoforge.network.feature_flags.no_vanilla_server": "This client does not support vanilla servers as it has custom FeatureFlags",

"neoforge.attribute.debug.base": "[Entity: %s | Item: %s]",
"neoforge.attribute.debug.base": "[%s%s]",

"neoforge.value.flat": "%s",
"neoforge.value.percent": "%s%%",
Expand Down
Loading