Advertisement
Vlad-00003

WeaponToItemOnDeath.cs

May 26th, 2023 (edited)
628
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.37 KB | None | 0 0
  1. using System.Collections.Generic;
  2.  
  3. namespace Oxide.Plugins
  4. {
  5.     [Info("EmptyPlugin", "Vlad-00003", "1.0.0")]
  6.     [Description("Testing plugin")]
  7.     /*
  8.      * Author info:
  9.      *   E-mail: Vlad-00003@mail.ru
  10.      *   Vk: vk.com/vlad_00003
  11.      * Русский текст для поддержки юникода. Иногда студия меняет кодировку в случае,
  12.      *   если нет неподдерживаемых ASCII символов.
  13.      */
  14.     internal class EmptyPlugin : RustPlugin
  15.     {
  16.         private void OnEntityDeath(BaseEntity ent, HitInfo info)
  17.         {
  18.             if (info == null || !(ent is BuildingBlock))
  19.                 return;
  20.             PrintWarning("Ent {0} died. Weapon: {1}", ent, GetWeaponInfo(info));
  21.         }
  22.        
  23.         private readonly Dictionary<string, string> _entityToItem = new Dictionary<string, string>();
  24.  
  25.         private void OnServerInitialized()
  26.         {
  27.             foreach (var def in ItemManager.GetItemDefinitions())
  28.             {
  29.                 var projectile = def.GetComponent<ItemModProjectile>();
  30.                 if (projectile != null) //Объект, который создаётся при броске\выстреле. Ракеты\пули тут.
  31.                 {
  32.                     //У hammer есть projectileObject, но он null. Не спрашивай.
  33.                     if(projectile.projectileObject.isValid)
  34.                         _entityToItem[projectile.projectileObject.resourcePath] = def.shortname;
  35.                 }
  36.                
  37.  
  38.                 //У оружия ближнего боя есть и HeldEntity и Projectile, т.к. их можно метать. Нам нужны оба варианта
  39.                 var modEntity = def.GetComponent<ItemModEntity>();
  40.                 if (modEntity == null)
  41.                     continue;
  42.                 // Это HeldEntity. При ударе в ближнем бою, без броска - используется именно он. Хотя при таком ударе мы всё ещё получаем Item через AttackEntity
  43.                 _entityToItem[modEntity.entityPrefab.resourcePath] = def.shortname;
  44.                
  45.                 //Исключительно взрывчатка и, почему-то, fun.casetterecorder
  46.                 var thrown = GameManager.server.FindPrefab(modEntity.entityPrefab.resourcePath).GetComponent<ThrownWeapon>();
  47.                 if (thrown)
  48.                     _entityToItem[thrown.prefabToThrow.resourcePath] = def.shortname;
  49.             }
  50.         }
  51.  
  52.         private string GetWeaponInfo(HitInfo hitInfo)
  53.         {
  54.             //Заглушка. Предмет всё равно есть в словаре, но если можно вытащить из предмета - зачем лезть в словарь?
  55.             var weaponItem = hitInfo.Weapon?.GetCachedItem();
  56.             if (weaponItem != null)
  57.                 return weaponItem.info.shortname;
  58.             //Хотя у нас всё равно есть словарь, так что можно и удалить строки выше
  59.             string shortName;
  60.             if (_entityToItem.TryGetValue(hitInfo.WeaponPrefab.PrefabName, out shortName))
  61.             {
  62.                 return shortName;
  63.             }
  64.  
  65.             return string.Empty;
  66.         }
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement