Skip to content

Instantly share code, notes, and snippets.

@root-cause
Last active April 6, 2021 18:24
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save root-cause/954064612ada82b6aa6d5cb7859cc008 to your computer and use it in GitHub Desktop.
Save root-cause/954064612ada82b6aa6d5cb7859cc008 to your computer and use it in GitHub Desktop.
// Also check out: https://gist.github.com/root-cause/7af0aa80b3ba67ef3de92e0d4161b575
using GTANetworkAPI;
namespace PreCommandTest
{
// Custom player class - an alternative to using GetData/SetData (and probably more) in 1.1+
public class CustomPlayer : Player
{
public int Money { get; set; }
// Constructor
public CustomPlayer(NetHandle handle) : base(handle) { }
}
// Custom command condition - useful in preventing players from using commands they shouldn't be using, as it runs before the command
public class RequiresMoneyAttribute : CommandConditionAttribute
{
public int Amount { get; set; }
public override bool Check(Player player, string cmdName, string cmdText)
{
// ...or you could just cast player to CustomPlayer...
if (player is CustomPlayer customPlayer && customPlayer.Money < Amount)
{
customPlayer.SendChatMessage("You can't afford to use this command.");
return false;
}
return true;
}
}
// Main script
public class WithCustomPlayerExample : Script
{
public WithCustomPlayerExample()
{
RAGE.Entities.Players.CreateEntity = (NetHandle handle) => new CustomPlayer(handle);
}
[Command("setmoney")]
public void SetMoneyCommand(CustomPlayer player, int amount)
{
player.Money = amount;
player.SendChatMessage($"Money changed to: ${player.Money}");
}
[RequiresMoney(Amount = 1000000)]
[Command("wastemoney")]
public void WasteMoneyCommand(CustomPlayer player)
{
player.Money -= 1000000;
player.SendChatMessage("$1 million gone just like that...");
player.SendChatMessage($"Money left: ${player.Money}");
}
}
}
@lbyte00
Copy link

lbyte00 commented Mar 7, 2021

Awesome 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment