@@ -4,6 +4,8 @@ const mc = require('../')
44const assert = require ( 'power-assert' )
55const { once } = require ( 'events' )
66
7+ const { getPort } = require ( './common/util' )
8+
79const w = {
810 piglin_safe : {
911 type : 'byte' ,
@@ -60,11 +62,17 @@ const w = {
6062}
6163
6264for ( const supportedVersion of mc . supportedVersions ) {
63- const PORT = Math . round ( 30000 + Math . random ( ) * 20000 )
65+ let PORT
6466 const mcData = require ( 'minecraft-data' ) ( supportedVersion )
6567 const version = mcData . version
6668
6769 describe ( 'mc-server ' + version . minecraftVersion , function ( ) {
70+
71+ this . beforeAll ( async function ( ) {
72+ PORT = await getPort ( )
73+ console . log ( `Using port for tests: ${ PORT } ` )
74+ } )
75+
6876 this . timeout ( 5000 )
6977 it ( 'starts listening and shuts down cleanly' , function ( done ) {
7078 const server = mc . createServer ( {
@@ -183,6 +191,32 @@ for (const supportedVersion of mc.supportedVersions) {
183191 } )
184192 server . on ( 'close' , done )
185193 } )
194+ it ( 'clients can be changed by beforeLogin' , function ( done ) {
195+ const notchUUID = '069a79f4-44e9-4726-a5be-fca90e38aaf5'
196+ const server = mc . createServer ( {
197+ 'online-mode' : false ,
198+ version : version . minecraftVersion ,
199+ port : PORT ,
200+ beforeLogin : ( client ) => {
201+ client . uuid = notchUUID
202+ }
203+ } )
204+ server . on ( 'listening' , function ( ) {
205+ const client = mc . createClient ( {
206+ username : 'notNotch' ,
207+ host : '127.0.0.1' ,
208+ version : version . minecraftVersion ,
209+ port : PORT
210+ } )
211+ client . on ( 'packet' , ( data , { name} ) => {
212+ if ( name === 'success' ) {
213+ assert . strictEqual ( data . uuid , notchUUID , 'UUID' )
214+ server . close ( )
215+ }
216+ } )
217+ } )
218+ server . on ( 'close' , done )
219+ } )
186220 it ( 'clients can log in and chat' , function ( done ) {
187221 const server = mc . createServer ( {
188222 'online-mode' : false ,
@@ -203,9 +237,9 @@ for (const supportedVersion of mc.supportedVersions) {
203237 entityId : client . id ,
204238 levelType : 'default' ,
205239 gameMode : 1 ,
206- previousGameMode : 255 ,
240+ previousGameMode : version . version >= 755 ? 0 : 255 ,
207241 worldNames : [ 'minecraft:overworld' ] ,
208- dimensionCodec : ( version . version >= 735 ? mcData . loginPacket . dimension : { name : '' , type : 'compound' , value : { dimension : { type : 'list' , value : { type : 'compound' , value : [ w ] } } } } ) ,
242+ dimensionCodec : version . version >= 755 ? mcData . loginPacket . dimensionCodec : ( version . version >= 735 ? mcData . loginPacket . dimension : { name : '' , type : 'compound' , value : { dimension : { type : 'list' , value : { type : 'compound' , value : [ w ] } } } } ) ,
209243 dimension : ( version . version >= 735 ? mcData . loginPacket . dimension : 0 ) ,
210244 worldName : 'minecraft:overworld' ,
211245 hashedSeed : [ 0 , 0 ] ,
@@ -328,9 +362,9 @@ for (const supportedVersion of mc.supportedVersions) {
328362 entityId : client . id ,
329363 levelType : 'default' ,
330364 gameMode : 1 ,
331- previousGameMode : 255 ,
365+ previousGameMode : version . version >= 755 ? 0 : 255 ,
332366 worldNames : [ 'minecraft:overworld' ] ,
333- dimensionCodec : ( version . version >= 735 ? mcData . loginPacket . dimension : { name : '' , type : 'compound' , value : { dimension : { type : 'list' , value : { type : 'compound' , value : [ w ] } } } } ) ,
367+ dimensionCodec : version . version >= 755 ? mcData . loginPacket . dimensionCodec : ( version . version >= 735 ? mcData . loginPacket . dimension : { name : '' , type : 'compound' , value : { dimension : { type : 'list' , value : { type : 'compound' , value : [ w ] } } } } ) ,
334368 dimension : ( version . version >= 735 ? mcData . loginPacket . dimension : 0 ) ,
335369 worldName : 'minecraft:overworld' ,
336370 hashedSeed : [ 0 , 0 ] ,
@@ -379,9 +413,9 @@ for (const supportedVersion of mc.supportedVersions) {
379413 entityId : client . id ,
380414 levelType : 'default' ,
381415 gameMode : 1 ,
382- previousGameMode : 255 ,
416+ previousGameMode : version . version >= 755 ? 0 : 255 ,
383417 worldNames : [ 'minecraft:overworld' ] ,
384- dimensionCodec : ( version . version >= 735 ? mcData . loginPacket . dimension : { name : '' , type : 'compound' , value : { dimension : { type : 'list' , value : { type : 'compound' , value : [ w ] } } } } ) ,
418+ dimensionCodec : version . version >= 755 ? mcData . loginPacket . dimensionCodec : ( version . version >= 735 ? mcData . loginPacket . dimension : { name : '' , type : 'compound' , value : { dimension : { type : 'list' , value : { type : 'compound' , value : [ w ] } } } } ) ,
385419 dimension : ( version . version >= 735 ? mcData . loginPacket . dimension : 0 ) ,
386420 worldName : 'minecraft:overworld' ,
387421 hashedSeed : [ 0 , 0 ] ,
0 commit comments