iPeer

Untitled

Sep 6th, 2012
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.31 KB | None | 0 0
  1. package com.ipeer.iutil.engine;
  2.  
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8. import java.io.RandomAccessFile;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11. import java.util.Properties;
  12.  
  13. public class AWeSomeChat implements Runnable {
  14.  
  15. private File file;
  16. public boolean IS_RUNNING = false;
  17. public List<String> online = new ArrayList<String>();
  18. protected Engine engine;
  19. public static Properties cache;
  20.  
  21. public AWeSomeChat(Engine engine, String file) {
  22. this.engine = engine;
  23. this.file = new File(file);
  24. cache = new Properties();
  25. if (Engine.AWeSomeChatCache.exists())
  26. try {
  27. cache.load(new FileInputStream(Engine.AWeSomeChatCache));
  28. } catch (FileNotFoundException e) {
  29. // TODO Auto-generated catch block
  30. e.printStackTrace();
  31. } catch (IOException e) {
  32. // TODO Auto-generated catch block
  33. e.printStackTrace();
  34. }
  35. }
  36.  
  37. public void start() {
  38. this.IS_RUNNING = true;
  39. (new Thread(this, "AWeSome Chat")).start();
  40. }
  41.  
  42. public static void main(String[] args) throws IOException {
  43. AWeSomeChat a = new AWeSomeChat(null, "/home/minecraft/servers/survival/server.log"/*"F:\\Dropbox\\Public\\Testtextfile.txt"*/);
  44. a.parseLine("2012-09-06 20:11:30 [INFO] <iPeer> I'm still testing my tailing", "");
  45. //a.start();
  46. }
  47.  
  48. @Override
  49. public void run() {
  50. try {
  51. String c = ((file.getAbsolutePath()).toLowerCase().contains("creative")) ? "Creative" : "Survival";
  52. //Reader a = new FileReader(file);
  53. //BufferedReader b = new BufferedReader(a);
  54. RandomAccessFile b = new RandomAccessFile(file, "r");
  55. String line = null;
  56. while (IS_RUNNING) {
  57. long marker = Long.valueOf(cache.getProperty(c, Long.toString(b.length())));
  58. cache.put(c, Long.toString(marker));
  59. cache.store(new FileOutputStream(Engine.AWeSomeChatCache), "");
  60. b.seek(marker);
  61. if ((line = b.readLine()) != null) {
  62. marker = b.getFilePointer();
  63. if (line.length() > 0)
  64. parseLine(line, c);
  65. cache.put(c, Long.toString(marker));
  66. cache.store(new FileOutputStream(Engine.AWeSomeChatCache), "");
  67. }
  68. else
  69. Thread.sleep(1000);
  70. }
  71. b.close();
  72. }
  73. catch (IOException e) {
  74. System.err.println("Unable to tail file:");
  75. e.printStackTrace();
  76. } catch (InterruptedException e) {
  77. System.err.println("Thread was interrupted: \n");
  78. e.printStackTrace();
  79. }
  80.  
  81. }
  82.  
  83. private void parseLine(String line, String c) throws IOException {
  84. String prefix = Engine.colour+"14["+Engine.colour+"13AWeSome "+c+Engine.colour+"14]:"+Engine.colour;
  85. //System.err.println(line);
  86. if (line.contains("lost connection:")) {
  87. String u = line.split(" ")[3];
  88. if (online.contains(u))
  89. online.remove(u);
  90. if (engine != null)
  91. engine.send("PRIVMSG #QuestHelp :"+prefix+" "+u+" disconnected.");
  92. else
  93. System.err.println(u+" disconnected.");
  94. }
  95.  
  96. else if (line.contains("logged in with entity id")) {
  97. String u = line.split(" ")[3].replaceAll("\\[.*\\]", "");
  98. online.add(u);
  99. if (engine != null)
  100. engine.send("PRIVMSG #QuestHelp :"+prefix+" "+u+" connected.");
  101. else
  102. System.err.println(u+" connected.");
  103. }
  104. else if (line.contains("[INFO] <")) {
  105. String[] data = line.split(" ");
  106. String u = data[3].replaceAll("(<|>)", "");
  107. String message = data[4];
  108. for (int x = 5; x < data.length; x++)
  109. message = message+" "+data[x];
  110. if (engine != null && !message.startsWith("P "))
  111. engine.send("PRIVMSG #QuestHelp :"+prefix+" "+u+": "+message);
  112. // else
  113. // System.err.println(u+": "+message);
  114. }
  115. }
  116.  
  117. public void sendPlayers(String sendPrefix, String c) throws IOException {
  118. String prefix = Engine.colour+"14["+Engine.colour+"13AWeSome "+c+Engine.colour+"14]:"+Engine.colour;
  119. String o = "";
  120. if (online.isEmpty())
  121. o = Engine.colour+"14 Nobody is currently online :(";
  122. else {
  123. int a = online.size();
  124. o = " "+c2(Integer.toString(a))+" "+c1((a > 1 ? "people" : "person"))+" online: "+c2(online.get(0));
  125. for (int x = 1; x < online.size(); x++)
  126. o = o+c1(", ")+c2(online.get(x));
  127. }
  128. engine.send(sendPrefix+prefix+o);
  129. }
  130.  
  131. private String c1(String s) {
  132. return Engine.colour+"14"+s;
  133. }
  134.  
  135. private String c2(String s) {
  136. return Engine.colour+"13"+s;
  137. }
  138.  
  139. }
Advertisement
Add Comment
Please, Sign In to add comment