Skip to content

Commit

Permalink
fix(bot): 🐛 save bot actual mc name (fix #193)
Browse files Browse the repository at this point in the history
  • Loading branch information
AnzhiZhang committed Jul 1, 2024
1 parent af051d9 commit 56b7826
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
18 changes: 13 additions & 5 deletions src/bot/bot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def __init__(
self.__auto_run_actions = auto_run_actions
self.__auto_update = auto_update

self.__mc_name: str = ''
self.__online: bool = False
self.__saved: bool = False

Expand Down Expand Up @@ -83,6 +84,10 @@ def auto_run_actions(self):
def auto_update(self):
return self.__auto_update

@property
def mc_name(self):
return self.__mc_name

@property
def online(self):
return self.__online
Expand Down Expand Up @@ -198,17 +203,20 @@ def spawn(self) -> None:
else:
raise BotOnlineException(self.name)

def spawned(self) -> None:
def spawned(self, mc_name: str) -> None:
"""
Handler when bot spawned.
"""
# set mc name
self.__mc_name = mc_name

# update online status
self.set_online(True)

# set gamemode
if self.saved or self.__plugin.config.force_gamemode:
self.__server.execute(
f'gamemode {self.__plugin.config.gamemode} {self.name}'
f'gamemode {self.__plugin.config.gamemode} {self.mc_name}'
)

# auto run actions
Expand All @@ -223,12 +231,12 @@ def kill(self) -> None:
if self.__online:
# auto update location
if self.auto_update:
self.set_location(self.__plugin.get_location(self.name))
self.set_location(self.__plugin.get_location(self.mc_name))
self.__plugin.bot_manager.save_data()

# kill
self.set_online(False)
self.__server.execute(f'player {self.name} kill')
self.__server.execute(f'player {self.mc_name} kill')
else:
raise BotOfflineException(self.name)

Expand All @@ -249,7 +257,7 @@ def run_actions(self, index: int = None) -> None:

# Run actions
for action in run_actions:
self.__server.execute(f'player {self.name} {action}')
self.__server.execute(f'player {self.mc_name} {action}')

def __str__(self):
return (
Expand Down
2 changes: 1 addition & 1 deletion src/bot/bot/event_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def on_player_joined(
bot = plugin.bot_manager.new_bot(name, location)

# Spawned handler
bot.spawned()
bot.spawned(player)

@staticmethod
@event_listener(MCDRPluginEvents.PLAYER_LEFT)
Expand Down

0 comments on commit 56b7826

Please sign in to comment.