2024-11-19 20:05:19 -05:00
import parsePlain from '../util/chatparse_plain.js'
import parseMc from '../util/chatparse_mc_withHex.js'
import settings from '../settings.js'
import version from '../version.js'
2024-10-23 21:18:45 -04:00
class SCTask {
constructor ( failTask , startFailed = false ) {
/ *
* failed : Whether to run this task
* failTask : Command to run when failed is true
* /
this . failed = startFailed
this . failTask = failTask
}
}
2024-10-23 22:59:54 -04:00
export default function load ( b ) {
2024-10-23 21:18:45 -04:00
b . sc _tasks = { }
b . selfcareRun = 0
2024-11-19 20:05:19 -05:00
b . _client . on ( 'login' , ( ) => {
2024-11-04 00:03:19 -05:00
b . interval . sc = setInterval ( ( ) => {
if ( Date . now ( ) - b . selfcareRun <= 600 ) {
return
2024-10-23 21:18:45 -04:00
}
2024-11-04 00:03:19 -05:00
for ( const i in b . sc _tasks ) {
if ( b . sc _tasks [ i ] . failed ) {
b . sc _tasks [ i ] . failTask ( )
}
}
b . selfcareRun = Date . now ( )
} , 40 )
} )
2024-10-23 21:18:45 -04:00
b . add _sc _task = ( name , failTask , startFailed ) => {
b . sc _tasks [ name ] = new SCTask ( failTask , startFailed )
}
// Self care tasks
// Operator
b . add _sc _task ( 'op' , ( ) => {
b . chat ( '/op @s[type=player]' )
} )
b . _client . on ( 'login' , ( p ) => {
b . entityId = p . entityId
} )
b . _client . on ( 'entity_status' , ( p ) => {
if ( p . entityId === b . entityId && p . entityStatus === 24 ) {
b . sc _tasks . op . failed = 1
} else if ( p . entityId === b . entityId && p . entityStatus === 28 ) {
b . sc _tasks . op . failed = 0
}
} )
// CommandSpy
if ( ! b . host . options . isVanilla ) {
b . add _sc _task ( 'cspy' , ( ) => {
b . chat ( '/cspy on' )
} , true )
b . on ( 'plainchat' , ( msg ) => {
if ( msg === 'Successfully disabled CommandSpy' ) {
b . sc _tasks . cspy . failed = 1
} else if ( msg === 'Successfully enabled CommandSpy' ) {
b . sc _tasks . cspy . failed = 0
}
} )
}
// Gamemode / end portal bug
b . add _sc _task ( 'gamemode' , ( ) => {
b . chat ( '/minecraft:gamemode creative' )
} )
b . _client . on ( 'game_state_change' , ( p ) => {
if ( p . reason === 3 && p . gameMode !== 1 ) {
b . sc _tasks . gamemode . failed = 1
} else if ( p . reason === 3 && p . gameMode === 1 ) {
b . sc _tasks . gamemode . failed = 0
} else if ( p . reason === 4 ) {
b . sc _tasks . respawn . failed = 1
}
} )
// Respawning after dying
b . add _sc _task ( 'respawn' , ( ) => {
b . _client . write ( 'client_command' , { actionId : 0 } ) // Simulates respawning
b . sc _tasks . respawn . failed = 0
} )
b . on ( 'chat_unparsed' , ( data ) => {
if ( data . json . translate === 'chat.disabled.options' || ( data . json . extra && data . json . extra [ 0 ] && data . json . extra [ 0 ] . translate === 'chat.disabled.options' ) ||
data . json . translate === 'Chat disabled in client options' || ( data . json . extra && data . json . extra [ 0 ] && data . json . extra [ 0 ] . translate === 'Chat disabled in client options' ) ) {
b . sc _tasks . respawn . failed = 1
}
} )
// Prefix tablist ads
if ( ! b . host . options . isVanilla ) {
b . adPrefix = {
translate : '[%s] %s' , // Since the bot aims to have an invisible name, the ad prefix should contain information about the bot.
color : 'white' ,
with : [
{
translate : '%s: %s' ,
color : settings . colors . secondary ,
with : [
{
text : 'Prefix'
} ,
{
2024-10-23 23:35:21 -04:00
text : settings . prefixes [ 0 ] ,
2024-10-23 21:18:45 -04:00
color : settings . colors . primary
}
]
} ,
{
text : version . botName ,
color : settings . colors . primary
}
]
}
b . add _sc _task ( 'playerlist_ads' , ( ) => {
b . chat ( ` /prefix ${ parseMc ( b . adPrefix ) . replaceAll ( '§' , '&' ) } ` )
} )
b . on ( 'playerdata' , ( uuid , displayName ) => {
if ( uuid === b . _client . uuid && ! displayName . startsWith ( parsePlain ( b . adPrefix ) ) ) {
b . sc _tasks . playerlist _ads . failed = 1
}
} )
b . on ( 'plainchat' , ( msg ) => {
if ( msg === ` You now have the tag: ${ parseMc ( b . adPrefix ) . replaceAll ( '§' , '&' ) } ` ) {
b . sc _tasks . playerlist _ads . failed = 0
}
} )
}
}