1- import { TicksPerSecond , world } from '@minecraft/server'
1+ import { Player , TicksPerSecond , world } from '@minecraft/server'
22import { MinecraftEffectTypes , MinecraftEffectTypesUnion } from '@minecraft/vanilla-data'
3- import { ms } from 'lib'
3+ import { doNothing , ms , RoadRegion } from 'lib'
4+ import { form } from 'lib/form/new'
5+ import { i18n } from 'lib/i18n/text'
46import { DurationalRecurringEvent } from 'lib/recurring-event'
7+ import { RegionEvents } from 'lib/region/events'
58import later from 'lib/utils/later'
69
710// TODO Add settings for players to not apply effects on them
@@ -10,30 +13,61 @@ import later from 'lib/utils/later'
1013// TODO Add menu to the survival menu
1114// TODO Add chat notification
1215
13- for ( const { effectType, startingOn } of [
14- { effectType : 'Haste' , startingOn : 1 } ,
15- { effectType : 'HealthBoost' , startingOn : 4 } ,
16- ] satisfies {
17- effectType : MinecraftEffectTypesUnion
18- startingOn : number
19- } [ ] ) {
20- new DurationalRecurringEvent (
21- `effect${ effectType } ` ,
22- later . parse . recur ( ) . every ( 5 ) . hour ( ) . startingOn ( startingOn ) ,
23- ms . from ( 'min' , 10 ) ,
24- ( ) => ( { } ) ,
25- ( _ , ctx ) => {
26- ctx . temp . system . runInterval (
27- ( ) => {
28- for ( const player of world . getAllPlayers ( ) )
29- player . addEffect ( MinecraftEffectTypes [ effectType ] , TicksPerSecond * 3 , {
30- amplifier : 2 ,
31- showParticles : false ,
32- } )
33- } ,
34- `effect${ effectType } ` ,
35- TicksPerSecond * 2 ,
36- )
37- } ,
38- )
16+ class RecurringEffect {
17+ static all : RecurringEffect [ ] = [ ]
18+
19+ readonly event : DurationalRecurringEvent
20+
21+ constructor (
22+ readonly effectType : MinecraftEffectTypesUnion ,
23+ readonly startingOn : number ,
24+ filter ?: ( p : Player ) => boolean ,
25+ readonly amplifier = 2 ,
26+ ) {
27+ RecurringEffect . all . push ( this )
28+ this . event = new DurationalRecurringEvent (
29+ `effect${ effectType } ` ,
30+ later . parse . recur ( ) . every ( 5 ) . hour ( ) . startingOn ( startingOn ) ,
31+ ms . from ( 'min' , 10 ) ,
32+ ( ) => ( { } ) ,
33+ ( _ , ctx ) => {
34+ for ( const player of world . getAllPlayers ( ) ) {
35+ player . success ( i18n . success `Событие! ${ effectType } силой ${ amplifier } на ${ 10 } минут` )
36+ }
37+ ctx . temp . system . runInterval (
38+ ( ) => {
39+ for ( const player of world . getAllPlayers ( ) ) {
40+ if ( filter && ! filter ( player ) ) continue
41+
42+ player . addEffect ( MinecraftEffectTypes [ effectType ] , TicksPerSecond * 3 , {
43+ amplifier,
44+ showParticles : false ,
45+ } )
46+ }
47+ } ,
48+ `effect${ effectType } ` ,
49+ TicksPerSecond * 2 ,
50+ )
51+ } ,
52+ )
53+ }
3954}
55+
56+ new RecurringEffect ( 'Haste' , 1 )
57+ new RecurringEffect ( 'HealthBoost' , 2 )
58+ new RecurringEffect (
59+ 'Speed' ,
60+ 3 ,
61+ p => RegionEvents . playerInRegionsCache . get ( p ) ?. some ( e => e instanceof RoadRegion ) ?? false ,
62+ 4 ,
63+ )
64+
65+ export const recurForm = form ( f => {
66+ f . title ( i18n `События` )
67+ for ( const event of RecurringEffect . all ) {
68+ f . button (
69+ event . effectType + ' ' + ( event . amplifier + 1 ) + '\n' + event . event . getNextOccurances ( 1 ) [ 0 ] ?. toHHMM ( ) ,
70+ doNothing ,
71+ )
72+ }
73+ } )
0 commit comments