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

120 lines
4 KiB
C#
Raw Normal View History

2020-07-07 11:38:15 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MinecraftClient.Inventory;
2020-07-09 14:30:24 +08:00
using MinecraftClient.Mapping;
2020-07-07 11:38:15 +08:00
namespace MinecraftClient.ChatBots
{
class AutoCarft : ChatBot
{
private bool waitingForResult = false;
private int inventoryInUse;
2020-07-09 14:30:24 +08:00
private enum ActionType
{
MoveTo,
WaitForUpdate,
Repeat
}
private class ActionStep
{
public ActionType Action;
public int Slot;
public int InventoryID;
}
2020-07-07 11:38:15 +08:00
public override void Initialize()
{
RegisterChatBotCommand("craft", "craft", CraftCommand);
2020-07-09 14:30:24 +08:00
RegisterChatBotCommand("open", "open", Open);
RegisterChatBotCommand("place", "place", Place);
}
public string Open(string command, string[] args)
{
double x = -258;
double y = 64;
double z = -187;
Location l = new Location(x, y, z);
SendPlaceBlock(l, Direction.Up);
SendAnimation();
return "Try to open";
}
public string Place(string command, string[] args)
{
double x = Convert.ToDouble(args[0]);
double y = Convert.ToDouble(args[1]);
double z = Convert.ToDouble(args[2]);
SendPlaceBlock(new Location(x, y, z), Direction.Down);
return "Try place";
2020-07-07 11:38:15 +08:00
}
public string CraftCommand(string command, string[] args)
{
Dictionary<int, ItemType> recipe = new Dictionary<int, ItemType>
{
{ 1, ItemType.Stone }
};
var inventory = GetInventories()[0];
int slotToPut = -2;
int slotToTake = -2;
inventoryInUse = 0;
foreach (KeyValuePair<int, ItemType> slot in recipe)
{
slotToPut = slot.Key + 1;
2020-07-09 14:30:24 +08:00
slotToTake = -2;
2020-07-07 11:38:15 +08:00
// Find material in our inventory
foreach (KeyValuePair<int, Item> item in inventory.Items)
{
if (slot.Value == item.Value.Type)
{
slotToTake = item.Key;
break;
}
}
if (slotToTake != -2)
{
// move found material to correct crafting slot
WindowAction(0, slotToTake, WindowActionType.LeftClick);
WindowAction(0, slotToPut, WindowActionType.LeftClick);
}
}
if (slotToPut != -2 && slotToTake != -2)
{
waitingForResult = true;
// Now wait for server to update the slot 0, craft result
return "Waiting for result";
}
else return "Failed before waiting for result";
}
public override void OnInventoryUpdate(int inventoryId)
{
ConsoleIO.WriteLine("Inventory " + inventoryId + " is being updated");
if (waitingForResult && inventoryInUse == inventoryId)
{
var inventory = GetInventories()[inventoryId];
if (inventory.Items.ContainsKey(0))
{
// slot 0 have item, click on it
WindowAction(0, 0, WindowActionType.LeftClick);
// Now wait for server to update our inventory
ConsoleIO.WriteLine("Crafting success");
}
else if (inventory.Items.ContainsKey(-1))
{
// Server have updated our cursor to the item we want to take out from craft result
// Now put the item back to our inventory
WindowAction(0, 37, WindowActionType.LeftClick);
ConsoleIO.WriteLine("Moved crafted item to inventory");
waitingForResult = false;
}
}
}
}
}