Skip to content

Commit

Permalink
added mob exclusions + enableMobDrops
Browse files Browse the repository at this point in the history
added mob exclusions + enableMobDrops

and attempted to fix an annoying exception occurring with the mob damage implementation
  • Loading branch information
RenderBr committed Nov 17, 2022
1 parent 3f569bc commit fc50139
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 7 deletions.
6 changes: 4 additions & 2 deletions SimpleEcon/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<int> excludedMobs { get; set; } = new List<int>() { 211, 210 };

public bool enableMobDrops { get; set; } = false;
public bool giveRewardsForPlaytime { get; set; } = false;
public int rewardtimer { get; set; } = 5;

Expand Down
11 changes: 11 additions & 0 deletions SimpleEcon/EconPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
52 changes: 47 additions & 5 deletions SimpleEcon/SimpleEcon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

}
Expand Down Expand Up @@ -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)
Expand All @@ -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));
}
Expand Down

0 comments on commit fc50139

Please sign in to comment.