2020-05-26 00:16:53 +05:00
using System ;
2020-03-26 15:01:42 +08:00
using System.Collections.Generic ;
using System.Linq ;
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
2020-06-20 15:01:16 +02: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
{
2020-03-29 18:41:26 +02:00
string [ ] args = getArgs ( command ) ;
if ( args . Length > = 1 )
{
try
{
2020-05-01 13:41:26 +02:00
int inventoryId ;
2020-08-03 20:13:07 +08:00
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 ;
2020-08-03 20:13:07 +08:00
if ( Enum . TryParse ( args [ 2 ] , true , out itemType ) )
2020-05-25 21:39:24 +02:00
{
2020-06-13 18:00:30 +05:00
if ( handler . GetGamemode ( ) = = 1 )
{
int count = int . Parse ( args [ 3 ] ) ;
2020-06-20 17:57:07 +05:00
if ( handler . DoCreativeGive ( slot , itemType , count , null ) )
2020-10-17 19:41:31 +08:00
return Translations . Get ( "cmd.inventory.creative_done" , itemType , count , slot ) ;
else return Translations . Get ( "cmd.inventory.creative_fail" ) ;
2020-06-13 18:00:30 +05:00
}
2020-10-17 19:41:31 +08:00
else return Translations . Get ( "cmd.inventory.need_creative" ) ;
2020-05-25 21:39:24 +02:00
}
else
{
2020-10-17 19:41:31 +08:00
return GetCmdDescTranslated ( ) ;
2020-05-25 21:39:24 +02:00
}
}
2020-10-17 19:41:31 +08:00
else return GetCmdDescTranslated ( ) ;
2020-05-25 21:39:24 +02:00
}
2021-05-15 20:11:07 +02:00
else if ( args [ 0 ] . ToLower ( ) = = "creativedelete" )
{
if ( args . Length > = 2 )
{
int slot = int . Parse ( args [ 1 ] ) ;
if ( handler . GetGamemode ( ) = = 1 )
{
if ( handler . DoCreativeGive ( slot , ItemType . Null , 0 , null ) )
return Translations . Get ( "cmd.inventory.creative_delete" , slot ) ;
else return Translations . Get ( "cmd.inventory.creative_fail" ) ;
}
else return Translations . Get ( "cmd.inventory.need_creative" ) ;
}
else return GetCmdDescTranslated ( ) ;
}
2020-08-03 20:13:07 +08: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
2021-12-28 11:15:11 +01:00
if ( availableIds . Count > 0 )
inventoryId = availableIds . Max ( ) ; // use foreground container
2020-10-17 19:41:31 +08:00
else return Translations . Get ( "cmd.inventory.container_not_found" ) ;
2020-08-03 20:13:07 +08:00
}
else if ( args [ 0 ] . ToLower ( ) = = "help" )
{
if ( args . Length > = 2 )
{
return GetSubCommandHelp ( args [ 1 ] ) ;
}
else return GetHelp ( ) ;
}
2020-05-01 13:41:26 +02:00
else inventoryId = int . Parse ( args [ 0 ] ) ;
2020-03-29 18:41:26 +02:00
string action = args . Length > 1
? args [ 1 ] . ToLower ( )
: "list" ;
switch ( action )
{
case "close" :
if ( handler . CloseInventory ( inventoryId ) )
2020-10-17 19:41:31 +08:00
return Translations . Get ( "cmd.inventory.close" , inventoryId ) ;
else return Translations . Get ( "cmd.inventory.close_fail" , inventoryId ) ;
2020-03-29 18:41:26 +02:00
case "list" :
Container inventory = handler . GetInventory ( inventoryId ) ;
2021-05-11 14:02:47 +08:00
if ( inventory = = null )
2020-10-17 19:41:31 +08:00
return Translations . Get ( "cmd.inventory.not_exist" , inventoryId ) ;
2021-05-11 14:02:47 +08:00
SortedDictionary < int , Item > itemsSorted = new SortedDictionary < int , Item > ( inventory . Items ) ;
2020-03-29 18:41:26 +02:00
List < string > response = new List < string > ( ) ;
2020-10-17 19:41:31 +08:00
response . Add ( Translations . Get ( "cmd.inventory.inventory" ) + " #" + inventoryId + " - " + inventory . Title + "§8" ) ;
2021-05-29 15:21:38 +02:00
string asciiArt = inventory . Type . GetAsciiArt ( ) ;
if ( asciiArt ! = null & & Settings . DisplayInventoryLayout )
response . Add ( asciiArt ) ;
2021-05-11 14:02:47 +08:00
int selectedHotbar = handler . GetCurrentSlot ( ) + 1 ;
foreach ( KeyValuePair < int , Item > item in itemsSorted )
2020-04-01 22:05:44 +02:00
{
2021-05-11 14:02:47 +08:00
int hotbar ;
bool isHotbar = inventory . IsHotbar ( item . Key , out hotbar ) ;
string hotbarString = isHotbar ? ( hotbar + 1 ) . ToString ( ) : " " ;
if ( ( hotbar + 1 ) = = selectedHotbar )
hotbarString = ">" + hotbarString ;
response . Add ( String . Format ( "{0,2} | #{1,-2}: {2}" , hotbarString , item . Key , item . Value . ToString ( ) ) ) ;
2020-04-01 22:05:44 +02:00
}
2021-05-11 14:02:47 +08:00
if ( inventoryId = = 0 )
response . Add ( Translations . Get ( "cmd.inventory.hotbar" , ( handler . GetCurrentSlot ( ) + 1 ) ) ) ;
2020-03-29 18:41:26 +02:00
return String . Join ( "\n" , response . ToArray ( ) ) ;
case "click" :
2020-05-22 23:06:20 +08:00
if ( args . Length > = 3 )
2020-03-29 18:41:26 +02:00
{
int slot = int . Parse ( args [ 2 ] ) ;
2020-05-24 09:33:21 +08:00
WindowActionType actionType = WindowActionType . LeftClick ;
2020-10-17 19:41:31 +08:00
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 ;
2020-10-17 19:41:31 +08:00
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 ;
2020-10-17 19:41:31 +08:00
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 ) ;
2020-10-17 19:41:31 +08:00
return Translations . Get ( "cmd.inventory.clicking" , Translations . Get ( keyName ) , slot , inventoryId ) ;
2020-03-29 18:41:26 +02:00
}
2020-10-17 19:41:31 +08:00
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 ) )
2020-10-17 19:41:31 +08:00
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 )
2020-10-17 19:41:31 +08:00
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
}
2020-10-17 19:41:31 +08:00
else return GetCmdDescTranslated ( ) ;
2020-03-29 18:41:26 +02:00
default :
2020-10-17 19:41:31 +08:00
return GetCmdDescTranslated ( ) ;
2020-03-29 18:41:26 +02:00
}
}
2020-10-17 19:41:31 +08:00
catch ( FormatException ) { return GetCmdDescTranslated ( ) ; }
2020-03-29 18:41:26 +02:00
}
else
{
Dictionary < int , Container > inventories = handler . GetInventories ( ) ;
List < string > response = new List < string > ( ) ;
2020-10-17 19:41:31 +08:00
response . Add ( Translations . Get ( "cmd.inventory.inventories" ) + ":" ) ;
2020-08-17 15:27:15 +05:00
foreach ( KeyValuePair < int , Container > inventory in inventories )
{
2020-03-29 18:41:26 +02:00
response . Add ( String . Format ( " #{0}: {1}" , inventory . Key , inventory . Value . Title + "§8" ) ) ;
2020-08-17 15:27:15 +05:00
}
2020-10-17 19:41:31 +08:00
response . Add ( CmdUsage ) ;
2020-03-29 18:41:26 +02:00
return String . Join ( "\n" , response ) ;
}
2020-03-26 15:01:42 +08:00
}
2020-10-17 19:41:31 +08:00
else 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
private string GetCommandDesc ( )
{
return GetBasicUsage ( ) + " Type \"/inventory help\" for more help" ;
}
private string GetAvailableActions ( )
{
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
}
private string GetBasicUsage ( )
{
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
}
private string GetHelp ( )
{
2021-05-15 20:11:07 +02:00
return Translations . Get ( "cmd.inventory.help.help" , GetAvailableActions ( ) ) ;
2020-08-03 20:13:07 +08:00
}
private string GetSubCommandHelp ( string cmd )
{
switch ( cmd )
{
case "list" :
2021-05-15 20:11:07 +02:00
return Translations . Get ( "cmd.inventory.help.list" ) + ' ' + Translations . Get ( "cmd.inventory.help.usage" ) + ": /inventory <player|container|<id>> list" ;
2020-08-03 20:13:07 +08:00
case "close" :
2021-05-15 20:11:07 +02:00
return Translations . Get ( "cmd.inventory.help.close" ) + ' ' + Translations . Get ( "cmd.inventory.help.usage" ) + ": /inventory <player|container|<id>> close" ;
2020-08-03 20:13:07 +08:00
case "click" :
2021-05-15 20:11:07 +02:00
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" ;
2020-08-03 20:13:07 +08:00
case "drop" :
2021-05-15 20:11:07 +02:00
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 "creativegive" :
return Translations . Get ( "cmd.inventory.help.creativegive" ) + ' ' + Translations . Get ( "cmd.inventory.help.usage" ) + ": /inventory creativegive <slot> <itemtype> <amount>" ;
case "creativedelete" :
return Translations . Get ( "cmd.inventory.help.creativedelete" ) + ' ' + Translations . Get ( "cmd.inventory.help.usage" ) + ": /inventory creativedelete <slot>" ;
2020-08-03 20:13:07 +08:00
case "help" :
return GetHelp ( ) ;
default :
2020-10-17 19:41:31 +08:00
return Translations . Get ( "cmd.inventory.help.unknown" ) + GetAvailableActions ( ) ;
2020-08-03 20:13:07 +08:00
}
}
#endregion
2020-03-26 15:01:42 +08:00
}
}