EzPlugins

AlwaysRepairYourItems

Oct 3rd, 2024 (edited)
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.23 KB | None | 0 0
  1. // My goal was to get the plugin to work properly by using RegisterUiElement as few times as possible. For this reason, do not use craftweapons_dialog/craftarmor_dialog/salvage_dialog/repair_dialog.
  2. using Turbo.Plugins.Default;
  3. using System;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7.  
  8. namespace Turbo.Plugins.Ez
  9. {
  10.     public class AlwaysRepairYourItems : BasePlugin, IAfterCollectHandler, INewAreaHandler
  11.     {
  12.         public bool Debug { get; set; } = false;
  13.  
  14.         public static IntPtr D3Hwnd = IntPtr.Zero;
  15.  
  16.         [DllImport("USER32.DLL")]
  17.         private static extern IntPtr FindWindow(string ClassName, string WindowText);
  18.  
  19.         [DllImport("USER32.DLL")]
  20.         private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
  21.  
  22.         public void mouseLClickUiE(IUiElement uie)  // D3Hwnd
  23.         {
  24.             var x = (int) (uie.Rectangle.X + uie.Rectangle.Width/2.0f);
  25.             var y = (int) (uie.Rectangle.Y + uie.Rectangle.Height/2.0f);
  26.             IntPtr lParam = (IntPtr)(y << 16 | (x & 65535));
  27.             SendMessage(D3Hwnd, 513U, (IntPtr)1, lParam);
  28.             SendMessage(D3Hwnd, 514U, (IntPtr)1, lParam);
  29.         }
  30.  
  31.         protected IUiElement VENDOR_MAIN;
  32.         protected IUiElement VENDOR_REPAIRALL;
  33.  
  34.         protected IUiElement VENDOR_TAB_0;
  35.         protected IUiElement VENDOR_TAB_1;
  36.         protected IUiElement VENDOR_TAB_2;
  37.         protected IUiElement VENDOR_TAB_3;
  38.  
  39.         private bool doRepair { get; set; } = false;
  40.         private long msLapseAction { get; set; } = 0;
  41.         private IUiElement TabActive    { get; set; } = null;
  42.         private ISnoArea LastSnoArea { get; set; } = null;
  43.  
  44.         public long msLapseMin { get; set; } = 10;
  45.         public bool AfterRepairGoToSalvageTab { get; set; }
  46.  
  47.         public AlwaysRepairYourItems()
  48.         {
  49.             Enabled = true;
  50.         }
  51.  
  52.         public override void Load(IController hud)
  53.         {
  54.             base.Load(hud);
  55.  
  56.             AfterRepairGoToSalvageTab = false;
  57.  
  58.             D3Hwnd = FindWindow("D3 Main Window Class", null); // D3Hwnd = FindWindow(null, "Diablo III");
  59.  
  60.             VENDOR_MAIN = Hud.Render.RegisterUiElement("Root.NormalLayer.vendor_dialog_mainPage", null, null);
  61.             VENDOR_REPAIRALL = Hud.Render.RegisterUiElement("Root.NormalLayer.vendor_dialog_mainPage.repair_dialog.RepairAll", null, null);
  62.  
  63.             VENDOR_TAB_0 = Hud.Render.RegisterUiElement("Root.NormalLayer.vendor_dialog_mainPage.tab_0", null, null);
  64.             VENDOR_TAB_1 = Hud.Render.RegisterUiElement("Root.NormalLayer.vendor_dialog_mainPage.tab_1", null, null);
  65.             VENDOR_TAB_2 = Hud.Render.RegisterUiElement("Root.NormalLayer.vendor_dialog_mainPage.tab_2", null, null);
  66.             VENDOR_TAB_3 = Hud.Render.RegisterUiElement("Root.NormalLayer.vendor_dialog_mainPage.tab_3", null, null);
  67.         }
  68.  
  69.         public void OnNewArea(bool newGame, ISnoArea area)
  70.         {
  71.             if (LastSnoArea != area)
  72.             {
  73.                 if (area.IsTown /*&& LastSnoArea?.IsTown != true*/) // once for each town or once for all towns
  74.                 {
  75.                     msLapseAction = Hud.Game.CurrentRealTimeMilliseconds;
  76.                     TabActive = null;
  77.                     doRepair = true;
  78.                 }
  79.                 LastSnoArea = area;
  80.             }
  81.         }
  82.  
  83.         public void AfterCollect()
  84.         {
  85.             if (!Hud.Game.IsInGame || !Hud.Game.IsInTown || Hud.Game.IsPaused ) return;
  86.  
  87.             if (doRepair)
  88.             {
  89.                 if (VENDOR_MAIN.Visible && VENDOR_TAB_3.Visible) // BlackSmith, Jeweler, Mystic
  90.                 {
  91.                     if (Hud.Game.CurrentRealTimeMilliseconds - msLapseAction > msLapseMin)
  92.                     {
  93.                         msLapseAction = Hud.Game.CurrentRealTimeMilliseconds;
  94.                         if (Hud.Game.Me.AnimationState == AcdAnimationState.Idle)
  95.                         {
  96.                             if (VENDOR_TAB_3.AnimState == 34) // Tab active 16-13-37-34-73 , 19-46-22-73 , 85-88-96-73  // 73 training => conflicts // (+-1) => Values for non-active tab (mouse over or not)
  97.                             {
  98.                                 if (VENDOR_REPAIRALL.Visible)
  99.                                 {
  100.                                     var match = Regex.Match(VENDOR_REPAIRALL.ReadText(Encoding.UTF8, true), @"([0-9]+)"); // @"([0-9]+([\.,][0-9]+)*)" for complete number ?
  101.                                     if (match.Success && match.Groups[1].Value != "0")
  102.                                     {
  103.                                         mouseLClickUiE(VENDOR_REPAIRALL);
  104.                                     }
  105.                                     else
  106.                                     {
  107.                                         if (AfterRepairGoToSalvageTab)
  108.                                             TabActive = VENDOR_TAB_2;
  109.                                         if (TabActive != null)
  110.                                         {
  111.                                             mouseLClickUiE(TabActive);
  112.                                             TabActive = null;
  113.                                         }
  114.                                         doRepair = false;
  115.                                     }
  116.                                 }
  117.                             }
  118.                             else // 73 is not necessary, and thus I avoid conflicts
  119.                             {
  120.                                 if (VENDOR_TAB_0.AnimState == 16)
  121.                                     TabActive = VENDOR_TAB_0;
  122.                                 else if (VENDOR_TAB_1.AnimState == 13)
  123.                                     TabActive = VENDOR_TAB_1;
  124.                                 else if (VENDOR_TAB_2.AnimState == 37)
  125.                                     TabActive = VENDOR_TAB_2;
  126.                                 if (TabActive != null)
  127.                                     mouseLClickUiE(VENDOR_TAB_3);
  128.                             }
  129.                         }
  130.                     }
  131.                 }
  132.             }
  133.         }
  134.     }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment