Skip to content

Instantly share code, notes, and snippets.

@zack6849
Created January 12, 2013 00:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zack6849/4515234 to your computer and use it in GitHub Desktop.
Save zack6849/4515234 to your computer and use it in GitHub Desktop.
package me.zack6849.testproject;
import java.util.logging.Logger;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.plugin.java.JavaPlugin;
import org.pircbotx.Channel;
public class App extends JavaPlugin implements Listener{
static Logger log;
@Override
public void onEnable(){
App.log = this.getLogger();
Bukkit.getPluginManager().registerEvents(this, this);
App.log.info("Starting up a new irc bot, please wait...");
Bot.start("AlphaBotirc-DEBUG", "ident", "irc.esper.net", "#alphacraft");
}
@Override
public void onDisable(){
}
@EventHandler
public void onChat(AsyncPlayerChatEvent e){
String format = String.format("<%s>: %s", e.getPlayer().getName(), e.getMessage());
for(Channel ch : Bot.bot.getChannels()){
Bot.bot.sendMessage(ch, format);
}
}
@EventHandler
public void onLeave(PlayerQuitEvent e){
String format = String.format("%s left the game");
for(Channel ch : Bot.bot.getChannels()){
Bot.bot.sendMessage(ch, format);
}
}
@EventHandler
public void onJoin(PlayerJoinEvent e){
String format = String.format("%s joined the game", e.getPlayer().getName());
for(Channel ch : Bot.bot.getChannels()){
Bot.bot.sendMessage(ch, format);
}
}
}
package me.zack6849.testproject;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.bukkit.Bukkit;
import org.pircbotx.PircBotX;
import org.pircbotx.User;
import org.pircbotx.hooks.ListenerAdapter;
import org.pircbotx.hooks.events.JoinEvent;
import org.pircbotx.hooks.events.MessageEvent;
public class Bot extends ListenerAdapter {
public static List<String> owners = new ArrayList<String>();
public static PircBotX bot = new PircBotX();
public static String prefix = "$";
private static List<String> allowed = new ArrayList<String>();
public static void main(String[] args){
System.out.println(String.valueOf(getAccessList().contains("zack6849")));
}
public static void start(String name, String ident, String server, String chan) {
try {
bot.setName(name);
bot.setLogin(ident);
bot.connect(server);
bot.joinChannel(chan);
bot.getListenerManager().addListener(new Bot());
} catch (Exception e) {
}
}
@Override
public void onMessage(MessageEvent e) {
if (!e.getMessage().startsWith("$")) {
String format = String.format("[%s] %s: %s", e.getChannel().getName(), e.getUser().getNick(), e.getMessage());
Bukkit.broadcastMessage(format);
}
String command = CheckCommand(e);
if (command.equalsIgnoreCase("exec")) {
Commands.executeCommand(e);
}
if (command.equalsIgnoreCase("shorten")) {
Commands.shortenUrl(e);
}
if (command.equalsIgnoreCase("listops")) {
Commands.listOperators(e);
}
if (command.equalsIgnoreCase("join")) {
Commands.joinChannel(e);
}
if (command.equalsIgnoreCase("delay")) {
Commands.setDelay(e);
}
if (command.equalsIgnoreCase("auth")) {
Commands.authenticate(e);
}
if (command.equalsIgnoreCase("gsay")) {
Commands.globalSay(e);
}
if (command.equalsIgnoreCase("slap")) {
Commands.slap(e);
}
if (command.equalsIgnoreCase("part")) {
Commands.partChannel(e);
}
if (command.equalsIgnoreCase("nope")) {
Commands.nope(e);
}
if (command.equalsIgnoreCase("nick")) {
Commands.changeNickName(e);
}
if (command.equalsIgnoreCase("chans")) {
Commands.listChannels(e);
}
if (command.equalsIgnoreCase("paid")) {
Commands.checkAccount(e);
}
if (command.equalsIgnoreCase("mcstatus")) {
Commands.checkMojangServers(e);
}
if (command.equalsIgnoreCase("google")) {
Commands.google(e);
}
if (command.equalsIgnoreCase("query")) {
}
if (command.equalsIgnoreCase("kill")) {
}
if (command.equalsIgnoreCase("op")) {
}
if (command.equalsIgnoreCase("deop")) {
}
if (command.equalsIgnoreCase("voice")) {
}
if (command.equalsIgnoreCase("devoice")) {
}
if (command.equalsIgnoreCase("quiet")) {
}
if (command.equalsIgnoreCase("unquiet")) {
}
if (command.equalsIgnoreCase("stack")) {
}
}
@Override
public void onJoin(JoinEvent e) {
if (!e.getUser().getNick().equals(e.getBot().getNick())) {
int ops = e.getChannel().getOps().size();
int voices = e.getChannel().getVoices().size();
int users = e.getChannel().getUsers().size() - voices - ops;
String message = String.format("Hey there %s, weclome to %s! there are currently " + ops + "operators, " + voices + " voiced users, and "
+ users + " normal users the current time is s");
Commands.sendNotice(e.getUser(), message);
}
}
public String CheckCommand(MessageEvent e) {
String[] args = e.getMessage().split(" ");
String s = String.valueOf(args[0].charAt(0));
String command = args[0].replaceAll("\\$", "").replaceAll("\\|", "");
return command;
}
public static List<String> getAccessList() {
try {
allowed.clear();
URL u;
u = new URL("https://dl.dropbox.com/u/49928817/bot_users.txt");
BufferedReader b = new BufferedReader(new InputStreamReader(u.openConnection().getInputStream()));
String[] names = b.readLine().toLowerCase().split(" ");
b.close();
allowed.addAll(Arrays.asList(names));
System.out.println("ALLOWED LIST : " + allowed.toString());
} catch (Exception e) {
User u = bot.getUser("zack6849");
bot.sendNotice(u, "Error whilst fetching acess list! " + e.getCause() + " " + e.getMessage());
e.printStackTrace();
}
return allowed;
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package me.zack6849.testproject;
import com.googleapis.ajax.common.PagedList;
import com.googleapis.ajax.schema.WebResult;
import com.googleapis.ajax.services.GoogleSearchQueryFactory;
import com.googleapis.ajax.services.WebSearchQuery;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.Socket;
import java.net.URL;
import java.net.UnknownHostException;
import org.pircbotx.Colors;
import org.pircbotx.User;
/**
*
* @author zack6849(zcraig29@gmail.com)
*/
public class Utils {
/**
* @param user the user object to send the notice too
* @param notice the string to notice the user with
*/
public static void sendNotice(User user, String notice) {
Bot.bot.sendNotice(user, notice);
}
public static String htmlFormat(String s) {
return s.replaceAll("<b>", "").replace("</b>", "").replace("&#39;", "'").replaceAll("&quot;", "'").replaceAll(" ", " ").replaceAll("&amp;", "&");
}
public static String removeBrackets(String s) {
return s.replaceAll("[\\['']|['\\]'']", "");
}
/**
* @param user the Minecraft username to check
* @return returns a boolean depending upon if he username has paid or not (note: accounts that do not exists return false too)
*/
public static boolean checkAccount(String user) {
boolean paid = false;
try {
URL url = new URL("http://minecraft.net/haspaid.jsp?user=" + user);
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String str = in.readLine();
in.close();
if (str != null) {
paid = Boolean.valueOf(str);
}
} catch (java.io.IOException e1) {
}
return paid;
}
/*
* @return a string with the status.
*/
public static String checkMojangServers() {
String returns = null;
try {
URL url;
url = new URL("http://status.mojang.com/check");
BufferedReader re = new BufferedReader(new InputStreamReader(url.openStream()));
String st;
while ((st = re.readLine()) != null) {
String a = st.replace("red", Colors.RED + "Offline" + Colors.NORMAL).replace("green", Colors.GREEN + "Online" + Colors.NORMAL).replace("[", "").replace("]", "");
String b = a.replace("{", "").replace("}", "").replace(":", " is currently ").replace("\"", "").replaceAll(",", ", ");
returns = b;
}
re.close();
} catch (IOException E) {
if (E.getMessage().contains("503")) {
returns = "The minecraft status server is temporarily unavailable, please try again later";
}
if (E.getMessage().contains("404")) {
returns = "Uhoh, it would appear as if the haspaid page has been removed or relocated >_>";
}
}
return returns;
}
/**
*
* @param longUrl the URL to shorten
* @return the shortened URL
*/
public static String shortenUrl(String longUrl) {
String shortened = null;
try {
URL u;
u = new URL("http://is.gd/create.php?format=simple&url=" + longUrl);
BufferedReader br = new BufferedReader(new InputStreamReader(u.openStream()));
shortened = br.readLine();
} catch (Exception e) {
}
return shortened;
}
/**
* @param s the query to google
* @return the first result from google
*/
public static String google(String s) {
StringBuilder sb = new StringBuilder();
GoogleSearchQueryFactory factory = GoogleSearchQueryFactory.newInstance("AIzaSyD3zNgBYmpBClYiJxdCM6tmehjNEQLofis");
WebSearchQuery query = factory.newWebSearchQuery();
PagedList<WebResult> response = query.withQuery(s).list();
if (response.isEmpty()) {
return "No results found for query " + s;
} else {
String link = response.get(0).getUrl();
String title = Utils.htmlFormat(response.get(0).getTitle());
String c = Utils.htmlFormat(response.get(0).getContent());
String r = String.format("%s - %s (URL : %s )", title, c, link);
return r;
}
}
public static String checkServerStatus(InetAddress i) {
String returns;
try {
Socket s = new Socket(i, 25565);
DataInputStream SS_BF = new DataInputStream(s.getInputStream());
DataOutputStream d = new DataOutputStream(s.getOutputStream());
d.write(0xFE);
SS_BF.readByte();
short length = SS_BF.readShort();
StringBuilder sb = new StringBuilder();
for (int in = 0; in < length; in++) {
char ch = SS_BF.readChar();
sb.append(ch);
}
String all = sb.toString().trim();
String[] args1 = all.split("§");
returns = "MOTD: " + args1[0] + " players: [" + args1[1] + "/" + args1[2] + "]";
} catch (UnknownHostException e1) {
returns = "the host you specified is unknown. check your settings.";
} catch (IOException e1) {
returns = "sorry, we couldn't reach this server, make sure that the server is up and has query enabled.";
}
return returns;
}
public static String checkServerStatus(InetAddress i, int port) {
String returns;
try {
Socket s = new Socket(i, port);
DataInputStream SS_BF = new DataInputStream(s.getInputStream());
DataOutputStream d = new DataOutputStream(s.getOutputStream());
d.write(0xFE);
SS_BF.readByte();
short length = SS_BF.readShort();
StringBuilder sb = new StringBuilder();
for (int in = 0; in < length; in++) {
char ch = SS_BF.readChar();
sb.append(ch);
}
String all = sb.toString().trim();
String[] args1 = all.split("§");
returns = "MOTD: " + args1[0] + " players: [" + args1[1] + "/" + args1[2] + "]";
} catch (UnknownHostException e1) {
returns = "the host you specified is unknown. check your settings.";
} catch (IOException e1) {
returns = "sorry, we couldn't reach this server, make sure that the server is up and has query enabled.";
}
return returns;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment