Java tutorial
/** * This file is part of Barry, licensed under the MIT License (MIT). * * Copyright (c) 2014 Henry Slawniak <http://mcme.co/> * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ package co.mcme.barry.listeners; import co.mcme.barry.BarryPlugin; import co.mcme.barry.UrlWatcher; import co.mcme.barry.YoutubeVideo; import co.mcme.barry.util.Util; import ec.util.MersenneTwisterFast; import java.net.URI; import java.net.URISyntaxException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import javax.script.ScriptException; import org.apache.commons.lang.StringUtils; import org.bukkit.ChatColor; import org.bukkit.Server; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.player.AsyncPlayerChatEvent; public class ChatListener implements Listener { private static Server s; private static BarryPlugin b; private static final Pattern url = Pattern .compile("^(\u00A7.)*?((?:(https?)://)?([-\\w_\\.]{2,}\\.[a-z]{2,4})(/\\S*?)?)(\u00A7.)*?$"); private static final Pattern die = Pattern.compile("^(\\d+)(d)(\\d+)$"); private static final MersenneTwisterFast twister = new MersenneTwisterFast(); public ChatListener(Server server, BarryPlugin inst) { s = server; b = inst; } @EventHandler(priority = EventPriority.MONITOR) public void onYTChat(AsyncPlayerChatEvent event) { String message = event.getMessage(); for (String str : message.split(" ")) { Matcher matcher = url.matcher(str); if (matcher.matches()) { try { URI uri = new URI(str); if (uri.getHost().contains("youtube.com")) { if (uri.getQuery().contains("v=")) { String id = uri.getQuery().replaceAll("v=", ""); String daturl = "http://gdata.youtube.com/feeds/api/videos/" + id + "?alt=json"; YoutubeVideo video = UrlWatcher.getYoutubeInfo(daturl); BarryPlugin.sendGlobalMessage(video.getMessageForTitle()); BarryPlugin.sendGlobalMessage(video.getMessageForDurationLinker(event.getPlayer())); } } else if (uri.getHost().contains("youtu.be")) { String[] split = str.split(".be/"); String id = split[1]; String daturl = "http://gdata.youtube.com/feeds/api/videos/" + id + "?alt=json"; YoutubeVideo video = UrlWatcher.getYoutubeInfo(daturl); BarryPlugin.sendGlobalMessage(video.getMessageForTitle()); BarryPlugin.sendGlobalMessage(video.getMessageForDurationLinker(event.getPlayer())); } } catch (URISyntaxException ex) { } } } } @EventHandler(priority = EventPriority.MONITOR) public void onMathChat(AsyncPlayerChatEvent event) { if (event.getMessage().contains(".math")) { Player p = event.getPlayer(); ScriptEngineManager mgr = new ScriptEngineManager(); ScriptEngine engine = mgr.getEngineByName("JavaScript"); String[] msg = event.getMessage().split(" "); if (msg.length > 1) { try { List<String> list = new ArrayList<>(Arrays.asList(msg)); list.removeAll(Arrays.asList("", null, ".math")); String expression = StringUtils.join(list, " "); BarryPlugin.sendGlobalMessage("(" + ChatColor.DARK_AQUA + p.getName() + ChatColor.WHITE + ") " + expression + " = " + engine.eval(expression)); } catch (ScriptException ex) { b.sendTargetedMessage("I can't figure out that math problem.", p); } } else { b.sendTargetedMessage("I need to know what math to do", p); } } } @EventHandler(priority = EventPriority.MONITOR) public void onRollChat(AsyncPlayerChatEvent event) { if (event.getMessage().contains(".roll")) { String message = event.getMessage(); for (String str : message.split(" ")) { Matcher matcher = die.matcher(str); if (matcher.matches()) { String[] explode = str.split("d"); int numdice = Integer.valueOf(explode[0]); if (numdice > 50) { numdice = 50; } int numsides = Integer.valueOf(explode[1]); Util.info("Rolling " + numdice + " d" + numsides + " for " + event.getPlayer().getName()); if (numdice > 1) { ArrayList<Integer> results = getMultiDiceRoll(numsides, numdice); int result = 0; StringBuilder out = new StringBuilder(); boolean first = true; for (int i : results) { if (first) { first = false; } else { out.append(ChatColor.YELLOW).append("+").append(ChatColor.AQUA); } out.append("(").append(i).append(")"); result += i; } BarryPlugin.sendGlobalMessage( ChatColor.AQUA + event.getPlayer().getName() + " rolled " + result); BarryPlugin.sendGlobalMessage(ChatColor.AQUA + out.toString()); } else { int roll = getDiceRoll(numsides, numdice); BarryPlugin.sendGlobalMessage( ChatColor.AQUA + event.getPlayer().getName() + " rolled " + roll); } } } } } private ArrayList<Integer> getMultiDiceRoll(int numsides, int numdice) { int current = 1; ArrayList<Integer> results = new ArrayList(); while (current <= numdice) { results.add(1 + twister.nextInt(numsides)); current++; } return results; } private int getDiceRoll(int numsides, int numdice) { return 1 + twister.nextInt(numsides); } }