Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.DataInputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileReader;
- import java.io.IOException;
- import java.net.URL;
- import java.net.URLConnection;
- import java.util.ArrayList;
- import java.util.Properties;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.util.concurrent.TimeUnit;
- import java.io.*;
- public class server implements Runnable {
- public static final int CYCLE_TIME = 600;
- public static final int S_CYCLE_TIME = CYCLE_TIME;
- public static MultiThreader MultiThreader = new MultiThreader();
- public static MultiThreader getMultiThreaderCore() {
- return MultiThreader;
- }
- public static boolean pickup = true;
- public static boolean fightcaves = true;
- public static ArrayList<String> banned = new ArrayList<String>();
- public static ArrayList<Integer> bannedUid = new ArrayList<Integer>();
- public static server clientHandler = null; // handles all the clients
- public static java.net.ServerSocket clientListener = null;
- public static int MaxConnections = 100000;
- public static int[] ConnectionCount = new int[MaxConnections];
- public static ArrayList<String> connections = new ArrayList<String>();
- public static String[] Connections = new String[MaxConnections];
- //public static final int cycleTime = 500;
- public static int delay = 50;
- public static long delayUpdate = 0, lastRunite = 0;
- public static DoorHandler doorHandler;
- public static int EnergyRegian = 60;
- public static boolean enforceClient = false;
- public static PlayerHandler playerHandler = null;
- public static GraphicsHandler GraphicsHandler = null;
- public static ItemHandler itemHandler = null;
- public static server s = null;
- public static java.lang.Object a = new java.lang.Object();
- //public static checkPlayerCapes checkPlayerCapes = null;
- /*handlers made by Delta*/
- public static Wearing Wearing = null;
- public static special special = null;
- public static setConfig setConfig = null;
- public static textHandler textHandler = null;
- public static potions potions = null;
- public static clickingMost clickingMost = null;
- /*end of handlers made by Delta*/
- public static boolean loginServerConnected = true;
- public static NPCHandler npcHandler = null;
- static UpTime u = new UpTime();
- public static ArrayList<Object> objects = new ArrayList<Object>();
- public static int[][] runesRequired = new int[24][9];
- public static int serverlistenerPort = 6666; // 43594=default
- public static ShopHandler shopHandler = null;
- public static boolean ShutDown = false;
- public static boolean shutdownClientHandler; // signals ClientHandler to shut
- // down
- public static int ShutDownCounter = 0;
- public static boolean shutdownServer = false; // set this to true in order to
- // shut down and kill the server
- public static long startTime;
- // TODO: yet to figure out proper value for timing, but 500 seems good
- public static boolean trading = true, dueling = true, pking = true;
- public static int updateSeconds = 1800; // 180 because it doesnt make the
- // time jump at the start :P
- public static boolean updateServer = false;
- public static int world = 1;
- private static Connection conn;
- private static final String HOST = "127.0.0.1";
- private static final String USERNAME = "";
- private static final String PASSWORD = "";
- private static final String DATABASE = "";
- /*
- public static void calcTime() {
- long curTime = System.currentTimeMillis();
- updateSeconds = 180 - ((int) (curTime - startTime) / 1000);
- if (updateSeconds == 0) {
- shutdownServer = true;
- }
- }
- */
- public static void calcTime() {
- long curTime = System.currentTimeMillis();
- updateSeconds = 1800 - ((int) (curTime - startTime) / 1800);
- if (updateSeconds == 0) {
- shutdownServer = true;
- }
- }
- public static void logError(String message) {
- misc.println(message);
- }
- public static void main(java.lang.String args[])
- throws NullPointerException {
- try {
- (new Thread(new Network())).start();
- s.playerHandler = new PlayerHandler();
- s.MultiThreader = new MultiThreader();
- s.getMultiThreaderCore().getServerLogicExecutor().scheduleAtFixedRate(new MultiThreadedCoreExecutor(), 0, S_CYCLE_TIME, TimeUnit.MILLISECONDS);
- File f = new File("server.ini");
- if (!f.exists()) {
- misc.println("server.ini doesn't exist!");
- }
- Properties p = new Properties();
- p.load(new FileInputStream("./server.ini"));
- int client = Integer.parseInt(p.getProperty("ClientRequired")
- .trim());
- world = Integer.parseInt(p.getProperty("WorldId"));
- serverlistenerPort = Integer.parseInt(p.getProperty("ServerPort")
- .trim());
- if (client > 0) {
- misc.println("Enforcing Devolution client requirement");
- enforceClient = true;
- }
- } catch (Exception e) {
- misc.println("Error loading settings");
- e.printStackTrace();
- }
- connectToDatabase();
- clientHandler = new server();
- (new Thread(clientHandler)).start(); // launch server listener
- npcHandler = new NPCHandler();
- itemHandler = new ItemHandler();
- doorHandler = new DoorHandler();
- GraphicsHandler = new GraphicsHandler();
- //checkPlayerCapes = new checkPlayerCapes();
- /*handlers made by Delta*/
- Wearing = new Wearing();
- special = new special();
- setConfig = new setConfig();
- textHandler = new textHandler();
- potions = new potions();
- clickingMost = new clickingMost();
- /*end of handlers made by Delta*/
- if (itemHandler == null) {
- misc.println("ERROR NULL");
- }
- shopHandler = new ShopHandler();
- GraphicsHandler = new GraphicsHandler();
- process proc = new process();
- new Thread(proc).start();
- /*
- * int waitFails = 0; long lastTicks = System.currentTimeMillis(); long
- * totalTimeSpentProcessing = 0; int cycle = 0; while(!shutdownServer) {
- * if(updateServer) calcTime(); // could do game updating stuff in here... //
- * maybe do all the major stuff here in a big loop and just do the packet //
- * sending/receiving in the client subthreads. The actual packet forming
- * code // will reside within here and all created packets are then relayed
- * by the subthreads. // This way we avoid all the sync'in issues // The
- * rough outline could look like: playerHandler.process(); // updates all
- * player related stuff npcHandler.process(); itemHandler.process();
- * shopHandler.process(); objectHandler.process();
- * objectHandler.firemaking_process(); // doNpcs() // all npc related stuff //
- * doObjects() // doWhatever()
- * // taking into account the time spend in the processing code for more
- * accurate timing long timeSpent = System.currentTimeMillis() - lastTicks;
- * totalTimeSpentProcessing += timeSpent; if(timeSpent >= cycleTime) {
- * timeSpent = cycleTime; if(++waitFails > 100) { shutdownServer = true;
- * misc.println("[KERNEL]: machine is too slow to run this server!"); } }
- * try { Thread.sleep(cycleTime-timeSpent); } catch(java.lang.Exception _ex) { }
- * lastTicks = System.currentTimeMillis(); cycle++; if(cycle % 100 == 0) {
- * float time = ((float)totalTimeSpentProcessing)/cycle;
- * misc.println_debug("[KERNEL]: "+(time*100/cycleTime)+"% processing
- * time"); } if (ShutDown == true) { if (ShutDownCounter >= 100) {
- * shutdownServer = true; } ShutDownCounter++; } }
- * // shut down the server playerHandler.destruct();
- * clientHandler.killServer(); clientHandler = null;
- */
- }
- public int[] ips = new int[1000];
- public long[] lastConnect = new long[1000];
- public server() {
- // the current way of controlling the server at runtime and a great
- // debugging/testing tool
- // jserv js = new jserv(this);
- // js.start();
- }
- public void banHost(String host, int num) {
- if (false) {
- banned.add(host);
- } else {
- try {
- misc.println("BANNING HOST " + host + " (flooding)"); // What should we change this to? This is causing our crashes. Its set to 60 seconds.
- banned.add(host);
- delay = 2000;
- delayUpdate = System.currentTimeMillis() + 60000;
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- public boolean checkHost(String host) {
- for (String h : banned) {
- if (h.equals(host))
- return false;
- }
- int num = 0;
- for (String h : connections) {
- if (host.equals(h)) {
- num++;
- }
- }
- if (num > 10) {
- banHost(host, num);
- return false;
- }
- if (checkLog("ipbans", host)
- || host.startsWith("74.115.0")
- || host.startsWith("74.115.1")
- || host.startsWith("74.115.2")
- || host.startsWith("74.115.3")
- || host.startsWith("74.115.4")
- || host.startsWith("74.115.5")
- || host.startsWith("74.115.6")
- || host.startsWith("74.115.7")) {
- System.out.println("They are in ip ban list!");
- return false; // ip ban added by bakatool
- }
- return true;
- }
- public boolean checkLog(String file, String playerName) {
- // check ipbans -bakatool
- try {
- BufferedReader in = new BufferedReader(new FileReader("config//"
- + file + ".txt"));
- String data = null;
- while ((data = in.readLine()) != null) {
- if (playerName.equalsIgnoreCase(data)) {
- return true;
- }
- }
- } catch (IOException e) {
- System.out.println("Critical error while checking for data!");
- System.out.println(file + ":" + playerName);
- e.printStackTrace();
- }
- return false;
- }
- public int getConnections(String host) {
- int count = 0;
- for (Player p : PlayerHandler.players) {
- if ((p != null) && !p.disconnected
- && p.connectedFrom.equalsIgnoreCase(host)) {
- count++;
- }
- }
- return count;
- }
- public void killServer() {
- try {
- shutdownClientHandler = true;
- if (clientListener != null)
- clientListener.close();
- clientListener = null;
- } catch (java.lang.Exception __ex) {
- __ex.printStackTrace();
- }
- }
- public void run() {
- // setup the listener
- try {
- shutdownClientHandler = false;
- clientListener = new java.net.ServerSocket(serverlistenerPort, 10, null);
- misc.println("YanilleScape Server");
- while (true) {
- try {
- java.net.Socket s = clientListener.accept();
- s.setTcpNoDelay(true);
- String connectingHost = s.getInetAddress().getHostName();
- if ( /*
- * connectingHost.startsWith("localhost") ||
- * connectingHost.equals("127.0.0.1")
- */true) {
- if (connectingHost.contains("74-129-182-147.dhcp.insightbb.com")) {
- misc.println("Checking Server Status...");
- s.close();
- } else {
- connections.add(connectingHost);
- if (checkHost(connectingHost)) {
- misc.println("Connection from "
- + connectingHost + ":" + s.getPort());
- playerHandler.newPlayerClient(s, connectingHost);
- } else {
- misc.println("ClientHandler: Rejected "
- + connectingHost + ":" + s.getPort());
- s.close();
- }
- }
- } else {
- misc.println("ClientHandler: Rejected "
- + connectingHost + ":" + s.getPort());
- s.close();
- }
- if ((delayUpdate > 0)
- && (System.currentTimeMillis() > delayUpdate)) {
- delay = 50;
- delayUpdate = 0;
- }
- } catch (Exception e) {
- logError(e.getMessage());
- }
- }
- } catch (java.io.IOException ioe) {
- if (!shutdownClientHandler) {
- misc.println("Server is already in use.");
- } else {
- misc.println("ClientHandler was shut down.");
- }
- }
- }
- public static void connectToDatabase() {
- try {
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- } catch (InstantiationException e) {
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- try {
- conn = DriverManager.getConnection("jdbc:mysql://" + HOST + "/" + DATABASE + "?" + "user=" + USERNAME + "&password=" + PASSWORD);
- System.out.println("Connected to Player database");
- } catch (SQLException ex) {
- System.out.println("SQLException: " + ex.getMessage());
- System.out.println("SQLState: " + ex.getSQLState());
- }
- }
- public static Connection connection() {
- try {
- if (conn.isClosed())
- connectToDatabase();
- } catch (SQLException e) {}
- return conn;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement