2020-05-26 00:16:53 +05:00
using System ;
2020-03-26 15:01:42 +08:00
using System.Collections.Generic ;
2022-10-04 11:53:07 +08:00
using System.Globalization ;
2020-03-26 15:01:42 +08:00
using System.Linq ;
2022-09-08 17:19:13 +08:00
using System.Text ;
2022-10-26 08:54:54 +08:00
using Brigadier.NET ;
2020-03-26 15:01:42 +08:00
using MinecraftClient.Inventory ;
namespace MinecraftClient.Commands
{
2020-03-29 18:41:26 +02:00
class Inventory : Command
2020-03-26 15:01:42 +08:00
{
2020-10-17 19:41:31 +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
2022-10-26 08:54:54 +08:00
public override void RegisterCommand ( McClient handler , CommandDispatcher < CommandSource > dispatcher )
{
}
2022-09-08 17:19:13 +08:00
public override string Run ( McClient handler , string command , Dictionary < string , object > ? localVars )
2020-03-26 15:01:42 +08:00
{
2020-03-29 18:41:26 +02:00
if ( handler . GetInventoryEnabled ( ) )
2020-03-26 15:01:42 +08:00
{
2022-10-02 18:31:08 +08:00
string [ ] args = GetArgs ( command ) ;
2020-03-29 18:41:26 +02:00
if ( args . Length > = 1 )
{
2022-09-08 17:19:13 +08:00
int inventoryId ;
if ( args [ 0 ] . ToLower ( ) = = "creativegive" )
2020-03-29 18:41:26 +02:00
{
2022-09-08 17:19:13 +08:00
if ( args . Length > = 4 )
2020-05-25 21:39:24 +02:00
{
2022-10-04 11:53:07 +08:00
if ( ! int . TryParse ( args [ 1 ] , NumberStyles . Any , CultureInfo . CurrentCulture , out int slot ) )
2022-09-08 17:19:13 +08:00
return GetCmdDescTranslated ( ) ;
if ( Enum . TryParse ( args [ 2 ] , true , out ItemType itemType ) )
2020-05-25 21:39:24 +02:00
{
2022-09-08 17:19:13 +08:00
if ( handler . GetGamemode ( ) = = 1 )
2020-05-25 21:39:24 +02:00
{
2022-10-04 11:53:07 +08:00
if ( ! int . TryParse ( args [ 3 ] , NumberStyles . Any , CultureInfo . CurrentCulture , out int count ) )
2022-09-08 17:19:13 +08:00
return GetCmdDescTranslated ( ) ;
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" ) ;
2020-05-25 21:39:24 +02:00
}
else
2022-09-08 17:19:13 +08:00
return Translations . Get ( "cmd.inventory.need_creative" ) ;
2020-05-25 21:39:24 +02:00
}
2022-09-08 17:19:13 +08:00
else
return GetCmdDescTranslated ( ) ;
2020-05-25 21:39:24 +02:00
}
2022-09-08 17:19:13 +08:00
else
return GetCmdDescTranslated ( ) ;
}
else if ( args [ 0 ] . ToLower ( ) = = "creativedelete" )
{
if ( args . Length > = 2 )
2021-05-15 20:11:07 +02:00
{
2022-10-04 11:53:07 +08:00
if ( ! int . TryParse ( args [ 1 ] , NumberStyles . Any , CultureInfo . CurrentCulture , out int slot ) )
2022-09-08 17:19:13 +08:00
return GetCmdDescTranslated ( ) ;
if ( handler . GetGamemode ( ) = = 1 )
2021-05-15 20:11:07 +02:00
{
2022-09-08 17:19:13 +08:00
if ( handler . DoCreativeGive ( slot , ItemType . Null , 0 , null ) )
return Translations . Get ( "cmd.inventory.creative_delete" , slot ) ;
else
return Translations . Get ( "cmd.inventory.creative_fail" ) ;
2021-05-15 20:11:07 +02:00
}
2022-09-08 17:19:13 +08:00
else
return Translations . Get ( "cmd.inventory.need_creative" ) ;
2021-05-15 20:11:07 +02:00
}
2022-09-22 21:58:51 +02:00
else
2022-09-08 17:19:13 +08:00
return GetCmdDescTranslated ( ) ;
}
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 > 0 )
inventoryId = availableIds . Max ( ) ; // use foreground container
else
return Translations . Get ( "cmd.inventory.container_not_found" ) ;
}
2022-09-22 22:03:57 +02:00
else if ( args [ 0 ] . ToLower ( ) . StartsWith ( "inventories" ) | | args [ 0 ] . ToLower ( ) . StartsWith ( "i" ) )
2022-09-22 21:58:51 +02:00
{
Dictionary < int , Container > inventories = handler . GetInventories ( ) ;
List < int > availableIds = inventories . Keys . ToList ( ) ;
StringBuilder response = new ( ) ;
2022-09-23 20:20:02 +02:00
response . AppendLine ( Translations . Get ( "cmd.inventory.inventories_available" ) ) ;
2022-09-22 21:58:51 +02:00
foreach ( int id in availableIds )
response . AppendLine ( String . Format ( " #{0} - {1}§8" , id , inventories [ id ] . Title ) ) ;
return response . ToString ( ) ;
}
2022-09-23 20:20:02 +02:00
else if ( args [ 0 ] . ToLower ( ) . StartsWith ( "search" ) | | args [ 0 ] . ToLower ( ) . StartsWith ( "s" ) )
{
if ( args . Length < 2 )
return GetCmdDescTranslated ( ) ;
if ( ! Enum . TryParse ( args [ 1 ] , true , out ItemType parsedItemType ) )
return GetCmdDescTranslated ( ) ;
bool shouldUseItemCount = args . Length > = 3 ;
int itemCount = 0 ;
2022-10-04 11:53:07 +08:00
if ( shouldUseItemCount & & ! int . TryParse ( args [ 2 ] , NumberStyles . Any , CultureInfo . CurrentCulture , out itemCount ) )
2022-09-23 20:20:02 +02:00
return GetCmdDescTranslated ( ) ;
Dictionary < int , Container > inventories = handler . GetInventories ( ) ;
Dictionary < int , List < Item > > foundItems = new ( ) ;
List < Container > availableInventories = inventories . Values . ToList ( ) ;
availableInventories . ForEach ( inventory = >
{
inventory . Items . Values
. ToList ( )
2022-10-02 18:31:08 +08:00
. FindAll ( item = > item . Type = = parsedItemType & & ( ! shouldUseItemCount | | item . Count = = itemCount ) )
2022-09-23 20:20:02 +02:00
. ForEach ( item = >
{
if ( ! foundItems . ContainsKey ( inventory . ID ) )
{
foundItems . Add ( inventory . ID , new List < Item > ( ) { item } ) ;
return ;
}
List < Item > invItems = foundItems [ inventory . ID ] ;
invItems . Add ( item ) ;
foundItems . Remove ( inventory . ID ) ;
foundItems . Add ( inventory . ID , invItems ) ;
} ) ;
} ) ;
if ( foundItems . Count = = 0 )
return Translations . Get ( "cmd.inventory.no_found_items" ) ;
StringBuilder response = new ( ) ;
response . AppendLine ( Translations . Get ( "cmd.inventory.found_items" ) + ":" ) ;
foreach ( ( int invId , List < Item > itemsList ) in new SortedDictionary < int , List < Item > > ( foundItems ) )
{
if ( itemsList . Count > 0 )
{
response . AppendLine ( String . Format ( "{0} (#{1}):" , inventories [ invId ] . Title , invId ) ) ;
foreach ( Item item in itemsList )
2022-10-17 20:00:33 +08:00
response . AppendLine ( String . Format ( "\t- {0}" , item . ToFullString ( ) ) ) ;
2022-09-23 20:20:02 +02:00
response . AppendLine ( " " ) ;
}
}
return response . ToString ( ) ;
}
2022-09-08 17:19:13 +08:00
else if ( args [ 0 ] . ToLower ( ) = = "help" )
{
if ( args . Length > = 2 )
return GetSubCommandHelp ( args [ 1 ] ) ;
else
return GetHelp ( ) ;
}
2022-10-04 11:53:07 +08:00
else if ( ! int . TryParse ( args [ 0 ] , NumberStyles . Any , CultureInfo . CurrentCulture , out inventoryId ) )
2022-09-08 17:19:13 +08:00
return GetCmdDescTranslated ( ) ;
Container ? inventory = handler . GetInventory ( inventoryId ) ;
if ( inventory = = null )
return Translations . Get ( "cmd.inventory.not_exist" , inventoryId ) ;
string action = args . Length > 1 ? args [ 1 ] . ToLower ( ) : "list" ;
if ( action = = "close" )
{
if ( handler . CloseInventory ( inventoryId ) )
return Translations . Get ( "cmd.inventory.close" , inventoryId ) ;
else
return Translations . Get ( "cmd.inventory.close_fail" , inventoryId ) ;
}
else if ( action = = "list" )
{
StringBuilder response = new ( ) ;
response . Append ( Translations . Get ( "cmd.inventory.inventory" ) ) ;
response . AppendLine ( String . Format ( " #{0} - {1}§8" , inventoryId , inventory . Title ) ) ;
2022-10-02 18:31:08 +08:00
string? asciiArt = inventory . Type . GetAsciiArt ( ) ;
2022-10-05 15:02:30 +08:00
if ( asciiArt ! = null & & Settings . Config . Main . Advanced . ShowInventoryLayout )
2022-09-08 17:19:13 +08:00
response . AppendLine ( asciiArt ) ;
int selectedHotbar = handler . GetCurrentSlot ( ) + 1 ;
foreach ( ( int itemId , Item item ) in new SortedDictionary < int , Item > ( inventory . Items ) )
2020-08-03 20:13:07 +08:00
{
2022-09-08 17:19:13 +08:00
bool isHotbar = inventory . IsHotbar ( itemId , out int hotbar ) ;
string hotbarString = isHotbar ? ( hotbar + 1 ) . ToString ( ) : " " ;
if ( ( hotbar + 1 ) = = selectedHotbar )
hotbarString = ">" + hotbarString ;
2022-10-17 20:00:33 +08:00
response . AppendLine ( String . Format ( "{0,2} | #{1,-2}: {2}" , hotbarString , itemId , item . ToFullString ( ) ) ) ;
2020-08-03 20:13:07 +08:00
}
2022-09-08 17:19:13 +08:00
if ( inventoryId = = 0 )
response . AppendLine ( Translations . Get ( "cmd.inventory.hotbar" , ( handler . GetCurrentSlot ( ) + 1 ) ) ) ;
response . Remove ( response . Length - 1 , 1 ) ; // Remove last '\n'
return response . ToString ( ) ;
}
else if ( action = = "click" & & args . Length > = 3 )
{
2022-10-04 11:53:07 +08:00
if ( ! int . TryParse ( args [ 2 ] , NumberStyles . Any , CultureInfo . CurrentCulture , out int slot ) )
2022-09-08 17:19:13 +08:00
return GetCmdDescTranslated ( ) ;
WindowActionType actionType = WindowActionType . LeftClick ;
string keyName = "cmd.inventory.left" ;
if ( args . Length > = 4 )
2020-08-03 20:13:07 +08:00
{
2022-09-08 17:19:13 +08:00
string b = args [ 3 ] ;
if ( b . ToLower ( ) [ 0 ] = = 'r' )
( actionType , keyName ) = ( WindowActionType . RightClick , "cmd.inventory.right" ) ;
else if ( b . ToLower ( ) [ 0 ] = = 'm' )
( actionType , keyName ) = ( WindowActionType . MiddleClick , "cmd.inventory.middle" ) ;
2020-08-03 20:13:07 +08:00
}
2022-09-08 17:19:13 +08:00
handler . DoWindowAction ( inventoryId , slot , actionType ) ;
return Translations . Get ( "cmd.inventory.clicking" , Translations . Get ( keyName ) , slot , inventoryId ) ;
}
else if ( action = = "shiftclick" & & args . Length > = 3 )
{
2022-10-04 11:53:07 +08:00
if ( ! int . TryParse ( args [ 2 ] , NumberStyles . Any , CultureInfo . CurrentCulture , out int slot ) )
2022-09-08 17:19:13 +08:00
return GetCmdDescTranslated ( ) ;
if ( ! handler . DoWindowAction ( inventoryId , slot , WindowActionType . ShiftClick ) )
return Translations . Get ( "cmd.inventory.shiftclick_fail" ) ;
return Translations . Get ( "cmd.inventory.shiftclick" , slot , inventoryId ) ;
}
else if ( action = = "drop" & & args . Length > = 3 )
{
2022-10-04 11:53:07 +08:00
if ( ! int . TryParse ( args [ 2 ] , NumberStyles . Any , CultureInfo . CurrentCulture , out int slot ) )
2022-09-08 17:19:13 +08:00
return GetCmdDescTranslated ( ) ;
// check item exist
if ( ! inventory . Items . ContainsKey ( slot ) )
return Translations . Get ( "cmd.inventory.no_item" , slot ) ;
WindowActionType actionType = WindowActionType . DropItem ;
if ( args . Length > = 4 & & args [ 3 ] . ToLower ( ) = = "all" )
actionType = WindowActionType . DropItemStack ;
if ( handler . DoWindowAction ( inventoryId , slot , actionType ) )
2020-03-29 18:41:26 +02:00
{
2022-09-08 17:19:13 +08:00
if ( actionType = = WindowActionType . DropItemStack )
return Translations . Get ( "cmd.inventory.drop_stack" , slot ) ;
else
return Translations . Get ( "cmd.inventory.drop" , slot ) ;
2020-03-29 18:41:26 +02:00
}
2022-09-08 17:19:13 +08:00
else
return "Failed" ;
2020-03-29 18:41:26 +02:00
}
2022-09-08 17:19:13 +08:00
else
return GetCmdDescTranslated ( ) ;
2020-03-29 18:41:26 +02:00
}
else
{
2022-09-08 17:19:13 +08:00
StringBuilder response = new ( ) ;
2022-09-08 17:39:58 +08:00
response . Append ( Translations . Get ( "cmd.inventory.inventories" ) ) . Append ( ":\n" ) ;
2022-09-08 17:19:13 +08:00
foreach ( ( int invId , Container inv ) in handler . GetInventories ( ) )
response . AppendLine ( String . Format ( " #{0}: {1}§8" , invId , inv . Title ) ) ;
response . Append ( CmdUsage ) ;
return response . ToString ( ) ;
2020-03-29 18:41:26 +02:00
}
2020-03-26 15:01:42 +08:00
}
2022-09-22 21:58:51 +02:00
else
2022-09-08 17:19:13 +08:00
return Translations . Get ( "extra.inventory_required" ) ;
2020-03-26 15:01:42 +08:00
}
2020-08-03 20:13:07 +08:00
#region Methods for commands help
2022-09-08 17:19:13 +08:00
private static string GetAvailableActions ( )
2020-08-03 20:13:07 +08:00
{
2021-05-15 20:11:07 +02:00
return Translations . Get ( "cmd.inventory.help.available" ) + ": list, close, click, drop, creativegive, creativedelete." ;
2020-08-03 20:13:07 +08:00
}
2022-09-08 17:19:13 +08:00
private static string GetBasicUsage ( )
2020-08-03 20:13:07 +08:00
{
2020-10-17 19:41:31 +08:00
return Translations . Get ( "cmd.inventory.help.basic" ) + ": /inventory <player|container|<id>> <action>." ;
2020-08-03 20:13:07 +08:00
}
2022-09-08 17:19:13 +08:00
private static string GetHelp ( )
2020-08-03 20:13:07 +08:00
{
2021-05-15 20:11:07 +02:00
return Translations . Get ( "cmd.inventory.help.help" , GetAvailableActions ( ) ) ;
2020-08-03 20:13:07 +08:00
}
2022-09-08 17:19:13 +08:00
private static string GetSubCommandHelp ( string cmd )
2020-08-03 20:13:07 +08:00
{
2022-09-08 17:19:13 +08:00
string usageStr = ' ' + Translations . Get ( "cmd.inventory.help.usage" ) + ": " ;
return cmd switch
2020-08-03 20:13:07 +08:00
{
2022-09-08 17:19:13 +08:00
"list" = > Translations . Get ( "cmd.inventory.help.list" ) + usageStr + "/inventory <player|container|<id>> list" ,
"close" = > Translations . Get ( "cmd.inventory.help.close" ) + usageStr + "/inventory <player|container|<id>> close" ,
"click" = > Translations . Get ( "cmd.inventory.help.click" ) + usageStr + "/inventory <player|container|<id>> click <slot> [left|right|middle]\nDefault is left click" ,
"shiftclick" = > Translations . Get ( "cmd.inventory.help.shiftclick" ) + usageStr + "/inventory <player|container|<id>> shiftclick <slot>" ,
"drop" = > Translations . Get ( "cmd.inventory.help.drop" ) + usageStr + "/inventory <player|container|<id>> drop <slot> [all]\nAll means drop full stack" ,
"creativegive" = > Translations . Get ( "cmd.inventory.help.creativegive" ) + usageStr + "/inventory creativegive <slot> <itemtype> <amount>" ,
"creativedelete" = > Translations . Get ( "cmd.inventory.help.creativedelete" ) + usageStr + "/inventory creativedelete <slot>" ,
2022-09-23 20:20:02 +02:00
"inventories" = > Translations . Get ( "cmd.inventory.help.inventories" ) + usageStr + "/inventory inventories" ,
"search" = > Translations . Get ( "cmd.inventory.help.search" ) + usageStr + "/inventory search <item type> [count]" ,
2022-09-08 17:19:13 +08:00
"help" = > GetHelp ( ) ,
_ = > Translations . Get ( "cmd.inventory.help.unknown" ) + GetAvailableActions ( ) ,
} ;
2020-08-03 20:13:07 +08:00
}
#endregion
2020-03-26 15:01:42 +08:00
}
}