Skip to content

Commit

Permalink
New Feature fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Dabsunter committed Apr 4, 2016
1 parent a299b68 commit 816f456
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 55 deletions.
7 changes: 5 additions & 2 deletions plugin.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
name: OMGRemote
main: fr.dabsunter.omgremote.Main
version: 1.0.0
description: Lanceur de serveur paralelle
version: 1.1.0
description: Lanceur de serveur paralelle

commands:
nothing: {}
5 changes: 2 additions & 3 deletions src/fr/dabsunter/omgremote/CommandListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,13 @@ public void onServerCommand(ServerCommandEvent e) {
String label = command[0].toLowerCase();
if(executors.containsKey(label)) {
String[] args = trim(command);
boolean cancel = executors.get(label).onCommand(
executors.get(label).onCommand(
e.getSender(),
null,
label,
args
);
if(cancel)
e.setCommand(null);
e.setCommand("nothing");
}


Expand Down
46 changes: 37 additions & 9 deletions src/fr/dabsunter/omgremote/ProcessManager.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package fr.dabsunter.omgremote;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -38,20 +41,45 @@ public ProcessManager(String name, List<String> command) {
public ProcessManager(String name, List<String> command, File directory) {
this.name = name;

File logs = new File("logs/remote-" + name + ".log");
try {
File logs = new File("logs/remote-" + name + ".log");
if(!logs.exists())
logs.createNewFile();
process = new ProcessBuilder()
.command(command)
.directory(directory)
.redirectInput(logs)
.redirectError(logs)
.redirectErrorStream(true)
.start();
} catch (IOException e) {
} catch (IOException ex) {
System.out.println("Error while starting " + name + " process.");
e.printStackTrace();
ex.printStackTrace();
return;
}

new Thread() {
@SuppressWarnings("deprecation")
public void run() {
if(!process.isAlive())
stop();
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = "";
try {
while((line = reader.readLine()) != null) {
BufferedWriter writer = new BufferedWriter(new FileWriter("logs/remote-" + name + ".log"));
writer.write(line);
writer.newLine();
writer.close();
}
} finally {
reader.close();
}
} catch(IOException ex) {
ex.printStackTrace();
}
}
}.start();;

managers.add(this);
}

Expand All @@ -64,18 +92,18 @@ public void write(String command) {
} finally {
writer.flush();
}
} catch(IOException ioe) {
} catch(IOException ex) {
System.out.println("Error while writing \"" + command + "\" in OutputStream of " + name + " process.");
ioe.printStackTrace();
ex.printStackTrace();
}
}

public void stop() {
process.destroy();
try {
process.waitFor(10, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
if(process.isAlive())
process.destroyForcibly();
Expand Down
85 changes: 45 additions & 40 deletions src/fr/dabsunter/omgremote/commands/FileCommands.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fr.dabsunter.omgremote.commands;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.bukkit.command.Command;
Expand All @@ -16,47 +17,51 @@ public class FileCommands implements CommandExecutor {

@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
switch(label) {
case "cd":
current = new File(current, Tools.join(args));
sender.sendMessage("Moved to " + current.getAbsolutePath());
break;
case "ls":
String msg = "Files in " + current.getAbsolutePath();
for(File file : current.listFiles()) {
msg += "\n" + file.getName();
if(file.isDirectory())
msg += "/";
try {
switch(label) {
case "cd":
current = new File(current, Tools.join(args));
sender.sendMessage("Moved to " + current.getCanonicalPath());
break;
case "ls":
String msg = "Files in " + current.getCanonicalPath();
for(File file : current.listFiles()) {
msg += "\n" + file.getName();
if(file.isDirectory())
msg += "/";
}
sender.sendMessage(msg);
break;
case "cp":
List<String> copy = Tools.parseCommand(Tools.join(args));
if(copy.size() != 2) {
sender.sendMessage("Specify a source and a destination");
} else {
File from = new File(current, copy.get(0));
File to = new File(current, copy.get(1));
FileManager.copy(from, to);
sender.sendMessage("Copied " + from.getCanonicalPath() + " to " + to.getCanonicalPath());
}
break;
case "mv":
List<String> move = Tools.parseCommand(Tools.join(args));
if(move.size() != 2) {
sender.sendMessage("Specify a source and a destination");
} else {
File from = new File(current, move.get(0));
File to = new File(current, move.get(1));
FileManager.move(from, to);
sender.sendMessage("Copied " + from.getCanonicalPath() + " to " + to.getCanonicalPath());
}
break;
case "rm":
File delete = new File(current, Tools.join(args));
FileManager.delete(delete);
sender.sendMessage("Deleted " + delete.getCanonicalPath() + " !");
break;
}
sender.sendMessage(msg);
break;
case "cp":
List<String> copy = Tools.parseCommand(Tools.join(args));
if(copy.size() != 2) {
sender.sendMessage("Specify a source and a destination");
} else {
File from = new File(current, copy.get(0));
File to = new File(current, copy.get(1));
FileManager.copy(from, to);
sender.sendMessage("Copied " + from.getAbsolutePath() + " to " + to.getAbsolutePath());
}
break;
case "mv":
List<String> move = Tools.parseCommand(Tools.join(args));
if(move.size() != 2) {
sender.sendMessage("Specify a source and a destination");
} else {
File from = new File(current, move.get(0));
File to = new File(current, move.get(1));
FileManager.move(from, to);
sender.sendMessage("Copied " + from.getAbsolutePath() + " to " + to.getAbsolutePath());
}
break;
case "rm":
File delete = new File(current, Tools.join(args));
FileManager.delete(delete);
sender.sendMessage("Deleted " + delete.getAbsolutePath() + " !");
break;
} catch(IOException ex) {
ex.printStackTrace();
}
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/fr/dabsunter/omgremote/commands/ProcessCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ private void start(CommandSender sender, String[] args) {
} else {
new ProcessManager(name, command);
selected = name;
sender.sendMessage("Successfully launched and selected " + name + "process");
sender.sendMessage("Successfully launched and selected " + name + " process");
}
}

Expand Down

0 comments on commit 816f456

Please sign in to comment.