New AutoDrop ChatBot (#1159)

* Add AutoDrop ChatBot
* Make AutoCraft config parse case insensitive
This commit is contained in:
ReinforceZwei 2020-07-31 03:06:36 +08:00 committed by GitHub
parent 432f55460d
commit 1b08e78463
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 152 additions and 4 deletions

View file

@ -393,7 +393,7 @@ namespace MinecraftClient.ChatBots
if (recipes.ContainsKey(lastRecipe)) if (recipes.ContainsKey(lastRecipe))
{ {
ItemType itemType; ItemType itemType;
if (Enum.TryParse(value, out itemType)) if (Enum.TryParse(value, true, out itemType))
{ {
if (recipes[lastRecipe].Materials != null && recipes[lastRecipe].Materials.Count > 0) if (recipes[lastRecipe].Materials != null && recipes[lastRecipe].Materials.Count > 0)
{ {
@ -437,7 +437,7 @@ namespace MinecraftClient.ChatBots
if (recipes.ContainsKey(lastRecipe)) if (recipes.ContainsKey(lastRecipe))
{ {
ItemType itemType; ItemType itemType;
if (Enum.TryParse(value, out itemType)) if (Enum.TryParse(value, true, out itemType))
{ {
recipes[lastRecipe].ResultItem = itemType; recipes[lastRecipe].ResultItem = itemType;
} }

View file

@ -0,0 +1,123 @@
using MinecraftClient.Inventory;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MinecraftClient.ChatBots
{
class AutoDrop : ChatBot
{
private enum Mode
{
Include, // Items in list will be dropped
Exclude, // Items in list will be kept
Everything // Everything will be dropped
}
private Mode dropMode = Mode.Include;
private int updateDebounce = 0;
private int updateDebounceValue = 2;
private int inventoryUpdated = -1;
private List<ItemType> itemList = new List<ItemType>();
public AutoDrop(string mode, string itemList)
{
if (!Enum.TryParse(mode, true, out dropMode))
{
LogToConsole("Cannot read drop mode from config. Using include mode.");
}
if (dropMode != Mode.Everything)
this.itemList = ItemListParser(itemList).ToList();
}
/// <summary>
/// Convert an item type string to item type array
/// </summary>
/// <param name="itemList">String to convert</param>
/// <returns>Item type array</returns>
private ItemType[] ItemListParser(string itemList)
{
string trimed = new string(itemList.Where(c => !char.IsWhiteSpace(c)).ToArray());
string[] list = trimed.Split(',');
List<ItemType> result = new List<ItemType>();
foreach (string t in list)
{
ItemType item;
if (Enum.TryParse(t, true, out item))
{
result.Add(item);
}
}
return result.ToArray();
}
public override void Initialize()
{
if (!GetInventoryEnabled())
{
LogToConsole("Inventory handling is disabled. Unloading...");
UnloadBot();
}
}
public override void Update()
{
if (updateDebounce > 0)
{
updateDebounce--;
if (updateDebounce <= 0)
{
OnUpdateFinish();
}
}
}
public override void OnInventoryUpdate(int inventoryId)
{
updateDebounce = updateDebounceValue;
inventoryUpdated = inventoryId;
}
private void OnUpdateFinish()
{
if (inventoryUpdated != -1)
{
var inventory = GetInventories()[inventoryUpdated];
var items = inventory.Items.ToDictionary(entry => entry.Key, entry => entry.Value);
if (dropMode == Mode.Include)
{
foreach (var item in items)
{
if (itemList.Contains(item.Value.Type))
{
// Drop it !!
WindowAction(inventoryUpdated, item.Key, WindowActionType.DropItemStack);
}
}
}
else if (dropMode == Mode.Exclude)
{
foreach (var item in items)
{
if (!itemList.Contains(item.Value.Type))
{
// Drop it !!
WindowAction(inventoryUpdated, item.Key, WindowActionType.DropItemStack);
}
}
}
else
{
foreach (var item in items)
{
// Drop it !!
WindowAction(inventoryUpdated, item.Key, WindowActionType.DropItemStack);
}
}
}
}
}
}

View file

@ -176,6 +176,7 @@ namespace MinecraftClient
if (Settings.AutoFishing_Enabled) { BotLoad(new ChatBots.AutoFishing()); } if (Settings.AutoFishing_Enabled) { BotLoad(new ChatBots.AutoFishing()); }
if (Settings.AutoEat_Enabled) { BotLoad(new ChatBots.AutoEat(Settings.AutoEat_hungerThreshold)); } if (Settings.AutoEat_Enabled) { BotLoad(new ChatBots.AutoEat(Settings.AutoEat_hungerThreshold)); }
if (Settings.AutoCraft_Enabled) { BotLoad(new AutoCraft(Settings.AutoCraft_configFile)); } if (Settings.AutoCraft_Enabled) { BotLoad(new AutoCraft(Settings.AutoCraft_configFile)); }
if (Settings.AutoDrop_Enabled) { BotLoad(new AutoDrop(Settings.AutoDrop_Mode, Settings.AutoDrop_items)); }
//Add your ChatBot here by uncommenting and adapting //Add your ChatBot here by uncommenting and adapting
//BotLoad(new ChatBots.YourBot()); //BotLoad(new ChatBots.YourBot());

View file

@ -73,6 +73,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="AutoTimeout.cs" /> <Compile Include="AutoTimeout.cs" />
<Compile Include="ChatBots\AutoDrop.cs" />
<Compile Include="Commands\Entitycmd.cs" /> <Compile Include="Commands\Entitycmd.cs" />
<Compile Include="ChatBots\Alerts.cs" /> <Compile Include="ChatBots\Alerts.cs" />
<Compile Include="ChatBots\AntiAFK.cs" /> <Compile Include="ChatBots\AntiAFK.cs" />

View file

@ -168,12 +168,17 @@ namespace MinecraftClient
public static bool AutoCraft_Enabled = false; public static bool AutoCraft_Enabled = false;
public static string AutoCraft_configFile = @"autocraft\config.ini"; public static string AutoCraft_configFile = @"autocraft\config.ini";
//AutoDrop
public static bool AutoDrop_Enabled = false;
public static string AutoDrop_Mode = "include";
public static string AutoDrop_items = "";
//Custom app variables and Minecraft accounts //Custom app variables and Minecraft accounts
private static readonly Dictionary<string, object> AppVars = new Dictionary<string, object>(); private static readonly Dictionary<string, object> AppVars = new Dictionary<string, object>();
private static readonly Dictionary<string, KeyValuePair<string, string>> Accounts = new Dictionary<string, KeyValuePair<string, string>>(); private static readonly Dictionary<string, KeyValuePair<string, string>> Accounts = new Dictionary<string, KeyValuePair<string, string>>();
private static readonly Dictionary<string, KeyValuePair<string, ushort>> Servers = new Dictionary<string, KeyValuePair<string, ushort>>(); private static readonly Dictionary<string, KeyValuePair<string, ushort>> Servers = new Dictionary<string, KeyValuePair<string, ushort>>();
private enum ParseMode { Default, Main, AppVars, Proxy, MCSettings, AntiAFK, Hangman, Alerts, ChatLog, AutoRelog, ScriptScheduler, RemoteControl, ChatFormat, AutoRespond, AutoAttack, AutoFishing, AutoEat, AutoCraft }; private enum ParseMode { Default, Main, AppVars, Proxy, MCSettings, AntiAFK, Hangman, Alerts, ChatLog, AutoRelog, ScriptScheduler, RemoteControl, ChatFormat, AutoRespond, AutoAttack, AutoFishing, AutoEat, AutoCraft, AutoDrop };
/// <summary> /// <summary>
/// Load settings from the give INI file /// Load settings from the give INI file
@ -218,6 +223,7 @@ namespace MinecraftClient
case "autofishing": pMode = ParseMode.AutoFishing; break; case "autofishing": pMode = ParseMode.AutoFishing; break;
case "autoeat": pMode = ParseMode.AutoEat; break; case "autoeat": pMode = ParseMode.AutoEat; break;
case "autocraft": pMode = ParseMode.AutoCraft; break; case "autocraft": pMode = ParseMode.AutoCraft; break;
case "autodrop": pMode = ParseMode.AutoDrop; break;
default: pMode = ParseMode.Default; break; default: pMode = ParseMode.Default; break;
} }
} }
@ -505,6 +511,15 @@ namespace MinecraftClient
} }
break; break;
case ParseMode.AutoDrop:
switch (argName.ToLower())
{
case "enabled": AutoDrop_Enabled = str2bool(argValue); break;
case "mode": AutoDrop_Mode = argValue; break;
case "items": AutoDrop_items = argValue; break;
}
break;
case ParseMode.MCSettings: case ParseMode.MCSettings:
switch (argName.ToLower()) switch (argName.ToLower())
{ {
@ -714,11 +729,19 @@ namespace MinecraftClient
+ "enabled=false\r\n" + "enabled=false\r\n"
+ "threshold=6\r\n" + "threshold=6\r\n"
+ "\r\n" + "\r\n"
+ "[AutoCraft]" + "[AutoCraft]\r\n"
+ "# Inventory Handling NEED to be enabled first\r\n" + "# Inventory Handling NEED to be enabled first\r\n"
+ "# Enable terrainandmovements if you need to use crafting table\r\n" + "# Enable terrainandmovements if you need to use crafting table\r\n"
+ "enabled=false\r\n" + "enabled=false\r\n"
+ "configfile=autocraft\\config.ini\r\n" + "configfile=autocraft\\config.ini\r\n"
+ "\r\n"
+ "[AutoDrop]\r\n"
+ "# Inventory Handling NEED to be enabled first\r\n"
+ "enabled=false\r\n"
+ "mode=include # include, exclude or everything. Include: drop item IN the list. Exclude: drop item NOT IN the list\r\n"
+ "items= # separate each item with comma ','\r\n"
+ "# For the naming of the items, please see \r\n"
+ "# https://github.com/ORelio/Minecraft-Console-Client/blob/master/MinecraftClient/Inventory/ItemType.cs \r\n"
+ "\r\n", Encoding.UTF8); + "\r\n", Encoding.UTF8);
} }