-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
100 lines (75 loc) · 3.27 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import json
import os
import sys
import importlib
import cli
import game
configFile = open("config.json", "r")
config = json.loads(configFile.read())
configFile.close()
owd = os.getcwd()
os.chdir(config["serverDirectory"])
os.system("LD_LIBRARY_PATH=.")
cliInstance = cli.CLI([os.path.join(config["serverDirectory"], "bedrock_server")])
gameInterfaceInstance = game.GameInterface(cliInstance)
os.chdir(owd)
started = False
while not started:
events = gameInterfaceInstance.captureEvents()
for event in events:
if event.type == "serverStart":
started = True
gameInterfaceInstance.sendChatMessage("§eLoading plugins...")
print("Loading plugins...")
while True:
plugins = []
for plugin in config["plugins"]:
plugins.append(importlib.import_module("plugins.{}.main".format(plugin["name"]), plugin["name"]))
if hasattr(plugins[-1], "__start__"):
try:
plugins[-1].__start__(game, gameInterfaceInstance, plugin["config"])
except Exception as e:
eType, eObj, eTb = sys.exc_info()
gameInterfaceInstance.sendChatMessage("§4Plugin {} failed: uncaught {}".format(plugins[-1].__name__.split(".")[1], e.__class__.__name__))
print("Plugin {} failed at start: uncaught {}: {} (in {} at line {})".format(
plugins[-1].__name__.split(".")[1],
e.__class__.__name__,
str(e),
os.path.split(eTb.tb_frame.f_code.co_filename)[1],
eTb.tb_lineno
))
gameInterfaceInstance.sendChatMessage("§ePlugin {} loaded".format(plugins[-1].__name__.split(".")[1]))
print("Plugin {} loaded".format(plugins[-1].__name__.split(".")[1]))
if len(plugins) == 1:
gameInterfaceInstance.sendChatMessage("§aLoaded 1 plugin")
print("Loaded 1 plugin")
else:
gameInterfaceInstance.sendChatMessage("§aLoaded {} plugins".format(len(plugins)))
print("Loaded {} plugins".format(len(plugins)))
while True:
configFile = open("config.json", "r")
try:
newConfig = json.loads(configFile.read())
except:
pass
configFile.close()
if newConfig != config:
config = newConfig
break
latestEvents = gameInterfaceInstance.captureEvents()
for plugin in plugins:
if hasattr(plugin, "__loop__"):
try:
plugin.__loop__(latestEvents)
except Exception as e:
eType, eObj, eTb = sys.exc_info()
gameInterfaceInstance.sendChatMessage("§4Plugin {} failed: uncaught {}".format(plugin.__name__.split(".")[1], e.__class__.__name__))
print("Plugin {} failed at loop: uncaught {}: {} (in {} at line {})".format(
plugin.__name__.split(".")[1],
e.__class__.__name__,
str(e),
os.path.split(eTb.tb_frame.f_code.co_filename)[1],
eTb.tb_lineno
))
gameInterfaceInstance.sendChatMessage("§ePlugin configuration modified, reloading plugins...")
print("Plugin configuration modified, reloading plugins...")