Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.stefan1200.jts3servermod.functions;
- import com.maxmind.geoip.LookupService;
- import com.maxmind.geoip.regionName;
- import de.stefan1200.jts3servermod.interfaces.HandleBotEvents;
- import de.stefan1200.jts3servermod.interfaces.HandleTS3Events;
- import de.stefan1200.jts3servermod.interfaces.JTS3ServerMod_Interface;
- import de.stefan1200.jts3serverquery.JTS3ServerQuery;
- import de.stefan1200.jts3serverquery.TS3ServerQueryException;
- import java.io.File;
- import java.io.IOException;
- import java.util.HashMap;
- public class Location
- implements HandleBotEvents, HandleTS3Events
- {
- private boolean enabled = false;
- private JTS3ServerQuery query;
- private JTS3ServerMod_Interface mod;
- private String prefix;
- private static LookupService lookupService;
- static {
- try {
- lookupService = new LookupService(new File("db/GeoLiteCity.dat"), 1);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public String botChatCommandHelp(String arg0) {
- return "Gets the location of a client";
- }
- public String[] botChatCommandList(HashMap<String, String> arg0, boolean isAdmin, boolean isFullAdmin) {
- if (!this.enabled) {
- return null;
- }
- if ((isAdmin) || (isFullAdmin)) {
- return new String[] { "<client name>" };
- }
- return null;
- }
- public boolean handleChatCommands(String name, HashMap<String, String> eventInfo, boolean isAdmin, boolean isFullAdmin) {
- if ((this.enabled) && ((isAdmin) || (isFullAdmin))) {
- try {
- if ((name.equals(null)) || (name.equals(""))) {
- return false;
- }
- sendMsg((String)eventInfo.get("invokerid"), "Working...");
- HashMap<String, String> c = getClients(name);
- if (c.isEmpty()) {
- sendMsg((String)eventInfo.get("invokerid"), "No matches found!");
- }
- else {
- sendMsg((String)eventInfo.get("invokerid"), "Found " + c.size() + " matches!");
- for (String a : c.keySet()) {
- com.maxmind.geoip.Location location = lookupService.getLocation((String)c.get(a));
- if (location != null) {
- location.region = regionName.regionNameByCode(location.countryCode, location.region);
- }
- sendMsg((String)eventInfo.get("invokerid"), a + ":\n" + format(location));
- }
- }
- return true;
- }
- catch (TS3ServerQueryException e) {
- e.printStackTrace();
- }
- }
- return false;
- }
- private void log(String msg) {
- this.mod.addLogEntry(this.prefix, (byte)1, msg, true);
- }
- private String format(com.maxmind.geoip.Location l) {
- return "Country = " + l.countryName + "\nCity = " + l.city + "\nArea Code = " + l.area_code + "\nPostal Code = " + l.postalCode;
- }
- private void sendMsg(String client, String msg) throws TS3ServerQueryException {
- this.query.sendTextMessage(Integer.parseInt(client), 1, msg);
- }
- private HashMap<String, String> getClients(String name) {
- HashMap<String, String> clients = new HashMap<>();
- for (HashMap<String, String> a : this.mod.getClientList()) {
- String n = ((String)a.get("client_nickname")).trim();
- if (n.matches(name.trim())) {
- clients.put(n, (String)a.get("connection_client_ip"));
- }
- }
- return clients;
- }
- public void handleClientEvents(String arg0, HashMap<String, String> arg1) {}
- public void activate() {
- log("Location service now active");
- this.enabled = true;
- }
- public void disable() {
- log("Location service is now disabled");
- this.enabled = false;
- }
- public void handleAfterCacheUpdate() {}
- public void handleOnBotConnect() {}
- public void initClass(JTS3ServerMod_Interface arg0, JTS3ServerQuery arg1, String arg2) {
- this.mod = arg0;
- this.query = arg1;
- this.prefix = arg2;
- }
- public boolean multipleInstances() {
- return false;
- }
- public void unload() {}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement