11package club .moddedminecraft .polychat .bukkitclient ;
22
3- import java .io .File ;
4- import java .io .FileInputStream ;
5- import java .io .FileOutputStream ;
6- import java .io .IOException ;
7- import java .net .Socket ;
8- import java .util .ArrayList ;
9- import java .util .Properties ;
10-
3+ import club .moddedminecraft .polychat .bukkitclient .threads .ActivePlayerThread ;
4+ import club .moddedminecraft .polychat .bukkitclient .threads .ReattachThread ;
115import club .moddedminecraft .polychat .networking .io .*;
126import org .bukkit .Bukkit ;
137import org .bukkit .ChatColor ;
8+ import org .bukkit .Server ;
149import org .bukkit .entity .Player ;
15- import org .bukkit .event .* ;
10+ import org .bukkit .event .Listener ;
1611import org .bukkit .plugin .java .JavaPlugin ;
17-
18- import club .moddedminecraft .polychat .bukkitclient .threads .ActivePlayerThread ;
19- import club .moddedminecraft .polychat .bukkitclient .threads .ReattachThread ;
2012import org .bukkit .scheduler .BukkitScheduler ;
2113
22- public final class BukkitClient extends JavaPlugin implements Listener {
23-
24- public static boolean shutdownClean = false ;
14+ import java .io .File ;
15+ import java .io .FileInputStream ;
16+ import java .io .FileOutputStream ;
17+ import java .io .IOException ;
18+ import java .net .Socket ;
19+ import java .util .ArrayList ;
20+ import java .util .HashMap ;
21+ import java .util .Properties ;
22+ import java .util .regex .Matcher ;
23+ import java .util .regex .Pattern ;
24+
25+ public final class BukkitClient extends JavaPlugin implements Listener {
26+
27+ private static final HashMap <Integer , ChatColor > colorHashMap = new HashMap <Integer , ChatColor >() {{
28+ put (0 , ChatColor .getByChar ('0' ));
29+ put (1 , ChatColor .getByChar ('1' ));
30+ put (2 , ChatColor .getByChar ('2' ));
31+ put (3 , ChatColor .getByChar ('3' ));
32+ put (4 , ChatColor .getByChar ('4' ));
33+ put (5 , ChatColor .getByChar ('5' ));
34+ put (6 , ChatColor .getByChar ('6' ));
35+ put (7 , ChatColor .getByChar ('7' ));
36+ put (8 , ChatColor .getByChar ('8' ));
37+ put (9 , ChatColor .getByChar ('9' ));
38+ put (10 , ChatColor .getByChar ('a' ));
39+ put (11 , ChatColor .getByChar ('b' ));
40+ put (12 , ChatColor .getByChar ('c' ));
41+ put (13 , ChatColor .getByChar ('d' ));
42+ put (14 , ChatColor .getByChar ('e' ));
43+ put (15 , ChatColor .getByChar ('f' ));
44+ }};
45+ public static boolean shutdownClean = false ;
2546 public static MessageBus messageBus = null ;
2647 public static Properties properties ;
48+ public static File propertiesFolder ;
2749 public static ReattachThread reattachThread ;
2850 public static ActivePlayerThread playerThread ;
2951 public static String idJson = null ;
3052 public static String idJsonNoColor = null ;
3153 public static boolean reattachKill = false ;
32- public static String serverIdText = null ;
33- public static ArrayList <String > commands = new ArrayList <>();
54+ public static ArrayList <BukkitCommandSender > commands = new ArrayList <>();
3455
3556 public static void handleClientConnection () {
3657 try {
37- messageBus = new MessageBus (new Socket (properties .getProperty ("address" ), Integer .parseInt (properties .getProperty ("port" ))), new ReceiverCallback (){
58+ messageBus = new MessageBus (new Socket (properties .getProperty ("address" ), Integer .parseInt (properties .getProperty ("port" ))), new ReceiverCallback () {
3859 @ Override
39- public void receive (AbstractMessage abstractMessage ){
60+ public void receive (AbstractMessage abstractMessage ) {
4061 EventListener .handleMessage (abstractMessage );
4162 }
4263 });
@@ -50,18 +71,44 @@ public void receive(AbstractMessage abstractMessage){
5071 public static void sendMessage (AbstractMessage message ) {
5172 try {
5273 messageBus .sendMessage (message );
53- } catch (NullPointerException ignored ) {}
74+ } catch (NullPointerException ignored ) {
75+ }
5476 }
5577
5678 public static void sendGameMessage (String message ) {
5779 Bukkit .broadcastMessage (message );
5880 }
5981
60- public static void runCommand (String command ) { //Command Firer
61- Bukkit .dispatchCommand (Bukkit .getConsoleSender (), command );
82+ public static int calculateParameters (String command ) {
83+ Pattern pattern = Pattern .compile ("(\\ $\\ d+)" );
84+ Matcher matcher = pattern .matcher (command );
85+ return matcher .groupCount ();
6286 }
63- public static void addCommand (String command ) { //Holding place for commands
64- commands .add (command );
87+
88+ public static ChatColor getColor (int color ) {
89+ return colorHashMap .getOrDefault (color , ChatColor .getByChar ("0" ));
90+ }
91+
92+
93+ public static void queueCommand (BukkitCommandSender sender ) { //Holding place for commands
94+ commands .add (sender );
95+ }
96+
97+ public static ArrayList <String > getOnlinePlayersNames () { //Might have to fix return type
98+ ArrayList <String > playerList = new ArrayList <>();
99+
100+ for (Player player : Bukkit .getServer ().getOnlinePlayers ()) {
101+ playerList .add (player .getName ());
102+ }
103+ return playerList ;
104+ }
105+
106+ public static int getMaxPlayers () {
107+ return Bukkit .getMaxPlayers ();
108+ }
109+
110+ public Server getBukkitServer () {
111+ return getServer ();
65112 }
66113
67114 @ Override
@@ -75,11 +122,11 @@ public void onEnable() {
75122
76123 handleClientConnection ();
77124
78- new EventListener (this );
125+ new EventListener (this );
79126
80- Runtime .getRuntime ().addShutdownHook (new Thread (new Runnable (){
127+ Runtime .getRuntime ().addShutdownHook (new Thread (new Runnable () {
81128 @ Override
82- public void run (){
129+ public void run () {
83130 shutdownHook ();
84131 }
85132 }));
@@ -100,26 +147,23 @@ public void run(){
100147 scheduler .scheduleSyncRepeatingTask (this , new Runnable () { //Repeating task for listening for commands so they are ran on the right thread
101148 @ Override
102149 public void run () {
103- if (commands .size () != 0 ) {
104- int i = 0 ;
105- while (i <commands .size ()){
106- runCommand (commands .get (i ));
107- i ++;
150+ for (BukkitCommandSender sender : commands ) {
151+ String command = sender .getCommand ();
152+ if (command != null ) {
153+ getServer ().dispatchCommand (sender , command );
108154 }
109- commands .clear ();
110155 }
156+ commands .clear ();
111157 }
112158 }, 0L , 20L );
113- if (!reattachKill ) { //only start it on a fresh start, not on a reload
159+ if (!reattachKill ) { //only start it on a fresh start, not on a reload
114160 reattachThread .start ();//actually start the thread at the end so the main thread is running already
115- }else {
116-
117161 }
118162 }
119-
163+
120164 @ Override
121165 public void onDisable () {
122- shutdownClean = true ;
166+ shutdownClean = true ;
123167 reattachKill = true ;
124168
125169 ServerStatusMessage offlineMsg = new ServerStatusMessage (properties .getProperty ("server_id" ), idJson , (short ) 2 );
@@ -137,7 +181,6 @@ public void onDisable() {
137181
138182 }
139183
140-
141184 public void shutdownHook () {
142185 //Sends either crashed or offline depending on if shutdown happened cleanly
143186 if (!shutdownClean ) {
@@ -152,43 +195,8 @@ public void shutdownHook() {
152195 messageBus .stop ();
153196 }
154197
155- public static ArrayList <String > getOnlinePlayersNames () { //Might have to fix return type
156- ArrayList <String > playerList = new ArrayList <> ();
157-
158- for (Player player : Bukkit .getServer ().getOnlinePlayers ()) {
159- playerList .add (player .getName ());
160- }
161- return playerList ;
162- }
163-
164- public static int getMaxPlayers () {
165- return Bukkit .getMaxPlayers ();
166- }
167-
168-
169- public static ChatColor colorSwitch (int colorInt ){
170- switch (colorInt ){
171- case 0 : return ChatColor .getByChar ('0' );
172- case 1 : return ChatColor .getByChar ('1' );
173- case 2 : return ChatColor .getByChar ('2' );
174- case 3 : return ChatColor .getByChar ('3' );
175- case 4 : return ChatColor .getByChar ('4' );
176- case 5 : return ChatColor .getByChar ('5' );
177- case 6 : return ChatColor .getByChar ('6' );
178- case 7 : return ChatColor .getByChar ('7' );
179- case 8 : return ChatColor .getByChar ('8' );
180- case 9 : return ChatColor .getByChar ('9' );
181- case 10 : return ChatColor .getByChar ('a' );
182- case 11 : return ChatColor .getByChar ('b' );
183- case 12 : return ChatColor .getByChar ('c' );
184- case 13 : return ChatColor .getByChar ('d' );
185- case 14 : return ChatColor .getByChar ('e' );
186- case 15 : return ChatColor .getByChar ('f' );
187- default : return ChatColor .getByChar ("0" );
188- }
189- }
190-
191198 public void handleConfiguration (File modConfigDir ) {
199+ BukkitClient .propertiesFolder = modConfigDir ;
192200 BukkitClient .properties = new Properties ();
193201 File config = new File (modConfigDir , "polychat.properties" );
194202
@@ -215,6 +223,7 @@ public void handleConfiguration(File modConfigDir) {
215223 }
216224 }
217225 }
226+
218227 public void handlePrefix () {
219228 String idText = properties .getProperty ("server_id" );
220229 ChatColor color ;
@@ -223,14 +232,12 @@ public void handlePrefix() {
223232 if ((code < 0 ) || (code > 15 )) {
224233 color = ChatColor .getByChar ('f' );
225234 } else {
226- color = colorSwitch (code );
235+ color = getColor (code );
227236 }
228237 idJsonNoColor = idText ;
229238 idText = color + "" + idText ;
230239 idJson = idText ;
231240 }
232241 }
233242
234-
235-
236243}
0 commit comments