C#:
using System;
using System.Collections.Generic;
using System.Linq;
using Oxide.Core;
using Oxide.Core.Plugins;
using UnityEngine;
using Random = System.Random;
namespace Oxide.Plugins
{
[Info("VoNPC", "Anathar", "0.0.7")]
public class VoNPC : RustPlugin
{
[PluginReference]
Plugin SoundLibrary;
public NpcData npcdata;
public class NpcData
{
public Dictionary<ulong,NpcAction> Npcaction = new Dictionary<ulong, NpcAction>();
}
public class NpcAction
{
public string OnHit;
public string OnUse;
public string OnEnter;
public string OnLeave;
public string OnKill;
public string OnRespawn;
public bool Type;
}
Dictionary<ulong,Dictionary<ulong,DateTime>> Timers = new Dictionary<ulong, Dictionary<ulong,DateTime>>();
private void SaveNpcData() => Interface.Oxide.DataFileSystem.WriteObject(Name,npcdata);
private void LoadNpcData() {
try {
npcdata = Interface.Oxide.DataFileSystem.ReadObject<NpcData>(Name);
} catch (Exception e) {
npcdata = new NpcData();
}
}
private class VoNpcEdit : MonoBehaviour
{
public BasePlayer player;
public ulong idnpc;
private void Awake()
{
player = GetComponent<BasePlayer>();
}
}
private void OnServerInitialized()
{
LoadNpcData();
}
void OnServerSave()
{
foreach (var pl in Timers.Keys.ToList())
{
foreach (var npc in Timers[pl].Keys.ToList())
{
if (DateTime.Now.Subtract(Timers[pl][npc]).TotalSeconds > 10) Timers[pl].Remove(npc);
}
}
}
void GetRandom()
{
foreach (var finder in Timers)
{
if(finder.Key ==163)
{
PrintError("Error Check");
}
}
}
[ChatCommand("vonpc_add")]
void vonpcadd(BasePlayer player, string command, string[] args)
{
if(!player.IsAdmin) return;
if (args.Length == 0)
{
SendReply(player, "/vonpc_add ID - Add NPC");
return;
}
if(!npcdata.Npcaction.ContainsKey(ulong.Parse(args[0])))
{
var data = new NpcAction{
OnHit = "",
OnUse = "",
OnEnter = "",
OnLeave = "",
OnKill = "",
OnRespawn = "",
Type = false
};
npcdata.Npcaction.Add(ulong.Parse(args[0]),data);
SaveNpcData();
var vonpcedit = player.gameObject.AddComponent<VoNpcEdit>();
vonpcedit.idnpc = ulong.Parse(args[0]);
SendReply(player, "Вы добавили NPC");
}
}
[ChatCommand("vonpc")]
void vonpc(BasePlayer player, string command, string[] args)
{
if(!player.IsAdmin) return;
var vonpcedit = player.GetComponent<VoNpcEdit>();
if (vonpcedit == null)
{
SendReply(player, "NPC Editor: You need to be editing an NPC, say /npc_add or /npc_edit");
return;
}
if (args.Length == 0)
{
SendReply(player, "/vonpc onuse name - set sound name when player use npc");
SendReply(player, "/vonpc onhit name - set sound name someone hiting npc");
SendReply(player, "/vonpc onenter name - set sound name when player enter in npc radius");
SendReply(player, "/vonpc leave name - set sound name when player leave npc radius");
SendReply(player, "/vonpc onkill name - set sound name when player killing npc");
SendReply(player, "/vonpc onrespawn name - set sound name when npc respawn");
SendReply(player, "/vonpc type All/Player - set type playing for only player or for all players");
}
if (args.Length > 1)
{
switch (args[0].ToLower())
{
case "onuse":
{
npcdata.Npcaction[vonpcedit.idnpc].OnUse = args[1];
SendReply(player,"Установлена озвучка нпс при нажатии на кнопку E");
SaveNpcData();
break;
}
case "onhit":
{
npcdata.Npcaction[vonpcedit.idnpc].OnHit = args[1];
SendReply(player,"Установлена озвучка нпс при уроне");
SaveNpcData();
break;
}
case "onenter":
{
npcdata.Npcaction[vonpcedit.idnpc].OnEnter = args[1];
SendReply(player,"Установлена озвучка нпс при входе радиус нпс");
SaveNpcData();
break;
}
case "onleave":
{
npcdata.Npcaction[vonpcedit.idnpc].OnLeave = args[1];
SendReply(player,"Установлена озвучка нпс при выходе из радиуса нпс");
SaveNpcData();
break;
}
case "onkill":
{
npcdata.Npcaction[vonpcedit.idnpc].OnKill = args[1];
SendReply(player,"Установлена озвучка нпс при убийстве нпс");
SaveNpcData();
break;
}
case "onrespawn":
{
npcdata.Npcaction[vonpcedit.idnpc].OnRespawn = args[1];
SendReply(player,"Установлена озвучка нпс при Возраждении");
SaveNpcData();
break;
}
case "type":
{
if (args[1].ToLower() == "All")
{
npcdata.Npcaction[vonpcedit.idnpc].Type = false;
SendReply(player,"Установлена озвучка которая будет слышна всем игрокам рядом с нпс");
SaveNpcData();
}
if (args[1].ToLower() == "Player")
{
npcdata.Npcaction[vonpcedit.idnpc].Type = true;
SendReply(player,"Установлена озвучка которая будет слышна только игроку");
SaveNpcData();
}
break;
}
}
}
}
[ChatCommand("vonpc_edit")]
void vonpcedit(BasePlayer player, string command, string[] args)
{
if(!player.IsAdmin) return;
if (player.GetComponent<VoNpcEdit>() != null)
{
SendReply(player,$"Вы уже редактируете озвучку нпс {player.GetComponent<VoNpcEdit>().idnpc}");
}
if (!npcdata.Npcaction.ContainsKey(ulong.Parse(args[0])))
{
SendReply(player,"Нету такого NPC");
}
var vonpcedit = player.gameObject.AddComponent<VoNpcEdit>();
vonpcedit.idnpc = ulong.Parse(args[0]);
SendReply(player, $"Вы начали редактирорвать озвучку NPC {vonpcedit.idnpc}");
}
[ChatCommand("vonpc_end")]
private void cmdChatNPCEnd(BasePlayer player, string command, string[] args)
{
var vonpcedit = player.GetComponent<VoNpcEdit>();
if (vonpcedit == null)
{
SendReply(player, "Вы уже редактируете озвучку");
return;
}
UnityEngine.Object.Destroy(vonpcedit);
SendReply(player, "Редактирование завершено");
}
void CheckDbTimer(ulong Uid)
{
if (!Timers.ContainsKey(Uid))
Timers.Add(Uid,new Dictionary<ulong, DateTime>());
}
void OnUseNPC(BasePlayer npc, BasePlayer player)
{
if(!npcdata.Npcaction.ContainsKey(npc.userID)) return;
var NpcData = npcdata.Npcaction[npc.userID];
CheckDbTimer(player.userID);
if (NpcData.OnUse != string.Empty)
{
if(!Timers[player.userID].ContainsKey(npc.userID))
{
Timers[player.userID].Add(npc.userID, DateTime.Now);
if (NpcData.Type)
SoundLibrary?.Call("GetSoundToPlayer", player.userID, npc.net.ID, NpcData.OnUse);
else
SoundLibrary?.Call("GetSoundToAll", npc.net.ID, NpcData.OnUse);
}
if (Timers[player.userID].ContainsKey(npc.userID) && DateTime.Now.Subtract(Timers[player.userID][npc.userID]).TotalSeconds > 10)
{
Timers[player.userID][npc.userID] = DateTime.Now;
if (NpcData.Type)
SoundLibrary?.Call("GetSoundToPlayer", player.userID, npc.net.ID, NpcData.OnUse);
else
SoundLibrary?.Call("GetSoundToAll", npc.net.ID, NpcData.OnUse);
}
}
}
void OnHitNPC(BasePlayer npc, HitInfo info)
{
if(!npcdata.Npcaction.ContainsKey(npc.userID)) return;
var NpcData = npcdata.Npcaction[npc.userID];
BasePlayer player = info.InitiatorPlayer;
CheckDbTimer(player.userID);
if (NpcData.OnHit != string.Empty)
{
if(!Timers[player.userID].ContainsKey(npc.userID))
{
Timers[player.userID].Add(npc.userID, DateTime.Now);
if (NpcData.Type)
SoundLibrary?.Call("GetSoundToPlayer", player.userID, npc.net.ID, NpcData.OnHit);
else
SoundLibrary?.Call("GetSoundToAll", npc.net.ID, NpcData.OnHit);
}
if (Timers[player.userID].ContainsKey(npc.userID) && DateTime.Now.Subtract(Timers[player.userID][npc.userID]).TotalSeconds > 10)
{
Timers[player.userID][npc.userID] = DateTime.Now;
if (NpcData.Type)
SoundLibrary?.Call("GetSoundToPlayer", player.userID, npc.net.ID, NpcData.OnHit);
else
SoundLibrary?.Call("GetSoundToAll", npc.net.ID, NpcData.OnHit);
}
}
}
void OnEnterNPC(BasePlayer npc, BasePlayer player)
{
if(!npcdata.Npcaction.ContainsKey(npc.userID)) return;
var NpcData = npcdata.Npcaction[npc.userID];;
CheckDbTimer(player.userID);
if (NpcData.OnEnter != string.Empty)
{
if(!Timers[player.userID].ContainsKey(npc.userID))
{
Timers[player.userID].Add(npc.userID, DateTime.Now);
if (NpcData.Type)
SoundLibrary?.Call("GetSoundToPlayer", player.userID, npc.net.ID, NpcData.OnEnter);
else
SoundLibrary?.Call("GetSoundToAll", npc.net.ID, NpcData.OnEnter);
}
if (Timers[player.userID].ContainsKey(npc.userID) && DateTime.Now.Subtract(Timers[player.userID][npc.userID]).TotalSeconds > 10)
{
Timers[player.userID][npc.userID] = DateTime.Now;
if (NpcData.Type)
SoundLibrary?.Call("GetSoundToPlayer", player.userID, npc.net.ID, NpcData.OnEnter);
else
SoundLibrary?.Call("GetSoundToAll", npc.net.ID, NpcData.OnEnter);
}
}
}
void OnLeaveNPC(BasePlayer npc, BasePlayer player)
{
if(!npcdata.Npcaction.ContainsKey(npc.userID)) return;
var NpcData = npcdata.Npcaction[npc.userID];
CheckDbTimer(player.userID);
if (NpcData.OnLeave != string.Empty)
{
if(!Timers[player.userID].ContainsKey(npc.userID))
{
Timers[player.userID].Add(npc.userID, DateTime.Now);
if (NpcData.Type)
SoundLibrary?.Call("GetSoundToPlayer", player.userID, npc.net.ID, NpcData.OnLeave);
else
SoundLibrary?.Call("GetSoundToAll", npc.net.ID, NpcData.OnLeave);
}
if (Timers[player.userID].ContainsKey(npc.userID) && DateTime.Now.Subtract(Timers[player.userID][npc.userID]).TotalSeconds > 10)
{
Timers[player.userID][npc.userID] = DateTime.Now;
if (NpcData.Type)
SoundLibrary?.Call("GetSoundToPlayer", player.userID, npc.net.ID, NpcData.OnLeave);
else
SoundLibrary?.Call("GetSoundToAll", npc.net.ID, NpcData.OnLeave);
}
}
}
void OnKillNPC(BasePlayer npc, BasePlayer player)
{
if(!npcdata.Npcaction.ContainsKey(npc.userID)) return;
var NpcData = npcdata.Npcaction[npc.userID];
CheckDbTimer(player.userID);
if (NpcData.OnKill != string.Empty)
{
if(!Timers[player.userID].ContainsKey(npc.userID))
{
Timers[player.userID].Add(npc.userID, DateTime.Now);
if (NpcData.Type)
SoundLibrary?.Call("GetSoundToPlayer", player.userID, npc.net.ID, NpcData.OnKill);
else
SoundLibrary?.Call("GetSoundToAll", npc.net.ID, NpcData.OnKill);
}
if (Timers[player.userID].ContainsKey(npc.userID) && DateTime.Now.Subtract(Timers[player.userID][npc.userID]).TotalSeconds > 10)
{
Timers[player.userID][npc.userID] = DateTime.Now;
if (NpcData.Type)
SoundLibrary?.Call("GetSoundToPlayer", player.userID, npc.net.ID, NpcData.OnKill);
else
SoundLibrary?.Call("GetSoundToAll", npc.net.ID, NpcData.OnKill);
}
}
}
void OnNPCRespawn(BasePlayer npc)
{
NextTick(() => {
if(!npcdata.Npcaction.ContainsKey(npc.userID)) return;
var NpcData = npcdata.Npcaction[npc.userID];
if(NpcData.OnRespawn != string.Empty)
SoundLibrary?.Call("GetSoundToAll", npc.net.ID, NpcData.OnRespawn);
});
}
}
}