11package com .macuyiko .minecraftpyserver ;
22
33import java .io .File ;
4+ import java .io .IOException ;
45import java .util .ArrayList ;
56import java .util .List ;
67
1213import com .macuyiko .minecraftpyserver .jython .JyCommandExecutor ;
1314import com .macuyiko .minecraftpyserver .jython .JyInterpreter ;
1415import com .macuyiko .minecraftpyserver .jython .JyWebSocketServer ;
15- import com .macuyiko .minecraftpyserver .jython .TelnetServer ;
16+ import com .macuyiko .minecraftpyserver .jython .JyTelnetServer ;
1617
1718public class MinecraftPyServerPlugin extends JavaPlugin {
1819
1920 public final static String PLUGIN_NAME = "MinecraftPyServer" ;
2021
2122 private List <JyInterpreter > pluginInterpreters = new ArrayList <JyInterpreter >();
23+ private JyWebSocketServer webSocketServer ;
24+ private JyTelnetServer telnetServer ;
25+ private JyChatServer commandServer ;
26+ private Thread telnetServerThread ;
2227
2328 @ Override
2429 public void onEnable () {
@@ -28,41 +33,43 @@ public void onEnable() {
2833
2934 int tcpsocketserverport = getConfig ().getInt ("pythonconsole.telnetport" , 44444 );
3035 int websocketserverport = getConfig ().getInt ("pythonconsole.websocketport" , 44445 );
31- boolean enablechatcommands = getConfig ().getString ("pythonconsole.enablechatcommands" , "true" ).equalsIgnoreCase ("true" );
36+ boolean enablechatcommands = getConfig ().getString ("pythonconsole.enablechatcommands" , "true" )
37+ .equalsIgnoreCase ("true" );
3238
3339 if (tcpsocketserverport > 0 )
34- startTelnetServer (this , tcpsocketserverport );
40+ telnetServer = startTelnetServer (this , tcpsocketserverport );
3541
3642 if (websocketserverport > 0 )
37- startWebSocketServer (this , websocketserverport );
43+ webSocketServer = startWebSocketServer (this , websocketserverport );
3844
3945 if (enablechatcommands ) {
40- JyChatServer commandServer = startChatServer (this );
46+ commandServer = startChatServer (this );
4147 this .getCommand ("py" ).setExecutor (new JyCommandExecutor (this , commandServer ));
4248 this .getCommand ("pyrestart" ).setExecutor (new JyCommandExecutor (this , commandServer ));
4349 this .getCommand ("pyload" ).setExecutor (new JyCommandExecutor (this , commandServer ));
50+ this .getCommand ("pyreload" ).setExecutor (new JyCommandExecutor (this , commandServer ));
4451 }
4552
46- File pluginDirectory = new File ("./python-plugins/" );
47- if (pluginDirectory .exists () && pluginDirectory .isDirectory ()) {
48- File [] files = pluginDirectory .listFiles ();
49- for (int i = 0 ; i < files .length ; i ++) {
50- if (files [i ].getName ().endsWith (".py" )) {
51- System .err .println ("[MinecraftPyServer] Parsing plugin: " + files [i ].getName ());
52- JyInterpreter pluginInterpreter = new JyInterpreter (true );
53- pluginInterpreter .execfile (files [i ]);
54- pluginInterpreters .add (pluginInterpreter );
55- }
56- }
57- }
53+ startPluginInterpreters ();
5854 }
5955
6056 public void onDisable () {
6157 log ("Unloading MinecraftPyServerPlugin" );
6258
63- for (JyInterpreter pluginInterpreter : pluginInterpreters ) {
64- pluginInterpreter .close ();
59+ stopPluginInterpreters ();
60+
61+ try {
62+ webSocketServer .stop ();
63+ } catch (IOException e ) {
64+ } catch (InterruptedException e ) {
65+ }
66+
67+ telnetServerThread .interrupt ();
68+ try {
69+ telnetServerThread .join (1000 );
70+ } catch (InterruptedException e ) {
6571 }
72+ telnetServer .close ();
6673 }
6774
6875 public void log (String message ) {
@@ -78,25 +85,48 @@ public void send(String player, String message) {
7885 send (p , message );
7986 }
8087
81- public static TelnetServer startTelnetServer (MinecraftPyServerPlugin mainPlugin , int telnetport ) {
82- TelnetServer server = new TelnetServer (mainPlugin , telnetport );
83- Thread t = new Thread (server );
84- t .start ();
88+ public void restartPluginInterpreters () {
89+ stopPluginInterpreters ();
90+ startPluginInterpreters ();
91+ }
92+
93+ private void startPluginInterpreters () {
94+ File pluginDirectory = new File ("./python-plugins/" );
95+ if (pluginDirectory .exists () && pluginDirectory .isDirectory ()) {
96+ File [] files = pluginDirectory .listFiles ();
97+ for (int i = 0 ; i < files .length ; i ++) {
98+ if (files [i ].getName ().endsWith (".py" )) {
99+ System .err .println ("[MinecraftPyServer] Parsing plugin: " + files [i ].getName ());
100+ JyInterpreter pluginInterpreter = new JyInterpreter (true );
101+ pluginInterpreter .execfile (files [i ]);
102+ pluginInterpreters .add (pluginInterpreter );
103+ }
104+ }
105+ }
106+ }
107+
108+ private void stopPluginInterpreters () {
109+ for (JyInterpreter pluginInterpreter : pluginInterpreters ) {
110+ pluginInterpreter .close ();
111+ }
112+ }
113+
114+ public JyTelnetServer startTelnetServer (MinecraftPyServerPlugin mainPlugin , int telnetport ) {
115+ JyTelnetServer server = new JyTelnetServer (mainPlugin , telnetport );
116+ telnetServerThread = new Thread (server );
117+ telnetServerThread .start ();
85118 return server ;
86119 }
87120
88- public static JyWebSocketServer startWebSocketServer (MinecraftPyServerPlugin mainPlugin , int websocketport ) {
121+ public JyWebSocketServer startWebSocketServer (MinecraftPyServerPlugin mainPlugin , int websocketport ) {
89122 JyWebSocketServer server = new JyWebSocketServer (mainPlugin , websocketport );
90123 server .start ();
91124 return server ;
92125 }
93126
94- public static JyChatServer startChatServer (MinecraftPyServerPlugin mainPlugin ) {
127+ public JyChatServer startChatServer (MinecraftPyServerPlugin mainPlugin ) {
95128 JyChatServer server = new JyChatServer (mainPlugin );
96129 return server ;
97130 }
98-
99-
100-
101131
102132}
0 commit comments