Skip to content

Commit f73d1cd

Browse files
author
YoFuzzy3
committed
New things...
Implemented queue saving for superb handling if the BungeeCord server or any MC server goes offline. Fixed some other stuff. Added Metrics.
1 parent 966f19e commit f73d1cd

7 files changed

Lines changed: 926 additions & 22 deletions

File tree

CommandSyncClient/src/com/fuzzoland/CommandSyncClient/CSC.java

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public class CSC extends JavaPlugin{
1818

1919
public ClientThread client;
2020
public List<String> oq = Collections.synchronizedList(new ArrayList<String>());
21+
public Integer qc = 0;
2122
public String spacer;
2223

2324
public void onEnable(){
@@ -33,9 +34,14 @@ public void onEnable(){
3334
e.printStackTrace();
3435
}
3536
spacer = data[3];
37+
loadData();
3638
getCommand("Sync").setExecutor(new CommandSynchronize(this));
3739
}
3840

41+
public void onDisable(){
42+
saveData();
43+
}
44+
3945
private String[] loadConfig(){
4046
String[] data = new String[5];
4147
try{
@@ -46,7 +52,7 @@ private String[] loadConfig(){
4652
PrintStream ps = new PrintStream(os);
4753
ps.println("ip=localhost");
4854
ps.println("port=9190");
49-
ps.println("heartbeat=5000");
55+
ps.println("heartbeat=1000");
5056
ps.println("spacer=@#@");
5157
ps.println("name=UNSET");
5258
ps.close();
@@ -70,4 +76,46 @@ private String[] loadConfig(){
7076
}
7177
return data;
7278
}
79+
80+
private void saveData(){
81+
try{
82+
OutputStream os = new FileOutputStream(new File(getDataFolder(), "data.txt"));
83+
PrintStream ps = new PrintStream(os);
84+
for(String s : oq){
85+
ps.println("q:" + s);
86+
}
87+
ps.println("c:" + String.valueOf(qc));
88+
ps.close();
89+
System.out.println("[CommandSync] All data saved.");
90+
}catch(IOException e){
91+
e.printStackTrace();
92+
}
93+
}
94+
95+
private void loadData(){
96+
try{
97+
File file = new File(getDataFolder(), "data.txt");
98+
if(file.exists()){
99+
BufferedReader br = new BufferedReader(new FileReader(file));
100+
try{
101+
String l = br.readLine();
102+
while(l != null){
103+
if(l.startsWith("q:")){
104+
oq.add(new String(l.substring(2)));
105+
}else if(l.startsWith("c:")){
106+
qc = Integer.parseInt(new String(l.substring(2)));
107+
}
108+
l = br.readLine();
109+
}
110+
System.out.println("[CommandSync] All data loaded.");
111+
}finally{
112+
br.close();
113+
}
114+
}else{
115+
System.out.println("[CommandSync] A data file was not found. If this is your first start-up with the plugin, this is normal.");
116+
}
117+
}catch(IOException e){
118+
e.printStackTrace();
119+
}
120+
}
73121
}

CommandSyncClient/src/com/fuzzoland/CommandSyncClient/ClientThread.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ public class ClientThread extends Thread{
1919
private PrintWriter out;
2020
private BufferedReader in;
2121
private Integer heartbeat = 0;
22-
private Integer qc = 0;
2322
private String name;
2423

2524
public ClientThread(CSC plugin, InetAddress ip, Integer port, Integer heartbeat, String name){
@@ -41,13 +40,15 @@ public void run(){
4140
}else{
4241
try{
4342
Integer size = plugin.oq.size();
44-
if(size > qc){
45-
for(int i = qc; i < size; i++){
43+
Integer count = plugin.qc;
44+
if(size > count){
45+
for(int i = count; i < size; i++){
46+
count++;
4647
String output = plugin.oq.get(i);
4748
out.println(output);
4849
System.out.println("[CommandSync] [" + socket.getInetAddress().getHostName() + ":" + socket.getPort() + "] " + "Sent output - " + output);
49-
qc++;
5050
}
51+
plugin.qc = count;
5152
}
5253
while(in.ready()){
5354
String input = in.readLine();
@@ -56,9 +57,6 @@ public void run(){
5657
String[] data = input.split(plugin.spacer);
5758
if(data[0].equals("console")){
5859
String command = data[2].replaceAll("\\+", " ");
59-
if(data[1].equals("single") && !data[3].equals(name)){
60-
return;
61-
}
6260
Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), command);
6361
System.out.println("[CommandSync] Ran command /" + command + ".");
6462
}
@@ -85,6 +83,7 @@ private void connect(){
8583
this.out = new PrintWriter(socket.getOutputStream(), true);
8684
this.in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
8785
this.connected = true;
86+
out.println(name);
8887
System.out.println("[CommandSync] Connected to " + ip.getHostName() + ":" + String.valueOf(port) + ".");
8988
}catch(IOException e){
9089
System.out.println("[CommandSync] Could not connect to the server.");

CommandSyncClient/src/plugin.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: CommandSyncClient
1+
name: CommandSync
22
main: com.fuzzoland.CommandSyncClient.CSC
33
version: 2.0b
44
author: YoFuzzy3

CommandSyncServer/src/com/fuzzoland/CommandSyncServer/CSS.java

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,21 @@
1111
import java.net.ServerSocket;
1212
import java.util.ArrayList;
1313
import java.util.Collections;
14+
import java.util.HashMap;
1415
import java.util.List;
16+
import java.util.Map;
17+
import java.util.Map.Entry;
18+
19+
import com.fuzzoland.CommandSyncServer.Metrics.Graph;
1520

1621
import net.md_5.bungee.api.plugin.Plugin;
1722

1823
public class CSS extends Plugin{
1924

2025
public ServerSocket server;
2126
public List<String> oq = Collections.synchronizedList(new ArrayList<String>());
27+
public List<String> pq = Collections.synchronizedList(new ArrayList<String>());
28+
public Map<String, Integer> qc = new HashMap<String, Integer>();
2229
public String spacer;
2330

2431
public void onEnable(){
@@ -31,6 +38,29 @@ public void onEnable(){
3138
e.printStackTrace();
3239
}
3340
spacer = data[3];
41+
loadData();
42+
try{
43+
Metrics metrics = new Metrics(this);
44+
Graph graph1 = metrics.createGraph("Total queries sent");
45+
graph1.addPlotter(new Metrics.Plotter(){
46+
public int getValue(){
47+
return oq.size();
48+
}
49+
});
50+
Graph graph2 = metrics.createGraph("Total servers linked");
51+
graph2.addPlotter(new Metrics.Plotter(){
52+
public int getValue(){
53+
return qc.keySet().size();
54+
}
55+
});
56+
metrics.start();
57+
}catch(IOException e){
58+
e.printStackTrace();
59+
}
60+
}
61+
62+
public void onDisable(){
63+
saveData();
3464
}
3565

3666
private String[] loadConfig(){
@@ -43,7 +73,7 @@ private String[] loadConfig(){
4373
PrintStream ps = new PrintStream(os);
4474
ps.println("ip=localhost");
4575
ps.println("port=9190");
46-
ps.println("heartbeat=5000");
76+
ps.println("heartbeat=1000");
4777
ps.println("spacer=@#@");
4878
ps.close();
4979
System.out.println("[CommandSync] New configuration file created.");
@@ -66,4 +96,49 @@ private String[] loadConfig(){
6696
}
6797
return data;
6898
}
99+
100+
private void saveData(){
101+
try{
102+
OutputStream os = new FileOutputStream(new File(getDataFolder(), "data.txt"));
103+
PrintStream ps = new PrintStream(os);
104+
for(String s : oq){
105+
ps.println("q:" + s);
106+
}
107+
for(Entry<String, Integer> e : qc.entrySet()){
108+
ps.println("c:" + e.getKey() + spacer + String.valueOf(e.getValue()));
109+
}
110+
ps.close();
111+
System.out.println("[CommandSync] All data saved.");
112+
}catch(IOException e){
113+
e.printStackTrace();
114+
}
115+
}
116+
117+
private void loadData(){
118+
try{
119+
File file = new File(getDataFolder(), "data.txt");
120+
if(file.exists()){
121+
BufferedReader br = new BufferedReader(new FileReader(file));
122+
try{
123+
String l = br.readLine();
124+
while(l != null){
125+
if(l.startsWith("q:")){
126+
oq.add(new String(l.substring(2)));
127+
}else if(l.startsWith("c:")){
128+
String[] parts = new String(l.substring(2)).split(spacer);
129+
qc.put(parts[0], Integer.parseInt(parts[1]));
130+
}
131+
l = br.readLine();
132+
}
133+
System.out.println("[CommandSync] All data loaded.");
134+
}finally{
135+
br.close();
136+
}
137+
}else{
138+
System.out.println("[CommandSync] A data file was not found. If this is your first start-up with the plugin, this is normal.");
139+
}
140+
}catch(IOException e){
141+
e.printStackTrace();
142+
}
143+
}
69144
}

CommandSyncServer/src/com/fuzzoland/CommandSyncServer/ClientHandler.java

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,33 @@ public class ClientHandler extends Thread{
1616
private PrintWriter out;
1717
private BufferedReader in;
1818
private Integer heartbeat = 0;
19-
private Integer qc = 0;
19+
private String name;
2020

2121
public ClientHandler(CSS plugin, Socket socket, Integer heartbeat) throws IOException{
2222
this.plugin = plugin;
2323
this.socket = socket;
2424
this.out = new PrintWriter(socket.getOutputStream(), true);
2525
this.in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
2626
this.heartbeat = heartbeat;
27-
System.out.println("[CommandSync] Received new connection from " + socket.getInetAddress().getHostName() + ":" + socket.getPort() + ".");
27+
this.name = in.readLine();
28+
if(!plugin.qc.containsKey(name)){
29+
plugin.qc.put(name, 0);
30+
}
31+
System.out.println("[CommandSync] Received new connection from " + socket.getInetAddress().getHostName() + ":" + socket.getPort() + " under name " + name + ".");
2832
}
2933

3034
public void run(){
3135
while(true){
3236
try{
3337
out.println("heartbeat");
3438
if(out.checkError()){
35-
System.out.println("[CommandSync] Connection from " + socket.getInetAddress().getHostName() + ":" + socket.getPort() + " has disconnected.");
39+
System.out.println("[CommandSync] Connection from " + socket.getInetAddress().getHostName() + ":" + socket.getPort() + " under name " + name + " has disconnected.");
3640
return;
3741
}
3842
while(in.ready()){
3943
String input = in.readLine();
4044
if(!input.equals("heartbeat")){
41-
System.out.println("[CommandSync] [" + socket.getInetAddress().getHostName() + ":" + socket.getPort() + "] " + "Received input - " + input);
45+
System.out.println("[CommandSync] [" + socket.getInetAddress().getHostName() + ":" + socket.getPort() + "] [" + name + "] Received input - " + input);
4246
String[] data = input.split(plugin.spacer);
4347
if(data[0].equals("player")){
4448
String command = "/" + data[2].replaceAll("\\+", " ");
@@ -53,25 +57,35 @@ public void run(){
5357
for(ProxiedPlayer player : ProxyServer.getInstance().getPlayers()){
5458
player.chat(command);
5559
}
56-
System.out.println("[CommandSync] Ran command " + command + " for all players.");
60+
System.out.println("[CommandSync] Ran command " + command + " for all online players.");
5761
}
5862
}else{
5963
if(data[1].equals("bungee")){
60-
// Execute command as BungeeCord console
64+
System.out.println("[CommandSync] A request to execute a BungeeCord command was received. Unfortunately this feature is not possible yet.");
6165
}else{
6266
plugin.oq.add(input);
6367
}
6468
}
6569
}
6670
}
6771
Integer size = plugin.oq.size();
68-
if(size > qc){
69-
for(int i = qc; i < size; i++){
72+
Integer count = plugin.qc.get(name);
73+
if(size > count){
74+
for(int i = count; i < size; i++){
75+
count++;
7076
String output = plugin.oq.get(i);
71-
out.println(output);
72-
System.out.println("[CommandSync] [" + socket.getInetAddress().getHostName() + ":" + socket.getPort() + "] " + "Sent output - " + output);
73-
qc++;
77+
String[] data = output.split(plugin.spacer);
78+
if(data[1].equals("single")){
79+
if(data[3].equals(name)){
80+
out.println(output);
81+
System.out.println("[CommandSync] [" + socket.getInetAddress().getHostName() + ":" + socket.getPort() + "] [" + name + "] Sent output - " + output);
82+
}
83+
}else{
84+
out.println(output);
85+
System.out.println("[CommandSync] [" + socket.getInetAddress().getHostName() + ":" + socket.getPort() + "] [" + name + "] Sent output - " + output);
86+
}
7487
}
88+
plugin.qc.put(name, count);
7589
}
7690
sleep(heartbeat);
7791
}catch(IOException e){

0 commit comments

Comments
 (0)