public static bool RebuildTooltip_SetItemTooltip(Terraria.Item __instance) { // The specially named __instance parameter is automatically filled in by Harmony to reference the Item instance that is calling this method. if (!PluginLoadedSuccessfully) // If there was a problem loading things for our plugin, we will abort this stub method so we dont crash Terraria. { return(true); // Allow the original method to execute } if (__instance.type == WeaponItemID) // Only apply our weapon's tooltip if the item that had RebuildTooltip called on it actually is our weapon. { // Terraria.UI.ItemTooltip does not have a public constructor, so we must use Activator to create an ItemTooltip instance var tooltip = (Terraria.UI.ItemTooltip)Activator.CreateInstance(typeof(Terraria.UI.ItemTooltip), true); // Terraria.Localization.LocalizedText also does not have a public constructor, so we need to use Reflection Type localizedTextType = typeof(Terraria.Localization.LocalizedText); var localizedTooltipText = (Terraria.Localization.LocalizedText)HHelpers.ActivateInstanceUsingFirstConstructor(localizedTextType, new object[] { "", WeaponItemTooltipText }); // This type has only one constructor // By using the ActivateInstanceUsingFirstConstructor() helper method, we avoid directly using Reflection and thus don't violate plugin Security Level 4 // Also, Terraria.UI.ItemTooltip._text is a private field, so we need to use Reflection to set its value. HHelpers.SetFieldValueWithReflection("_text", tooltip, localizedTooltipText); // The recommended way to do this is to use HHelpers.SetFieldValueWithReflection(), which is compliant with all security levels. // You can, of course, use reflection yourself, but then your plugin will violate plugin Security Level 4. __instance.ToolTip = tooltip; // Assign the newly-created tooltip to the Item WeaponItemTooltip = tooltip; // And hold onto it for later return(false); // Don't allow the original method to execute } return(true); // Allow the original method to execute }
public static bool GetTooltip_GetItemTooltip(Terraria.UI.ItemTooltip __result, int itemId) { // By assigning something to the specially named __result parameter, we are changing the return value of the original method. // The itemId parameter is automatically filled in by Harmony to be the same value as the itemId parameter that was passed to the original method. if (!PluginLoadedSuccessfully) // If there was a problem loading things for our plugin, we will abort this stub method so we dont crash Terraria. { return(true); // Allow the original method to execute } if (itemId == WeaponItemID) // Only skip the original method and return our weapon's tooltip if the itemId is actually our weapon's ID { __result = WeaponItemTooltip; return(false); // Don't allow the original method to execute } return(true); // Allow the original method to execute }