Jump to content

Need help with plugin


Recommended Posts

Hello, I'm trying do a simple plugin, which will auto buying food/water, because in my server (RU) don't work plugins, that already there are. So, can someone check my code and say, where did I mistakes? Because VS got me too many errors.

I have to say, so I'm not C# programmer, I'm most of specalization on JS, and because of that, I can do many errors, I'm trying used for solve their ChatGPT, but it don't helping so much, so I need people's help. 

My code:

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using wManager.Wow.Helpers;
using wManager.Wow.ObjectManager;
using wManager.Plugin;
using robotManager.Helpful;
using robotManager.Products;
using wManager.Wow.Enums;

public class FoodWaterPlugin : IPlugin
{
    private readonly List<ItemInfo> foodItems = new List<ItemInfo>
    {
        new ItemInfo(4540, 1, 5),
        new ItemInfo(4541, 5, 15),
        new ItemInfo(4542, 10, 20),
        new ItemInfo(4544, 15, 25),
        new ItemInfo(4601, 20, 30),
        new ItemInfo(8950, 25, 35),
        new ItemInfo(8076, 30, 40),
        new ItemInfo(4599, 35, 45),
        new ItemInfo(2287, 40, 50),
        new ItemInfo(8952, 45, 55),
        new ItemInfo(2679, 50, 60),
        new ItemInfo(2680, 55, 65),
        new ItemInfo(2681, 60, 70),
        new ItemInfo(2682, 65, 75),
        new ItemInfo(2684, 70, 75),
        new ItemInfo(2687, 75, 80),
        new ItemInfo(2683, 80, 80),
        new ItemInfo(2681, 55, 60),
        new ItemInfo(2685, 60, 65),
        new ItemInfo(3727, 65, 70),
        new ItemInfo(3726, 70, 75),
        new ItemInfo(3728, 75, 80),
        new ItemInfo(4594, 80, 80)
    };

    private readonly List<ItemInfo> waterItems = new List<ItemInfo>
    {
        new ItemInfo(159, 1, 5),
        new ItemInfo(1179, 5, 15),
        new ItemInfo(1205, 15, 25),
        new ItemInfo(1708, 25, 35),
        new ItemInfo(1645, 35, 45),
        new ItemInfo(8766, 45, 55),
        new ItemInfo(28399, 55, 65),
        new ItemInfo(33044, 65, 75),
        new ItemInfo(27860, 75, 80),
        new ItemInfo(33445, 80, 80),
        new ItemInfo(3356, 10, 20),
        new ItemInfo(2287, 20, 30),
        new ItemInfo(27422, 30, 40),
        new ItemInfo(33176, 40, 50),
        new ItemInfo(33177, 50, 60),
        new ItemInfo(33178, 60, 70),
        new ItemInfo(33179, 70, 80),
        new ItemInfo(33180, 80, 80),
        new ItemInfo(40731, 80, 80),
        new ItemInfo(19999, 40, 50),
        new ItemInfo(5469, 15, 25),
        new ItemInfo(13923, 30, 40),
        new ItemInfo(13443, 60, 70)
    };

    public void Initialize()
    {
        Logging.Write("[FoodWaterPlugin] Initialized.");
        EventsLua.AttachEventLua("UNIT_SPELLCAST_SUCCEEDED", (s, e) => OnSpellCastSucceeded(s, e));
        CheckAndBuyFoodWater();
    }

    public void Dispose()
    {
        Logging.Write("[FoodWaterPlugin] Disposed.");
        EventsLua.DetachEventLua("UNIT_SPELLCAST_SUCCEEDED", (s, e) => OnSpellCastSucceeded(s, e));
    }

    public void Settings()
    {
    }

    private void OnSpellCastSucceeded(string luaEvent, List<string> args)
    {
        if (args[0] == "player")
        {
            CheckAndBuyFoodWater();
        }
    }

    private void CheckAndBuyFoodWater()
    {
        if (ObjectManager.Me.IsDead || ObjectManager.Me.InCombatFlagOnly)
            return;

        int foodCount = GetItemCount(GetBestFood());
        int waterCount = GetItemCount(GetBestWater());

        if (foodCount < 5 || waterCount < 5)
        {
            GoToVendorAndBuy();
        }
    }

    private uint GetBestFood()
    {
        int playerLevel = ObjectManager.Me.Level;
        return foodItems.Where(item => item.MinLevel <= playerLevel && item.MaxLevel >= playerLevel)
                        .OrderByDescending(item => item.MinLevel)
                        .Select(item => item.Entry)
                        .FirstOrDefault();
    }

    private uint GetBestWater()
    {
        int playerLevel = ObjectManager.Me.Level;
        return waterItems.Where(item => item.MinLevel <= playerLevel && item.MaxLevel >= playerLevel)
                         .OrderByDescending(item => item.MinLevel)
                         .Select(item => item.Entry)
                         .FirstOrDefault();
    }

    private int GetItemCount(uint itemId)
    {
        return ItemsManager.GetItemCountByIdLUA((int)itemId);
    }

    private void GoToVendorAndBuy()
    {
        // Ищем ближайшего вендора
        WoWUnit vendor = ObjectManager.GetNearestWoWUnit(ObjectManager.GetWoWUnitVendor()).FirstOrDefault();
        if (vendor != null)
        {
            // Идем к вендору и покупаем
            MovementManager.Go(PathFinder.FindPath(vendor.Position));
            vendor.Target();
            Interact.InteractGameObject(vendor.GetBaseAddress);
            Lua.LuaDoString($"BuyItem({GetBestFood()}, 20)");
            Lua.LuaDoString($"BuyItem({GetBestWater()}, 20)");
            AddToDoNotSell(GetBestFood().ToString());
            AddToDoNotSell(GetBestWater().ToString());
        }
    }

    private void AddToDoNotSell(string itemId)
    {
        if (!ItemsManager.GetDoNotSellList().Contains(itemId))
        {
            ItemsManager.GetDoNotSellList().Add(itemId);
        }
    }

    private void RemoveFromDoNotSell(string itemId)
    {
        if (ItemsManager.GetDoNotSellList().Contains(itemId))
        {
            ItemsManager.GetDoNotSellList().Remove(itemId);
        }
    }

    private class ItemInfo
    {
        public uint Entry { get; }
        public int MinLevel { get; }
        public int MaxLevel { get; }

        public ItemInfo(uint entry, int minLevel, int maxLevel)
        {
            Entry = entry;
            MinLevel = minLevel;
            MaxLevel = maxLevel;
        }
    }
}



I'm trying do a little database with item, which bot have to buy, when they're gone, also add to plugin check my lvl and check lvl item, swap then when he's had bigger lvl, also add food/drinks to don't sell list and delete then they become low lvl. 

I hope for your help, if you help me with this, I upload this plugin for everyone, who have same problems and they can't use HMP or Wholesome vendor... 

@Droidz

Link to comment
Share on other sites

Hi,

Your code has several syntax problems (which I fixed), and some logic problems (which I did not correct), but I gave advice in the code comments (I did not test the code) :

using System.Collections.Generic;
using System.Linq;
using wManager.Wow.Helpers;
using wManager.Wow.ObjectManager;
using wManager.Plugin;
using robotManager.Helpful;
using wManager.Wow.Bot.States;

public class Main : IPlugin
{
    private readonly List<ItemInfo> foodItems = new List<ItemInfo>
    {
        new ItemInfo(4540, 1, 5),
        new ItemInfo(4541, 5, 15),
        new ItemInfo(4542, 10, 20),
        new ItemInfo(4544, 15, 25),
        new ItemInfo(4601, 20, 30),
        new ItemInfo(8950, 25, 35),
        new ItemInfo(8076, 30, 40),
        new ItemInfo(4599, 35, 45),
        new ItemInfo(2287, 40, 50),
        new ItemInfo(8952, 45, 55),
        new ItemInfo(2679, 50, 60),
        new ItemInfo(2680, 55, 65),
        new ItemInfo(2681, 60, 70),
        new ItemInfo(2682, 65, 75),
        new ItemInfo(2684, 70, 75),
        new ItemInfo(2687, 75, 80),
        new ItemInfo(2683, 80, 80),
        new ItemInfo(2681, 55, 60),
        new ItemInfo(2685, 60, 65),
        new ItemInfo(3727, 65, 70),
        new ItemInfo(3726, 70, 75),
        new ItemInfo(3728, 75, 80),
        new ItemInfo(4594, 80, 80)
    };

    private readonly List<ItemInfo> waterItems = new List<ItemInfo>
    {
        new ItemInfo(159, 1, 5),
        new ItemInfo(1179, 5, 15),
        new ItemInfo(1205, 15, 25),
        new ItemInfo(1708, 25, 35),
        new ItemInfo(1645, 35, 45),
        new ItemInfo(8766, 45, 55),
        new ItemInfo(28399, 55, 65),
        new ItemInfo(33044, 65, 75),
        new ItemInfo(27860, 75, 80),
        new ItemInfo(33445, 80, 80),
        new ItemInfo(3356, 10, 20),
        new ItemInfo(2287, 20, 30),
        new ItemInfo(27422, 30, 40),
        new ItemInfo(33176, 40, 50),
        new ItemInfo(33177, 50, 60),
        new ItemInfo(33178, 60, 70),
        new ItemInfo(33179, 70, 80),
        new ItemInfo(33180, 80, 80),
        new ItemInfo(40731, 80, 80),
        new ItemInfo(19999, 40, 50),
        new ItemInfo(5469, 15, 25),
        new ItemInfo(13923, 30, 40),
        new ItemInfo(13443, 60, 70)
    };

    public void Initialize()
    {
        Logging.Write("[FoodWaterPlugin] Initialized.");
        /* Should better to do run your code in OnBeforeCheckIfNeedToRunState event of ToTown state
        robotManager.Events.FiniteStateMachineEvents.OnBeforeCheckIfNeedToRunState += (engine, state, cancelable) =>
        {
            if (Conditions.InGameAndConnectedAndAliveAndProductStartedNotInPause &&
                !Conditions.IsAttackedAndCannotIgnore &&
                state is ToTown)
            {
                CheckAndBuyFoodWater();
            }
        };
         */
        EventsLuaWithArgs.OnEventsLuaStringWithArgs += OnSpellCastSucceeded;
        CheckAndBuyFoodWater();
    }

    public void Dispose()
    {
        EventsLuaWithArgs.OnEventsLuaStringWithArgs -= OnSpellCastSucceeded;
        Logging.Write("[FoodWaterPlugin] Disposed.");
    }

    public void Settings()
    {
    }

    private void OnSpellCastSucceeded(string luaEvent, List<string> args)
    {
        /* Maybe should be more strict with conditions
         if (!Conditions.InGameAndConnectedAndAliveAndProductStartedNotInPause ||
            Conditions.IsAttackedAndCannotIgnore)
            return;
        */
        
        if (luaEvent == "UNIT_SPELLCAST_SUCCEEDED" &&
            args.Count > 0 &&
            args[0] == "player")
        {
            CheckAndBuyFoodWater();
        }
    }

    private void CheckAndBuyFoodWater()
    {
        if (ObjectManager.Me.IsDead || ObjectManager.Me.InCombatFlagOnly)
            return;

        int foodCount = GetItemCount(GetBestFood());
        int waterCount = GetItemCount(GetBestWater());

        if (foodCount < 5 || waterCount < 5)
        {
            GoToVendorAndBuy();
        }
    }

    private uint GetBestFood()
    {
        var playerLevel = ObjectManager.Me.Level;
        return foodItems.Where(item => item.MinLevel <= playerLevel && item.MaxLevel >= playerLevel)
                        .OrderByDescending(item => item.MinLevel)
                        .Select(item => item.Entry)
                        .FirstOrDefault();
    }

    private uint GetBestWater()
    {
        var playerLevel = ObjectManager.Me.Level;
        return waterItems.Where(item => item.MinLevel <= playerLevel && item.MaxLevel >= playerLevel)
                         .OrderByDescending(item => item.MinLevel)
                         .Select(item => item.Entry)
                         .FirstOrDefault();
    }

    private int GetItemCount(uint itemId)
    {
        return ItemsManager.GetItemCountByIdLUA(itemId);
    }

    private void GoToVendorAndBuy()
    {
        /* Should use default totown state, don't forget to add vendor to npc db
        var foodName = ItemsManager.GetNameById(GetBestFood());
        var waterName = ItemsManager.GetNameById(GetBestWater());
        wManager.wManagerSetting.CurrentSetting.TryToUseBestBagFoodDrink = false;
        wManager.wManagerSetting.CurrentSetting.FoodAmount = 20;
        wManager.wManagerSetting.CurrentSetting.DrinkAmount = 20;
        wManager.wManagerSetting.CurrentSetting.FoodName = foodName;
        wManager.wManagerSetting.CurrentSetting.DrinkName = waterName;
        ToTown.ForceToTown = true;
        */
        
        // Ищем ближайшего вендора
        WoWUnit vendor = ObjectManager.GetNearestWoWUnit(ObjectManager.GetWoWUnitVendor());
        if (vendor != null)
        {
            // Идем к вендору и покупаем
            MovementManager.Go(PathFinder.FindPath(vendor.Position));
            Interact.InteractGameObject(vendor.GetBaseAddress);
            Lua.LuaDoString($"BuyItem({GetBestFood()}, 20)");
            Lua.LuaDoString($"BuyItem({GetBestWater()}, 20)");
            AddToDoNotSell(GetBestFood());
            AddToDoNotSell(GetBestWater());
        }
    }

    private void AddToDoNotSell(uint itemId)
    {
        var name = ItemsManager.GetNameById(itemId);
        if (!string.IsNullOrWhiteSpace(name) &&
            !wManager.wManagerSetting.CurrentSetting.DoNotSellList.Contains(name))
        {
            wManager.wManagerSetting.CurrentSetting.DoNotSellList.Add(name);
        }
    }

    private void RemoveFromDoNotSell(uint itemId)
    {
        var name = ItemsManager.GetNameById(itemId);
        if (!string.IsNullOrWhiteSpace(name) &&
            wManager.wManagerSetting.CurrentSetting.DoNotSellList.Contains(name))
        {
            wManager.wManagerSetting.CurrentSetting.DoNotSellList.Remove(name);
        }
    }

    private class ItemInfo
    {
        public uint Entry { get; }
        public int MinLevel { get; }
        public int MaxLevel { get; }

        public ItemInfo(uint entry, int minLevel, int maxLevel)
        {
            Entry = entry;
            MinLevel = minLevel;
            MaxLevel = maxLevel;
        }
    }
}

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...