/** Starts the minute counter */ private void startMinutesCounter() { try { BufferedReader minuteFile = new BufferedReader(new FileReader("./data/minutes.log")); Server.minutesCounter = Integer.parseInt(minuteFile.readLine()); minuteFile.close(); // CLOSEFILE } catch (Exception e) { e.printStackTrace(); } World.submit( new Tick(25) { @Override public void execute() { setMinutesCounter(getMinutesCounter() + 1); for (Player player : World.getPlayers()) { if (player == null) { continue; } player.getAllotment().doCalculations(); player.getFlowers().doCalculations(); player.getHerbs().doCalculations(); player.getHops().doCalculations(); player.getBushes().doCalculations(); player.getTrees().doCalculations(); player.getFruitTrees().doCalculations(); player.getSpecialPlantOne().doCalculations(); player.getSpecialPlantTwo().doCalculations(); // lowering all player items timer ItemManager.getInstance().lowerAllItemsTimers(player); } } }); }
/** * Sleeps for the cycle delay. * * @throws InterruptedException */ private void sleep() { try { long sleepTime = cycleRate - cycleTimer.elapsed(); boolean sleep = sleepTime > 0 && sleepTime <= 600; for (int i = 0; i < PacketManager.SIZE; i++) { Benchmark b = PacketManager.packetBenchmarks[i]; if (!sleep && b.getTime() > 0) System.out.println( "Packet " + i + "[" + PacketManager.packets[i].getClass().getSimpleName() + "] took " + b.getTime() + " ms."); b.reset(); } if (sleep) { Benchmarks.resetAll(); // System.out.println("[ENGINE]: Sleeping for " + sleepTime + "ms"); Thread.sleep(sleepTime); } else { // The server has reached maximum load, players may now lag. cycle = (100 + Math.abs(sleepTime) / (cycleRate / 100)); /*if (cycle > 999) { initiateRestart(); }*/ System.out.println("[WARNING]: Server load: " + cycle + "%!"); Benchmarks.printAll(); Benchmarks.resetAll(); for (int i = 0; i < 5; i++) System.out.println(""); } } catch (Exception ex) { ex.printStackTrace(); } finally { cycleTimer.reset(); } }
/** Performs a server cycle. */ private void cycle() { int loggedIn = 0; Benchmark b = Benchmarks.getBenchmark("loginQueue"); b.start(); while (!loginQueue.isEmpty() && loggedIn++ < 50) { Player player = loginQueue.poll(); try { player.finishLogin(); player.setLoginStage(LoginStages.LOGGED_IN); } catch (Exception ex) { // ex.printStackTrace(); System.out.println("Error, infinite DC loop for this player"); player.disconnect(); } } b.stop(); b = Benchmarks.getBenchmark("handleNetworkPackets"); b.start(); // Handle all network events. try { selector.selectNow(); for (SelectionKey selectionKey : selector.selectedKeys()) { if (selectionKey.isValid()) { if (selectionKey.isReadable()) { // Tell the client to handle the packet. PacketManager.handleIncomingData((Player) selectionKey.attachment()); } } } } catch (Exception ex) { ex.printStackTrace(); } b.stop(); // Next, perform game processing. try { PluginManager.tick(); World.process(); } catch (Exception ex) { ex.printStackTrace(); } b = Benchmarks.getBenchmark("disconnectingPlayers"); b.start(); synchronized (disconnectedPlayers) { for (Iterator<Player> players = disconnectedPlayers.iterator(); players.hasNext(); ) { Player player = players.next(); if (player.logoutDisabled()) continue; player.logout(); players.remove(); } } b.stop(); if (infoDisplayCounter == 0) { System.out.println( "[ENGINE]: Server load: " + cycle + "% with " + World.playerAmount() + " players"); infoDisplayCounter = 300; } else { infoDisplayCounter--; } }
@Override public void run() { try { Thread.currentThread().setName("ServerEngine"); System.setOut(new Misc.TimestampLogger(System.out)); System.setErr(new Misc.TimestampLogger(System.err, "./data/err.log")); address = new InetSocketAddress(host, port); System.out.println("Starting " + Constants.SERVER_NAME + " on " + address + "..."); // load shutdown hook Thread shutdownhook = new ShutDownHook(); Runtime.getRuntime().addShutdownHook(shutdownhook); PacketManager.loadPackets(); Cache.load(); // load scripts Misc.loadScripts(new File("./data/ruby/")); GlobalVariables.patchNotes = Misc.loadPatchNotes(); GlobalVariables.info = Misc.loadInfo(); GlobalVariables.npcDump = Misc.getNpcDump(); GlobalVariables.itemDump = Misc.getItemDump(); // load all xstream related files. XStreamUtil.loadAllFiles(); // item weights ItemDefinition.loadWeight(); // interfaces RSInterface.load(); // Load plugins PluginManager.loadPlugins(); // Load regions ObjectDef.loadConfig(); Region.load(); Rangable.load(); // Load objects ObjectLoader objectLoader = new ObjectLoader(); objectLoader.load(); GameObjectData.init(); // load combat manager CombatManager.init(); // Load minute timer startMinutesCounter(); // global drops GlobalGroundItem.initialize(); // load npc ls Npc.loadNpcDrops(); // mage arena timers AlchemistPlayground.loadAlchemistPlayGround(); EnchantingChamber.loadEnchantingChamber(); CreatureGraveyard.loadCreatureGraveyard(); // spawning world fishing spots FishingSpots.spawnFishingSpots(); QuestHandler.init(); NpcLoader.loadAutoSpawn("./data/npcs/spawn-config.cfg"); HighscoresManager.load(); // Start up and get a'rollin! startup(); System.out.println("Online!"); while (!Thread.interrupted()) { try { cycle(); sleep(); } catch (Exception ex) { PlayerSave.saveAllPlayers(); ex.printStackTrace(); } } scheduler.schedule( new Task() { @Override protected void execute() { if (Thread.interrupted()) { PlayerSave.saveAllPlayers(); stop(); return; } try { cycle(); } catch (Exception ex) { PlayerSave.saveAllPlayers(); ex.printStackTrace(); stop(); } } }); } catch (Exception ex) { ex.printStackTrace(); } PluginManager.close(); }