File tree Expand file tree Collapse file tree
scripts/gamemode/promises Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import { Player , GameMode } from '@minecraft/server' ;
2+
3+ /**
4+ * @remarks
5+ * Gets the game mode for an entity.
6+ * @param {Player } player
7+ * The player to retrieve.
8+ * @returns {Promise<GameMode> } Player's gamemode
9+ * @throws This function can throw errors.
10+ */
11+ export function getGamemode ( player ) {
12+ if ( ! ( player instanceof Player ) ) throw new TypeError ( 'Parameter is not an instance of Player' ) ;
13+ return Promise . any ( [
14+ player . runCommandAsync ( `testfor @s[m=${ GameMode . survival } ]` ) . then ( ( ) => GameMode . survival ) ,
15+ player . runCommandAsync ( `testfor @s[m=${ GameMode . creative } ]` ) . then ( ( ) => GameMode . creative ) ,
16+ player . runCommandAsync ( `testfor @s[m=${ GameMode . adventure } ]` ) . then ( ( ) => GameMode . adventure ) ,
17+ player . runCommandAsync ( `testfor @s[m=${ GameMode . spectator } ]` ) . then ( ( ) => GameMode . spectator ) ,
18+ ] ) . catch ( ( ) => null ) ;
19+ } ;
20+
21+ /**
22+ * @remarks
23+ * Set the game mode for an entity.
24+ * @param {Player } player
25+ * The player to retrieve.
26+ * @param {GameMode } gameMode
27+ * @throws This function can throw errors.
28+ */
29+ export function setGamemode ( player , gameMode ) {
30+ if ( ! ( player instanceof Player ) || Object . values ( GameMode ) . includes ( gameMode ) ) throw new TypeError ( 'Type conversion failed' ) ;
31+ player . runCommandAsync ( `gamemode ${ gameMode } ` ) ;
32+ } ;
Original file line number Diff line number Diff line change 1+ import { getGamemode , setGamemode } from 'gamemode/promises/index' ;
2+ import { world , GameMode } from '@minecraft/server' ;
3+
4+ world . events . chat . subscribe ( ( { sender} ) => {
5+ getGamemode ( sender ) . then ( res => { sender . tell ( res ) } ) ;
6+ setGamemode ( sender , GameMode . creative ) ;
7+ } ) ;
You can’t perform that action at this time.
0 commit comments