Get Item name from Prefab name? Solved
Posted 5 years ago
Last updated 5 years ago
Hey all
I'm a Software Engineer but pretty new to rust / oxide development...so don't be too harsh ;D.
Problem
After melee interacting I want to give the player the item he interacted with. From the HitInfo I can get the prefab name, but that's not the item name I have to use to ItemManager.CreateByName.
Examples
(left is name from HitInfo, right is needed string to CreateByName)
- researchtable.deployed -> research.table
- repairbench_deployed -> box.repair.bench
- refinery_small_deployed -> small.oil.refinery
Is there a way to get around manually mapping all the items? Can I get the parent name of the deployed instance or something like that?
Thanks already in advance
The RemoverTool plugin has code for taking something from a HitInfo and turning it into an item. Check the OnHammerHit and GiveRefund parts. It correlates names and itemids in a dictionary.
You sir are a genius. Thanks a lot man...that was exactly what I was looking for! Works like a treat.
If anyone else is looking for that:
//the needed dictionary
private readonly Dictionary<string, string> shortPrefabNameToDeployableName = new Dictionary<string, string>();
//within OnServerInitialized
if (!itemShortNameToItemId.ContainsKey(itemDefinition.shortname))
{
itemShortNameToItemId.Add(itemDefinition.shortname, itemDefinition.itemid);
}
var deployablePrefab = itemDefinition.GetComponent<ItemModDeployable>()?.entityPrefab?.resourcePath;
if (string.IsNullOrEmpty(deployablePrefab))
{
continue;
}
var shortPrefabName = GameManager.server.FindPrefab(deployablePrefab)?.GetComponent<BaseEntity>()?.ShortPrefabName;
if (!string.IsNullOrEmpty(shortPrefabName) && !shortPrefabNameToDeployableName.ContainsKey(shortPrefabName))
{
shortPrefabNameToDeployableName.Add(shortPrefabName, itemDefinition.shortname);
}
//and then to lookup and give the deployable
string itemName;
if (shortPrefabNameToDeployableName.TryGetValue(info.HitEntity.ShortPrefabName, out itemName))
{
player.ChatMessage("Trying to craft " + itemName);
var item = ItemManager.CreateByName(itemName, 1, targetSkinId);
player.ChatMessage("Giving item " + item.name);
player.GiveItem(item);
info.HitEntity.Kill();
}
else
{
player.ChatMessage("Could not craft entity");
}
Locked automatically
5 years ago