diff --git a/SimpleEcon/Config.cs b/SimpleEcon/Config.cs index 5991178..b907c88 100644 --- a/SimpleEcon/Config.cs +++ b/SimpleEcon/Config.cs @@ -7,12 +7,14 @@ namespace SimpleEcon { - public class Config - { + public class Config + { public string currencyNameSingular { get; set; } = "dollar"; public string currencyNamePlural { get; set; } = "dollars"; + public List excludedMobs { get; set; } = new List() { 211, 210 }; + public bool enableMobDrops { get; set; } = false; public bool giveRewardsForPlaytime { get; set; } = false; public int rewardtimer { get; set; } = 5; diff --git a/SimpleEcon/EconPlayer.cs b/SimpleEcon/EconPlayer.cs index d4aa3ae..0df3813 100644 --- a/SimpleEcon/EconPlayer.cs +++ b/SimpleEcon/EconPlayer.cs @@ -28,8 +28,19 @@ public static class PlayerManager { public static EconPlayer GetPlayer(int playerId) { + if(playerId == null) + { + return null; + } + + if (TShock.Players[playerId] == null) + { + return null; + } + var name = TShock.Players[playerId].Name; + return SimpleEcon.econPlayers.Find(p => p.name == name); } public static EconPlayer GetPlayer(string name) diff --git a/SimpleEcon/SimpleEcon.cs b/SimpleEcon/SimpleEcon.cs index 9c4616e..c6b1eaf 100644 --- a/SimpleEcon/SimpleEcon.cs +++ b/SimpleEcon/SimpleEcon.cs @@ -95,12 +95,40 @@ public async void rewardsManager() void NetHooks_SendData(SendDataEventArgs e) { + if(config.enableMobDrops == false) + { + return; + } + TSPlayer player = null; + + if (e.Handled == true + || (player = TSPlayer.FindByNameOrID(e.ignoreClient.ToString())[0]) == null) + { + return; + } + if (e.MsgId == PacketTypes.NpcStrike) { + if (Main.npc[e.number] == null) + { + return; + } NPC npc = Main.npc[e.number]; + + if (config.excludedMobs.Count > 0) + { + foreach(int mob in config.excludedMobs) + { + if (npc.netID == mob) + { + return; + } + } + } + if (npc.life <= 0) { - var player = TSPlayer.FindByNameOrID(e.ignoreClient.ToString()); + player = TSPlayer.FindByNameOrID(e.ignoreClient.ToString())[0]; Color color; int totalGiven = 1; @@ -168,14 +196,14 @@ void NetHooks_SendData(SendDataEventArgs e) color = Color.Red; } - PlayerManager.GetPlayer(player[0].Name).balance += totalGiven; + PlayerManager.GetPlayer(player.Name).balance += totalGiven; if (totalGiven == 1) { - player[0].SendMessage("+ " + totalGiven + " " + config.currencyNameSingular + " from killing " + npc.FullName, color); + player.SendMessage("+ " + totalGiven + " " + config.currencyNameSingular + " from killing " + npc.FullName, color); } else { - player[0].SendMessage("+ " + totalGiven + " " + config.currencyNamePlural + " from killing " + npc.FullName, color); + player.SendMessage("+ " + totalGiven + " " + config.currencyNamePlural + " from killing " + npc.FullName, color); } } @@ -280,13 +308,17 @@ private void Reloaded(ReloadEventArgs e) private void PlayerJoin(GreetPlayerEventArgs args) { + if(TShock.Players[args.Who] == null) + { + return; + } + TSPlayer player = TShock.Players[args.Who]; EconPlayer p = new EconPlayer(player.Name, player); econPlayers.Add(p); InitPlayerEcon(p); - } public void InitPlayerEcon(EconPlayer p) @@ -308,8 +340,18 @@ public void UpdatePlayer(EconPlayer p) private void PlayerLeave(LeaveEventArgs args) { + if(TShock.Players[args.Who] == null) + { + return; + } + TSPlayer player = TShock.Players[args.Who]; + if (PlayerManager.GetPlayer(player.Name) == null) { + return; + } + + dbManager.SavePlayer(PlayerManager.GetPlayer(player.Name)); econPlayers.Remove(new EconPlayer(player.Name, player)); }