EzPlugins

PickItemPlugin

Feb 15th, 2025 (edited)
654
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.98 KB | None | 0 0
  1. using System;
  2. using Turbo.Plugins.Default;
  3. using System.Linq;
  4. using System.Collections.Generic;
  5. using System.Windows.Forms;
  6. using SharpDX.DirectInput;
  7. using System.Runtime.InteropServices;
  8. using System.Threading;
  9.  
  10. namespace Turbo.Plugins.Ez
  11. {
  12.     public class PickItemPlugin : BasePlugin, IItemPickedHandler, IAfterCollectHandler, INewAreaHandler, IKeyEventHandler
  13.     {
  14.         public static IntPtr D3Hwnd = IntPtr.Zero;
  15.         [DllImport("USER32.DLL")]
  16.         private static extern IntPtr FindWindow(string ClassName, string WindowText);
  17.  
  18.         [DllImport("user32.dll")]
  19.         public static extern bool SetCursorPos(int x, int y);
  20.  
  21.         [DllImport("user32.dll")]
  22.         private static extern void mouse_event(uint dwFlags, int dx, int dy, uint dwData, IntPtr dwExtraInfo);
  23.  
  24.         [DllImport("USER32.DLL")]
  25.         private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
  26.  
  27.         private static IntPtr MakeLParam(int x, int y)
  28.         {
  29.             return (IntPtr)(y << 16 | (x & 65535));
  30.         }
  31.  
  32.         private static void MouseLeftClick()
  33.         {
  34.             mouse_event(6U, 0, 0, 0U, IntPtr.Zero);
  35.         }
  36.  
  37.         private void DragAndDropItemInventory(IItem item, int x, int y)
  38.         {
  39.             var rect = Hud.Inventory.GetItemRect(item);
  40.             if (rect != System.Drawing.RectangleF.Empty)
  41.             {
  42.                 IntPtr lParam = MakeLParam((int)(rect.X + rect.Width/2.0f), (int)(rect.Y + rect.Height/2.0f));
  43.                 SendMessage(D3Hwnd, 513U, (IntPtr)1, lParam);
  44.                 lParam = MakeLParam(x, y);
  45.                 SendMessage(D3Hwnd, 514U, (IntPtr)1, lParam);
  46.             }
  47.         }
  48.  
  49.         protected IUiElement SeasonalLeaf;
  50.         public IKeyEvent ToggleKeyEvent1 { get; set; }  // ON/OFF
  51.         public IKeyEvent ToggleKeyEvent2 { get; set; }  // ON/OFF
  52.         public int dist_Far { get; set; }
  53.         public int dist_Near { get; set; }
  54.         public Keys KeyNoPick { get; set; }
  55.  
  56.         private long MsPicked { get; set; } = 0;
  57.         private long MsDragAndDrop { get; set; } = 0;
  58.         private bool pick_Enabled { get; set; } = false;
  59.         private bool inv_Enabled { get; set; } = false;
  60.         private bool inv_Running { get; set; } = false;
  61.         private bool PetDB { get; set; } = false;
  62.         private bool PetNMR { get; set; } = false;
  63.         private uint LastAreaSno { get; set; } = 0;
  64.  
  65.         private HashSet<uint> EnmityAreas { get; set; } = new HashSet<uint> { 488740, 488741, 488742, 488743, 488745, 488746, 488767, 488770, 488799, 488800, 488870, 488871, 488872, 488873, 488874 };
  66.         private Dictionary<int, int> ItemsDicc { get; set; } = new Dictionary<int, int>();
  67.         private HashSet<ActorSnoEnum> itemsToPick = new HashSet<ActorSnoEnum> // Items that do not need space in inventory
  68.         {
  69.             ActorSnoEnum._crafting_assortedparts_05,    // 361984, Reusable Parts   // craft normal
  70.             ActorSnoEnum._crafting_magic_05,            // 361985, Arcane Dust      // craft magic
  71.             ActorSnoEnum._crafting_rare_05,             // 361986, Veiled Crystal   // craft rare
  72.  
  73.             ActorSnoEnum._crafting_legendary_05,        // 361988, Forgotten Soul   // craft legendary
  74.  
  75.             ActorSnoEnum._crafting_looted_reagent_05,   // 361989, breaths of death // craft rare
  76.  
  77.             ActorSnoEnum._tieredlootrunkey_0,           // 408416, Keystone         // loot normal
  78.  
  79.             ActorSnoEnum._craftingreagent_legendary_set_borns_x1,           //365020, A1 Mats // craft Legenday
  80.             ActorSnoEnum._craftingreagent_legendary_set_cains_x1,           //364281, A2 Mats // craft Legenday
  81.             ActorSnoEnum._craftingreagent_legendary_set_demon_x1,           //364290, A3 Mats // craft Legenday
  82.             ActorSnoEnum._craftingreagent_legendary_set_hallowed_x1,        //364305, A4 Mats // craft Legenday
  83.             ActorSnoEnum._craftingreagent_legendary_set_captaincrimsons_x1, //364975, A5 Mats // craft Legenday
  84.  
  85.             ActorSnoEnum._demonorgan_skeletonking_x1,   // 364722,  // normal uberstuff
  86.             ActorSnoEnum._demonorgan_ghom_x1,           // 364723,  // normal uberstuff
  87.             ActorSnoEnum._demonorgan_siegebreaker_x1,   // 364724,  // normal uberstuff
  88.             ActorSnoEnum._demonorgan_diablo_x1,         // 364725,  // normal uberstuff
  89.         };
  90.         public HashSet<ISnoItem> BlackList { get; set; } = new HashSet<ISnoItem>();
  91.  
  92.         public PickItemPlugin()
  93.         {
  94.             Enabled = true;
  95.         }
  96.  
  97.         public override void Load(IController hud)
  98.         {
  99.             base.Load(hud);
  100.  
  101.             dist_Near = 15; // pick auto
  102.             dist_Far = 55; // F4
  103.             KeyNoPick = Keys.C; // As long as you have this key pressed it will not pick up items // https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.keys
  104.  
  105.             ToggleKeyEvent1 = Hud.Input.CreateKeyEvent(true, Key.F4, controlPressed: true, altPressed: false, shiftPressed: false); // Trhow
  106.             ToggleKeyEvent2 = Hud.Input.CreateKeyEvent(true, Key.F4, controlPressed: false, altPressed: false, shiftPressed: false); // Reset && Distance
  107.  
  108.             SeasonalLeaf = Hud.Render.RegisterUiElement("Root.NormalLayer.minimap_dialog_backgroundScreen.minimap_dialog_pve.BoostWrapper.BoostsDifficultyStackPanel.BoostsStackWrapper.BoostsStackPanel.Seasonal", null, null);
  109.  
  110.             D3Hwnd = FindWindow("D3 Main Window Class", null); // D3Hwnd = FindWindow(null, "Diablo III");
  111.  
  112.             if (Hud.Game.IsInGame) // TurboHud Free
  113.             {
  114.                 LastAreaSno = Hud.Game.Me.SnoArea.Sno;
  115.                 DiscardMats2();
  116.             }
  117.         }
  118.  
  119.         public void OnKeyEvent(IKeyEvent keyEvent)
  120.         {
  121.             if (keyEvent.IsPressed)
  122.             {
  123.                 if (ToggleKeyEvent1.Matches(keyEvent))
  124.                 {
  125.                     inv_Enabled = !inv_Enabled;
  126.                 }
  127.                 else if (ToggleKeyEvent2.Matches(keyEvent))
  128.                 {
  129.                     if (pick_Enabled)
  130.                     {
  131.                         pick_Enabled = false;
  132.                     }
  133.                     else
  134.                     {
  135.                         ItemsDicc.Clear();
  136.                         pick_Enabled = true;
  137.                     }
  138.                 }
  139.             }
  140.         }
  141.  
  142.         public void OnItemPicked(IItem item)
  143.         {
  144.             if (ItemsDicc.ContainsKey(item.Seed))
  145.                 ItemsDicc.Remove(item.Seed);
  146.         }
  147.  
  148.         public void OnNewArea(bool newGame, ISnoArea area) // Compatibility Turbo Hud Free && Bug Lmod (dual area)
  149.         {
  150.             if (LastAreaSno != area.Sno)
  151.             {
  152.                 LastAreaSno = area.Sno;
  153.                 if (area.IsTown) ItemsDicc.Clear();
  154.                 DiscardMats2();
  155.             }
  156.         }
  157.  
  158.         private void DiscardMats2()
  159.         {
  160.             if (Hud.Game.Me.Hero.Seasonal || SeasonalLeaf.Visible)  // double checking (Hud.Game.Me.Hero.Seasonal not updated)
  161.             {
  162.                 var petSeason = Hud.Game.Actors.Any(p => p.SummonerAcdDynamicId == Hud.Game.Me.SummonerId && p.GetAttributeValueAsUInt(Hud.Sno.Attributes.Pet_Type, 1048575) == 25);
  163.                 PetDB = petSeason && Hud.Game.Me.Powers.BuffIsActive(488027);                                                       // 488027 => Pets collect breaths of death
  164.                 PetNMR = (petSeason && Hud.Game.Me.Powers.BuffIsActive(488030)) || EnmityAreas.Contains(Hud.Game.Me.SnoArea.Sno);   // 488030 => Pets salvage rare, magical, normal // in a Vision of Enmity always discard
  165.             }
  166.             else
  167.             {
  168.                 PetDB = false;
  169.                 PetNMR = false;
  170.             }
  171.         }
  172.  
  173.         public void AfterCollect()
  174.         {
  175.             if (!Hud.Game.IsInGame || Hud.Game.IsPaused) return;
  176.             if (Hud.Game.CurrentRealTimeMilliseconds - MsPicked > 100 && !Hud.Input.IsKeyDown(KeyNoPick) && Hud.Window.IsForeground && Hud.Game.Me.AnimationState == AcdAnimationState.Idle && !Hud.Render.WorldMapUiElement.Visible)
  177.             {
  178.                 if (!Hud.Game.Me.Powers.CantMove && !Hud.Game.Me.Powers.BuffIsActive(439438) && (!Hud.Inventory.InventoryMainUiElement.Visible || pick_Enabled))
  179.                 {
  180.                     var distance = pick_Enabled?dist_Far:dist_Near; // Hud.Window.GroundRectangle.Contains((int)i.ScreenCoordinate.X, (int)i.ScreenCoordinate.Y)
  181.                     var items = Hud.Game.Items.Where(i => i.Location == ItemLocation.Floor && i.CentralXyDistanceToMe < distance && i.IsOnScreen && !BlackList.Contains(i.SnoItem)).OrderBy(o => o.CentralXyDistanceToMe);
  182.                     var picking = false;
  183.                     foreach(var item in items)
  184.                     {
  185.                         if (itemsToPick.Contains(item.SnoActor.Sno))
  186.                         {
  187.                             if (item.SnoActor.Sno == ActorSnoEnum._horadricrelic)
  188.                             {
  189.                                 var maxShards = 500 + (Hud.Game.Me.HighestSoloRiftLevel * 10);
  190.                                 if (Hud.Game.Me.Materials.BloodShard == maxShards)  continue;
  191.                             }
  192.                             else if (item.SnoActor.Sno == ActorSnoEnum._crafting_looted_reagent_05)
  193.                             {
  194.                                 if (PetDB) continue;
  195.                             }
  196.                         }
  197.                         else if (item.AccountBound && !item.BoundToMyAccount)   continue;
  198.                         else
  199.                         {
  200.                             var s = Hud.Game.Me.InventorySpaceTotal - Hud.Game.InventorySpaceUsed;
  201.                             if (s == 0)
  202.                             {
  203.                                 if (Hud.Inventory.ItemsInInventory.Any(i => i.SnoActor.Sno == item.SnoActor.Sno))
  204.                                 {
  205.                                     if (item.SnoActor.Sno == ActorSnoEnum._p74_consumable_sanctify_legendary_item)
  206.                                     {
  207.                                         if (item.StatList.FirstOrDefault(q => q.Id == "Unidentified#1048575")?.DoubleValue == 1) continue;  // item.Unidentified bugged
  208.                                     }
  209.                                     else if (item.StatList.FirstOrDefault(q => q.Id == "ItemStackQuantityLo#1048575") == null) continue;
  210.                                 }
  211.                                 else continue;
  212.                             }
  213.                             else if (s == 1 && item.SnoItem.Kind != ItemKind.uberstuff && item.SnoItem.Kind != ItemKind.craft && item.SnoItem.Kind != ItemKind.gem
  214.                                             && item.SnoItem.MainGroupCode != "gems_unique" && item.SnoItem.MainGroupCode != "ring" && item.SnoItem.MainGroupCode != "amulet" && item.SnoItem.MainGroupCode != "belt" && item.SnoItem.MainGroupCode != "consumable"
  215.                                     )   continue;
  216.                             else if (item.IsMagic)
  217.                             {
  218.                                 if (PetNMR) continue;
  219.                             }
  220.                             else if (item.IsNormal)
  221.                             {
  222.                                 if (item.SnoItem.Kind == ItemKind.loot)
  223.                                 {
  224.                                     if (PetNMR && !item.AccountBound)   continue;
  225.                                 }
  226.                                 else if (item.SnoItem.Kind != ItemKind.uberstuff && item.SnoItem.Kind != ItemKind.craft && item.SnoItem.Kind != ItemKind.gem )  continue;
  227.                             }
  228.                             else if (!item.IsLegendary) continue;
  229.                         }
  230.                         if (!ItemsDicc.TryGetValue(item.Seed,out var n))
  231.                         {
  232.                             n = 1;
  233.                         }
  234.                         else if (++n > 5)   continue;
  235.                         ItemsDicc[item.Seed] = n;
  236.                         picking = true;
  237.                         MsPicked = Hud.Game.CurrentRealTimeMilliseconds;
  238.  
  239.                         SetCursorPos((int)item.ScreenCoordinate.X + Hud.Window.Offset.X, (int)(item.ScreenCoordinate.Y - Hud.Window.Size.Height/55) + Hud.Window.Offset.Y);
  240.                         Thread.Sleep(1);
  241.                         if (Hud.Game.SelectedActor == null || Hud.Game.SelectedActor.GizmoType == GizmoType.Item)
  242.                         {
  243.                             MouseLeftClick();
  244.                         }
  245.                         break;
  246.                     }
  247.                     if (!picking && pick_Enabled)
  248.                     {
  249.                         pick_Enabled = false;
  250.                     }
  251.                 }
  252.             }
  253.  
  254.             if (inv_Enabled)
  255.             {
  256.                 if (Hud.Inventory.InventoryMainUiElement.Visible && Hud.Window.IsForeground)
  257.                 {
  258.                     if (Hud.Game.CurrentRealTimeMilliseconds - MsDragAndDrop > 10)
  259.                     {
  260.  
  261.                         MsDragAndDrop = Hud.Game.CurrentRealTimeMilliseconds;
  262.                         var itemInv = PetNMR ?  Hud.Inventory.ItemsInInventory.FirstOrDefault(i => (i.SnoItem.Kind == ItemKind.loot && !i.AccountBound && (i.IsRare || i.IsMagic || i.IsNormal)) || (!i.IsInventoryLocked && BlackList.Contains(i.SnoItem)))
  263.                                                 : Hud.Inventory.ItemsInInventory.FirstOrDefault(i => (i.SnoItem.Kind == ItemKind.loot && !i.AccountBound && i.IsRare) || (!i.IsInventoryLocked && BlackList.Contains(i.SnoItem)));
  264.                         if (itemInv != null)
  265.                         {
  266.                             DragAndDropItemInventory(itemInv, (int)(Hud.Window.Size.Width/2.0f), (int) (Hud.Window.Size.Height/2.0f));
  267.                         }
  268.                         else
  269.                         {
  270.                             inv_Enabled = false;
  271.                         }
  272.                     }
  273.                 }
  274.                 else
  275.                 {
  276.                     inv_Enabled = false;
  277.                 }
  278.             }
  279.         }
  280.     }
  281. }
Advertisement
Add Comment
Please, Sign In to add comment