Minecraft-Console-Client/MinecraftClient/ChatBots/AutoDrop.cs

237 lines
9 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using MinecraftClient.Inventory;
2022-10-05 15:02:30 +08:00
using Tomlet.Attributes;
using static MinecraftClient.ChatBots.AutoDrop.Configs;
namespace MinecraftClient.ChatBots
{
2022-10-05 15:02:30 +08:00
public class AutoDrop : ChatBot
{
2022-10-05 15:02:30 +08:00
public static Configs Config = new();
[TomlDoNotInlineObject]
public class Configs
{
2022-10-05 15:02:30 +08:00
[NonSerialized]
private const string BotName = "AutoDrop";
2022-10-05 15:02:30 +08:00
public bool Enabled = false;
[TomlInlineComment("$ChatBot.AutoDrop.Mode$")]
2022-10-05 15:02:30 +08:00
public DropMode Mode = DropMode.include;
2022-10-05 15:02:30 +08:00
public List<ItemType> Items = new() { ItemType.Cobblestone, ItemType.Dirt };
public void OnSettingUpdate() { }
public enum DropMode
{
2022-10-05 15:02:30 +08:00
include, // Items in list will be dropped
exclude, // Items in list will be kept
everything // Everything will be dropped
}
}
2022-10-05 15:02:30 +08:00
private int updateDebounce = 0;
private readonly int updateDebounceValue = 2;
private int inventoryUpdated = -1;
2020-07-31 15:49:22 +08:00
public string CommandHandler(string cmd, string[] args)
{
if (args.Length > 0)
{
switch (args[0].ToLower())
{
case "on":
2022-10-05 15:02:30 +08:00
Config.Enabled = true;
2020-07-31 15:49:22 +08:00
inventoryUpdated = 0;
OnUpdateFinish();
return Translations.bot_autoDrop_on;
2020-07-31 15:49:22 +08:00
case "off":
2022-10-05 15:02:30 +08:00
Config.Enabled = false;
return Translations.bot_autoDrop_off;
2020-07-31 15:49:22 +08:00
case "add":
if (args.Length >= 2)
{
if (Enum.TryParse(args[1], true, out ItemType item))
2020-07-31 15:49:22 +08:00
{
2022-10-05 15:02:30 +08:00
Config.Items.Add(item);
return string.Format(Translations.bot_autoDrop_added, item.ToString());
2020-07-31 15:49:22 +08:00
}
else
{
return string.Format(Translations.bot_autoDrop_incorrect_name, args[1]);
2020-07-31 15:49:22 +08:00
}
}
else
{
return Translations.cmd_inventory_help_usage + ": add <item name>";
2020-07-31 15:49:22 +08:00
}
case "remove":
if (args.Length >= 2)
{
if (Enum.TryParse(args[1], true, out ItemType item))
2020-07-31 15:49:22 +08:00
{
2022-10-05 15:02:30 +08:00
if (Config.Items.Contains(item))
2020-07-31 15:49:22 +08:00
{
2022-10-05 15:02:30 +08:00
Config.Items.Remove(item);
return string.Format(Translations.bot_autoDrop_removed, item.ToString());
2020-07-31 15:49:22 +08:00
}
else
{
return Translations.bot_autoDrop_not_in_list;
2020-07-31 15:49:22 +08:00
}
}
else
{
return string.Format(Translations.bot_autoDrop_incorrect_name, args[1]);
2020-07-31 15:49:22 +08:00
}
}
else
{
return Translations.cmd_inventory_help_usage + ": remove <item name>";
2020-07-31 15:49:22 +08:00
}
case "list":
2022-10-05 15:02:30 +08:00
if (Config.Items.Count > 0)
2020-07-31 15:49:22 +08:00
{
return string.Format(Translations.bot_autoDrop_list, Config.Items.Count, string.Join("\n", Config.Items));
2020-07-31 15:49:22 +08:00
}
else
{
return Translations.bot_autoDrop_no_item;
2020-07-31 15:49:22 +08:00
}
case "mode":
if (args.Length >= 2)
{
switch (args[1].ToLower())
{
case "include":
2022-10-05 15:02:30 +08:00
Config.Mode = DropMode.include;
break;
case "exclude":
2022-10-05 15:02:30 +08:00
Config.Mode = DropMode.exclude;
break;
case "everything":
2022-10-05 15:02:30 +08:00
Config.Mode = DropMode.everything;
break;
default:
return Translations.bot_autoDrop_unknown_mode; // Unknwon mode. Available modes: Include, Exclude, Everything
}
inventoryUpdated = 0;
OnUpdateFinish();
return string.Format(Translations.bot_autoDrop_switched, Config.Mode.ToString()); // Switched to {0} mode.
}
else
{
return Translations.bot_autoDrop_unknown_mode;
}
2020-07-31 15:49:22 +08:00
default:
return GetHelp();
}
}
else
{
return GetHelp();
}
}
private static string GetHelp()
2020-07-31 15:49:22 +08:00
{
return string.Format(Translations.general_available_cmd, "on, off, add, remove, list, mode");
2020-07-31 15:49:22 +08:00
}
public override void Initialize()
{
if (!GetInventoryEnabled())
{
LogToConsole(Translations.extra_inventory_required);
LogToConsole(Translations.general_bot_unload);
UnloadBot();
return;
}
RegisterChatBotCommand("autodrop", Translations.bot_autoDrop_cmd, GetHelp(), CommandHandler);
RegisterChatBotCommand("ad", Translations.bot_autoDrop_alias, GetHelp(), CommandHandler);
}
public override void Update()
{
if (updateDebounce > 0)
{
updateDebounce--;
if (updateDebounce <= 0)
{
OnUpdateFinish();
}
}
}
public override void OnInventoryUpdate(int inventoryId)
{
2022-10-05 15:02:30 +08:00
if (Config.Enabled)
2020-07-31 15:49:22 +08:00
{
updateDebounce = updateDebounceValue;
// Always interact container if available (larger ID) because they included player inventory (ID 0)
if (inventoryId >= inventoryUpdated)
inventoryUpdated = inventoryId;
2020-07-31 15:49:22 +08:00
}
}
private void OnUpdateFinish()
{
if (inventoryUpdated != -1)
{
if (!GetInventories().ContainsKey(inventoryUpdated))
{
// Inventory updated but no inventory ?
LogDebugToConsole(string.Format(Translations.bot_autoDrop_no_inventory, inventoryUpdated));
return;
}
var inventory = GetInventories()[inventoryUpdated];
var items = inventory.Items.ToDictionary(entry => entry.Key, entry => entry.Value);
2022-10-05 15:02:30 +08:00
if (Config.Mode == DropMode.include)
{
foreach (var item in items)
{
// Ingore crafting result slot
if (item.Key == 0)
continue;
2022-10-05 15:02:30 +08:00
if (Config.Items.Contains(item.Value.Type))
{
// Drop it !!
WindowAction(inventoryUpdated, item.Key, WindowActionType.DropItemStack);
}
}
}
2022-10-05 15:02:30 +08:00
else if (Config.Mode == DropMode.exclude)
{
foreach (var item in items)
{
// Ingore crafting result slot
if (item.Key == 0)
continue;
2022-10-05 15:02:30 +08:00
if (!Config.Items.Contains(item.Value.Type))
{
// Drop it !!
WindowAction(inventoryUpdated, item.Key, WindowActionType.DropItemStack);
}
}
}
else
{
foreach (var item in items)
{
// Ingore crafting result slot
if (item.Key == 0)
continue;
// Drop it !!
WindowAction(inventoryUpdated, item.Key, WindowActionType.DropItemStack);
}
}
2020-07-31 15:49:22 +08:00
inventoryUpdated = -1;
}
}
}
}