Skip to content

Commit 966f19e

Browse files
author
YoFuzzy3
committed
Implement command and fix some bugs
Still a few bugs lurking, will fix tomorrow.
1 parent 0a884f3 commit 966f19e

7 files changed

Lines changed: 115 additions & 18 deletions

File tree

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,26 @@ public class CSC extends JavaPlugin{
1818

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

2223
public void onEnable(){
2324
String[] data = loadConfig();
25+
if(data[4].equals("UNSET")){
26+
System.out.println("[CommandSync] !!! YOU MUST SET THE SERVER'S NAME IN THE CONFIG BEFORE THE PLUGIN WILL WORK FOR THIS SERVER !!!");
27+
return;
28+
}
2429
try{
25-
client = new ClientThread(this, InetAddress.getByName(data[0]), Integer.parseInt(data[1]), Integer.parseInt(data[2]));
30+
client = new ClientThread(this, InetAddress.getByName(data[0]), Integer.parseInt(data[1]), Integer.parseInt(data[2]), data[4]);
2631
client.start();
2732
}catch(Exception e){
2833
e.printStackTrace();
2934
}
35+
spacer = data[3];
3036
getCommand("Sync").setExecutor(new CommandSynchronize(this));
3137
}
3238

3339
private String[] loadConfig(){
34-
String[] data = new String[2];
40+
String[] data = new String[5];
3541
try{
3642
File file = getDataFolder();
3743
if(!file.exists()){
@@ -41,7 +47,8 @@ private String[] loadConfig(){
4147
ps.println("ip=localhost");
4248
ps.println("port=9190");
4349
ps.println("heartbeat=5000");
44-
ps.println("name=null");
50+
ps.println("spacer=@#@");
51+
ps.println("name=UNSET");
4552
ps.close();
4653
System.out.println("[CommandSync] New configuration file created.");
4754
}

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import java.net.InetAddress;
88
import java.net.Socket;
99

10+
import org.bukkit.Bukkit;
11+
1012
public class ClientThread extends Thread{
1113

1214
private CSC plugin;
@@ -18,12 +20,14 @@ public class ClientThread extends Thread{
1820
private BufferedReader in;
1921
private Integer heartbeat = 0;
2022
private Integer qc = 0;
23+
private String name;
2124

22-
public ClientThread(CSC plugin, InetAddress ip, Integer port, Integer heartbeat){
25+
public ClientThread(CSC plugin, InetAddress ip, Integer port, Integer heartbeat, String name){
2326
this.plugin = plugin;
2427
this.ip = ip;
2528
this.port = port;
2629
this.heartbeat = heartbeat;
30+
this.name = name;
2731
connect();
2832
}
2933

@@ -45,11 +49,19 @@ public void run(){
4549
qc++;
4650
}
4751
}
48-
if(in.ready()){
52+
while(in.ready()){
4953
String input = in.readLine();
5054
if(!input.equals("heartbeat")){
5155
System.out.println("[CommandSync] [" + socket.getInetAddress().getHostName() + ":" + socket.getPort() + "] " + "Received input - " + input);
52-
// Process input
56+
String[] data = input.split(plugin.spacer);
57+
if(data[0].equals("console")){
58+
String command = data[2].replaceAll("\\+", " ");
59+
if(data[1].equals("single") && !data[3].equals(name)){
60+
return;
61+
}
62+
Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), command);
63+
System.out.println("[CommandSync] Ran command /" + command + ".");
64+
}
5365
}
5466
}
5567
}catch(IOException e){

CommandSyncClient/src/com/fuzzoland/CommandSyncClient/CommandSynchronize.java

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.fuzzoland.CommandSyncClient;
22

3-
import java.util.Random;
3+
import org.apache.commons.lang.WordUtils;
44

55
import org.bukkit.ChatColor;
66
import org.bukkit.command.Command;
@@ -16,11 +16,61 @@ public CommandSynchronize(CSC plugin){
1616
}
1717

1818
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
19-
if(sender.hasPermission("commandsync.synchronize")){
20-
plugin.oq.add("test" + String.valueOf(new Random().nextInt()));
19+
if(sender.hasPermission("sync.use")){
20+
if(args.length >= 0){
21+
if(args.length <= 2){
22+
sender.sendMessage(ChatColor.BLUE + "CommandSync by YoFuzzy3");
23+
if(args.length >= 1){
24+
if(args[0].equalsIgnoreCase("console")){
25+
sender.sendMessage(ChatColor.GREEN + "/sync console single <command+args> <server>");
26+
sender.sendMessage(ChatColor.GREEN + "/sync console all <command+args>");
27+
sender.sendMessage(ChatColor.GREEN + "/sync console bungee <command+args>");
28+
}else if(args[0].equalsIgnoreCase("player")){
29+
sender.sendMessage(ChatColor.GREEN + "/sync player single <command+args> <player>");
30+
sender.sendMessage(ChatColor.GREEN + "/sync player all <command+args>");
31+
}else{
32+
sender.sendMessage(ChatColor.RED + "Type /sync for help.");
33+
}
34+
}else{
35+
sender.sendMessage(ChatColor.GREEN + "/sync console");
36+
sender.sendMessage(ChatColor.GREEN + "/sync player");
37+
sender.sendMessage(ChatColor.BLUE + "Type the command for more info.");
38+
}
39+
sender.sendMessage(ChatColor.BLUE + "Visit www.spigotmc.org/resources/commandsync.115 for help.");
40+
}else if(args.length >= 3){
41+
if(args[0].equalsIgnoreCase("console")){
42+
if(args[1].equalsIgnoreCase("single") || args[1].equalsIgnoreCase("all") || args[1].equalsIgnoreCase("bungee")){
43+
makeData(args, sender);
44+
}else{
45+
sender.sendMessage(ChatColor.RED + "Type /sync for help!");
46+
}
47+
}else if(args[0].equalsIgnoreCase("player")){
48+
if(args[1].equalsIgnoreCase("single") || args[1].equalsIgnoreCase("all")){
49+
makeData(args, sender);
50+
}else{
51+
sender.sendMessage(ChatColor.RED + "Type /sync for help!");
52+
}
53+
}else{
54+
sender.sendMessage(ChatColor.RED + "Type /sync for help!");
55+
}
56+
}
57+
}
2158
}else{
2259
sender.sendMessage(ChatColor.RED + "You do not have permission to use that command.");
2360
}
2461
return true;
2562
}
63+
64+
private void makeData(String[] args, CommandSender sender){
65+
String data = args[0].toLowerCase() + plugin.spacer + args[1].toLowerCase() + plugin.spacer + args[2];
66+
String message = ChatColor.GREEN + "Syncing command /" + args[2].replaceAll("\\+", " ") + " to " + args[0];
67+
if(args.length == 4){
68+
data = data + plugin.spacer + args[3];
69+
message = message + " [" + args[3] + "]...";
70+
}else{
71+
message = message + " [" + WordUtils.capitalizeFully(args[1]) + "]...";
72+
}
73+
plugin.oq.add(data);
74+
sender.sendMessage(message);
75+
}
2676
}

CommandSyncClient/src/plugin.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
name: CommandSyncClient
22
main: com.fuzzoland.CommandSyncClient.CSC
3-
version: 2.0
3+
version: 2.0b
44
author: YoFuzzy3
55

66
commands:
7-
Sync:
8-
description: Sync commands and more!
7+
sync:
8+
description: Synchronize commands across servers.
99

1010
permissions:
11-
Sync.use:
12-
description: Access to /Sync!
11+
sync.use:
12+
description: Access to /sync.
1313
default: op

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public class CSS extends Plugin{
1919

2020
public ServerSocket server;
2121
public List<String> oq = Collections.synchronizedList(new ArrayList<String>());
22+
public String spacer;
2223

2324
public void onEnable(){
2425
String[] data = loadConfig();
@@ -29,10 +30,11 @@ public void onEnable(){
2930
}catch(Exception e){
3031
e.printStackTrace();
3132
}
33+
spacer = data[3];
3234
}
3335

3436
private String[] loadConfig(){
35-
String[] data = new String[2];
37+
String[] data = new String[4];
3638
try{
3739
File file = getDataFolder();
3840
if(!file.exists()){
@@ -42,6 +44,7 @@ private String[] loadConfig(){
4244
ps.println("ip=localhost");
4345
ps.println("port=9190");
4446
ps.println("heartbeat=5000");
47+
ps.println("spacer=@#@");
4548
ps.close();
4649
System.out.println("[CommandSync] New configuration file created.");
4750
}

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

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
import java.io.PrintWriter;
77
import java.net.Socket;
88

9+
import net.md_5.bungee.api.ProxyServer;
10+
import net.md_5.bungee.api.connection.ProxiedPlayer;
11+
912
public class ClientHandler extends Thread{
1013

1114
private CSS plugin;
@@ -32,11 +35,33 @@ public void run(){
3235
System.out.println("[CommandSync] Connection from " + socket.getInetAddress().getHostName() + ":" + socket.getPort() + " has disconnected.");
3336
return;
3437
}
35-
if(in.ready()){
38+
while(in.ready()){
3639
String input = in.readLine();
3740
if(!input.equals("heartbeat")){
3841
System.out.println("[CommandSync] [" + socket.getInetAddress().getHostName() + ":" + socket.getPort() + "] " + "Received input - " + input);
39-
plugin.oq.add(input);
42+
String[] data = input.split(plugin.spacer);
43+
if(data[0].equals("player")){
44+
String command = "/" + data[2].replaceAll("\\+", " ");
45+
if(data[1].equals("single")){
46+
String name = data[3];
47+
ProxiedPlayer player = ProxyServer.getInstance().getPlayer(name);
48+
if(player != null){
49+
player.chat(command);
50+
}
51+
System.out.println("[CommandSync] Ran command " + command + " for player " + name + ".");
52+
}else if(data[1].equals("all")){
53+
for(ProxiedPlayer player : ProxyServer.getInstance().getPlayers()){
54+
player.chat(command);
55+
}
56+
System.out.println("[CommandSync] Ran command " + command + " for all players.");
57+
}
58+
}else{
59+
if(data[1].equals("bungee")){
60+
// Execute command as BungeeCord console
61+
}else{
62+
plugin.oq.add(input);
63+
}
64+
}
4065
}
4166
}
4267
Integer size = plugin.oq.size();

CommandSyncServer/src/plugin.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
name: CommandSyncServer
22
main: com.fuzzoland.CommandSyncServer.CSS
3-
version: 2.0
3+
version: 2.0b
44
author: YoFuzzy3

0 commit comments

Comments
 (0)