Skip to content

Commit

Permalink
Add the ability to skip entire groups to GatherSkipped
Browse files Browse the repository at this point in the history
  • Loading branch information
Shadows-of-Fire committed Sep 21, 2024
1 parent 2c95e34 commit e9e5871
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@

package net.neoforged.neoforge.client.event;

import java.util.EnumSet;
import java.util.HashSet;
import java.util.Set;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.EquipmentSlotGroup;
import net.minecraft.world.item.ItemStack;
import net.neoforged.bus.api.Event;
import net.neoforged.neoforge.common.util.AttributeTooltipContext;
Expand All @@ -20,13 +23,15 @@
*/
public class GatherSkippedAttributeTooltipsEvent extends Event {
protected final ItemStack stack;
protected final Set<ResourceLocation> skips;
protected final Set<ResourceLocation> skippedIds;
protected final Set<EquipmentSlotGroup> skippedGroups;
protected final AttributeTooltipContext ctx;
protected boolean skipAll = false;

public GatherSkippedAttributeTooltipsEvent(ItemStack stack, Set<ResourceLocation> skips, AttributeTooltipContext ctx) {
public GatherSkippedAttributeTooltipsEvent(ItemStack stack, AttributeTooltipContext ctx) {
this.stack = stack;
this.skips = skips;
this.skippedIds = new HashSet<>();
this.skippedGroups = EnumSet.noneOf(EquipmentSlotGroup.class);
this.ctx = ctx;
}

Expand All @@ -48,14 +53,28 @@ public ItemStack getStack() {
* Marks the id of a specific attribute modifier as skipped, causing it to not be displayed in the tooltip.
*/
public void skipId(ResourceLocation id) {
this.skips.add(id);
this.skippedIds.add(id);
}

/**
* Marks an entire {@link EquipmentSlotGroup} as skipped, preventing all modifiers for that group from showing.
*/
public void skipGroup(EquipmentSlotGroup group) {
this.skippedGroups.add(group);
}

/**
* Checks if a given id is skipped or not. If all modifiers are skipped, this method always returns true.
*/
public boolean isSkipped(ResourceLocation id) {
return this.skipAll || this.skips.contains(id);
return this.skipAll || this.skippedIds.contains(id);
}

/**
* Checks if a given group is skipped or not. If all modifiers are skipped, this method always returns true.
*/
public boolean isSkipped(EquipmentSlotGroup group) {
return this.skipAll || this.skippedGroups.contains(group);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.function.Consumer;
import net.minecraft.ChatFormatting;
import net.minecraft.client.Minecraft;
Expand Down Expand Up @@ -103,7 +101,7 @@ public static void addAttributeTooltips(ItemStack stack, Consumer<Component> too
}

/**
* Unconditionally applies the attribute modifier tooltips for all attribute modifiers present on the item stack.
* Applies the attribute modifier tooltips for all attribute modifiers present on the item stack.
* <p>
* Before application, this method posts the {@link GatherSkippedAttributeTooltipsEvent} to determine which tooltips should be skipped.
* <p>
Expand All @@ -113,17 +111,20 @@ public static void addAttributeTooltips(ItemStack stack, Consumer<Component> too
* @param ctx The tooltip context.
*/
public static void applyModifierTooltips(ItemStack stack, Consumer<Component> tooltip, AttributeTooltipContext ctx) {
Set<ResourceLocation> skips = new HashSet<>();
var event = NeoForge.EVENT_BUS.post(new GatherSkippedAttributeTooltipsEvent(stack, skips, ctx));
var event = NeoForge.EVENT_BUS.post(new GatherSkippedAttributeTooltipsEvent(stack, ctx));
if (event.isSkippingAll()) {
return;
}

for (EquipmentSlotGroup group : EquipmentSlotGroup.values()) {
if (event.isSkipped(group)) {
continue;
}

Multimap<Holder<Attribute>, AttributeModifier> modifiers = getSortedModifiers(stack, group);

// Remove any skipped modifiers before doing any logic
modifiers.values().removeIf(m -> skips.contains(m.id()));
modifiers.values().removeIf(m -> event.isSkipped(m.id()));

if (modifiers.isEmpty()) {
continue;
Expand Down

0 comments on commit e9e5871

Please sign in to comment.