Skip to content

Commit 7b9c29b

Browse files
author
YoFuzzy3
committed
Fix some stuff
Fixed config loader, added debug log, changed some code style
1 parent 708d86b commit 7b9c29b

10 files changed

Lines changed: 318 additions & 219 deletions

File tree

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

Lines changed: 56 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -14,103 +14,111 @@
1414

1515
import org.bukkit.plugin.java.JavaPlugin;
1616

17-
public class CSC extends JavaPlugin{
17+
public class CSC extends JavaPlugin {
1818

1919
public ClientThread client;
2020
public List<String> oq = Collections.synchronizedList(new ArrayList<String>());
2121
public Integer qc = 0;
2222
public String spacer = "@#@";
2323
public String pass;
24+
public Debugger debugger;
2425

25-
public void onEnable(){
26+
public void onEnable() {
2627
String[] data = loadConfig();
27-
if(data[3].equals("UNSET") || data[4].equals("UNSET")){
28-
System.out.println("[CommandSync] !!! THE CONFIG FILE CONTAINS UNSET VALUES - YOU MUST FIX THEM BEFORE THE PLUGIN WILL WORK !!! ");
28+
if(data[3].equals("UNSET") || data[4].equals("UNSET")) {
29+
debugger.debug("THE CONFIG FILE CONTAINS UNSET VALUES - YOU MUST FIX THEM BEFORE THE PLUGIN WILL WORK !!! ");
2930
return;
3031
}
31-
try{
32+
try {
3233
client = new ClientThread(this, InetAddress.getByName(data[0]), Integer.parseInt(data[1]), Integer.parseInt(data[2]), data[3]);
3334
client.start();
34-
}catch(Exception e){
35+
} catch(Exception e) {
3536
e.printStackTrace();
3637
}
3738
pass = data[4];
3839
loadData();
3940
getCommand("Sync").setExecutor(new CommandSynchronize(this));
4041
}
4142

42-
public void onDisable(){
43+
public void onDisable() {
4344
saveData();
45+
debugger.close();
4446
}
45-
46-
private String[] loadConfig(){
47-
String[] defaults = new String[]{
48-
"ip=localhost", "port=9190", "heartbeat=1000", "name=UNSET", "pass=UNSET"
47+
48+
private String[] loadConfig() {
49+
String[] defaults = new String[] {
50+
"ip=localhost", "port=9190", "heartbeat=1000", "name=UNSET", "pass=UNSET", "debug=false"
4951
};
5052
String[] data = new String[defaults.length];
51-
try{
52-
File file = new File(getDataFolder(), "config.txt");
53-
if(!file.exists()){
54-
file.createNewFile();
55-
}
56-
PrintStream ps = new PrintStream(new FileOutputStream(file));
57-
BufferedReader br = new BufferedReader(new FileReader(file));
58-
for(int i = 0; i < defaults.length; i++){
59-
String l = br.readLine();
60-
if(l == null){
61-
ps.println(defaults[i]);
62-
data[i] = defaults[i].split("=")[1];
63-
}else{
64-
data[i] = l.split("=")[1];
65-
}
66-
}
67-
ps.close();
68-
br.close();
69-
System.out.println("[CommandSync] Configuration file loaded.");
70-
}catch(IOException e){
71-
e.printStackTrace();
72-
}
73-
return data;
53+
try {
54+
File file = new File(getDataFolder(), "config.txt");
55+
if(!file.exists()) {
56+
file.createNewFile();
57+
}
58+
BufferedReader br = new BufferedReader(new FileReader(file));
59+
for(int i = 0; i < defaults.length; i++) {
60+
String l = br.readLine();
61+
if(l == null || l.isEmpty()) {
62+
data[i] = defaults[i].split("=")[1];
63+
} else {
64+
data[i] = l.split("=")[1];
65+
defaults[i] = l;
66+
}
67+
}
68+
br.close();
69+
file.delete();
70+
file.createNewFile();
71+
PrintStream ps = new PrintStream(new FileOutputStream(file));
72+
for(int i = 0; i < defaults.length; i++) {
73+
ps.println(defaults[i]);
74+
}
75+
ps.close();
76+
debugger = new Debugger(this, Boolean.valueOf(data[5]));
77+
debugger.debug("Configuration file loaded.");
78+
} catch(IOException e) {
79+
e.printStackTrace();
80+
}
81+
return data;
7482
}
7583

76-
private void saveData(){
84+
private void saveData() {
7785
try{
7886
OutputStream os = new FileOutputStream(new File(getDataFolder(), "data.txt"));
7987
PrintStream ps = new PrintStream(os);
80-
for(String s : oq){
88+
for(String s : oq) {
8189
ps.println("oq:" + s);
8290
}
8391
ps.println("qc:" + String.valueOf(qc));
8492
ps.close();
85-
System.out.println("[CommandSync] All data saved.");
93+
debugger.debug("All data saved.");
8694
}catch(IOException e){
8795
e.printStackTrace();
8896
}
8997
}
9098

91-
private void loadData(){
99+
private void loadData() {
92100
try{
93101
File file = new File(getDataFolder(), "data.txt");
94-
if(file.exists()){
102+
if(file.exists()) {
95103
BufferedReader br = new BufferedReader(new FileReader(file));
96-
try{
104+
try {
97105
String l = br.readLine();
98-
while(l != null){
99-
if(l.startsWith("oq:")){
106+
while(l != null) {
107+
if(l.startsWith("oq:")) {
100108
oq.add(new String(l.substring(3)));
101-
}else if(l.startsWith("qc:")){
109+
} else if(l.startsWith("qc:")) {
102110
qc = Integer.parseInt(new String(l.substring(3)));
103111
}
104112
l = br.readLine();
105113
}
106-
System.out.println("[CommandSync] All data loaded.");
107-
}finally{
114+
debugger.debug("All data loaded.");
115+
} finally {
108116
br.close();
109117
}
110-
}else{
111-
System.out.println("[CommandSync] A data file was not found. If this is your first start-up with the plugin, this is normal.");
118+
} else {
119+
debugger.debug("A data file was not found. If this is your first start-up with the plugin, this is normal.");
112120
}
113-
}catch(IOException e){
121+
} catch(IOException e) {
114122
e.printStackTrace();
115123
}
116124
}

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

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
import org.bukkit.Bukkit;
1111

12-
public class ClientThread extends Thread{
12+
public class ClientThread extends Thread {
1313

1414
private CSC plugin;
1515
private InetAddress ip;
@@ -21,7 +21,7 @@ public class ClientThread extends Thread{
2121
private Integer heartbeat = 0;
2222
private String name;
2323

24-
public ClientThread(CSC plugin, InetAddress ip, Integer port, Integer heartbeat, String name){
24+
public ClientThread(CSC plugin, InetAddress ip, Integer port, Integer heartbeat, String name) {
2525
this.plugin = plugin;
2626
this.ip = ip;
2727
this.port = port;
@@ -30,79 +30,79 @@ public ClientThread(CSC plugin, InetAddress ip, Integer port, Integer heartbeat,
3030
connect(false);
3131
}
3232

33-
public void run(){
34-
while(true){
35-
if(connected){
33+
public void run() {
34+
while(true) {
35+
if(connected) {
3636
out.println("heartbeat");
37-
if(out.checkError()){
37+
if(out.checkError()) {
3838
connected = false;
39-
System.out.println("[CommandSync] Lost connection to the server.");
40-
}else{
41-
try{
39+
plugin.debugger.debug("Lost connection to the server.");
40+
} else {
41+
try {
4242
Integer size = plugin.oq.size();
4343
Integer count = plugin.qc;
44-
if(size > count){
45-
for(int i = count; i < size; i++){
44+
if(size > count) {
45+
for(int i = count; i < size; i++) {
4646
count++;
4747
String output = plugin.oq.get(i);
4848
out.println(output);
49-
System.out.println("[CommandSync] [" + socket.getInetAddress().getHostName() + ":" + socket.getPort() + "] " + "Sent output - " + output);
49+
plugin.debugger.debug("[" + socket.getInetAddress().getHostName() + ":" + socket.getPort() + "] " + "Sent output - " + output);
5050
}
5151
plugin.qc = count;
5252
}
53-
while(in.ready()){
53+
while(in.ready()) {
5454
String input = in.readLine();
55-
if(!input.equals("heartbeat")){
56-
System.out.println("[CommandSync] [" + socket.getInetAddress().getHostName() + ":" + socket.getPort() + "] " + "Received input - " + input);
55+
if(!input.equals("heartbeat")) {
56+
plugin.debugger.debug("[" + socket.getInetAddress().getHostName() + ":" + socket.getPort() + "] " + "Received input - " + input);
5757
String[] data = input.split(plugin.spacer);
58-
if(data[0].equals("console")){
58+
if(data[0].equals("console")) {
5959
String command = data[2].replaceAll("\\+", " ");
6060
Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), command);
61-
System.out.println("[CommandSync] Ran command /" + command + ".");
61+
plugin.debugger.debug("Ran command /" + command + ".");
6262
}
6363
}
6464
}
65-
}catch(IOException e){
65+
} catch(IOException e) {
6666
e.printStackTrace();
6767
}
6868
}
69-
}else{
69+
} else {
7070
connect(true);
7171
}
72-
try{
72+
try {
7373
sleep(heartbeat);
74-
}catch(InterruptedException e){
74+
} catch(InterruptedException e) {
7575
e.printStackTrace();
7676
}
7777
}
7878
}
7979

80-
private void connect(Boolean sleep){
81-
if(sleep){
82-
try{
80+
private void connect(Boolean sleep) {
81+
if(sleep) {
82+
try {
8383
sleep(10000);
84-
}catch(InterruptedException e){
84+
} catch(InterruptedException e) {
8585
e.printStackTrace();
8686
}
8787
}
88-
try{
88+
try {
8989
socket = new Socket(ip, port);
9090
out = new PrintWriter(socket.getOutputStream(), true);
9191
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
9292
out.println(name);
93-
if(in.readLine().equals("n")){
94-
System.out.println("[CommandSync] The name " + name + " is already connected.");
93+
if(in.readLine().equals("n")) {
94+
plugin.debugger.debug("The name " + name + " is already connected.");
9595
return;
9696
}
9797
out.println(plugin.pass);
98-
if(in.readLine().equals("n")){
99-
System.out.println("[CommandSync] The password is invalid.");
98+
if(in.readLine().equals("n")) {
99+
plugin.debugger.debug("The password is invalid.");
100100
return;
101101
}
102102
connected = true;
103-
System.out.println("[CommandSync] Connected to " + ip.getHostName() + ":" + String.valueOf(port) + " under name " + name + ".");
104-
}catch(IOException e){
105-
System.out.println("[CommandSync] Could not connect to the server.");
103+
plugin.debugger.debug("Connected to " + ip.getHostName() + ":" + String.valueOf(port) + " under name " + name + ".");
104+
} catch(IOException e) {
105+
plugin.debugger.debug("Could not connect to the server.");
106106
}
107107
}
108108
}

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

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

33
import org.apache.commons.lang.WordUtils;
4-
54
import org.bukkit.ChatColor;
65
import org.bukkit.command.Command;
76
import org.bukkit.command.CommandExecutor;
87
import org.bukkit.command.CommandSender;
98

10-
public class CommandSynchronize implements CommandExecutor{
9+
public class CommandSynchronize implements CommandExecutor {
1110

1211
private CSC plugin;
1312

14-
public CommandSynchronize(CSC plugin){
13+
public CommandSynchronize(CSC plugin) {
1514
this.plugin = plugin;
1615
}
1716

18-
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
19-
if(sender.hasPermission("sync.use")){
20-
if(args.length >= 0){
21-
if(args.length <= 2){
17+
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
18+
if(sender.hasPermission("sync.use")) {
19+
if(args.length >= 0) {
20+
if(args.length <= 2) {
2221
sender.sendMessage(ChatColor.BLUE + "CommandSync by YoFuzzy3");
23-
if(args.length >= 1){
22+
if(args.length >= 1) {
2423
if(args[0].equalsIgnoreCase("console")){
2524
sender.sendMessage(ChatColor.GREEN + "/sync console single <command+args> <server>");
2625
sender.sendMessage(ChatColor.GREEN + "/sync console all <command+args>");
2726
sender.sendMessage(ChatColor.GREEN + "/sync console bungee <command+args>");
28-
}else if(args[0].equalsIgnoreCase("player")){
27+
} else if(args[0].equalsIgnoreCase("player")) {
2928
sender.sendMessage(ChatColor.GREEN + "/sync player single <command+args> <player>");
3029
sender.sendMessage(ChatColor.GREEN + "/sync player all <command+args>");
31-
}else{
30+
} else {
3231
sender.sendMessage(ChatColor.RED + "Type /sync for help.");
3332
}
34-
}else{
33+
} else {
3534
sender.sendMessage(ChatColor.GREEN + "/sync console");
3635
sender.sendMessage(ChatColor.GREEN + "/sync player");
3736
sender.sendMessage(ChatColor.BLUE + "Type the command for more info.");
3837
}
3938
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")){
39+
} else if(args.length >= 3) {
40+
if(args[0].equalsIgnoreCase("console")) {
41+
if(args[1].equalsIgnoreCase("single") || args[1].equalsIgnoreCase("all") || args[1].equalsIgnoreCase("bungee")) {
4342
makeData(args, sender);
44-
}else{
43+
} else {
4544
sender.sendMessage(ChatColor.RED + "Type /sync for help!");
4645
}
47-
}else if(args[0].equalsIgnoreCase("player")){
48-
if(args[1].equalsIgnoreCase("single") || args[1].equalsIgnoreCase("all")){
46+
} else if(args[0].equalsIgnoreCase("player")) {
47+
if(args[1].equalsIgnoreCase("single") || args[1].equalsIgnoreCase("all")) {
4948
makeData(args, sender);
50-
}else{
49+
} else {
5150
sender.sendMessage(ChatColor.RED + "Type /sync for help!");
5251
}
53-
}else{
52+
} else {
5453
sender.sendMessage(ChatColor.RED + "Type /sync for help!");
5554
}
5655
}
5756
}
58-
}else{
57+
} else {
5958
sender.sendMessage(ChatColor.RED + "You do not have permission to use that command.");
6059
}
6160
return true;
6261
}
6362

64-
private void makeData(String[] args, CommandSender sender){
63+
private void makeData(String[] args, CommandSender sender) {
6564
String data = args[0].toLowerCase() + plugin.spacer + args[1].toLowerCase() + plugin.spacer + args[2];
6665
String message = ChatColor.GREEN + "Syncing command /" + args[2].replaceAll("\\+", " ") + " to " + args[0];
67-
if(args.length == 4){
66+
if(args.length == 4) {
6867
data = data + plugin.spacer + args[3];
6968
message = message + " [" + args[3] + "]...";
70-
}else{
69+
} else {
7170
message = message + " [" + WordUtils.capitalizeFully(args[1]) + "]...";
7271
}
7372
plugin.oq.add(data);

0 commit comments

Comments
 (0)