From 15bd5f0d304997425ba1be99e441b2431276acd7 Mon Sep 17 00:00:00 2001 From: Take Weiland Date: Sun, 5 Jun 2022 16:24:54 +0200 Subject: [PATCH] [1.18] Allow custom rarities to directly modify the style (#8648) --- .../net/minecraft/client/gui/Gui.java.patch | 8 +++- .../minecraft/world/item/ItemStack.java.patch | 18 ++++++++ .../minecraft/world/item/Rarity.java.patch | 23 +++++++++- .../debug/misc/CustomRarityTest.java | 45 +++++++++++++++++++ src/test/resources/META-INF/mods.toml | 2 + 5 files changed, 93 insertions(+), 3 deletions(-) create mode 100644 src/test/java/net/minecraftforge/debug/misc/CustomRarityTest.java diff --git a/patches/minecraft/net/minecraft/client/gui/Gui.java.patch b/patches/minecraft/net/minecraft/client/gui/Gui.java.patch index 0eaaad81ce..72682e4eda 100644 --- a/patches/minecraft/net/minecraft/client/gui/Gui.java.patch +++ b/patches/minecraft/net/minecraft/client/gui/Gui.java.patch @@ -27,7 +27,13 @@ } } -@@ -625,7 +_,8 @@ +@@ -620,12 +_,13 @@ + public void m_93069_(PoseStack p_93070_) { + this.f_92986_.m_91307_().m_6180_("selectedItemName"); + if (this.f_92993_ > 0 && !this.f_92994_.m_41619_()) { +- MutableComponent mutablecomponent = (new TextComponent("")).m_7220_(this.f_92994_.m_41786_()).m_130940_(this.f_92994_.m_41791_().f_43022_); ++ MutableComponent mutablecomponent = (new TextComponent("")).m_7220_(this.f_92994_.m_41786_()).m_130938_(this.f_92994_.m_41791_().getStyleModifier()); + if (this.f_92994_.m_41788_()) { mutablecomponent.m_130940_(ChatFormatting.ITALIC); } diff --git a/patches/minecraft/net/minecraft/world/item/ItemStack.java.patch b/patches/minecraft/net/minecraft/world/item/ItemStack.java.patch index db2b603388..06de2aa7ac 100644 --- a/patches/minecraft/net/minecraft/world/item/ItemStack.java.patch +++ b/patches/minecraft/net/minecraft/world/item/ItemStack.java.patch @@ -197,6 +197,15 @@ this.m_41721_(this.m_41773_()); } +@@ -599,7 +_,7 @@ + + public List m_41651_(@Nullable Player p_41652_, TooltipFlag p_41653_) { + List list = Lists.newArrayList(); +- MutableComponent mutablecomponent = (new TextComponent("")).m_7220_(this.m_41786_()).m_130940_(this.m_41791_().f_43022_); ++ MutableComponent mutablecomponent = (new TextComponent("")).m_7220_(this.m_41786_()).m_130938_(this.m_41791_().getStyleModifier()); + if (this.m_41788_()) { + mutablecomponent.m_130940_(ChatFormatting.ITALIC); + } @@ -738,6 +_,7 @@ } } @@ -226,6 +235,15 @@ return multimap; } +@@ -904,7 +_,7 @@ + + MutableComponent mutablecomponent1 = ComponentUtils.m_130748_(mutablecomponent); + if (!this.f_41591_) { +- mutablecomponent1.m_130940_(this.m_41791_().f_43022_).m_130938_((p_204127_) -> { ++ mutablecomponent1.m_130938_(this.m_41791_().getStyleModifier()).m_130938_((p_204127_) -> { + return p_204127_.m_131144_(new HoverEvent(HoverEvent.Action.f_130832_, new HoverEvent.ItemStackInfo(this))); + }); + } @@ -957,12 +_,31 @@ this.m_41720_().m_5929_(p_41732_, p_41733_, this, p_41734_); } diff --git a/patches/minecraft/net/minecraft/world/item/Rarity.java.patch b/patches/minecraft/net/minecraft/world/item/Rarity.java.patch index 3f879b67fc..a7607d4c4d 100644 --- a/patches/minecraft/net/minecraft/world/item/Rarity.java.patch +++ b/patches/minecraft/net/minecraft/world/item/Rarity.java.patch @@ -1,6 +1,6 @@ --- a/net/minecraft/world/item/Rarity.java +++ b/net/minecraft/world/item/Rarity.java -@@ -2,7 +_,7 @@ +@@ -2,15 +_,36 @@ import net.minecraft.ChatFormatting; @@ -9,13 +9,32 @@ COMMON(ChatFormatting.WHITE), UNCOMMON(ChatFormatting.YELLOW), RARE(ChatFormatting.AQUA), -@@ -12,5 +_,9 @@ + EPIC(ChatFormatting.LIGHT_PURPLE); + ++ /** @deprecated Forge: Use {@link #getStyleModifier()} */ ++ @Deprecated + public final ChatFormatting f_43022_; ++ private final java.util.function.UnaryOperator styleModifier; private Rarity(ChatFormatting p_43028_) { this.f_43022_ = p_43028_; ++ this.styleModifier = style -> style.m_131140_(p_43028_); ++ } ++ ++ Rarity(java.util.function.UnaryOperator styleModifier) { ++ this.f_43022_ = ChatFormatting.BLACK; ++ this.styleModifier = styleModifier; ++ } ++ ++ public java.util.function.UnaryOperator getStyleModifier() { ++ return this.styleModifier; + } + + public static Rarity create(String name, ChatFormatting p_43028_) { ++ throw new IllegalStateException("Enum not extended"); ++ } ++ ++ public static Rarity create(String name, java.util.function.UnaryOperator styleModifier) { + throw new IllegalStateException("Enum not extended"); } } diff --git a/src/test/java/net/minecraftforge/debug/misc/CustomRarityTest.java b/src/test/java/net/minecraftforge/debug/misc/CustomRarityTest.java new file mode 100644 index 0000000000..2908850bf0 --- /dev/null +++ b/src/test/java/net/minecraftforge/debug/misc/CustomRarityTest.java @@ -0,0 +1,45 @@ +/* + * Copyright (c) Forge Development LLC and contributors + * SPDX-License-Identifier: LGPL-2.1-only + */ + +package net.minecraftforge.debug.misc; + +import net.minecraft.ChatFormatting; +import net.minecraft.world.item.Item; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.Rarity; +import net.minecraftforge.fml.common.Mod; +import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; +import net.minecraftforge.registries.DeferredRegister; +import net.minecraftforge.registries.ForgeRegistries; +import net.minecraftforge.registries.RegistryObject; + +@Mod(CustomRarityTest.MOD_ID) +public class CustomRarityTest { + + private static final boolean ENABLED = true; + static final String MOD_ID = "custom_rarity_test"; + + private static final Rarity CUSTOM_RARITY = Rarity.create(MOD_ID + "_CUSTOM", style -> style.withItalic(true).withColor(ChatFormatting.DARK_AQUA)); + + private static final DeferredRegister ITEMS = DeferredRegister.create(ForgeRegistries.ITEMS, MOD_ID); + private static final RegistryObject CUSTOM_ITEM = ITEMS.register( + "test", + () -> new Item(new Item.Properties()) + { + @Override + public Rarity getRarity(ItemStack p_41461_) + { + return CUSTOM_RARITY; + } + } + ); + + public CustomRarityTest() { + if (ENABLED) + { + ITEMS.register(FMLJavaModLoadingContext.get().getModEventBus()); + } + } +} diff --git a/src/test/resources/META-INF/mods.toml b/src/test/resources/META-INF/mods.toml index 3ec9671b0e..fbe3785e97 100644 --- a/src/test/resources/META-INF/mods.toml +++ b/src/test/resources/META-INF/mods.toml @@ -222,5 +222,7 @@ license="LGPL v2.1" modId="player_negotiation_event_test" [[mods]] modId="custom_transformtype_test" +[[mods]] + modId="custom_rarity_test" # ADD ABOVE THIS LINE