1- var __spreadArray = ( this && this . __spreadArray ) || function ( to , from , pack ) {
2- if ( pack || arguments . length === 2 ) for ( var i = 0 , l = from . length , ar ; i < l ; i ++ ) {
3- if ( ar || ! ( i in from ) ) {
4- if ( ! ar ) ar = Array . prototype . slice . call ( from , 0 , i ) ;
5- ar [ i ] = from [ i ] ;
6- }
7- }
8- return to . concat ( ar || Array . prototype . slice . call ( from ) ) ;
9- } ;
101/**
112 * @license MIT
123 * @author JaylyMC
134 * @link https://github.com/JaylyDev/GameTestDB
145 */
15- import { world , Vector } from "@minecraft/server" ;
6+ import { world , Vector , system } from "@minecraft/server" ;
167import "@minecraft/server-gametest" ; // import "@minecraft/server-gametest" native module to support Simulated Players
178/**
189 * @license MIT
@@ -55,53 +46,16 @@ function comparePlayer(playerA, playerB) {
5546 * Keeping this class private to avoid confusion between
5647 * Player class from "@minecraft/server" module.
5748 */
58- var Player = /** @class */ ( function ( ) {
59- /**
60- * @remarks
61- * Manually assign some player property to the new player class
62- * before failed to get property due to GameTest can't fetch
63- * despawned entities.
64- * @param {MinecraftPlayer } player
65- */
66- function Player ( player ) {
67- // PRIVATE PROPERTIES
68- // This properties should not be used by public
69- try {
70- this . __PlayerBlockFromViewDirection = player . getBlockFromViewDirection ( ) ;
71- }
72- catch ( _a ) { }
73- ;
74- try {
75- this . __PlayerEntitiesFromViewDirection = player . getEntitiesFromViewDirection ( ) ;
76- }
77- catch ( _b ) { }
78- ;
79- this . __PlayerComponents = cloneJSON ( player . getComponents ( ) ) ;
80- this . __PlayerTags = player . getTags ( ) ;
81- // PUBLIC PROPERTIES
82- this . dimension = player . dimension ;
83- this . headLocation = player . headLocation ;
84- this . id = player . id ;
85- this . isSneaking = player . isSneaking ;
86- this . location = player . location ;
87- this . name = player . name ;
88- this . nameTag = player . nameTag ;
89- this . rotation = player . rotation ;
90- this . scoreboard = player . scoreboard ;
91- this . selectedSlot = player . selectedSlot ;
92- this . target = player . target ;
93- this . getVelocity ( ) = player . getVelocity ( ) ;
94- this . viewDirection = new Vector ( player . getViewDirection ( ) . x , player . getViewDirection ( ) . y , player . getViewDirection ( ) . z ) ;
95- }
49+ class Player {
9650 /**
9751 * @remarks
9852 * Gets the first block that intersects with the vector of the
9953 * view of this entity.
10054 * @throws This function can throw errors.
10155 */
102- Player . prototype . getBlockFromViewDirection = function ( ) {
56+ getBlockFromViewDirection ( ) {
10357 return this . __PlayerBlockFromViewDirection ;
104- } ;
58+ }
10559 ;
10660 /**
10761 * @remarks
@@ -113,23 +67,23 @@ var Player = /** @class */ (function () {
11367 * 'minecraft:' is assumed. If the component is not present on
11468 * the entity, undefined is returned.
11569 */
116- Player . prototype . getComponent = function ( componentId ) {
70+ getComponent ( componentId ) {
11771 return this . __PlayerComponents . find ( function ( component ) {
11872 if ( ! componentId . startsWith ( "minecraft:" ) )
11973 componentId = "minecraft:" + componentId ;
120- if ( component . id === componentId )
74+ if ( component . typeId === componentId )
12175 return true ;
12276 } ) ;
123- } ;
77+ }
12478 ;
12579 /**
12680 * @remarks
12781 * Returns all components that are both present on this entity
12882 * and supported by the API.
12983 */
130- Player . prototype . getComponents = function ( ) {
84+ getComponents ( ) {
13185 return this . __PlayerComponents ;
132- } ;
86+ }
13387 ;
13488 /**
13589 * @remarks
@@ -138,18 +92,18 @@ var Player = /** @class */ (function () {
13892 * Additional options for processing this raycast query.
13993 * @throws This function can throw errors.
14094 */
141- Player . prototype . getEntitiesFromViewDirection = function ( ) {
95+ getEntitiesFromViewDirection ( ) {
14296 return this . __PlayerEntitiesFromViewDirection ;
143- } ;
97+ }
14498 ;
14599 /**
146100 * @remarks
147101 * Returns all tags associated with an entity.
148102 * @throws This function can throw errors.
149103 */
150- Player . prototype . getTags = function ( ) {
104+ getTags ( ) {
151105 return this . __PlayerTags ;
152- } ;
106+ }
153107 ;
154108 /**
155109 * @remarks
@@ -160,13 +114,13 @@ var Player = /** @class */ (function () {
160114 * to retrieve. If no namespace prefix is specified,
161115 * 'minecraft:' is assumed.
162116 */
163- Player . prototype . hasComponent = function ( componentId ) {
164- var componentIndex = this . __PlayerComponents . findIndex ( function ( entityComponent ) { return entityComponent . id === componentId ; } ) ;
117+ hasComponent ( componentId ) {
118+ let componentIndex = this . __PlayerComponents . findIndex ( ( entityComponent ) => entityComponent . id === componentId ) ;
165119 if ( componentIndex < 0 )
166120 return false ;
167121 else
168122 return true ;
169- } ;
123+ }
170124 ;
171125 /**
172126 * @remarks
@@ -175,92 +129,120 @@ var Player = /** @class */ (function () {
175129 * Identifier of the tag to test for.
176130 * @throws This function can throw errors.
177131 */
178- Player . prototype . hasTag = function ( tag ) {
179- var tagIndex = this . __PlayerTags . findIndex ( function ( playerTag ) { return playerTag === tag ; } ) ;
132+ hasTag ( tag ) {
133+ let tagIndex = this . __PlayerTags . findIndex ( playerTag => playerTag === tag ) ;
180134 if ( tagIndex < 0 )
181135 return false ;
182136 else
183137 return true ;
184- } ;
138+ }
185139 ;
140+ /**
141+ * @remarks
142+ * Manually assign some player property to the new player class
143+ * before failed to get property due to GameTest can't fetch
144+ * despawned entities.
145+ * @param {MinecraftPlayer } player
146+ */
147+ constructor ( player ) {
148+ // PRIVATE PROPERTIES
149+ // This properties should not be used by public
150+ try {
151+ this . __PlayerBlockFromViewDirection = player . getBlockFromViewDirection ( ) ;
152+ }
153+ catch ( _a ) { }
154+ ;
155+ try {
156+ this . __PlayerEntitiesFromViewDirection = player . getEntitiesFromViewDirection ( ) ;
157+ }
158+ catch ( _b ) { }
159+ ;
160+ this . __PlayerComponents = cloneJSON ( player . getComponents ( ) ) ;
161+ this . __PlayerTags = player . getTags ( ) ;
162+ // PUBLIC PROPERTIES
163+ const velocity = player . getVelocity ( ) ;
164+ this . dimension = player . dimension ;
165+ this . headLocation = player . getHeadLocation ( ) ;
166+ this . id = player . id ;
167+ this . typeId = player . typeId ;
168+ this . isSneaking = player . isSneaking ;
169+ this . location = player . location ;
170+ this . name = player . name ;
171+ this . nameTag = player . nameTag ;
172+ this . rotation = player . getRotation ( ) ;
173+ this . scoreboard = player . scoreboard ;
174+ this . selectedSlot = player . selectedSlot ;
175+ this . target = player . target ;
176+ this . velocity = new Vector ( velocity . x , velocity . y , velocity . z ) ;
177+ this . viewDirection = new Vector ( player . getViewDirection ( ) . x , player . getViewDirection ( ) . y , player . getViewDirection ( ) . z ) ;
178+ }
186179 ;
187- return Player ;
188- } ( ) ) ;
180+ }
189181;
190182/**
191183 * Contains information regarding a player that has left the
192184 * world.
193185 */
194- var PlayerLeaveEvent = /** @ class */ ( function ( ) {
195- function PlayerLeaveEvent ( player ) {
186+ export class PlayerLeaveEvent {
187+ constructor ( player ) {
196188 this . player = player ;
197189 }
198190 ;
199- return PlayerLeaveEvent ;
200- } ( ) ) ;
201- export { PlayerLeaveEvent } ;
191+ }
202192;
203193/**
204194 * Manages callbacks that are connected to a player leaving the
205195 * world.
206196 */
207- var PlayerLeaveEventSignal = /** @class */ ( function ( ) {
208- function PlayerLeaveEventSignal ( ) {
209- }
197+ export class PlayerLeaveEventSignal {
210198 /**
211199 * @remarks
212200 * Adds a callback that will be called when a player leaves the
213201 * world.
214202 * @param {(arg: PlayerLeaveEvent) => void } callback
215203 * @return {(arg: PlayerLeaveEvent) => void }
216204 */
217- PlayerLeaveEventSignal . prototype . subscribe = function ( callback ) {
205+ subscribe ( callback ) {
218206 callback [ "playerLeave" ] = true ;
219- var players = __spreadArray ( [ ] , world . getPlayers ( ) , true ) . map ( function ( pl ) { return new Player ( pl ) ; } ) ;
220- var executedPlayers = [ ] ;
221- var TickEventCallback = system . runInterval ( function ( ) {
207+ let players = [ ... world . getPlayers ( ) ] . map ( pl => new Player ( pl ) ) ;
208+ let executedPlayers = [ ] ;
209+ let TickEventCallback = system . runInterval ( ( ) => {
222210 if ( callback [ "playerLeave" ] !== true )
223211 system . clearRun ( TickEventCallback ) ;
224212 // Change from player class to custom player class
225- var currentPlayers = __spreadArray ( [ ] , world . getPlayers ( ) , true ) ;
226- var _loop_1 = function ( player ) {
227- var executedPlayerIndex = executedPlayers . findIndex ( function ( executedPlayer ) { return comparePlayer ( executedPlayer , player ) ; } ) ;
228- if ( ! currentPlayers . find ( function ( pl ) { return comparePlayer ( pl , player ) ; } ) && executedPlayerIndex < 0 ) {
213+ let currentPlayers = [ ... world . getPlayers ( ) ] ;
214+ for ( let player of players ) {
215+ let executedPlayerIndex = executedPlayers . findIndex ( executedPlayer => comparePlayer ( executedPlayer , player ) ) ;
216+ if ( ! currentPlayers . find ( pl => comparePlayer ( pl , player ) ) && executedPlayerIndex < 0 ) {
229217 executedPlayers . push ( player ) ;
230- var onPlayerSpawn_1 = world . events . playerSpawn . subscribe ( function ( playerJoinEvent ) {
231- var playerIndex = executedPlayers . findIndex ( function ( pl ) { return comparePlayer ( pl , playerJoinEvent . player ) ; } ) ;
218+ let onPlayerSpawn = world . events . playerSpawn . subscribe ( ( playerJoinEvent ) => {
219+ let playerIndex = executedPlayers . findIndex ( pl => comparePlayer ( pl , playerJoinEvent . player ) ) ;
232220 if ( playerIndex >= 0 ) {
233221 executedPlayers . splice ( playerIndex ) ;
234- world . events . playerSpawn . unsubscribe ( onPlayerSpawn_1 ) ;
222+ world . events . playerSpawn . unsubscribe ( onPlayerSpawn ) ;
235223 }
236224 ;
237225 } ) ;
238226 callback ( new PlayerLeaveEvent ( player ) ) ;
239227 }
240228 ;
241- } ;
242- for ( var _i = 0 , players_1 = players ; _i < players_1 . length ; _i ++ ) {
243- var player = players_1 [ _i ] ;
244- _loop_1 ( player ) ;
245229 }
246230 ;
247231 players = [ ] ;
248- players = __spreadArray ( [ ] , world . getPlayers ( ) , true ) . map ( function ( pl ) { return new Player ( pl ) ; } ) ;
232+ players = [ ... world . getPlayers ( ) ] . map ( pl => new Player ( pl ) ) ;
249233 } ) ;
250234 return callback ;
251- } ;
235+ }
252236 ;
253237 /**
254238 * @remarks
255239 * Removes a callback from being called when a player leaves
256240 * the world.
257241 * @param {(arg: PlayerLeaveEvent) => void } callback
258242 */
259- PlayerLeaveEventSignal . prototype . unsubscribe = function ( callback ) {
243+ unsubscribe ( callback ) {
260244 callback [ "playerLeave" ] = false ;
261- } ;
245+ }
262246 ;
263- return PlayerLeaveEventSignal ;
264- } ( ) ) ;
265- export { PlayerLeaveEventSignal } ;
247+ }
266248;
0 commit comments