From 06c170617c1c06b38846467ea35f29bf2fb36ca1 Mon Sep 17 00:00:00 2001 From: Yehonal Date: Sun, 9 Oct 2022 22:13:15 +0200 Subject: [PATCH] feat: add example.ts --- .gitignore | 1 + scripts/typescript/examples/example.ts | 65 ++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 scripts/typescript/examples/example.ts diff --git a/.gitignore b/.gitignore index 7f7ed2b..9c2d02b 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ !/scripts/*/ /scripts/*/* !/scripts/*/.gitkeep +!/scripts/typescript/examples !/scripts/typescript/tsconfig.json !/scripts/typescript/package.json /var/* diff --git a/scripts/typescript/examples/example.ts b/scripts/typescript/examples/example.ts new file mode 100644 index 0000000..08aa8fb --- /dev/null +++ b/scripts/typescript/examples/example.ts @@ -0,0 +1,65 @@ +/** + * Example with arrow function + * + * This is just an Hello World + */ +export const OnPlayerLogin: player_event_on_login = (event, player) => { + player.SendChatMessageToPlayer( + ChatMsg.CHAT_MSG_WHISPER, + Language.LANG_UNIVERSAL, + "Hello World", + player + ); +}; + +RegisterPlayerEvent(PlayerEvents.PLAYER_EVENT_ON_LOGIN, (...args) => + OnPlayerLogin(...args) +); + +/** + * + * This is an example of how to use TypeScript classes + * and implement hook methods inside it. + * + * A class can be useful to implement more complex + * scripts that can be instantiated or extended + * + */ +export class PlayerChat { + previousMessage: string; + + /** + * + * @param firstMessage The first message to display + */ + constructor(firstMessage: string) { + this.previousMessage = firstMessage; + } + + OnPlayerChat: player_event_on_chat = ( + event: number, + player: Player, + msg: string + ): string | boolean => { + const sanitized = msg; + + msg = sanitized != msg ? `${sanitized} (Sanitized)` : msg; + + player.SendChatMessageToPlayer( + ChatMsg.CHAT_MSG_WHISPER, + Language.LANG_UNIVERSAL, + `Previous message: ${this.previousMessage}, current message: ${msg}`, + player + ); + + this.previousMessage = msg; + + return true; + }; +} + +const playerChat = new PlayerChat("First Message"); + +RegisterPlayerEvent(PlayerEvents.PLAYER_EVENT_ON_CHAT, (...args) => + playerChat.OnPlayerChat(...args) +);