Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- import java.util.regex.*;
- import net.minecraft.client.Minecraft;
- import org.lwjgl.input.Keyboard;
- import bsh.*;
- public class mod_bsh extends BaseMod {
- public static final String modbackendname="bsh";
- public Interpreter interpreter;
- //public JFrame frame;
- //public JConsole console;
- public File dir;
- public static Minecraft mc;
- public static mod_bsh inst;
- public GuiShell shellgui;
- public ErrorConsole console;
- public String files[] = {
- "rc.bsh",
- "util.bsh",
- "keys.bsh"
- };
- public String[] reobfFiles = {
- "classes.properties",
- "reobf.properties"
- };
- public Properties mappings;
- public Pattern reobfPattern;
- public boolean reobf = !dt.class.getSimpleName().equals("World");
- public Queue<Object> serialcmds = new LinkedList<Object>();
- public mod_bsh() {
- try {
- java.lang.reflect.Method m = ModLoader.class.getDeclaredMethod("SetInGameHook", BaseMod.class, boolean.class, boolean.class);
- m.invoke(null, this, true, false);
- } catch (Throwable e) {}
- System.out.println("Reobf: "+reobf);
- inst = this;
- mc = ModLoader.getMinecraftInstance();
- initMappings();
- initConsole();
- addSerial("chat(\"help() to read the README\");");
- if (!install(false)) {
- 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.\");");
- }
- initShell();
- }
- public void initConsole() {
- console = new ErrorConsole();
- }
- public Keybinding bind(String key, boolean sticky, Runnable code) {
- return new Keybinding(key,sticky,code).bind();
- }
- public Keybinding bind(String key, boolean sticky, String code) {
- return new Keybinding(key,sticky,code).bind();
- }
- /**
- * Add an action to be called in the main MC thread in OSDHook.
- * @param s The action. Either a String to be evaluated or a Runnable.
- * Exceptions will be reported to the console window and ignored.
- */
- public void addSerial(Object s) {
- synchronized(serialcmds) {
- serialcmds.add(s);
- }
- }
- public void source(File file) {
- System.err.println("sourcing "+file);
- if (!file.exists() ||
- !file.isFile() ||
- !file.canRead()) {
- System.err.println("bsh: can't read "+file);
- return;
- }
- try {
- eval(file);
- } catch (Throwable e) {
- error(e);
- }
- }
- public Object eval(String s) throws EvalError {
- if (reobf) {
- s = reobf(s);
- }
- return interpreter.eval(s);
- }
- public Object eval(File f) throws EvalError, IOException{
- return eval(readFile(f));
- }
- public String readFile(File file) throws IOException {
- StringBuffer fileData = new StringBuffer(1000);
- BufferedReader reader = new BufferedReader(new FileReader(file));
- char[] buf = new char[1024];
- int numRead=0;
- while((numRead=reader.read(buf)) != -1){
- String readData = String.valueOf(buf, 0, numRead);
- fileData.append(readData);
- buf = new char[1024];
- }
- reader.close();
- return fileData.toString();
- }
- public String reobf(File file) throws IOException {
- String code = readFile(file);
- return reobf(code);
- }
- public String reobf(String code) {
- int prevStart = 0;
- StringBuilder sb = new StringBuilder();
- Matcher matcher = reobfPattern.matcher(code);
- while (matcher.find()) {
- sb.append(code.substring(prevStart, matcher.start()));
- sb.append(mappings.getProperty(matcher.group()));
- prevStart = matcher.end();
- }
- sb.append(code.substring(prevStart, code.length()));
- //for (String key: mappings.stringPropertyNames()) {
- //code = code.replaceAll("\\b"+key+"\\b", mappings.getProperty(key));
- //}
- return sb.toString();
- }
- public void initMappings() {
- mappings = new Properties();
- try {
- for (String f: reobfFiles) {
- InputStream i = mod_bsh.class.getResourceAsStream("/mod_bsh/"+f);
- mappings.load(i);
- }
- } catch (Throwable e) {
- error(e);
- }
- StringBuilder sb = new StringBuilder("\\b(?:");
- boolean first = true;
- for (String key: mappings.stringPropertyNames()) {
- if (first)
- first = false;
- else
- sb.append("|");
- sb.append(key);
- }
- sb.append(")\\b");
- reobfPattern = Pattern.compile(sb.toString());
- }
- public void source(String filename) {
- source(new File(dir, filename));
- }
- public boolean forceInstall() {
- return install(true);
- }
- public boolean install(String f, boolean force) {
- File file = new File(dir, f);
- if (file.exists()) {
- if (force) {
- System.err.println("Overwriting "+f);
- } else {
- System.err.println("Not creating "+f+", already exists.");
- return false;
- }
- }
- System.err.println("Creating "+file);
- OutputStream o = null;
- InputStream i = null;
- try {
- o = new FileOutputStream(file);
- i = mod_bsh.class.getResourceAsStream("/mod_bsh/"+f);
- byte[] b = new byte[4096];
- int read;
- while ((read = i.read(b)) != -1) {
- o.write(b, 0, read);
- }
- o.flush();
- } catch (IOException e) {
- System.err.println("bsh: can't create "+file);
- e.printStackTrace();
- return false;
- } finally {
- try {
- if (o!=null) o.close();
- if (i!=null) i.close();
- } catch (Throwable e) {}
- }
- return true;
- }
- public boolean install(boolean force) {
- dir = Minecraft.a("minecraft/mods/"+/*Settings.*/modbackendname+"/");
- if (!dir.exists())
- dir.mkdirs();
- boolean allInstalled = true;
- for (String f: files) {
- allInstalled = allInstalled && install(f, force);
- }
- return allInstalled;
- }
- public void initShell() {
- interpreter = new Interpreter();
- shellgui = new GuiShell();
- try {
- interpreter.set("mc", mc);
- interpreter.set("mod", this);
- } catch (EvalError e) {
- throw new RuntimeException(e);
- }
- source(new File(dir, "rc.bsh"));
- }
- /**
- * Print a message to the beanshell window, or System.out if not yet available
- */
- public void print(Object o) {
- console.print(o);
- }
- /**
- * Print an error message to the beanshell window, or System.err if not yet available
- * @param o The Object to print.
- */
- public void error(Object o) {
- console.error(o);
- }
- public void OnTickInGame(Minecraft game) {
- OSDHook(game, game.q != null);
- }
- public void OSDHook(Minecraft game, boolean ingui) {
- if (ingui)
- return;
- Keybinding.handleAll();
- runSerialCmds();
- }
- //public void addTick(Object code) {
- //addSerial(new EntityDummy(code));
- //}
- public void runSerialCmds() {
- Object cmd = null;
- synchronized(serialcmds) {
- cmd = serialcmds.poll();
- }
- if (cmd == null)
- return;
- try {
- if (cmd instanceof String) {
- interpreter.eval((String)cmd);
- } else if (cmd instanceof Runnable) {
- ((Runnable)cmd).run();
- } else {
- error("Unknown callback type: "+cmd.getClass().getName());
- }
- } catch (Throwable e) {
- error(e);
- }
- }
- String format(Object o) {
- if (o == null)
- return "null";
- if (o instanceof ay) {
- ay pos = (ay)o;
- return String.format("(%.1f|%.1f|%.1f)", pos.a, pos.b, pos.c);
- }
- if (o instanceof om) {
- om mob = (om)o;
- String name = ha.b(mob);
- if (name == null)
- name = o.getClass().getName();
- if (mod_bsh.inst.mc.g == null)
- return String.format("%s@(%.0f|%.0f|%.0f)", name, mob.aI, mob.aJ, mob.aK);
- else
- return String.format("%s@%.1f", name, mob.f(mod_bsh.inst.mc.g));
- }
- if (o instanceof Collection) {
- Collection<Object> c = (Collection<Object>)o;
- StringBuilder sb = new StringBuilder();
- sb.append("[");
- boolean first = true;
- for (Object e: c) {
- if (first)
- first = false;
- else
- sb.append(", ");
- sb.append(format(e));
- }
- sb.append("]");
- return sb.toString();
- }
- if (o instanceof qk) {
- return n.a().b(((qk)o).i());
- }
- if (o instanceof ex) {
- return n.a().b(((ex)o).a());
- }
- //if (o instanceof jw) {
- //jw comp = new jw();
- //((jw)o).b(comp);
- //return String.format("%s@(%.0f|%.0f)", comp.i("id"), comp.e("x")-mc.g.aG, comp.e("z")-mc.g.aI);
- //}
- return o.toString();
- }
- public String Version() {
- return "1.3_01v5";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment