Skip to content

Commit

Permalink
Finish API-ifying the implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Shadows-of-Fire committed Sep 19, 2024
1 parent 90c20b0 commit 610bee9
Show file tree
Hide file tree
Showing 11 changed files with 421 additions and 163 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,25 @@
public static final Codec<Holder<Attribute>> CODEC = BuiltInRegistries.ATTRIBUTE.holderByNameCodec();
public static final StreamCodec<RegistryFriendlyByteBuf, Holder<Attribute>> STREAM_CODEC = ByteBufCodecs.holderRegistry(Registries.ATTRIBUTE);
private final double defaultValue;
@@ -50,6 +_,21 @@

public ChatFormatting getStyle(boolean p_347715_) {
return this.sentiment.getStyle(p_347715_);
+ }
+
+ // Neo: Patch in the default implementation of IAttributeExtension#getMergedStyle since we need access to Attribute#sentiment
+
+ protected static final net.minecraft.network.chat.TextColor MERGED_RED = net.minecraft.network.chat.TextColor.fromRgb(0xF93131);
+ protected static final net.minecraft.network.chat.TextColor MERGED_BLUE = net.minecraft.network.chat.TextColor.fromRgb(0x7A7AF9);
+ protected static final net.minecraft.network.chat.TextColor MERGED_GRAY = net.minecraft.network.chat.TextColor.fromRgb(0xCCCCCC);
+
+ @Override
+ public net.minecraft.network.chat.TextColor getMergedStyle(boolean isPositive) {
+ return switch (this.sentiment) {
+ case POSITIVE -> isPositive ? MERGED_BLUE : MERGED_RED;
+ case NEGATIVE -> isPositive ? MERGED_RED : MERGED_BLUE;
+ case NEUTRAL -> MERGED_GRAY;
+ };
}

public static enum Sentiment {
5 changes: 3 additions & 2 deletions patches/net/minecraft/world/item/ItemStack.java.patch
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,14 @@
if (this.has(DataComponents.CUSTOM_NAME)) {
mutablecomponent.withStyle(ChatFormatting.ITALIC);
}
@@ -752,7 +_,8 @@
@@ -752,7 +_,9 @@
this.addToTooltip(DataComponents.ENCHANTMENTS, p_339637_, consumer, p_41653_);
this.addToTooltip(DataComponents.DYED_COLOR, p_339637_, consumer, p_41653_);
this.addToTooltip(DataComponents.LORE, p_339637_, consumer, p_41653_);
- this.addAttributeTooltips(consumer, p_41652_);
+ // Neo: Replace attribute tooltips with custom handling
+ net.neoforged.neoforge.client.util.TooltipUtil.addAttributeTooltips(p_41652_, this, consumer, p_41653_);
+ net.neoforged.neoforge.client.util.TooltipUtil.addAttributeTooltips(this, consumer,
+ net.neoforged.neoforge.client.util.TooltipUtil.AttributeTooltipContext.of(p_41652_, p_339637_, p_41653_));
this.addToTooltip(DataComponents.UNBREAKABLE, p_339637_, consumer, p_41653_);
AdventureModePredicate adventuremodepredicate = this.get(DataComponents.CAN_BREAK);
if (adventuremodepredicate != null && adventuremodepredicate.showInTooltip()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,38 @@
package net.neoforged.neoforge.client.event;

import java.util.function.Consumer;
import net.minecraft.core.component.DataComponents;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.item.component.ItemAttributeModifiers;
import net.neoforged.api.distmarker.Dist;
import net.neoforged.neoforge.event.entity.player.PlayerEvent;
import org.jetbrains.annotations.Nullable;
import net.neoforged.bus.api.Event;
import net.neoforged.neoforge.client.util.TooltipUtil;
import net.neoforged.neoforge.client.util.TooltipUtil.AttributeTooltipContext;

/**
* This event is used to add additional attribute tooltip lines without having to manually locate the inject point.
* This event is fired after attribute tooltip lines have been added to an item stack's tooltip in {@link TooltipUtil#addAttributeTooltips}.
* <p>
* It can be used to add additional tooltip lines adjacent to the attribute lines without having to manually locate the inject point.
* <p>
* This event is only fired on the {@linkplain Dist#CLIENT physical client}.
*/
public class AddAttributeTooltipsEvent extends PlayerEvent {
public class AddAttributeTooltipsEvent extends Event {
protected final ItemStack stack;
protected final Consumer<Component> tooltip;
protected final TooltipFlag flag;
protected final AttributeTooltipContext ctx;

public AddAttributeTooltipsEvent(ItemStack stack, @Nullable Player player, Consumer<Component> tooltip, TooltipFlag flag) {
super(player);
public AddAttributeTooltipsEvent(ItemStack stack, Consumer<Component> tooltip, AttributeTooltipContext ctx) {
this.stack = stack;
this.tooltip = tooltip;
this.flag = flag;
this.ctx = ctx;
}

/**
* Use to determine if the advanced information on item tooltips is being shown, toggled by F3+H.
* The current tooltip context.
*/
public TooltipFlag getFlags() {
return this.flag;
public AttributeTooltipContext getContext() {
return this.ctx;
}

/**
Expand All @@ -46,18 +48,20 @@ public ItemStack getStack() {
}

/**
* Adds a single {@link Component} to the itemstack's tooltip.
* Adds one or more {@link Component}s to the tooltip.
*/
public void addTooltipLine(Component comp) {
this.tooltip.accept(comp);
public void addTooltipLines(Component... comps) {
for (Component comp : comps) {
this.tooltip.accept(comp);
}
}

/**
* This event is fired with a null player during startup when populating search trees for tooltips.
* Checks if the attribute tooltips should be shown on the current item stack.
* <p>
* This event is fired even if the component would prevent the normal tooltip lines from showing.
*/
@Override
@Nullable
public Player getEntity() {
return super.getEntity();
public boolean shouldShow() {
return this.stack.getOrDefault(DataComponents.ATTRIBUTE_MODIFIERS, ItemAttributeModifiers.EMPTY).showInTooltip();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,35 @@

import java.util.Set;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import net.neoforged.api.distmarker.Dist;
import net.neoforged.neoforge.event.entity.player.PlayerEvent;
import org.jetbrains.annotations.Nullable;
import net.neoforged.bus.api.Event;
import net.neoforged.neoforge.client.util.TooltipUtil.AttributeTooltipContext;

/**
* This event is used to collect UUIDs of attribute modifiers that will not be displayed in item tooltips.
* This event is used to collect the IDs of attribute modifiers that will not be displayed in item tooltips.
* <p>
* This allows hiding specific modifiers for whatever reason. They will still be shown in the attributes GUI.
* It allows hiding some (or all) of the modifiers, potentially for displaying them in an alternative way (or for hiding information from the player).
* <p>
* This event is only fired on the {@linkplain Dist#CLIENT physical client}.
*/
public class GatherSkippedAttributeTooltipsEvent extends PlayerEvent {
public class GatherSkippedAttributeTooltipsEvent extends Event {
protected final ItemStack stack;
protected final Set<ResourceLocation> skips;
protected final TooltipFlag flag;
protected final AttributeTooltipContext ctx;
protected boolean skipAll = false;

public GatherSkippedAttributeTooltipsEvent(ItemStack stack, @Nullable Player player, Set<ResourceLocation> skips, TooltipFlag flag) {
super(player);
public GatherSkippedAttributeTooltipsEvent(ItemStack stack, Set<ResourceLocation> skips, AttributeTooltipContext ctx) {
this.stack = stack;
this.skips = skips;
this.flag = flag;
this.ctx = ctx;
}

/**
* Use to determine if the advanced information on item tooltips is being shown, toggled by F3+H.
* The current tooltip context.
*/
public TooltipFlag getFlags() {
return this.flag;
public AttributeTooltipContext getContext() {
return this.ctx;
}

/**
Expand All @@ -48,18 +46,30 @@ public ItemStack getStack() {
}

/**
* Mark the ResourceLocation of a specific attribute modifier as skipped, causing it to not be displayed in the tooltip.
* Marks the id of a specific attribute modifier as skipped, causing it to not be displayed in the tooltip.
*/
public void skipID(ResourceLocation id) {
public void skipId(ResourceLocation id) {
this.skips.add(id);
}

/**
* This event is fired with a null player during startup when populating search trees for tooltips.
* Checks if a given id is skipped or not. If all modifiers are skipped, this method always returns true.
*/
@Override
@Nullable
public Player getEntity() {
return super.getEntity();
public boolean isSkipped(ResourceLocation id) {
return this.skipAll || this.skips.contains(id);
}

/**
* Sets if the event should skip displaying all attribute modifiers.
*/
public void setSkipAll(boolean skip) {
this.skipAll = skip;
}

/**
* Checks if the event will cause all attribute modifiers to be skipped.
*/
public boolean isSkippingAll() {
return this.skipAll;
}
}
Loading

0 comments on commit 610bee9

Please sign in to comment.