Advertisement
Guest User

Untitled

a guest
Dec 19th, 2015
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.70 KB | None | 0 0
  1. package de.stefan1200.jts3servermod.functions;
  2.  
  3. import com.maxmind.geoip.LookupService;
  4. import com.maxmind.geoip.regionName;
  5. import de.stefan1200.jts3servermod.interfaces.HandleBotEvents;
  6. import de.stefan1200.jts3servermod.interfaces.HandleTS3Events;
  7. import de.stefan1200.jts3servermod.interfaces.JTS3ServerMod_Interface;
  8. import de.stefan1200.jts3serverquery.JTS3ServerQuery;
  9. import de.stefan1200.jts3serverquery.TS3ServerQueryException;
  10. import java.io.File;
  11. import java.io.IOException;
  12. import java.util.HashMap;
  13.  
  14. public class Location
  15. implements HandleBotEvents, HandleTS3Events
  16. {
  17.     private boolean enabled = false;
  18.     private JTS3ServerQuery query;
  19.     private JTS3ServerMod_Interface mod;
  20.     private String prefix;
  21.     private static LookupService lookupService;
  22.  
  23.     static {
  24.         try {
  25.             lookupService = new LookupService(new File("db/GeoLiteCity.dat"), 1);
  26.         } catch (IOException e) {
  27.             e.printStackTrace();
  28.         }
  29.     }
  30.  
  31.     public String botChatCommandHelp(String arg0) {
  32.         return "Gets the location of a client";
  33.     }
  34.  
  35.     public String[] botChatCommandList(HashMap<String, String> arg0, boolean isAdmin, boolean isFullAdmin) {
  36.         if (!this.enabled) {
  37.             return null;
  38.         }
  39.         if ((isAdmin) || (isFullAdmin)) {
  40.             return new String[] { "<client name>" };
  41.         }
  42.         return null;
  43.     }
  44.  
  45.     public boolean handleChatCommands(String name, HashMap<String, String> eventInfo, boolean isAdmin, boolean isFullAdmin) {
  46.         if ((this.enabled) && ((isAdmin) || (isFullAdmin))) {
  47.             try {
  48.                 if ((name.equals(null)) || (name.equals(""))) {
  49.                     return false;
  50.                 }
  51.                 sendMsg((String)eventInfo.get("invokerid"), "Working...");
  52.                 HashMap<String, String> c = getClients(name);
  53.                 if (c.isEmpty()) {
  54.                     sendMsg((String)eventInfo.get("invokerid"), "No matches found!");
  55.                 }
  56.                 else {
  57.                     sendMsg((String)eventInfo.get("invokerid"), "Found " + c.size() + " matches!");
  58.                     for (String a : c.keySet()) {
  59.                         com.maxmind.geoip.Location location = lookupService.getLocation((String)c.get(a));
  60.                         if (location != null) {
  61.                             location.region = regionName.regionNameByCode(location.countryCode, location.region);
  62.                         }
  63.                         sendMsg((String)eventInfo.get("invokerid"), a + ":\n" + format(location));
  64.                     }
  65.                 }
  66.                 return true;
  67.             }
  68.             catch (TS3ServerQueryException e) {
  69.                 e.printStackTrace();
  70.             }
  71.         }
  72.         return false;
  73.     }
  74.  
  75.     private void log(String msg) {
  76.         this.mod.addLogEntry(this.prefix, (byte)1, msg, true);
  77.     }
  78.  
  79.     private String format(com.maxmind.geoip.Location l) {
  80.         return "Country = " + l.countryName + "\nCity = " + l.city + "\nArea Code = " + l.area_code + "\nPostal Code =  " + l.postalCode;
  81.     }
  82.  
  83.     private void sendMsg(String client, String msg) throws TS3ServerQueryException {
  84.         this.query.sendTextMessage(Integer.parseInt(client), 1, msg);
  85.     }
  86.  
  87.     private HashMap<String, String> getClients(String name) {
  88.         HashMap<String, String> clients = new HashMap<>();
  89.         for (HashMap<String, String> a : this.mod.getClientList()) {
  90.             String n = ((String)a.get("client_nickname")).trim();
  91.             if (n.matches(name.trim())) {
  92.                 clients.put(n, (String)a.get("connection_client_ip"));
  93.             }
  94.         }
  95.         return clients;
  96.     }
  97.  
  98.     public void handleClientEvents(String arg0, HashMap<String, String> arg1) {}
  99.  
  100.     public void activate() {
  101.         log("Location service now active");
  102.         this.enabled = true;
  103.     }
  104.  
  105.     public void disable() {
  106.         log("Location service is now disabled");
  107.         this.enabled = false;
  108.     }
  109.  
  110.     public void handleAfterCacheUpdate() {}
  111.  
  112.     public void handleOnBotConnect() {}
  113.  
  114.     public void initClass(JTS3ServerMod_Interface arg0, JTS3ServerQuery arg1, String arg2) {
  115.         this.mod = arg0;
  116.         this.query = arg1;
  117.         this.prefix = arg2;
  118.     }
  119.  
  120.     public boolean multipleInstances() {
  121.         return false;
  122.     }
  123.  
  124.     public void unload() {}
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement