Adding my mod code
[Info("GiveSpear", "Me", "0.0.1")]
public class GiveSpear : RustPlugin
{
private void Init()
{
cmd.AddChatCommand("spear", this, GivePlayerSpear);
}
private void GivePlayerSpear(BasePlayer player, string command, string[] args)
{
if (player == null)
return;
Server.Broadcast("Giving Spear");
var item = ItemManager.CreateByName("spear.stone", 1);
if (item == null)
{
Puts($"Spear was not able to be created properly");
return;
}
if (!item.MoveToContainer(player.inventory.containerBelt))
{
item.Remove();
return;
}
player.UpdateActiveItem(item.uid);
player.SendNetworkUpdate();
}
}
Merged postThis gives the player a spear and then does try to activate it, however just reselects the previous active item. The player does do the animation to re-pull out their previous item.
Merged postI have also tried to do the following, and this lowers the players active item, however doesnt change it to the new item. - and the original item is still highlighted blue in their inventory
var heldItem = player.GetActiveItem()?.GetHeldEntity() as HeldEntity;
heldItem.SetHeld(false);
//player.UpdateActiveItem(item.uid);
var spearHeldItem = item.GetHeldEntity() as HeldEntity;
spearHeldItem?.SetHeld(true);
player.SendNetworkUpdate();
If I uncomment the UpdateActiveItem it behaves the same as before
Merged postI added this function and I can se it is switching and switching back
void OnActiveItemChanged(BasePlayer player, Item oldItem, Item newItem)
{
Server.Broadcast($"{oldItem?.info?.displayName.english} => {newItem?.info?.displayName.english}");
}
And this is what I see in the output
Hammer => Stone Spear
Stone Spear => Hammer
Merged postI even tried adding the following, however it still switches back, i see in the output "Attempting Stopping of switch?" however i still see
Stone Spear => Hammer also
private ConcurrentDictionary<ulong, uint> _waiting = new ConcurrentDictionary<ulong, uint>();
private void GivePlayerSpear(BasePlayer player, string command, string[] args)
{
if (player == null)
return;
var item = ItemManager.CreateByName("spear.stone", 1);
if (item == null)
{
Puts($"Spear was not able to be created properly");
return;
}
if (!item.MoveToContainer(player.inventory.containerBelt, 4, false))
{
item.Remove();
return;
}
player.UpdateActiveItem(item.uid);
_waiting.AddOrUpdate(player.userID, item.uid, (uid, iid) => { return iid; });
}
object OnActiveItemChange(BasePlayer player, Item oldItem, uint newItemId)
{
uint uid;
if (_waiting.TryRemove(player.userID, out uid))
{
if (uid == oldItem?.uid)
{
Server.Broadcast("Attempting Stopping of switch?");
return false;
}
}
return null;
}
Merged postAnyone know how to set a players active item?