Minecraft-Console-Client/MinecraftClient/Commands/Inventory.cs

134 lines
7.2 KiB
C#
Raw Normal View History

2020-03-26 15:01:42 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MinecraftClient.Inventory;
namespace MinecraftClient.Commands
{
class Inventory : Command
2020-03-26 15:01:42 +08:00
{
2020-03-28 15:01:08 +01:00
public override string CMDName { get { return "inventory"; } }
2020-05-24 15:17:05 +02:00
public override string CMDDesc { get { return "inventory <<id>|player|container> <list|close|drop <slot> <1|all>|click <slot> <left|right|middle>>: Interact with inventories"; } }
2020-03-26 15:01:42 +08:00
public override string Run(McTcpClient handler, string command, Dictionary<string, object> localVars)
2020-03-26 15:01:42 +08:00
{
if (handler.GetInventoryEnabled())
2020-03-26 15:01:42 +08:00
{
string[] args = getArgs(command);
if (args.Length >= 1)
{
try
{
int inventoryId;
if (args[0].ToLower() == "player")
{
// player inventory is always ID 0
inventoryId = 0;
}
else if (args[0].ToLower() == "container")
{
List<int> availableIds = handler.GetInventories().Keys.ToList();
availableIds.Remove(0); // remove player inventory ID from list
if (availableIds.Count == 1)
inventoryId = availableIds[0]; // one container, use it
else return "Cannot find container, please retry with explicit ID";
}
else inventoryId = int.Parse(args[0]);
string action = args.Length > 1
? args[1].ToLower()
: "list";
switch (action)
{
case "close":
if (handler.CloseInventory(inventoryId))
return "Closing Inventoy #" + inventoryId;
else return "Failed to close Inventory #" + inventoryId;
case "list":
Container inventory = handler.GetInventory(inventoryId);
if(inventory==null)
return "Inventory #" + inventoryId + " do not exist";
List<string> response = new List<string>();
response.Add("Inventory #" + inventoryId + " - " + inventory.Title + "§8");
foreach (KeyValuePair<int, Item> item in inventory.Items)
{
string displayName = item.Value.DisplayName;
if (String.IsNullOrEmpty(displayName))
response.Add(String.Format(" #{0}: {1} x{2}", item.Key, item.Value.Type, item.Value.Count));
else response.Add(String.Format(" #{0}: {1} x{2} - {3}§8", item.Key, item.Value.Type, item.Value.Count, displayName));
}
2020-04-08 18:24:26 +08:00
if (inventoryId == 0) response.Add("Your selected hotbar is " + (handler.GetCurrentSlot() + 1));
return String.Join("\n", response.ToArray());
case "click":
2020-05-22 23:06:20 +08:00
if (args.Length >= 3)
{
int slot = int.Parse(args[2]);
2020-05-24 09:33:21 +08:00
WindowActionType actionType = WindowActionType.LeftClick;
2020-05-23 09:20:48 +08:00
string keyName = "Left";
2020-05-24 15:17:05 +02:00
if (args.Length >= 4)
2020-05-22 23:06:20 +08:00
{
string b = args[3];
2020-05-24 09:33:21 +08:00
if (b.ToLower()[0] == 'r')
2020-05-23 09:20:48 +08:00
{
2020-05-24 09:33:21 +08:00
actionType = WindowActionType.RightClick;
2020-05-23 09:20:48 +08:00
keyName = "Right";
}
2020-05-24 09:33:21 +08:00
if (b.ToLower()[0] == 'm')
2020-05-23 09:20:48 +08:00
{
2020-05-24 09:33:21 +08:00
actionType = WindowActionType.MiddleClick;
2020-05-23 09:20:48 +08:00
keyName = "Middle";
}
2020-05-22 23:06:20 +08:00
}
2020-05-24 09:33:21 +08:00
handler.DoWindowAction(inventoryId, slot, actionType);
2020-05-23 09:20:48 +08:00
return keyName + " clicking slot " + slot + " in window #" + inventoryId;
}
else return CMDDesc;
2020-05-24 10:33:09 +08:00
case "drop":
if (args.Length >= 3)
{
int slot = int.Parse(args[2]);
2020-05-24 15:06:06 +08:00
// check item exist
if (!handler.GetInventory(inventoryId).Items.ContainsKey(slot))
return "No item in slot #" + slot;
WindowActionType actionType = WindowActionType.DropItem;
2020-05-24 15:17:05 +02:00
if (args.Length >= 4)
2020-05-24 15:06:06 +08:00
{
if (args[3].ToLower() == "all")
{
actionType = WindowActionType.DropItemStack;
}
}
if (handler.DoWindowAction(inventoryId, slot, actionType))
{
2020-05-24 15:17:05 +02:00
if (actionType == WindowActionType.DropItemStack)
return "Dropped whole item stack from slot #" + slot;
else return "Dropped 1 item from slot #" + slot;
2020-05-24 15:06:06 +08:00
}
else
{
return "Failed";
}
2020-05-24 10:33:09 +08:00
}
2020-05-24 15:06:06 +08:00
else return CMDDesc;
default:
return CMDDesc;
}
}
catch (FormatException) { return CMDDesc; }
}
else
{
Dictionary<int, Container> inventories = handler.GetInventories();
List<string> response = new List<string>();
response.Add("Inventories:");
foreach (var inventory in inventories)
response.Add(String.Format(" #{0}: {1}", inventory.Key, inventory.Value.Title + "§8"));
response.Add(CMDDesc);
return String.Join("\n", response);
}
2020-03-26 15:01:42 +08:00
}
else return "Please enable inventoryhandling in config to use this command.";
2020-03-26 15:01:42 +08:00
}
}
}