Skip to content

Commit

Permalink
Added a command to (re)name items in the Anvil
Browse files Browse the repository at this point in the history
Added a command to (re)name items in the Anvil
  • Loading branch information
milutinke authored Jun 3, 2023
2 parents 051ee70 + f77d584 commit 5de84d7
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 4 deletions.
63 changes: 63 additions & 0 deletions MinecraftClient/Commands/NameItem.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
15 changes: 15 additions & 0 deletions MinecraftClient/McClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2391,6 +2391,21 @@ public bool SpectateByUUID(Guid UUID)
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
Expand Down
5 changes: 5 additions & 0 deletions MinecraftClient/Protocol/Handlers/Protocol16.cs
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,11 @@ public bool SendSpectate(Guid UUID)
{
return false; //Currently not implemented
}

public bool SendRenameItem(string itemName)
{
return false;
}

public bool SendPlayerSession(PlayerKeyPair? playerKeyPair)
{
Expand Down
23 changes: 23 additions & 0 deletions MinecraftClient/Protocol/Handlers/Protocol18.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4150,6 +4150,29 @@ public bool SendPlayerSession(PlayerKeyPair? playerKeyPair)
}
}

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];
Expand Down
6 changes: 6 additions & 0 deletions MinecraftClient/Protocol/IMinecraftCom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,12 @@ public interface IMinecraftCom : IDisposable, IAutoComplete
/// </summary>
/// <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
Expand Down
8 changes: 8 additions & 0 deletions MinecraftClient/Protocol/IMinecraftComHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -477,5 +477,13 @@ public interface IMinecraftComHandler
/// <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);
}
}
38 changes: 37 additions & 1 deletion MinecraftClient/Resources/Translations/Translations.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 21 additions & 3 deletions MinecraftClient/Resources/Translations/Translations.resx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Expand Down Expand Up @@ -2086,9 +2086,27 @@ Logging in...</value>
<value>§bSession with an id §a{0}§b has been renamed to: §a{1}§b!</value>
</data>
<data name="bot.WebSocketBot.session_id_failed_to_change" xml:space="preserve">
<value>§cSession with an id §a{0}§b failed to chage the ID to: §c{1}§b!</value>
<value>§cSession with an id §a{0}§b failed to change the ID to: §c{1}§b!</value>
</data>
<data name="bot.WebSocketBot.session_authenticated" xml:space="preserve">
<value>§bSession with an id §a{0}§b has been succesfully authenticated!</value>
<value>§bSession with an id §a{0}§b has been successfully authenticated!</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 again!</value>
</data>
<data name="cmd.nameitem.successful" xml:space="preserve">
<value>Successfully sent the item name to the server, you can now click 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>

0 comments on commit 5de84d7

Please sign in to comment.