11import { EntityTypes , Player , StructureRotation , StructureSaveMode , system , world } from '@minecraft/server'
2- import { MinecraftBlockTypes } from '@minecraft/vanilla-data'
2+ import { MinecraftBlockTypes , MinecraftEntityTypes } from '@minecraft/vanilla-data'
33import {
44 ActionForm ,
55 adventureModeRegions ,
@@ -21,9 +21,11 @@ import { createLogger } from 'lib/utils/logger'
2121import { structureLikeRotate , structureLikeRotateRelative , toAbsolute , toRelative } from 'lib/utils/structure'
2222import { Dungeon } from './loot'
2323
24+ const logger = createLogger ( 'dungeon' )
25+
2426export interface DungeonRegionDatabase extends JsonObject {
2527 chests : Record < string , number | null >
26- spawnerCooldowns : Record < string , number >
28+ spawnerCooldowns ? : Record < string , number >
2729 structureId : string
2830 rotation : StructureRotation
2931 terrainStructureId : string
@@ -68,9 +70,10 @@ export class DungeonRegion extends Region {
6870 }
6971
7072 for ( const spawner of dungeon . spawners ) {
73+ dungeon . ldb . spawnerCooldowns ??= { }
7174 const cooldown = dungeon . ldb . spawnerCooldowns [ spawner . id ]
7275 if ( ! cooldown || Cooldown . isExpired ( cooldown , spawner . restoreTime ) ) {
73- dungeon . spawn ( spawner )
76+ if ( dungeon . spawn ( spawner ) ) dungeon . ldb . spawnerCooldowns [ spawner . id ] = Date . now ( )
7477 }
7578 }
7679
@@ -136,9 +139,11 @@ export class DungeonRegion extends Region {
136139 for ( const f of toRotated ( enderChestPositions ) ) this . createChest ( f , powerfullLoot )
137140
138141 for ( const { loc, inv } of shulkers ) {
142+ this . ldb . spawnerCooldowns ??= { }
143+
139144 const [ rotated ] = this . rotate ( [ this . fromRelativeToAbsolute ( loc ) ] )
140145 if ( ! rotated ) {
141- console . warn ( 'Not rotated' )
146+ logger . warn ( 'Not rotated' )
142147 continue
143148 }
144149
@@ -169,15 +174,15 @@ export class DungeonRegion extends Region {
169174 }
170175
171176 configureSize ( ) {
172- // console.log ('Configuring size of', this.structureId, this.linkedDatabase)
177+ // logger.info ('Configuring size of', this.structureId, this.linkedDatabase)
173178 if ( isKeyof ( this . structureId , structureFiles ) ) {
174179 this . structureFile = structureFiles [ this . structureId ]
175- // console.log ('Created dungeon with size', Vector.string(this.structureSize))
180+ // logger.info ('Created dungeon with size', Vector.string(this.structureSize))
176181 } else return false
177182
178183 if ( this . area instanceof SphereArea ) {
179184 this . area . radius = Vec . distance ( this . getStructurePosition ( ) , this . area . center )
180- // console.log ('Changed radius of dungeon to', this.area.radius)
185+ // logger.info ('Changed radius of dungeon to', this.area.radius)
181186 }
182187
183188 return true
@@ -236,11 +241,19 @@ export class DungeonRegion extends Region {
236241 } )
237242 this . ldb . terrainStructureId = id
238243 this . ldb . terrainStructurePosition = from
239- console . log ( 'Saved backup for' , this . structureId , 'with id' , id )
244+ logger . info ( 'Saved backup for' , this . structureId , 'with id' , id )
240245 }
241246
242- console . log ( 'Placing structure' , position , this . structureId )
247+ logger . info ( 'Placing structure' , position , this . structureId )
243248 world . structureManager . place ( this . structureId , this . dimension , position , { rotation : this . ldb . rotation } )
249+
250+ for ( const spawner of this . spawners ) {
251+ try {
252+ this . dimension . setBlockType ( spawner . location , MinecraftBlockTypes . Air )
253+ } catch ( e ) {
254+ logger . error ( 'clear spawner' , e )
255+ }
256+ }
244257 }
245258
246259 fromAbsoluteToRelative ( vector : Vector3 ) {
@@ -274,7 +287,7 @@ export class DungeonRegion extends Region {
274287 for ( const value of inv ) {
275288 const entityTypeId = / ^ (?: m i n e c r a f t : ) ( .+ ) _ s p a w n _ e g g $ / . exec ( value . typeId ) ?. [ 1 ]
276289 if ( ! entityTypeId ) {
277- console . warn ( 'No entity type id for' , value . typeId )
290+ logger . warn ( 'No entity type id for' , value . typeId )
278291 continue
279292 }
280293
@@ -283,7 +296,7 @@ export class DungeonRegion extends Region {
283296 this . createSpawner ( rotated , entities , restoreTime )
284297 }
285298
286- protected createSpawner ( location : Vector3 , entities : DungeonSpawner [ 'entities' ] , restoreTime = ms . from ( 'min ' , 20 ) ) {
299+ protected createSpawner ( location : Vector3 , entities : DungeonSpawner [ 'entities' ] , restoreTime = ms . from ( 'sec ' , 30 ) ) {
287300 this . spawners . push ( {
288301 id : Vec . string ( location ) ,
289302 location,
@@ -294,14 +307,18 @@ export class DungeonRegion extends Region {
294307
295308 private static invalidSpawnerTypeIds = new Set < string > ( )
296309
310+ private brokenTypeIdsMap = new Map ( [ [ 'evoker' , MinecraftEntityTypes . EvocationIllager ] ] )
311+
297312 private spawn ( spawner : DungeonSpawner ) {
298- if ( ! anyPlayerNear ( spawner . location , this . dimensionType , 20 ) ) return
313+ if ( ! anyPlayerNear ( spawner . location , this . dimensionType , 50 ) ) return
299314
300- for ( const { typeId, amount } of spawner . entities ) {
315+ // eslint-disable-next-line prefer-const
316+ for ( let { typeId, amount } of spawner . entities ) {
317+ typeId = this . brokenTypeIdsMap . get ( typeId ) ?? typeId
301318 const type = EntityTypes . get < string > ( typeId )
302319 if ( ! type ) {
303320 if ( ! DungeonRegion . invalidSpawnerTypeIds . has ( typeId ) ) {
304- console . warn ( 'Dungeon spawner invalid typeId' , typeId , spawner )
321+ logger . warn ( 'Dungeon spawner invalid typeId' , typeId , spawner )
305322 DungeonRegion . invalidSpawnerTypeIds . add ( typeId )
306323 }
307324 continue
@@ -310,8 +327,16 @@ export class DungeonRegion extends Region {
310327 const entities = this . dimension . getEntities ( { location : spawner . location , maxDistance : 20 } )
311328 if ( entities . length < amount ) {
312329 const toSpawn = amount - entities . length
330+ logger . info (
331+ noI18n `Spawning for ${ this . displayName } at ${ Vec . string ( spawner . location , true ) } : ${ toSpawn } ${ typeId } ` ,
332+ )
313333 for ( let i = 0 ; i < toSpawn ; i ++ ) {
314- this . dimension . spawnEntity ( type , spawner . location )
334+ try {
335+ this . dimension . spawnEntity ( type , spawner . location )
336+ return true
337+ } catch ( e ) {
338+ logger . error ( 'spawn' , e )
339+ }
315340 }
316341 }
317342 }
@@ -324,7 +349,7 @@ export class DungeonRegion extends Region {
324349 }
325350
326351 protected createChest ( location : Vector3 , loot : LootTable , restoreTime = ms . from ( 'min' , 20 ) ) {
327- // console.log (
352+ // logger.info (
328353 // 'Created a chest at',
329354 // Vector.string(location, true),
330355 // Vector.string(this.fromRelativeToAbsolute(location)),
@@ -361,12 +386,12 @@ export class DungeonRegion extends Region {
361386 system . delay ( ( ) => {
362387 try {
363388 if ( id ) {
364- console . log ( 'Placing structure' , id , 'at' , position )
389+ logger . info ( 'Placing structure' , id , 'at' , position )
365390 world . structureManager . place ( id , dimension , position )
366391 world . structureManager . delete ( id )
367392 }
368393 } catch ( e ) {
369- console . warn ( 'Unable to place structure with id' , id , e )
394+ logger . warn ( 'Unable to place structure with id' , id , e )
370395 }
371396 } )
372397 }
0 commit comments