Advertisement
fiveriverflow

Offline

Jan 13th, 2017
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.06 KB | None | 0 0
  1. try {
  2.             String name = playerCommand.split(" ")[2];
  3.             int points Integer.parseInt(playerCommand.split(" ")[1])
  4.             Client c2 = PlayerHandler.getPlayerByName(name);
  5.             if (c2 != null) {
  6.                 c2.slayerPoints += points;
  7.                 c.sendMessage(Added + points + " Slayer Points to " + Misc.formatPlayerName(c2.playerName));
  8.                 return;
  9.             }
  10.             c.sendMessage("Player is offline, modifying offline player."); 
  11.             FileInputStream fstream = new FileInputStream("./Data/characters/" + name + ".txt");
  12.             BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
  13.             String strLine;
  14.             StringBuilder fileContent = new StringBuilder();
  15.             //Read File Line By Line
  16.             while ((strLine = br.readLine()) != null) {
  17.                 // Print the content on the console
  18.                 System.out.println(strLine);
  19.                 String tokens[] = strLine.split(" = ");
  20.                 if (tokens.length > 0) {
  21.                     // Here tokens[0] will have value of ID
  22.                     if (tokens[0].equals("slayerPoints")) {
  23.                         tokens[1] = (Integer.parseInt(tokens[1]) + points);
  24.                         String newLine = tokens[0] + " = " + tokens[1];
  25.                         fileContent.append(newLine);
  26.                         fileContent.append("\n");
  27.                     } else {
  28.                         // update content as it is
  29.                         fileContent.append(strLine);
  30.                         fileContent.append("\n");
  31.                     }
  32.                 }
  33.             }
  34.             // Now fileContent will have updated content , which you can override into file
  35.             FileWriter fstreamWrite = new FileWriter("./Data/characters/" + name + ".txt");
  36.             BufferedWriter out = new BufferedWriter(fstreamWrite);
  37.             out.write(fileContent.toString());
  38.             out.close();
  39.             //Close the input stream
  40.             in.close();
  41.         } catch (Exception e) {//Catch exception if any
  42.             System.err.println("Error: " + e.getMessage());
  43.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement