Advertisement
Guest User

Untitled

a guest
May 9th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.35 KB | None | 0 0
  1. package server;
  2.  
  3. import server.npcs.*;
  4. import server.players.*;
  5. import server.items.*;
  6. import server.util.*;
  7. import server.world.*;
  8. import server.players.Packets.*;
  9. import server.players.MiscHandlers.*;
  10.  
  11. import java.io.BufferedReader;
  12. import java.io.DataInputStream;
  13. import java.io.File;
  14. import java.io.FileInputStream;
  15. import java.io.FileReader;
  16. import java.io.IOException;
  17. import java.net.URL;
  18. import java.net.URLConnection;
  19. import java.util.ArrayList;
  20. import java.util.Properties;
  21. import java.util.*;
  22. import java.sql.*;
  23.  
  24. public class server implements Runnable {
  25.     public static Connection connection = null;
  26.     public server() {
  27.             try {
  28.             // Load the JDBC driver
  29.             String driverName = "org.gjt.mm.mysql.Driver"; // MySQL MM JDBC driver
  30.             Class.forName(driverName);
  31.            
  32. // Create a connection to the database
  33.             String serverName = "Fallout";
  34.             String mydatabase = "Fallout"; // besure to change to your server name
  35.             String url = "jdbc:mysql://localhost:3306/" + mydatabase; // if 127.0.0.1 dont work use localhost
  36.             String username = "root"; // Your phpmyadmin username (default = root)
  37.             String password = "shmexzi"; // Your phpmyadmin password
  38.             connection = DriverManager.getConnection(url, username, password);
  39.             misc.println("Mysql Connected");
  40.         } catch (ClassNotFoundException e) {
  41.             misc.println("Class Not Found");
  42.     killServer();
  43.         } catch (SQLException e) {
  44.             misc.println("Sql Error");
  45.     killServer();
  46.        }
  47.     }
  48.     /* THIS IS THE EXPERIENCE MULTIPLER */
  49.     public static textHandler textHandler = new textHandler();
  50.     public static int uptime;
  51.     public static int SERVER_EXPERIENCE = 20;
  52.     public static npcsdrops npcsdrops = new npcsdrops();
  53.     public static final boolean lockyell = false;
  54.     public static PrayerHandler PrayerHandler = null;
  55.     public static WorldObject worldObject = new WorldObject();
  56.     public static lvlHandler lvlHandler = new lvlHandler();
  57.     public static NpcAnimHandler NpcAnimHandler = new NpcAnimHandler();
  58.     public static ArrayList<String> banned = new ArrayList<String>();
  59.     public static WeaponHandler WeaponHandler = null;
  60.     public static npcController npcController = null;
  61.     public static ArrayList<Integer> bannedUid = new ArrayList<Integer>();
  62.     public static server clientHandler = null; // handles all the clients
  63.     public static java.net.ServerSocket clientListener = null;
  64.     public static int MaxConnections = 100000;
  65.     public static int[] ConnectionCount = new int[MaxConnections];
  66.     public static ArrayList<String> connections = new ArrayList<String>();
  67.     public static String[] Connections = new String[MaxConnections];
  68.     public static final int cycleTime = 500;
  69.     public static int delay = 500;
  70.     public static long delayUpdate = 0, lastRunite = 0;
  71.     public static int EnergyRegian = 60;
  72.     public static boolean enforceClient = false;
  73.     public static GraphicsHandler GraphicsHandler = null;
  74.     public static ItemHandler itemHandler = null;
  75.     public static boolean loginServerConnected = true;
  76.     public static NPCHandler npcHandler = null;
  77.     public static PlayerHandler playerHandler = null;
  78.     public static int[][] runesRequired = new int[24][9];
  79.     public static int serverlistenerPort = Constants.SERVER_PORT; // 43594=default
  80.     public static ShopHandler shopHandler = null;
  81.     public static boolean ShutDown = false;
  82.     public static boolean shutdownClientHandler; // signals ClientHandler to shut
  83.     // down
  84.     public static int ShutDownCounter = 0;
  85.     public static boolean shutdownServer = false; // set this to true in order to
  86.     // shut down and kill the server
  87.     public static long startTime;
  88.     // TODO: yet to figure out proper value for timing, but 500 seems good
  89.     public static boolean trading = true, dueling = true, pking = true;
  90.     public static int updateSeconds = 180; // 180 because it doesnt make the
  91.     // time jump at the start :P
  92.     public static List<Event> events = new ArrayList<Event>();
  93.     public static List<Event> eventsToAdd = new ArrayList<Event>();
  94.     public static List<Event> eventsToRemove = new ArrayList<Event>();
  95.  
  96.     public static void registerEvent(Event event) {
  97.         eventsToAdd.add(event);
  98.     }
  99.  
  100.     public static void deregisterEvent(Event event) {
  101.         eventsToRemove.add(event);
  102.     }
  103.  
  104.     public static void startEvents()
  105.     {
  106.     }
  107.  
  108.     public static void processEvents() {
  109.         for(Event e : eventsToAdd) {
  110.             events.add(e);
  111.         }
  112.         eventsToAdd.clear();
  113.         for(Event e : events) {
  114.             if(e.isStopped()) {
  115.                 eventsToRemove.add(e);
  116.             } else if(e.isReady()) {
  117.                 e.run();
  118.             }
  119.         }
  120.         for(Event e : eventsToRemove) {
  121.             events.remove(e);
  122.         }
  123.         eventsToRemove.clear();
  124.     }
  125.     public static boolean updateServer = false;
  126.  
  127.     public static int world = 1;
  128.  
  129.     public static void calcTime() {
  130.         long curTime = System.currentTimeMillis();
  131.         updateSeconds = 180 - ((int) (curTime - startTime) / 1000);
  132.         if (updateSeconds == 0) {
  133.             shutdownServer = true;
  134.         }
  135.     }
  136.    
  137.     public static void logError(String message) {
  138.         misc.println(message);
  139.     }
  140.  
  141.     public static void main(java.lang.String args[])
  142.             throws NullPointerException {
  143.         clientHandler = new server();
  144.         (new Thread(clientHandler)).start();
  145.         startEvents();
  146.         processEvents();
  147.         playerHandler = new PlayerHandler();
  148.         npcHandler = new NPCHandler();
  149.         itemHandler = new ItemHandler();
  150.         shopHandler = new ShopHandler();
  151.         PrayerHandler = new PrayerHandler();
  152.         GraphicsHandler = new GraphicsHandler();
  153.         process proc = new process();
  154.         new Thread(proc).start();
  155.     }
  156.  
  157.  
  158.     public static void openPage(String pageName) {
  159.         try {
  160.             URL page = new URL(pageName);
  161.             URLConnection conn = page.openConnection();
  162.             DataInputStream in = new DataInputStream(conn.getInputStream());
  163.             String source, pageSource = "";
  164.             while ((source = in.readLine()) != null) {
  165.                 pageSource += source;
  166.             }
  167.         } catch (Exception e) {
  168.             e.printStackTrace();
  169.             return;
  170.         }
  171.     }
  172.  
  173.     public int[] ips = new int[1000];
  174.  
  175.     public long[] lastConnect = new long[1000];
  176.     public boolean checkHost(String host) {
  177.         for (String h : banned) {
  178.             if (h.equals(host))
  179.                 return false;
  180.         }
  181.         int num = 0;
  182.         for (String h : connections) {
  183.             if (host.equals(h)) {
  184.                 num++;
  185.             }
  186.         }
  187.  
  188.         if (checkLog("ipbans", host)) {
  189.             return false; // ip ban added by bakatool
  190.         }
  191.         return true;
  192.     }
  193.  
  194.     public boolean checkLog(String file, String playerName) {
  195.         // check ipbans -bakatool
  196.         try {
  197.             BufferedReader in = new BufferedReader(new FileReader("./bin/config/"
  198.                     + file + ".txt"));
  199.             String data = null;
  200.             while ((data = in.readLine()) != null) {
  201.                 if (playerName.equalsIgnoreCase(data)) {
  202.                     return true;
  203.                 }
  204.             }
  205.         } catch (IOException e) {
  206.             System.out.println("Critical error while checking for data!");
  207.             System.out.println(file + ":" + playerName);
  208.             e.printStackTrace();
  209.         }
  210.         return false;
  211.     }
  212.  
  213.     public int getConnections(String host) {
  214.         int count = 0;
  215.         for (Player p : PlayerHandler.players) {
  216.             if ((p != null) && !p.disconnected
  217.                     && p.connectedFrom.equalsIgnoreCase(host)) {
  218.                 count++;
  219.             }
  220.         }
  221.         return count;
  222.     }
  223.  
  224.     public void killServer() {
  225.         try {
  226.             shutdownClientHandler = true;
  227.             if (clientListener != null)
  228.                 clientListener.close();
  229.             clientListener = null;
  230.         } catch (java.lang.Exception __ex) {
  231.             __ex.printStackTrace();
  232.         }
  233.     }
  234.  
  235.     public void run() {
  236.         try {
  237.             shutdownClientHandler = false;
  238.             clientListener = new java.net.ServerSocket();
  239.             clientListener.bind(new java.net.InetSocketAddress(java.net.InetAddress.getByName("0.0.0.0"), serverlistenerPort), 250);
  240.             while(true) {
  241.                 java.net.Socket s = clientListener.accept();
  242.                 String connectingHost = s.getInetAddress().getHostName();
  243.                 if(true) {
  244.                     int Found = -1;
  245.                     for (int i = 0; i < MaxConnections; i++) {
  246.                         if (Connections[i] == connectingHost) {
  247.                             Found = ConnectionCount[i];
  248.                             break;
  249.                         }
  250.                     }
  251.                     if (checkHost(connectingHost)) {
  252.                         playerHandler.newPlayerClient(s, connectingHost);
  253.                     } else {
  254.                         s.close();
  255.                     }
  256.                 } else {
  257.                     misc.println("ClientHandler: Rejected "+connectingHost+":"+s.getPort());
  258.                     s.close();
  259.                 }
  260.             }
  261.         } catch(java.io.IOException ioe) {
  262.             if(!shutdownClientHandler) {
  263.                 misc.println("Error: Unable to startup listener on "+serverlistenerPort+" - port already in use?");
  264.             } else {
  265.                 misc.println("ClientHandler was shut down.");
  266.             }
  267.         }
  268.     }
  269. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement