Skip to content

Commit

Permalink
python command to launch script
Browse files Browse the repository at this point in the history
  • Loading branch information
arpruss committed Jan 21, 2015
1 parent 4ff03d1 commit c3ab72e
Show file tree
Hide file tree
Showing 3 changed files with 184 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package mobi.omegacentauri.raspberryjammod;

import java.util.ArrayList;
import java.util.List;

import net.minecraft.command.CommandException;
import net.minecraft.command.ICommand;
import net.minecraft.command.ICommandSender;
import net.minecraft.util.BlockPos;

public class PythonExternalCommand extends ScriptExternalCommand {

@Override
public String getName() {
return "python";
}

@Override
public List getAliases() {
List<String> aliases = new ArrayList<String>();
aliases.add(getName());
aliases.add("py");
return aliases;
}

@Override
public String getCommandUsage(ICommandSender sender) {
return "python script [arguments]: run script (omit script to stop previous script)";
}

@Override
public boolean canCommandSenderUse(ICommandSender sender) {
return true;
}

@Override
public List addTabCompletionOptions(ICommandSender sender, String[] args,
BlockPos pos) {
return null;
}

@Override
public boolean isUsernameIndex(String[] args, int index) {
return false;
}

@Override
public int compareTo(Object o) {
return 0;
}

@Override
protected String getScriptProcessorBase() {
return "python";
}

@Override
protected String[] getExtensions() {
return new String[] { ".py", ".python" };
}

@Override
protected String[] getScriptPaths() {
return new String[] { "mcpipy/" , "mcpimods/python/"};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.common.ForgeHooks;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.ForgeEventFactory;
import net.minecraftforge.fml.common.FMLCommonHandler;
Expand All @@ -34,7 +35,7 @@
public class RaspberryJamMod
{
public static final String MODID = "raspberryjammod";
public static final String VERSION = "0.03";
public static final String VERSION = "0.04";
public static final String NAME = "Raspberry Jam Mod";
private MinecraftCommunicator mcc;

Expand All @@ -46,7 +47,7 @@ public void onServerStopping(FMLServerStoppingEvent event) {
}

@EventHandler
public void onServerStarted(FMLServerStartedEvent event) // FMLInitializationEvent event)
public void onServerStarting(FMLServerStartingEvent event) // FMLInitializationEvent event)
{
// some example code
System.out.println("Raspberry Jam Mod started");
Expand Down Expand Up @@ -77,6 +78,6 @@ public void run() {
System.out.println("Threw "+e1);
}

event.registerServerCommand(new PythonExternalCommand());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package mobi.omegacentauri.raspberryjammod;

import java.io.File;
import java.io.IOException;
import java.lang.ProcessBuilder.Redirect;
import java.util.ArrayList;
import java.util.List;

import net.minecraft.command.CommandException;
import net.minecraft.command.ICommand;
import net.minecraft.command.ICommandSender;
import net.minecraft.util.BlockPos;

public abstract class ScriptExternalCommand implements ICommand {
abstract protected String getScriptProcessorBase();
abstract protected String[] getExtensions();
abstract protected String[] getScriptPaths();
Process runningScript = null;
final String scriptProcessorPath;

public ScriptExternalCommand() {
scriptProcessorPath = getScriptProcessorPath();
}

@Override
public List getAliases() {
List<String> aliases = new ArrayList<String>();
aliases.add(getName());
return aliases;
}

protected String getScriptProcessorPath() {
String base = getScriptProcessorBase();

String pathVar = System.getenv("PATH");
System.out.println(pathVar);
if (pathVar == null)
return base;

String exeExt = System.getProperty("os.name").startsWith("Windows") ? ".exe" : "";

String[] paths = pathVar.split(System.getProperty("path.separator"));

for (String dir : paths) {
String p = dir + System.getProperty("file.separator") + base + exeExt;
if (new File(p).canExecute())
return p;
}

return base;
}


@Override
public void execute(ICommandSender sender, String[] args)
throws CommandException {
if (runningScript != null) {
runningScript.destroy();
runningScript = null;
if (args.length == 0) {
return;
}
}

File script = getScript(args[0]);
if (script == null) {
throw new CommandException("Cannot find script");
}

List<String> cmd = new ArrayList<String>();
cmd.add(scriptProcessorPath);
cmd.add(script.getName());
for (int i = 1 ; i < args.length ; i++)
cmd.add(args[i]);

ProcessBuilder pb = new ProcessBuilder(cmd);
pb.redirectErrorStream(true);
pb.directory(script.getParentFile());
pb.inheritIO();
pb.command(cmd);
try {
System.out.println("Running "+script);
runningScript = pb.start();
} catch (IOException e) {
throw new CommandException("Error "+e);
}
}

protected File getScript(String base) {
String[] paths = getScriptPaths();
String[] exts = getExtensions();

for (String ext : exts) {
if (ext.startsWith(".") && base.endsWith(ext)) {
// already have extension
exts = new String[]{""};
break;
}
}

for (String path : paths) {
for (String ext : exts) {
try {
File f = new File(path + base + ext);
if (f.canRead())
return f;
}
catch(SecurityException e) {
}
}
}
return null;
}
}

0 comments on commit c3ab72e

Please sign in to comment.