forked from JaylyDev/ScriptAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
35 lines (33 loc) · 1.37 KB
/
index.js
File metadata and controls
35 lines (33 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Script example for ScriptAPI
// Author: Jayly#1397 <Jayly Discord>
// Project: https://github.com/JaylyDev/ScriptAPI
import { Player, GameMode } from '@minecraft/server';
/**
* @remarks
* Gets the game mode for an entity asynchronously.
* @param {Player} player
* The player to retrieve.
* @returns {Promise<GameMode>} Player's gamemode
* @throws This function can throw errors.
*/
export function getGamemode(player) {
if (!(player instanceof Player)) throw new TypeError('Parameter is not an instance of Player');
return Promise.any([
player.runCommandAsync(`testfor @s[m=${GameMode.survival}]`).then(() => GameMode.survival),
player.runCommandAsync(`testfor @s[m=${GameMode.creative}]`).then(() => GameMode.creative),
player.runCommandAsync(`testfor @s[m=${GameMode.adventure}]`).then(() => GameMode.adventure),
player.runCommandAsync(`testfor @s[m=${GameMode.spectator}]`).then(() => GameMode.spectator),
]).catch(() => null);
};
/**
* @remarks
* Set the game mode for an entity.
* @param {Player} player
* The player to retrieve.
* @param {GameMode} gameMode
* @throws This function can throw errors.
*/
export function setGamemode(player, gameMode) {
if (!(player instanceof Player) || Object.values(GameMode).includes(gameMode)) throw new TypeError('Type conversion failed');
player.runCommand(`gamemode ${gameMode}`);
};