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

225 lines
12 KiB
C#
Raw Normal View History

using System;
2020-03-26 15:01:42 +08:00
using System.Collections.Generic;
using System.Linq;
using MinecraftClient.Inventory;
namespace MinecraftClient.Commands
{
class Inventory : Command
2020-03-26 15:01:42 +08:00
{
public override string CmdName { get { return "inventory"; } }
public override string CmdUsage { get { return GetBasicUsage(); } }
public override string CmdDesc { get { return "cmd.inventory.desc"; } }
2020-03-26 15:01:42 +08:00
public override string Run(McClient 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() == "creativegive")
2020-05-25 21:39:24 +02:00
{
if (args.Length >= 4)
{
int slot = int.Parse(args[1]);
ItemType itemType = ItemType.Stone;
if (Enum.TryParse(args[2], true, out itemType))
2020-05-25 21:39:24 +02:00
{
if (handler.GetGamemode() == 1)
{
int count = int.Parse(args[3]);
Implement OnMapData, OnTitle, UpdateSign, OnEntityEquipment, Useblock (#1071) * + Fix null PlayerInventory + Fix null PlayerInventory * Update Protocol18.cs * Update McTcpClient.cs + Fix https://github.com/ORelio/Minecraft-Console-Client/issues/1022 * Update Protocol18.cs + MapData * Update PacketIncomingType.cs + MapData * Update Protocol18PacketTypes.cs * Update IMinecraftComHandler.cs + OnMapData * Update McTcpClient.cs + OnMapData * Update ChatBot.cs + OnMapData * Update Protocol18.cs * Update Protocol18PacketTypes.cs + Fix * Update PacketIncomingType.cs + Title * Update Protocol18PacketTypes.cs * Update Protocol18.cs * Update IMinecraftComHandler.cs + OnTitle * Update McTcpClient.cs * Update ChatBot.cs + OnTitle * Update Protocol18.cs Fix * Update IMinecraftComHandler.cs * Update McTcpClient.cs * add ClearInventories() * add ClearInventories() * Update McTcpClient.cs + OnTitle * Preparing to Add BlockAction * Update PacketOutgoingType.cs * Update PacketOutgoingType.cs * Update Protocol18.cs + SendUpdateSign * Update Protocol16.cs + SendUpdateSign * Update IMinecraftCom.cs + SendUpdateSign * Update McTcpClient.cs + UpdateSign * Update ChatBot.cs + UpdateSign * Update McTcpClient.cs Update PlaceBlock * Update ChatBot.cs * Update McTcpClient.cs * add SendCreativeInventoryAction nbt add SendCreativeInventoryAction nbt * Update Protocol18.cs * Update Protocol16.cs * Update McTcpClient.cs * Update ChatBot.cs * Update Inventory.cs * Update Protocol18PacketTypes.cs * Update PacketIncomingType.cs * Update Protocol18PacketTypes.cs * Update Protocol18PacketTypes.cs Fix * Update Protocol18PacketTypes.cs Fix * Update IMinecraftComHandler.cs * Update IMinecraftComHandler.cs * Update ChatBot.cs * Update McTcpClient.cs + OnEntityEquipment * Update Protocol18.cs * Update McTcpClient.cs * Update McTcpClient.cs * Update McTcpClient.cs * Update ChatBot.cs * Update McTcpClient.cs * Update McTcpClient.cs * Update ChatBot.cs * Update McTcpClient.cs * Update McTcpClient.cs * Update ChatBot.cs * Update McTcpClient.cs * Update McTcpClient.cs * Update Protocol18.cs * Update McTcpClient.cs * Update ChatBot.cs * Update ChatBot.cs * Update McTcpClient.cs * Update McTcpClient.cs * Update McTcpClient.cs * Update Protocol18.cs * Create Useblock.cs * Update MinecraftClient.csproj * Update McTcpClient.cs
2020-06-20 17:57:07 +05:00
if (handler.DoCreativeGive(slot, itemType, count, null))
return Translations.Get("cmd.inventory.creative_done", itemType, count, slot);
else return Translations.Get("cmd.inventory.creative_fail");
}
else return Translations.Get("cmd.inventory.need_creative");
2020-05-25 21:39:24 +02:00
}
else
{
return GetCmdDescTranslated();
2020-05-25 21:39:24 +02:00
}
}
else return GetCmdDescTranslated();
2020-05-25 21:39:24 +02:00
}
else if (args[0].ToLower().StartsWith("p"))
{
// player inventory is always ID 0
inventoryId = 0;
}
else if (args[0].ToLower().StartsWith("c"))
{
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 Translations.Get("cmd.inventory.container_not_found");
}
else if (args[0].ToLower() == "help")
{
if (args.Length >= 2)
{
return GetSubCommandHelp(args[1]);
}
else return GetHelp();
}
else inventoryId = int.Parse(args[0]);
string action = args.Length > 1
? args[1].ToLower()
: "list";
switch (action)
{
case "close":
if (handler.CloseInventory(inventoryId))
return Translations.Get("cmd.inventory.close", inventoryId);
else return Translations.Get("cmd.inventory.close_fail", inventoryId);
case "list":
Container inventory = handler.GetInventory(inventoryId);
if(inventory==null)
return Translations.Get("cmd.inventory.not_exist", inventoryId);
List<string> response = new List<string>();
response.Add(Translations.Get("cmd.inventory.inventory") + " #" + inventoryId + " - " + inventory.Title + "§8");
foreach (KeyValuePair<int, Item> item in inventory.Items)
{
string displayName = item.Value.DisplayName;
if (String.IsNullOrEmpty(displayName))
{
if (item.Value.Damage != 0)
response.Add(String.Format(" #{0}: {1} x{2} | {3}: {4}", item.Key, item.Value.Type, item.Value.Count, Translations.Get("cmd.inventory.damage"), item.Value.Damage));
else
response.Add(String.Format(" #{0}: {1} x{2}", item.Key, item.Value.Type, item.Value.Count));
}
else
{
if (item.Value.Damage != 0)
response.Add(String.Format(" #{0}: {1} x{2} - {3}§8 | {4}: {5}", item.Key, item.Value.Type, item.Value.Count, displayName, Translations.Get("cmd.inventory.damage"), item.Value.Damage));
else
response.Add(String.Format(" #{0}: {1} x{2} - {3}§8", item.Key, item.Value.Type, item.Value.Count, displayName));
}
}
if (inventoryId == 0) response.Add(Translations.Get("cmd.inventory.hotbar", (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;
string keyName = "cmd.inventory.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;
keyName = "cmd.inventory.right";
2020-05-23 09:20:48 +08:00
}
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;
keyName = "cmd.inventory.middle";
2020-05-23 09:20:48 +08:00
}
2020-05-22 23:06:20 +08:00
}
2020-05-24 09:33:21 +08:00
handler.DoWindowAction(inventoryId, slot, actionType);
return Translations.Get("cmd.inventory.clicking", Translations.Get(keyName), slot, inventoryId);
}
else return CmdUsage;
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 Translations.Get("cmd.inventory.no_item", slot);
2020-05-24 15:06:06 +08:00
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 Translations.Get("cmd.inventory.drop_stack", slot);
else return Translations.Get("cmd.inventory.drop", slot);
2020-05-24 15:06:06 +08:00
}
else
{
return "Failed";
}
2020-05-24 10:33:09 +08:00
}
else return GetCmdDescTranslated();
default:
return GetCmdDescTranslated();
}
}
catch (FormatException) { return GetCmdDescTranslated(); }
}
else
{
Dictionary<int, Container> inventories = handler.GetInventories();
List<string> response = new List<string>();
response.Add(Translations.Get("cmd.inventory.inventories") + ":");
foreach (KeyValuePair<int, Container> inventory in inventories)
{
response.Add(String.Format(" #{0}: {1}", inventory.Key, inventory.Value.Title + "§8"));
}
response.Add(CmdUsage);
return String.Join("\n", response);
}
2020-03-26 15:01:42 +08:00
}
else return Translations.Get("extra.inventory_required");
2020-03-26 15:01:42 +08:00
}
#region Methods for commands help
private string GetCommandDesc()
{
return GetBasicUsage() + " Type \"/inventory help\" for more help";
}
private string GetAvailableActions()
{
return Translations.Get("cmd.inventory.help.available") + ": list, close, click, drop.";
}
private string GetBasicUsage()
{
return Translations.Get("cmd.inventory.help.basic") + ": /inventory <player|container|<id>> <action>.";
}
private string GetHelp()
{
return Translations.Get("cmd.inventory.help.help", GetAvailableActions(), GetCreativeGiveHelp());
}
private string GetCreativeGiveHelp()
{
return Translations.Get("cmd.inventory.help.usage") + ": /inventory creativegive <slot> <itemtype> <count>";
}
private string GetSubCommandHelp(string cmd)
{
switch (cmd)
{
case "list":
return Translations.Get("cmd.inventory.help.list") + Translations.Get("cmd.inventory.help.usage") + ": /inventory <player|container|<id>> list";
case "close":
return Translations.Get("cmd.inventory.help.close") + Translations.Get("cmd.inventory.help.usage") + ": /inventory <player|container|<id>> close";
case "click":
return Translations.Get("cmd.inventory.help.click") + Translations.Get("cmd.inventory.help.usage") + ": /inventory <player|container|<id>> click <slot> [left|right|middle]. \nDefault is left click";
case "drop":
return Translations.Get("cmd.inventory.help.drop") + Translations.Get("cmd.inventory.help.usage") + ": /inventory <player|container|<id>> drop <slot> [all]. \nAll means drop full stack";
case "help":
return GetHelp();
default:
return Translations.Get("cmd.inventory.help.unknown") + GetAvailableActions();
}
}
#endregion
2020-03-26 15:01:42 +08:00
}
}