Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// 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

This comment has been minimized.

Copy link

@lbyte00 lbyte00 commented Mar 7, 2021

Awesome 🚀

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