303

Untitled

303
Mar 19th, 2011
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.87 KB | None | 0 0
  1. package net.minecraft.src;
  2.  
  3. import java.io.*;
  4. import java.util.*;
  5. import net.minecraft.client.Minecraft;
  6. import org.lwjgl.input.Keyboard;
  7. import javax.script.*;
  8.  
  9. public class mod_ircchat extends BaseMod  {
  10.     public static final String modbackendname="ircchat";
  11.     public File dir;
  12.     public File config;
  13.     public static mod_ircchat inst;
  14.     public GuiChatIRC gui;
  15.     public IrcBot irc;
  16.     public ScriptEngine engine;
  17.  
  18.     public String name;
  19.     public int key_open = Keyboard.KEY_RETURN;
  20.     public boolean onlySSP = false;
  21.     public boolean closeOnEnter = false;
  22.     public List<String> highlights = new ArrayList<String>();
  23.     public List<String> bot_users = new ArrayList<String>();
  24.     public String ping_sound = "random.pop";
  25.     public float ping_volume = 1F;
  26.     public String server = "";
  27.     public String channel = "";
  28.     public boolean autoconnect = false;
  29.  
  30.     public String username;
  31.  
  32.     public List<String> keys = Arrays.asList(new String[]{
  33.         "key",
  34.         "close_on_enter",
  35.         "only_ssp",
  36.         "nick",
  37.         "highlights",
  38.         "ping_sound",
  39.         "ping_volume",
  40.         "bot_users",
  41.         "autoconnect",
  42.         "server",
  43.         "channel",
  44.         "username"
  45.     });
  46.  
  47.     public mod_ircchat() {
  48.         try {
  49.             inst = this;
  50.             Minecraft mc = ModLoader.getMinecraftInstance();
  51.  
  52.             if (mc != null && mc.session != null)
  53.                 username = name = mc.session.username;
  54.             else
  55.                 username = name = mod_ircchat.class.getName();
  56.  
  57.             dir =  Minecraft.getAppDir("minecraft/mods/"+/*Settings.*/modbackendname+"/");
  58.             config = new File(dir, "config.txt");
  59.             loadConfig();
  60.             saveConfig();
  61.  
  62.             if (!dir.exists())
  63.                 dir.mkdirs();
  64.  
  65.             gui = new GuiChatIRC();
  66.             irc = new IrcBot(name, username);
  67.  
  68.             engine = new ScriptEngineManager().getEngineByName("javascript");
  69.  
  70.             if (engine != null) {
  71.                 Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
  72.                 bindings.put("game", mc);
  73.                 bindings.put("irc", irc);
  74.                 bindings.put("gui", gui);
  75.                 bindings.put("js", engine);
  76.                 bindings.put("mod", this);
  77.             }
  78.  
  79.             if (autoconnect) {
  80.                 if (server != null && (server=server.trim()).length() !=0) {
  81.                     irc.do_connect(server);
  82.                    
  83.  
  84.                     if (irc.isConnected()
  85.                         && channel != null
  86.                         && (channel=channel.trim()).length() != 0) {
  87.                         irc.do_join(channel);
  88.                     }
  89.                 }
  90.             }
  91.         } catch (Throwable e) {
  92.             ModLoader.ThrowException("@_@", e);
  93.         }
  94.     }
  95.  
  96.     public Properties getCurrentProperties() {
  97.         Properties props = new Properties();
  98.         props.setProperty("nick", name);
  99.         props.setProperty("ping_sound", ping_sound);
  100.         props.setProperty("server", server);
  101.         props.setProperty("channel", channel);
  102.         props.setProperty("username", username);
  103.         props.setProperty("ping_volume", String.format("%.1f", ping_volume));
  104.         props.setProperty("only_ssp", Boolean.toString(onlySSP));
  105.         props.setProperty("autoconnect", Boolean.toString(autoconnect));
  106.         props.setProperty("close_on_enter", Boolean.toString(closeOnEnter));
  107.         props.setProperty("key", Keyboard.getKeyName(key_open));
  108.  
  109.         String shl = "";
  110.         boolean first = true;
  111.  
  112.  
  113.         for (String hl: highlights) {
  114.             if (first)
  115.                 first = false;
  116.             else
  117.                 shl += " ";
  118.  
  119.             shl+=hl;
  120.         }
  121.  
  122.         props.setProperty("highlights", shl);
  123.  
  124.         String susers = "";
  125.         first = true;
  126.  
  127.  
  128.         for (String user: bot_users) {
  129.             if (first)
  130.                 first = false;
  131.             else
  132.                 susers += " ";
  133.  
  134.             susers+=user;
  135.         }
  136.  
  137.         props.setProperty("bot_users", susers);
  138.  
  139.         return props;
  140.     }
  141.  
  142.     public void loadConfig(Properties props) {
  143.         String sname = props.getProperty("nick");
  144.  
  145.         if (sname != null)
  146.             name = sname;
  147.  
  148.         String susername = props.getProperty("username");
  149.  
  150.         if (susername != null)
  151.             username = sname;
  152.  
  153.         String sserver = props.getProperty("server");
  154.  
  155.         if (sserver != null)
  156.             server = sserver;
  157.  
  158.         String schannel = props.getProperty("channel");
  159.  
  160.         if (schannel != null)
  161.             channel = schannel;
  162.  
  163.         String sping_sound = props.getProperty("ping_sound");
  164.  
  165.         if (sping_sound != null)
  166.             ping_sound = sping_sound;
  167.  
  168.         String sonlySSP = props.getProperty("only_ssp");
  169.  
  170.         if (sonlySSP != null) {
  171.             Boolean bonlySSP = Boolean.valueOf(sonlySSP);
  172.  
  173.             if (bonlySSP != null)
  174.                 onlySSP = bonlySSP;
  175.         }
  176.  
  177.         String scloseOnEnter = props.getProperty("close_on_enter");
  178.  
  179.         if (scloseOnEnter != null) {
  180.             Boolean bcloseOnEnter = Boolean.valueOf(scloseOnEnter);
  181.  
  182.             if (bcloseOnEnter != null)
  183.                 closeOnEnter = bcloseOnEnter;
  184.         }
  185.  
  186.         String sautoconnect = props.getProperty("autoconnect");
  187.  
  188.         if (sautoconnect != null) {
  189.             Boolean bautoconnect = Boolean.valueOf(sautoconnect);
  190.  
  191.             if (bautoconnect != null)
  192.                 autoconnect = bautoconnect;
  193.         }
  194.  
  195.         String sping_volume = props.getProperty("ping_volume");
  196.  
  197.         if (sping_volume != null) {
  198.             try {
  199.                 ping_volume = Float.parseFloat(sping_volume);
  200.             } catch (NumberFormatException e) {}
  201.         }
  202.  
  203.         String skey = props.getProperty("key");
  204.  
  205.         if (skey != null) {
  206.             int ikey = Keyboard.getKeyIndex(skey);
  207.             if (ikey != Keyboard.KEY_NONE)
  208.                 key_open = ikey;
  209.         }
  210.  
  211.         String shl = props.getProperty("highlights");
  212.  
  213.         if (shl != null) {
  214.             String[] hls = shl.split("\\s+");
  215.             highlights = new ArrayList<String>();
  216.  
  217.             for (String hl: hls) {
  218.                 hl = hl.trim();
  219.                 if (hl.length() > 0)
  220.                     highlights.add(hl);
  221.             }
  222.  
  223.         }
  224.  
  225.         String susers = props.getProperty("bot_users");
  226.  
  227.         if (susers != null) {
  228.             String[] users = susers.split("\\s+");
  229.             bot_users = new ArrayList<String>();
  230.  
  231.             for (String user: users) {
  232.                 user = user.trim();
  233.  
  234.                 if (user.length() > 0)
  235.                     bot_users.add(user);
  236.             }
  237.         }
  238.     }
  239.  
  240.     public void loadConfig() {
  241.         if (!config.exists() || !config.canRead() || !config.isFile()) {
  242.             System.out.println(mod_ircchat.class.getName()+": couldn't load config");
  243.         }
  244.  
  245.         Properties props = new Properties();
  246.         FileInputStream in = null;
  247.  
  248.         try {
  249.             in = new FileInputStream(config);
  250.             props.load(in);
  251.         } catch (IOException e) {
  252.             System.out.println(mod_ircchat.class.getName()+": couldn't load config");
  253.             return;
  254.         } finally {
  255.             if (in != null) {
  256.                 try {
  257.                     in.close();
  258.                 } catch (IOException e) {}
  259.             }
  260.         }
  261.        
  262.         loadConfig(props);
  263.  
  264.     }
  265.  
  266.     public void saveConfig() {
  267.         Properties props = getCurrentProperties();
  268.  
  269.         FileOutputStream out = null;
  270.  
  271.         try {
  272.             out = new FileOutputStream(config);
  273.             props.store(out, "---mod_ircchat---");
  274.         } catch (IOException e) {
  275.             System.out.println(mod_ircchat.class.getName()+": couldn't save config to "+config);
  276.             return;
  277.         } finally {
  278.             if (out != null) {
  279.                 try {
  280.                     out.close();
  281.                 } catch (IOException e) {}
  282.             }
  283.         }
  284.     }
  285.  
  286.     public void saveConfig(Properties changes) {
  287.         loadConfig(changes);
  288.         saveConfig();
  289.     }
  290.  
  291.     public boolean setConfig(String key, String value) {
  292.         if (!keys.contains(key))
  293.             return false;
  294.        
  295.         Properties props = new Properties();
  296.         props.setProperty(key, value);
  297.         saveConfig(props);
  298.         return true;
  299.     }
  300.  
  301.     public String getConfig(String key) {
  302.         return getCurrentProperties().getProperty(key);
  303.     }
  304.  
  305.     public void OSDHook(Minecraft game, boolean ingui) {
  306.         if (ingui)
  307.             return;
  308.  
  309.         if (onlySSP && game.isMultiplayerWorld())
  310.             return;
  311.  
  312.  
  313.         if (Keyboard.isKeyDown(key_open)) {
  314.             gui.run();
  315.         }
  316.     }
  317.  
  318.     public String Version() {
  319.         return "1.2v2";
  320.     }
  321. }
Advertisement
Add Comment
Please, Sign In to add comment