-
-
Save root-cause/954064612ada82b6aa6d5cb7859cc008 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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}"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Awesome 🚀