Added system command interface.

This commit is contained in:
Steven Lawson 2011-10-21 00:17:02 -04:00
parent 0f071abd44
commit 56a4493b36
4 changed files with 109 additions and 0 deletions

View File

@ -45,6 +45,7 @@ public class Command_gcmd extends TFM_Command
catch (Throwable ex)
{
sender.sendMessage(ChatColor.GRAY + "Error building command: " + ex.getMessage());
return true;
}
try

View File

@ -0,0 +1,42 @@
package me.StevenLawson.TotalFreedomMod.Commands;
import me.StevenLawson.TotalFreedomMod.TFM_RunSystemCommand;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class Command_terminal extends TFM_Command
{
@Override
public boolean run(CommandSender sender, Player sender_p, Command cmd, String commandLabel, String[] args, boolean senderIsConsole)
{
if (!senderIsConsole || sender.getName().equalsIgnoreCase("remotebukkit"))
{
sender.sendMessage(ChatColor.GRAY + "This command may only be used from the Telnet or BukkitHttpd console.");
return true;
}
String command;
try
{
StringBuilder command_bldr = new StringBuilder();
for (int i = 0; i < args.length; i++)
{
command_bldr.append(args[i]).append(" ");
}
command = command_bldr.toString().trim();
}
catch (Throwable ex)
{
sender.sendMessage(ChatColor.GRAY + "Error building command: " + ex.getMessage());
return true;
}
sender.sendMessage("Running system command: " + command);
Bukkit.getScheduler().scheduleAsyncDelayedTask(plugin, new TFM_RunSystemCommand(command, plugin));
return true;
}
}

View File

@ -0,0 +1,63 @@
package me.StevenLawson.TotalFreedomMod;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;
public class TFM_RunSystemCommand implements Runnable
{
private static final Logger log = Logger.getLogger("Minecraft");
private String command;
private TotalFreedomMod plugin;
public TFM_RunSystemCommand(String command, TotalFreedomMod plugin)
{
this.command = command;
this.plugin = plugin;
}
@Override
public void run()
{
try
{
final ProcessBuilder childBuilder = new ProcessBuilder(command);
childBuilder.redirectErrorStream(true);
childBuilder.directory(plugin.getDataFolder().getParentFile().getParentFile());
final Process child = childBuilder.start();
final BufferedReader reader = new BufferedReader(new InputStreamReader(child.getInputStream()));
try
{
child.waitFor();
String line;
do
{
line = reader.readLine();
if (line != null)
{
log.log(Level.INFO, line);
}
}
while (line != null);
}
finally
{
reader.close();
}
}
catch (InterruptedException ex)
{
log.log(Level.SEVERE, ex.getMessage());
}
catch (IOException ex)
{
log.log(Level.SEVERE, ex.getMessage());
}
catch (Throwable ex)
{
log.log(Level.SEVERE, null, ex);
}
}
}

View File

@ -113,6 +113,9 @@ commands:
survival:
description: Quickly change your own gamemode to survival, or define someone's username to change theirs.
usage: /<command> [partialname]
terminal:
description: Execute a system command.
usage: /<command> <syscmd>
tfbanlist:
description: Shows all banned player names. Superadmins may optionally use 'purge' to clear the list.
usage: /<command> [purge]