Skip to content

Instantly share code, notes, and snippets.

@Double0negative
Last active January 4, 2016 19:58
Show Gist options
  • Save Double0negative/d0823ec7bbcbbb1835b1 to your computer and use it in GitHub Desktop.
Save Double0negative/d0823ec7bbcbbb1835b1 to your computer and use it in GitHub Desktop.
{
"name": "PigTales",
"main": "PigTales"
}
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import org.mcsg.bot.skype.Bot;
import org.mcsg.bot.skype.ChatManager;
import org.mcsg.bot.skype.McsgBotPlugin;
import org.mcsg.bot.skype.commands.SubCommand;
import org.mcsg.bot.skype.util.Arguments;
import org.mcsg.bot.skype.util.StringUtils;
import org.mcsg.bot.skype.web.WebClient;
import com.samczsun.skype4j.chat.Chat;
import com.samczsun.skype4j.user.User;
public class PigTales implements McsgBotPlugin, SubCommand {
public static String create(String title, String author, String... lines) {
if (lines == null || lines.length < 3) {
throw new IllegalArgumentException("Must provide at least 3 lines.");
}
if ((lines.length & 1) == 0) {
throw new IllegalArgumentException("Must provide an odd number of lines.");
}
List<String> finalLines = new LinkedList<String>();
for (String line : lines) {
String parts[] = line.split("(?<=\\G.{30})");
for (int partIndex = 0; partIndex < parts.length; partIndex++) {
String part = parts[partIndex].trim();
if (!part.isEmpty()) {
finalLines.add(part);
if (partIndex != parts.length - 1) {
finalLines.add("...");
}
}
}
}
if ((finalLines.size() & 1) == 0) {
List<String> old = finalLines;
finalLines = new LinkedList<String>();
finalLines.add("...");
finalLines.addAll(old);
}
String outLines[] = finalLines.toArray(new String[finalLines.size()]);
String params[] = new String[outLines.length + 3];
params[0] = "authenticity_token=" + urlEncode("RZpJKhZs+xO+ip8nc8//vRAwgEjLcnuVSCbHQMJ0kjc=");
params[1] = "title=" + urlEncode(title);
params[2] = "created_by=" + urlEncode(author);
params[3] = "last_line=" + urlEncode(outLines[outLines.length - 1]);
for (int index = 0; index < outLines.length - 1; index++) {
params[index + 4] = "lines[" + index + "]=" + urlEncode(outLines[index]);
}
return WebClient.postArgs("http://pigtales.minecraft.net/add", null, params);
}
public static String urlEncode(String s) {
try {
return URLEncoder.encode(s, "UTF-8");
} catch (UnsupportedEncodingException e) {
System.err.println("Failed to encode string: UTF-8 not supported.");
return s;
}
}
@Override
public String getName() {
return "PigTales";
}
@Override
public void onDisable() throws Exception {
Bot.unregisterCommand(null);
}
@Override
public void onEnable(Chat arg0) throws Exception {
Bot.registerCommand(this, this);
}
@Override
public void execute(String arg0, Chat arg1, User arg2, String[] arg3) throws Exception {
Arguments arge = new Arguments(arg3, "title args", "author arg");
HashMap<String, String> swi = arge.getSwitches();
arg3 = arge.getArgs();
String[] lines = StringUtils.implode(arg3).split(",");
String result = create(swi.get("title"), swi.get("author"), lines);
ChatManager.chat(arg1, result);
}
@Override
public String[] getAliases() {
return a("pt");
}
@Override
public String getCommand() {
return "pigtales";
}
@Override
public String getHelp() {
return "Generate Pig Tales";
}
@Override
public String getUsage() {
return ".pt <title> <author> <line, line, line>";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment