Skip to content

Commit

Permalink
1.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
RenderBr committed Dec 1, 2022
1 parent 63e4af7 commit e12478d
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 2 deletions.
Binary file not shown.
Binary file not shown.
Binary file modified .vs/SimpleEcon/v17/.futdcache.v2
Binary file not shown.
2 changes: 2 additions & 0 deletions SimpleEcon/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public class Config
public bool giveRewardsForPlaytime { get; set; } = false;
public int rewardtimer { get; set; } = 5;

public double DropOnDeath { get; set; } = 0;

public void Write()
{
string path = Path.Combine(TShock.SavePath, "SimpleEcon.json");
Expand Down
47 changes: 46 additions & 1 deletion SimpleEcon/EconPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ public class EconPlayer

public float balance { get; set; }

public float RetrieveBalance()
{
return this.balance;
}

public EconPlayer(string playerName, TSPlayer player)
{
this.name = playerName;
Expand Down Expand Up @@ -72,6 +77,46 @@ public static void UpdatePlayerBalance(string name, float am)
return;
}



public static void UpdatePlayerBalance(EconPlayer ply, float am)
{
var p = SimpleEcon.econPlayers.Find(p => p.name == ply.name);
p.balance = am;
SimpleEcon.dbManager.SavePlayer(p);
return;
}

public static void SubtractPlayerBalance(EconPlayer ply, float toRemove)
{
var p = SimpleEcon.econPlayers.Find(p => p.name == ply.name);
p.balance -= toRemove;
if(p.balance <= 0)
{
p.balance = 0;
}
SimpleEcon.dbManager.SavePlayer(p);
return;
}

public static void AddPlayerBalance(EconPlayer ply, float toAdd)
{
var p = SimpleEcon.econPlayers.Find(p => p.name == ply.name);
p.balance += toAdd;
if (p.balance <= 0)
{
p.balance = 0;
}
SimpleEcon.dbManager.SavePlayer(p);
return;
}

public static void ResetPlayerBalance(EconPlayer ply)
{
var p = SimpleEcon.econPlayers.Find(p => p.name == ply.name);
p.balance = 0;
SimpleEcon.dbManager.SavePlayer(p);
return;
}

}
}
79 changes: 78 additions & 1 deletion SimpleEcon/SimpleEcon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace SimpleEcon
public class SimpleEcon : TerrariaPlugin
{
public override string Name => "Simple Economy";
public override Version Version => new Version(1, 0, 3);
public override Version Version => new Version(1, 1, 0);
public override string Author => "Average";
public override string Description => "A simple, light-weight economy TShock V5 plugin that can also be utilized by other plugins. No bullshit dependecies!";
public static List<EconPlayer> econPlayers = new List<EconPlayer>();
Expand Down Expand Up @@ -62,6 +62,7 @@ public override void Initialize()
}
dbManager = new Database(_db);
TShockAPI.Hooks.PlayerHooks.PlayerPostLogin += PlayerJoin;
TShockAPI.GetDataHandlers.KillMe += PlayerDead;
ServerApi.Hooks.ServerLeave.Register(this, PlayerLeave);
ServerApi.Hooks.GameInitialize.Register(this, Loaded);
ServerApi.Hooks.NetSendData.Register(this, OnNpcStrike);
Expand All @@ -75,10 +76,32 @@ private void Loaded(EventArgs args)
Commands.ChatCommands.Add(new Command("se.user", BalTop, "baltop", "ecotop", "top"));
Commands.ChatCommands.Add(new Command("se.user", PayUser, "pay", "transfer"));
Commands.ChatCommands.Add(new Command("se.admin", GiveBal, "givebal", "gbal"));
Commands.ChatCommands.Add(new Command("se.admin", TakeBal, "takebal", "tbal"));
Commands.ChatCommands.Add(new Command("se.admin", ResetBal, "takebal", "tbal"));

rewardsManager();
}

public void PlayerDead(object sender, GetDataHandlers.KillMeEventArgs args)
{
if (args.Player.IsLoggedIn && config.DropOnDeath > 0)
{

EconPlayer p = PlayerManager.GetPlayer(args.Player.Name);
var toLose = (float)(p.balance * config.DropOnDeath);
p.balance -= toLose;
if (config.announceMobDrops)
{
args.Player.SendMessage($"You lost {toLose} {((toLose == 0) ? config.currencyNameSingular : config.currencyNamePlural) } from dying!", Color.Orange);
return;
}
}
else
{
return;
}
}

public async void rewardsManager()
{
if (config.giveRewardsForPlaytime == true)
Expand Down Expand Up @@ -255,6 +278,60 @@ private void GiveBal(CommandArgs args)

}

private void ResetBal(CommandArgs args)
{
if (args.Parameters.Count == 0)
{
args.Player.SendErrorMessage("Please enter a player name! /resetbal <user> <amount>");
return;
}
if (args.Parameters.Count == 1)
{
args.Player.SendErrorMessage($"Please enter a quantity to send! /resetbal {args.Parameters[0]} <amount>");
return;
}

TSPlayer player = TSPlayer.FindByNameOrID(args.Parameters[0])[0];
if (player == null)
{
args.Player.SendErrorMessage("Invalid player!");
return;
}
float amount = float.Parse(args.Parameters[1]);
PlayerManager.GetPlayer(player.Name).balance = 0;
dbManager.SaveAllPlayers();
player.SendErrorMessage($"The moderator {args.Player.Name} has reset your balance! Your new balance is: {PlayerManager.GetPlayer(player.Name).balance} {(PlayerManager.GetPlayer(player.Name).balance == 1 ? config.currencyNameSingular : config.currencyNamePlural)}");
return;

}

private void TakeBal(CommandArgs args)
{
if (args.Parameters.Count == 0)
{
args.Player.SendErrorMessage("Please enter a player name! /tbal <user> <amount>");
return;
}
if (args.Parameters.Count == 1)
{
args.Player.SendErrorMessage($"Please enter a quantity to take away from the player! /tbal {args.Parameters[0]} <amount>");
return;
}

TSPlayer player = TSPlayer.FindByNameOrID(args.Parameters[0])[0];
if (player == null)
{
args.Player.SendErrorMessage("Invalid player!");
return;
}
float amount = float.Parse(args.Parameters[1]);
PlayerManager.GetPlayer(player.Name).balance -= amount;
dbManager.SaveAllPlayers();
player.SendErrorMessage($"The moderator {args.Player.Name} has manipulated your currency and removed {amount} {(amount == 1 ? config.currencyNameSingular : config.currencyNamePlural)} from your account! Your new balance is: {PlayerManager.GetPlayer(player.Name).balance} {(PlayerManager.GetPlayer(player.Name).balance == 1 ? config.currencyNameSingular : config.currencyNamePlural)}");
return;

}

private void PayUser(CommandArgs args)
{
if(args.Parameters.Count == 0)
Expand Down

0 comments on commit e12478d

Please sign in to comment.