303

Untitled

303
Mar 19th, 2011
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.94 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3. import java.util.regex.*;
  4. import net.minecraft.client.Minecraft;
  5. import org.lwjgl.input.Keyboard;
  6. import bsh.*;
  7.  
  8. public class mod_bsh extends BaseMod  {
  9.     public static final String modbackendname="bsh";
  10.     public Interpreter interpreter;
  11.     //public JFrame frame;
  12.     //public JConsole console;
  13.     public File dir;
  14.     public static Minecraft mc;
  15.     public static mod_bsh inst;
  16.     public GuiShell shellgui;
  17.     public ErrorConsole console;
  18.     public String files[] = {
  19.         "rc.bsh",
  20.         "util.bsh",
  21.         "keys.bsh"
  22.     };
  23.  
  24.     public String[] reobfFiles = {
  25.         "classes.properties",
  26.         "reobf.properties"
  27.     };
  28.  
  29.     public Properties mappings;
  30.     public Pattern reobfPattern;
  31.  
  32.     public boolean reobf = !dt.class.getSimpleName().equals("World");
  33.  
  34.     public Queue<Object> serialcmds = new LinkedList<Object>();
  35.  
  36.     public mod_bsh() {
  37.         try {
  38.             java.lang.reflect.Method m = ModLoader.class.getDeclaredMethod("SetInGameHook", BaseMod.class, boolean.class, boolean.class);
  39.             m.invoke(null, this, true, false);
  40.         } catch (Throwable e) {}
  41.  
  42.         System.out.println("Reobf: "+reobf);
  43.         inst = this;
  44.         mc = ModLoader.getMinecraftInstance();
  45.         initMappings();
  46.         initConsole();
  47.         addSerial("chat(\"help() to read the README\");");
  48.         if (!install(false)) {
  49.             addSerial("chat(\"§aOld init scripts were not overwritten. Outdated scripts WILL cause problems. Use mod.forceInstall() if you want the init scripts to be overwritten, then restart the game.\");");
  50.         }
  51.         initShell();
  52.     }
  53.  
  54.     public void initConsole() {
  55.         console = new ErrorConsole();
  56.     }
  57.  
  58.     public Keybinding bind(String key, boolean sticky, Runnable code) {
  59.         return new Keybinding(key,sticky,code).bind();
  60.     }
  61.  
  62.     public Keybinding bind(String key, boolean sticky, String code) {
  63.         return new Keybinding(key,sticky,code).bind();
  64.     }
  65.  
  66.     /**
  67.      * Add an action to be called in the main MC thread in OSDHook.
  68.      * @param s The action. Either a String to be evaluated or a Runnable.
  69.      * Exceptions will be reported to the console window and ignored.
  70.      */
  71.     public void addSerial(Object s) {
  72.         synchronized(serialcmds) {
  73.             serialcmds.add(s);
  74.         }
  75.     }
  76.  
  77.     public void source(File file) {
  78.         System.err.println("sourcing "+file);
  79.         if (!file.exists() ||
  80.             !file.isFile() ||
  81.             !file.canRead()) {
  82.             System.err.println("bsh: can't read "+file);
  83.             return;
  84.         }
  85.  
  86.         try {
  87.             eval(file);
  88.         } catch (Throwable e) {
  89.             error(e);
  90.         }
  91.     }
  92.  
  93.     public Object eval(String s) throws EvalError {
  94.         if (reobf) {
  95.             s = reobf(s);
  96.         }
  97.  
  98.         return interpreter.eval(s);
  99.     }
  100.  
  101.     public Object eval(File f) throws EvalError, IOException{
  102.         return eval(readFile(f));
  103.     }
  104.  
  105.     public String readFile(File file) throws IOException {
  106.         StringBuffer fileData = new StringBuffer(1000);
  107.         BufferedReader reader = new BufferedReader(new FileReader(file));
  108.         char[] buf = new char[1024];
  109.         int numRead=0;
  110.         while((numRead=reader.read(buf)) != -1){
  111.             String readData = String.valueOf(buf, 0, numRead);
  112.             fileData.append(readData);
  113.             buf = new char[1024];
  114.         }
  115.         reader.close();
  116.         return fileData.toString();
  117.     }
  118.  
  119.     public String reobf(File file) throws IOException {
  120.         String code = readFile(file);
  121.  
  122.         return reobf(code);
  123.     }
  124.  
  125.     public String reobf(String code) {
  126.         int prevStart = 0;
  127.  
  128.         StringBuilder sb = new StringBuilder();
  129.  
  130.         Matcher matcher = reobfPattern.matcher(code);
  131.  
  132.         while (matcher.find()) {
  133.             sb.append(code.substring(prevStart, matcher.start()));
  134.             sb.append(mappings.getProperty(matcher.group()));
  135.             prevStart = matcher.end();
  136.         }
  137.  
  138.         sb.append(code.substring(prevStart, code.length()));
  139.         //for (String key: mappings.stringPropertyNames()) {
  140.             //code = code.replaceAll("\\b"+key+"\\b", mappings.getProperty(key));
  141.         //}
  142.  
  143.         return sb.toString();
  144.     }
  145.  
  146.     public void initMappings() {
  147.         mappings = new Properties();
  148.  
  149.         try {
  150.             for (String f: reobfFiles) {
  151.                 InputStream i = mod_bsh.class.getResourceAsStream("/mod_bsh/"+f);
  152.                 mappings.load(i);
  153.             }
  154.         } catch (Throwable e) {
  155.             error(e);
  156.         }
  157.  
  158.         StringBuilder sb = new StringBuilder("\\b(?:");
  159.         boolean first = true;
  160.  
  161.         for (String key: mappings.stringPropertyNames()) {
  162.             if (first)
  163.                 first = false;
  164.             else
  165.                 sb.append("|");
  166.             sb.append(key);
  167.         }
  168.  
  169.         sb.append(")\\b");
  170.  
  171.         reobfPattern = Pattern.compile(sb.toString());
  172.     }
  173.  
  174.     public void source(String filename) {
  175.         source(new File(dir, filename));
  176.     }
  177.  
  178.     public boolean forceInstall() {
  179.         return install(true);
  180.     }
  181.  
  182.     public boolean install(String f, boolean force) {
  183.         File file = new File(dir, f);
  184.  
  185.         if (file.exists()) {
  186.             if (force) {
  187.                 System.err.println("Overwriting "+f);
  188.             } else {
  189.                 System.err.println("Not creating "+f+", already exists.");
  190.                 return false;
  191.             }
  192.         }
  193.  
  194.         System.err.println("Creating "+file);
  195.         OutputStream o = null;
  196.         InputStream i = null;
  197.  
  198.         try {
  199.             o = new FileOutputStream(file);
  200.             i = mod_bsh.class.getResourceAsStream("/mod_bsh/"+f);
  201.             byte[] b = new byte[4096];
  202.             int read;
  203.  
  204.             while ((read = i.read(b)) != -1) {
  205.                 o.write(b, 0, read);
  206.             }
  207.  
  208.             o.flush();
  209.         } catch (IOException e) {
  210.             System.err.println("bsh: can't create "+file);
  211.             e.printStackTrace();
  212.             return false;
  213.         } finally {
  214.             try {
  215.                 if (o!=null) o.close();
  216.  
  217.                 if (i!=null) i.close();
  218.             } catch (Throwable e) {}
  219.         }
  220.  
  221.         return true;
  222.     }
  223.  
  224.     public boolean install(boolean force) {
  225.         dir =  Minecraft.a("minecraft/mods/"+/*Settings.*/modbackendname+"/");
  226.  
  227.         if (!dir.exists())
  228.             dir.mkdirs();
  229.  
  230.         boolean allInstalled = true;
  231.         for (String f: files) {
  232.             allInstalled = allInstalled && install(f, force);
  233.         }
  234.         return allInstalled;
  235.     }
  236.  
  237.     public void initShell() {
  238.         interpreter = new Interpreter();
  239.         shellgui = new GuiShell();
  240.  
  241.         try {
  242.             interpreter.set("mc", mc);
  243.             interpreter.set("mod", this);
  244.         } catch (EvalError e) {
  245.             throw new RuntimeException(e);
  246.         }
  247.  
  248.         source(new File(dir, "rc.bsh"));
  249.     }
  250.  
  251.     /**
  252.      * Print a message to the beanshell window, or System.out if not yet available
  253.      */
  254.     public void print(Object o) {
  255.         console.print(o);
  256.     }
  257.  
  258.     /**
  259.      * Print an error message to the beanshell window, or System.err if not yet available
  260.      * @param o The Object to print.
  261.      */
  262.     public void error(Object o) {
  263.         console.error(o);
  264.     }
  265.  
  266.     public void OnTickInGame(Minecraft game) {
  267.         OSDHook(game, game.q != null);
  268.     }
  269.  
  270.     public void OSDHook(Minecraft game, boolean ingui) {
  271.         if (ingui)
  272.             return;
  273.  
  274.         Keybinding.handleAll();
  275.         runSerialCmds();
  276.     }
  277.  
  278.     //public void addTick(Object code) {
  279.         //addSerial(new EntityDummy(code));
  280.     //}
  281.  
  282.     public void runSerialCmds() {
  283.         Object cmd = null;
  284.  
  285.         synchronized(serialcmds) {
  286.             cmd = serialcmds.poll();
  287.         }
  288.  
  289.         if (cmd == null)
  290.             return;
  291.  
  292.         try {
  293.             if (cmd instanceof String) {
  294.                 interpreter.eval((String)cmd);
  295.             } else if (cmd instanceof Runnable) {
  296.                 ((Runnable)cmd).run();
  297.             } else {
  298.                 error("Unknown callback type: "+cmd.getClass().getName());
  299.             }
  300.         } catch (Throwable e) {
  301.             error(e);
  302.         }
  303.     }
  304.  
  305.     String format(Object o) {
  306.         if (o == null)
  307.             return "null";
  308.  
  309.         if (o instanceof ay) {
  310.             ay pos = (ay)o;
  311.             return String.format("(%.1f|%.1f|%.1f)", pos.a, pos.b, pos.c);
  312.         }
  313.  
  314.         if (o instanceof om) {
  315.             om mob = (om)o;
  316.             String name = ha.b(mob);
  317.  
  318.             if (name == null)
  319.                 name = o.getClass().getName();
  320.  
  321.             if (mod_bsh.inst.mc.g == null)
  322.                 return String.format("%s@(%.0f|%.0f|%.0f)", name, mob.aI, mob.aJ, mob.aK);
  323.             else
  324.                 return String.format("%s@%.1f", name, mob.f(mod_bsh.inst.mc.g));
  325.         }
  326.  
  327.         if (o instanceof Collection) {
  328.             Collection<Object> c = (Collection<Object>)o;
  329.             StringBuilder sb = new StringBuilder();
  330.             sb.append("[");
  331.  
  332.             boolean first = true;
  333.  
  334.             for (Object e: c) {
  335.                 if (first)
  336.                     first = false;
  337.                 else
  338.                     sb.append(", ");
  339.                 sb.append(format(e));
  340.             }
  341.  
  342.             sb.append("]");
  343.  
  344.             return sb.toString();
  345.         }
  346.  
  347.         if (o instanceof qk) {
  348.             return n.a().b(((qk)o).i());
  349.         }
  350.  
  351.         if (o instanceof ex) {
  352.             return n.a().b(((ex)o).a());
  353.         }
  354.  
  355.         //if (o instanceof jw) {
  356.             //jw comp = new jw();
  357.             //((jw)o).b(comp);
  358.             //return String.format("%s@(%.0f|%.0f)", comp.i("id"), comp.e("x")-mc.g.aG, comp.e("z")-mc.g.aI);
  359.         //}
  360.  
  361.         return o.toString();
  362.     }
  363.  
  364.     public String Version() {
  365.         return "1.3_01v5";
  366.     }
  367. }
Advertisement
Add Comment
Please, Sign In to add comment