Vaerys_Dawn

Beautifully commented code.

Nov 4th, 2016
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.66 KB | None | 0 0
  1.     private void updateChannel() {
  2.         Utility.deleteMessage(channel.getMessages());
  3.         StringBuilder builder = new StringBuilder();
  4.         ArrayList<String> stringChunks = new ArrayList<>();
  5.         String lastAttepmt;
  6.         String imagePrefix = "#image#{";
  7.         String imageSuffix = "}";
  8.         String image;
  9.  
  10.         String[] contentsSplit;
  11.         //prep for the everything...
  12.         for (String s : infoContents) {
  13.             if (!s.startsWith("//")) {
  14.                 //the file is first split up into 1800 or less chunks to make it easier to manage
  15.                 if ((builder + doTextTags(s) + "\n").length() > 1800) {
  16.                     stringChunks.add(builder.toString());
  17.                     builder.delete(0, builder.length());
  18.                 }
  19.                 builder.append(doTextTags(s) + "\n");
  20.             }
  21.         }
  22.         stringChunks.add(builder.toString());
  23.         builder.delete(0, builder.length());
  24.  
  25.         //actual code.
  26.         for (String contents : stringChunks) {
  27.             try {
  28.                 contents = TagSystem.tagNoNL(contents);
  29.                 //test to see if adding the next set will make the message to long and stops that.
  30.                 if ((builder.toString() + contents).length() > 1800) {
  31.                     Utility.sendMessage(builder.toString(), channel);
  32.                     Thread.sleep(2000);
  33.                     builder.delete(0, builder.length());
  34.                 }
  35.                 do {
  36.                     lastAttepmt = contents;
  37.                     String toAppend = "";
  38.                     if (contents.contains(imagePrefix)) {
  39.                         //getting image file location
  40.                         image = StringUtils.substringBetween(contents, imagePrefix, imageSuffix);
  41.                         //splitting it so that the stuff before the image can be sent
  42.                         contentsSplit = contents.split(Pattern.quote(imagePrefix + image + imageSuffix));
  43.  
  44.                         //if the first part of the array isn't empty get that.
  45.                         if (contentsSplit != null && contentsSplit.length != 0) {
  46.                             toAppend = contentsSplit[0];
  47.                         }
  48.                         //append that to the thinger
  49.                         builder.append(toAppend);
  50.                         //send what you have already and then clear the builder.
  51.                         Utility.sendMessage(builder.toString(), channel);
  52.                         builder.delete(0, builder.length());
  53.                         Thread.sleep(2000);
  54.                         //clear the stuff that was just sent and the the image code from things
  55.                         contents = contents.replaceFirst(Pattern.quote(toAppend + imagePrefix + image + imageSuffix), "");
  56.                         //set up the file
  57.                         File file = new File(Utility.getGuildImageDir(guild.getID()) + image);
  58.                         //if it exists send it if not send and error
  59.                         if (!file.exists()) {
  60.                             Utility.sendMessage(file.getPath() + " Does not Exist", channel);
  61.                             Thread.sleep(2000);
  62.                         } else {
  63.                             Utility.sendFile("", channel, file);
  64.                         }
  65.                     }
  66.                 } while (!contents.equals(lastAttepmt) && contents.contains(imagePrefix));
  67.                 builder.append(contents);
  68.             } catch (InterruptedException e) {
  69.                 e.printStackTrace();
  70.             }
  71.         }
  72.         //after everything send the last set
  73.         Utility.sendMessage(builder.toString(), channel);
  74.     }
Advertisement
Add Comment
Please, Sign In to add comment