Added a command to (re)name items in the Anvil

This commit is contained in:
Anon 2023-06-03 16:12:17 +02:00
parent 9855e2e0f1
commit fce12db33f
8 changed files with 174 additions and 0 deletions

View file

@ -0,0 +1,63 @@
using System.Linq;
using Brigadier.NET;
using Brigadier.NET.Builder;
using MinecraftClient.CommandHandler;
using MinecraftClient.Inventory;
namespace MinecraftClient.Commands
{
public class NameItem : Command
{
public override string CmdName => "nameitem";
public override string CmdUsage => "nameitem <item name>";
public override string CmdDesc => Translations.cmd_nameitem_desc;
public override void RegisterCommand(CommandDispatcher<CmdResult> dispatcher)
{
dispatcher.Register(l => l.Literal("help")
.Then(l => l.Literal(CmdName)
.Executes(r => GetUsage(r.Source, string.Empty))
)
);
dispatcher.Register(l => l.Literal(CmdName)
.Then(l => l.Argument("any", Arguments.GreedyString())
.Executes(r => DoSetItemName(r.Source, Arguments.GetString(r, "any"))))
);
}
private int GetUsage(CmdResult r, string? cmd)
{
return r.SetAndReturn(cmd switch
{
#pragma warning disable format // @formatter:off
_ => GetCmdDescTranslated(),
#pragma warning restore format // @formatter:on
});
}
private int DoSetItemName(CmdResult r, string itemName)
{
var handler = CmdResult.currentHandler!;
if (itemName.Trim().Length == 0)
return r.SetAndReturn(CmdResult.Status.Fail, Translations.cmd_nameitem_item_name_empty);
var currentInventory = handler.GetInventories().Count == 0
? null
: handler.GetInventories().Values.ToList().Last();
if (currentInventory is not { Type: ContainerType.Anvil })
return r.SetAndReturn(CmdResult.Status.Fail, Translations.cmd_nameitem_no_anvil_inventory_open);
if (currentInventory.Items[0].IsEmpty)
return r.SetAndReturn(CmdResult.Status.Fail,
Translations.cmd_nameitem_first_slot_empty);
return handler.SendRenameItem(itemName)
? r.SetAndReturn(CmdResult.Status.Done, Translations.cmd_nameitem_successful)
: r.SetAndReturn(CmdResult.Status.Fail, Translations.cmd_nameitem_failed);
}
}
}

View file

@ -2390,6 +2390,21 @@ namespace MinecraftClient
return false;
}
}
/// <summary>
/// Send the server a command to type in the item name in the Anvil inventory when it's open.
/// </summary>
/// <param name="itemName">The new item name</param>
public bool SendRenameItem(string itemName)
{
if (inventories.Count == 0)
return false;
if (inventories.Values.ToList().Last().Type != ContainerType.Anvil)
return false;
return handler.SendRenameItem(itemName);
}
#endregion
#region Event handlers: An event occurs on the Server

View file

@ -910,6 +910,11 @@ namespace MinecraftClient.Protocol.Handlers
return false; //Currently not implemented
}
public bool SendRenameItem(string itemName)
{
return false;
}
public bool SendPlayerSession(PlayerKeyPair? playerKeyPair)
{
return false;

View file

@ -4150,6 +4150,29 @@ namespace MinecraftClient.Protocol.Handlers
}
}
public bool SendRenameItem(string itemName)
{
try
{
List<byte> packet = new();
packet.AddRange(dataTypes.GetString(itemName.Length > 50 ? itemName[..50] : itemName));
SendPacket(PacketTypesOut.NameItem, packet);
return true;
}
catch (SocketException)
{
return false;
}
catch (System.IO.IOException)
{
return false;
}
catch (ObjectDisposedException)
{
return false;
}
}
private byte[] GenerateSalt()
{
byte[] salt = new byte[8];

View file

@ -261,6 +261,12 @@ namespace MinecraftClient.Protocol
/// <returns></returns>
bool SendPlayerSession(PlayerKeyPair? playerKeyPair);
/// <summary>
/// Send the server a command to type in the item name in the Anvil inventory when it's open.
/// </summary>
/// <param name="itemName">The new item name</param>
bool SendRenameItem(string itemName);
/// <summary>
/// Get net read thread (main thread) ID
/// </summary>

View file

@ -477,5 +477,13 @@ namespace MinecraftClient.Protocol
/// <returns>True if packet was successfully sent</returns>
bool ClickContainerButton(int windowId, int buttonId);
/// <summary>
/// Send a rename item packet when the anvil inventory is open and there is an item in the first slot
/// </summary>
/// <param name="itemName">New name (max 50 characters)</param>
/// <returns>True if packet was successfully sent</returns>
bool SendRenameItem(string itemName);
}
}

View file

@ -3854,5 +3854,41 @@ namespace MinecraftClient {
return ResourceManager.GetString("cmd.items.collector.stopped", resourceCulture);
}
}
internal static string cmd_nameitem_item_name_empty {
get {
return ResourceManager.GetString("cmd.nameitem.item.name.empty", resourceCulture);
}
}
internal static string cmd_nameitem_no_anvil_inventory_open {
get {
return ResourceManager.GetString("cmd.nameitem.no.anvil.inventory.open", resourceCulture);
}
}
internal static string cmd_nameitem_first_slot_empty {
get {
return ResourceManager.GetString("cmd.nameitem.first.slot.empty", resourceCulture);
}
}
internal static string cmd_nameitem_failed {
get {
return ResourceManager.GetString("cmd.nameitem.failed", resourceCulture);
}
}
internal static string cmd_nameitem_successful {
get {
return ResourceManager.GetString("cmd.nameitem.successful", resourceCulture);
}
}
internal static string cmd_nameitem_desc {
get {
return ResourceManager.GetString("cmd.nameitem.desc", resourceCulture);
}
}
}
}

View file

@ -2058,4 +2058,22 @@ Logging in...</value>
<data name="cmd.items.collector.stopped" xml:space="preserve">
<value>Stopped collecting items.</value>
</data>
<data name="cmd.nameitem.item.name.empty" xml:space="preserve">
<value>The item name cannot be empty!</value>
</data>
<data name="cmd.nameitem.no.anvil.inventory.open" xml:space="preserve">
<value>You need to have an Anvil inventory opened and active in order to use this command!</value>
</data>
<data name="cmd.nameitem.first.slot.empty" xml:space="preserve">
<value>You need to place an item in the first slot (slot id: 0) of the Anvil inventory!</value>
</data>
<data name="cmd.nameitem.failed" xml:space="preserve">
<value>Failed to send the packet for naming an item, try agian!</value>
</data>
<data name="cmd.nameitem.successful" xml:space="preserve">
<value>Succesfully sent the item name to the server, you can now clikc on the slot number 2 in the Anvil inventory to rename the item.</value>
</data>
<data name="cmd.nameitem.desc" xml:space="preserve">
<value>Set an item name when an Anvil inventory is active and the item is in the first slot.</value>
</data>
</root>