@@ -27,15 +27,21 @@ def __init__(self, logger, core, config_file):
2727 }
2828 json .dump (self .configs , open (config_file , 'w' , encoding = 'utf-8' ), indent = 2 )
2929
30- self .cycle_enabled = self .configs .get ('cycle_enabled' , True )
31-
3230 # load mcBasicLib
3331 self .utils = core .get_plugin ('mcBasicLib' )
3432 if self .utils is None :
3533 self .logger .error ('Failed to load plugin mcBasicLib. ScoreboardHelper will be disabled.' )
3634 self .logger .error ('Please make sure that mcBasicLib has been added to plugins.' )
3735 return # self.disabled = True
3836
37+ # initialize cycle timer
38+ self .cycle_enabled = self .configs .get ('cycle_enabled' , True )
39+ self .cycle_index = 0
40+ self .cycle_timer = QtCore .QTimer ()
41+ self .cycle_timer .timeout .connect (self .cycle_timer_action )
42+ cycle_interval = self .configs .get ('sec_between_cycle' , 15 ) * 1000
43+ self .cycle_timer .start (cycle_interval ) # start cycle timer
44+
3945 # connect signals and slots
4046 self .utils .sig_input .connect (self .on_player_input )
4147
@@ -53,7 +59,7 @@ def __init__(self, logger, core, config_file):
5359
5460 def unknown_command (self , player ):
5561 self .logger .warning ('Unknown command sent by player {}.' .format (player .name ))
56- self .utils .tell (player , 'Unknown command. Type "!sb help" for help.' )
62+ self .utils .tell (player , f 'Unknown command. Type "{ self . _cmd_prefix } help" for help.' )
5763
5864
5965 @QtCore .pyqtSlot (tuple )
@@ -68,25 +74,41 @@ def on_player_input(self, pair):
6874 else :
6975 self .unknown_command (player )
7076
77+
78+ # Timer-triggered Functions
79+ def cycle_timer_action (self ):
80+ if self .cycle_enabled :
81+ cycle_sb_list = self .configs .get ('cycle_scoreboards' , [])
82+ self .cycle_index %= len (cycle_sb_list )
83+ self .core .write_server (f'/scoreboard objectives setdisplay sidebar { cycle_sb_list [self .cycle_index ]} ' )
84+ self .cycle_index += 1
85+
86+
87+ def view_timer_end (self ):
88+ self .cycle_enabled = True
89+ self .cycle_index -= 1
90+ self .cycle_timer_action ()
91+ self .cycle_timer .start ()
92+
7193
72- # Plugin Functions
94+ # Plugin Command Functions
7395 def help (self , player , args : list ):
7496 if len (args ):
7597 self .unknown_command (player )
7698 return
7799
78- help_info = '''\
100+ help_info = f '''\
79101 ------------------ ScoreboardHelper Command List ------------------
80- "!sb help": Show this help message.
81- "!sb list": List all scoreboards.
82- "!sb view <name>": View a certain scoreboard for a period of time.
102+ "{ self . _cmd_prefix } help": Show this help message.
103+ "{ self . _cmd_prefix } list": List all scoreboards.
104+ "{ self . _cmd_prefix } view <name>": View a certain scoreboard for a period of time.
83105-------------------------------------------------------------------'''
84- op_help_info = '''\
106+ op_help_info = f '''\
85107 ----------------- ScoreboardHelper OP Command List ----------------
86- "!sb cycle <true|false>": Turn on/off scoreboard cycling.
87- "!sb <add|rm> <visible|cycle> <name>":
108+ "{ self . _cmd_prefix } cycle <true|false>": Turn on/off scoreboard cycling.
109+ "{ self . _cmd_prefix } <add|rm> <visible|cycle> <name>":
88110 Add/remove a scoreboard from visible/cycle list.
89- "!sb settime <visible|cycle> <time_in_sec>":
111+ "{ self . _cmd_prefix } settime <visible|cycle> <time_in_sec>":
90112 Set cycle interval / view duration time in sec.
91113-------------------------------------------------------------------'''
92114 help_msg = help_info + (op_help_info if player .is_op () else '' )
@@ -116,17 +138,15 @@ def view_sb(self, player, args: list):
116138
117139 sb_name = args [0 ]
118140 if sb_name not in self .configs .get ('visible_scoreboards' , []):
119- self .utils .tell (player , f'Invalid scoreboard \' { sb_name } \' . Use \' !sb list\' to see available scoreboards.' )
141+ self .utils .tell (player , f'Invalid scoreboard \' { sb_name } \' . Use \' { self . _cmd_prefix } list\' to see available scoreboards.' )
120142 return
121143
122144 self .cycle_enabled = False
145+ self .cycle_timer .stop ()
123146 self .core .write_server (f'/scoreboard objectives setdisplay sidebar { sb_name } ' )
124147 interval = self .configs .get ('sec_view_stay' , 3 ) * 1000
125148 self .view_timer = QtCore .QTimer ()
126- self .view_timer .singleShot (interval , self .view_timer_end )
127-
128- def view_timer_end (self ):
129- self .cycle_enabled = True
149+ self .view_timer .singleShot (interval , self .view_timer_end ) # do view_timer_end once after interval
130150
131151
132152 def add_sb (self , player , args : list ):
0 commit comments