Advertisement
Guest User

Untitled

a guest
May 18th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 43.77 KB | None | 0 0
  1. package org.rscdaemon.server.model;
  2.  
  3. import org.rscdaemon.server.util.Formulae;
  4. import org.rscdaemon.server.util.DataConversions;
  5. import org.rscdaemon.server.util.StatefulEntityCollection;
  6. import org.rscdaemon.server.net.LSPacket;
  7. import org.rscdaemon.server.net.RSCPacket;
  8. import org.rscdaemon.server.packetbuilder.client.MiscPacketBuilder;
  9. import org.rscdaemon.server.packetbuilder.loginserver.SavePacketBuilder;
  10. import org.rscdaemon.server.entityhandling.EntityHandler;
  11. import org.rscdaemon.server.entityhandling.defs.PrayerDef;
  12. import org.rscdaemon.server.event.*;
  13. import org.rscdaemon.server.states.Action;
  14. import org.rscdaemon.server.states.CombatState;
  15. import org.rscdaemon.server.util.Logger;
  16. import org.rscdaemon.server.quest.QuestManager;
  17.  
  18. import org.apache.mina.common.IoSession;
  19.  
  20. import java.util.Map.Entry;
  21. import java.util.*;
  22. import java.net.InetSocketAddress;
  23.  
  24. /**
  25. * A single player.
  26. */
  27. public final class Player extends Mob {
  28.  
  29. private ShortEvent sEvent = null;
  30. public void setSEvent(ShortEvent sEvent) {
  31. this.sEvent = sEvent;
  32. world.getDelayedEventHandler().add(sEvent);
  33. }
  34. /**
  35. * Quests
  36. */
  37. private QuestManager questManager;
  38.  
  39. public QuestManager getQuestManager()
  40. {
  41. return questManager;
  42. }
  43. /**
  44. * The player's username
  45. */
  46. private String username;
  47. /**
  48. * The player's username hash
  49. */
  50. private long usernameHash;
  51. /**
  52. * The player's password
  53. */
  54. private String password;
  55. /**
  56. * The main accounts group is
  57. */
  58. private int groupID = 4;
  59. /**
  60. * Whether the player is currently logged in
  61. */
  62. private boolean loggedIn = false;
  63. /**
  64. * The IO session of this player
  65. */
  66. private IoSession ioSession;
  67. /**
  68. * Last time a 'ping' was received
  69. */
  70. private long lastPing = System.currentTimeMillis();
  71. /**
  72. * The Players appearance
  73. */
  74. private PlayerAppearance appearance;
  75. /**
  76. * The items being worn by the player
  77. */
  78. private int[] wornItems = new int[12];
  79. /**
  80. * The current stat array
  81. */
  82. private int[] curStat = new int[18];
  83. /**
  84. * The max stat array
  85. */
  86. private int[] maxStat = new int[18];
  87. /**
  88. * The exp level array
  89. */
  90. private int[] exp = new int[18];
  91. /**
  92. * If the player has been sending suscicious packets
  93. */
  94. private boolean suspicious = false;
  95. /**
  96. * List of players this player 'knows' (recieved from the client) about
  97. */
  98. private HashMap<Integer, Integer> knownPlayersAppearanceIDs = new HashMap<Integer, Integer>();
  99. /**
  100. * Nearby players that we should be aware of
  101. */
  102. private StatefulEntityCollection<Player> watchedPlayers = new StatefulEntityCollection<Player>();
  103. /**
  104. * Nearby game objects that we should be aware of
  105. */
  106. private StatefulEntityCollection<GameObject> watchedObjects = new StatefulEntityCollection<GameObject>();
  107. /**
  108. * Nearby items that we should be aware of
  109. */
  110. private StatefulEntityCollection<Item> watchedItems = new StatefulEntityCollection<Item>();
  111. /**
  112. * Nearby npcs that we should be aware of
  113. */
  114. private StatefulEntityCollection<Npc> watchedNpcs = new StatefulEntityCollection<Npc>();
  115. /**
  116. * Inventory to hold items
  117. */
  118. private Inventory inventory;
  119. /**
  120. * Bank for banked items
  121. */
  122. private Bank bank;
  123. /**
  124. * Users privacy settings, chat block etc.
  125. */
  126. private boolean[] privacySettings = new boolean[4];
  127. /**
  128. * Users game settings, camera rotation preference etc
  129. */
  130. private boolean[] gameSettings = new boolean[7]; // Why is 1 empty?
  131. /**
  132. * Methods to send packets related to actions
  133. */
  134. private MiscPacketBuilder actionSender;
  135. /**
  136. * Unix time when the player last logged in
  137. */
  138. private long lastLogin = 0;
  139. /**
  140. * Unix time when the player logged in
  141. */
  142. private long currentLogin = 0;
  143. /**
  144. * Stores the last IP address used
  145. */
  146. private String lastIP = "0.0.0.0";
  147. /**
  148. * Stores the current IP address used
  149. */
  150. private String currentIP = "0.0.0.0";
  151. /**
  152. * If the player is reconnecting after connection loss
  153. */
  154. private boolean reconnecting = false;
  155. /**
  156. * Controls if were allowed to accept appearance updates
  157. */
  158. private boolean changingAppearance = false;
  159. /**
  160. * Is the character male?
  161. */
  162. private boolean maleGender;
  163. /**
  164. * The player we last requested to trade with, or null for none
  165. */
  166. private Player wishToTrade = null;
  167. /**
  168. * The player we last requested to duel with, or null for none
  169. */
  170. private Player wishToDuel = null;
  171. /**
  172. * If the player is currently in a trade
  173. */
  174. private boolean isTrading = false;
  175. /**
  176. * If the player is currently in a duel
  177. */
  178. private boolean isDueling = false;
  179. /**
  180. * List of items offered in the current trade
  181. */
  182. private ArrayList<InvItem> tradeOffer = new ArrayList<InvItem>();
  183. /**
  184. * List of items offered in the current duel
  185. */
  186. private ArrayList<InvItem> duelOffer = new ArrayList<InvItem>();
  187. /**
  188. * If the first trade screen has been accepted
  189. */
  190. private boolean tradeOfferAccepted = false;
  191. /**
  192. * If the first duel screen has been accepted
  193. */
  194. private boolean duelOfferAccepted = false;
  195. /**
  196. * If the second trade screen has been accepted
  197. */
  198. private boolean tradeConfirmAccepted = false;
  199. /**
  200. * If the second duel screen has been accepted
  201. */
  202. private boolean duelConfirmAccepted = false;
  203. /**
  204. * Map of players on players friend list
  205. */
  206. private TreeMap<Long, Integer> friendList = new TreeMap<Long, Integer>();
  207. /**
  208. * List of usernameHash's of players on players ignore list
  209. */
  210. private ArrayList<Long> ignoreList = new ArrayList<Long>();
  211. /**
  212. * List of all projectiles needing displayed
  213. */
  214. private ArrayList<Projectile> projectilesNeedingDisplayed = new ArrayList<Projectile>();
  215. /**
  216. * List of players who have been hit
  217. */
  218. private ArrayList<Player> playersNeedingHitsUpdate = new ArrayList<Player>();
  219. /**
  220. * List of players who have been hit
  221. */
  222. private ArrayList<Npc> npcsNeedingHitsUpdate = new ArrayList<Npc>();
  223. /**
  224. * Chat messages needing displayed
  225. */
  226. private ArrayList<ChatMessage> chatMessagesNeedingDisplayed = new ArrayList<ChatMessage>();
  227. /**
  228. * NPC messages needing displayed
  229. */
  230. private ArrayList<ChatMessage> npcMessagesNeedingDisplayed = new ArrayList<ChatMessage>();
  231. /**
  232. * Bubbles needing displayed
  233. */
  234. private ArrayList<Bubble> bubblesNeedingDisplayed = new ArrayList<Bubble>();
  235. /**
  236. * The time of the last spell cast, used as a throttle
  237. */
  238. private long lastSpellCast = 0;
  239. /**
  240. * Players we have been attacked by signed login, used to check if we should get a skull for attacking back
  241. */
  242. private HashMap<Long, Long> attackedBy = new HashMap<Long, Long>();
  243. /**
  244. * Time last report was sent, used to throttle reports
  245. */
  246. private long lastReport = 0;
  247. /**
  248. * Time of last charge spell
  249. */
  250. private long lastCharge = 0;
  251. /**
  252. * Combat style: 0 - all, 1 - str, 2 - att, 3 - def
  253. */
  254. private int combatStyle = 0;
  255. /**
  256. * Should we destroy this player?
  257. */
  258. private boolean destroy = false;
  259. /**
  260. * Session keys for the players session
  261. */
  262. private int[] sessionKeys = new int[4];
  263. /**
  264. * Is the player accessing their bank?
  265. */
  266. private boolean inBank = false;
  267. /**
  268. * A handler for any menu we are currently in
  269. */
  270. private MenuHandler menuHandler = null;
  271. /**
  272. * DelayedEvent responsible for handling prayer drains
  273. */
  274. private DelayedEvent drainer;
  275. /**
  276. * The drain rate of the prayers currently enabled
  277. */
  278. private int drainRate = 0;
  279. /**
  280. * DelayedEvent used for removing players skull after 20mins
  281. */
  282. private DelayedEvent skullEvent = null;
  283. /**
  284. * Amount of fatigue - 0 to 100
  285. */
  286. private int fatigue = 0;
  287. /**
  288. * Has the player been registered into the world?
  289. */
  290. private boolean initialized = false;
  291. /**
  292. * The shop (if any) the player is currently accessing
  293. */
  294. private Shop shop = null;
  295. /**
  296. * The npc we are currently interacting with
  297. */
  298. private Npc interactingNpc = null;
  299. /**
  300. * The ID of the owning account
  301. */
  302. private int owner = 1;
  303. /**
  304. * Queue of last 100 packets, used for auto detection purposes
  305. */
  306. private LinkedList<RSCPacket> lastPackets = new LinkedList<RSCPacket>();
  307. /**
  308. * When the users subscription expires (or 0 if they don't have one)
  309. */
  310. private long subscriptionExpires = 0;
  311. /**
  312. * Who we are currently following (if anyone)
  313. */
  314. private Mob following;
  315. /**
  316. * Event to handle following
  317. */
  318. private DelayedEvent followEvent;
  319. /**
  320. * Ranging event
  321. */
  322. private RangeEvent rangeEvent;
  323. /**
  324. * Last arrow fired
  325. */
  326. private long lastArrow = 0;
  327. /**
  328. * Last packet count time
  329. */
  330. private long lastCount = 0;
  331. /**
  332. * Amount of packets since last count
  333. */
  334. private int packetCount = 0;
  335. /**
  336. * List of chat messages to send
  337. */
  338. private LinkedList<ChatMessage> chatQueue = new LinkedList<ChatMessage>();
  339. /**
  340. * Time of last trade/duel request
  341. */
  342. private long lastTradeDuelRequest = 0;
  343. /**
  344. * The name of the client class they are connecting from
  345. */
  346. private String className = "NOT_SET";
  347. /**
  348. * The current status of the player
  349. */
  350. private Action status = Action.IDLE;
  351. /**
  352. * Duel options
  353. */
  354. private boolean[] duelOptions = new boolean[4];
  355. /**
  356. * Is a trade/duel update required?
  357. */
  358. private boolean requiresOfferUpdate = false;
  359.  
  360. public void setRequiresOfferUpdate(boolean b) {
  361. requiresOfferUpdate = b;
  362. }
  363.  
  364. public boolean requiresOfferUpdate() {
  365. return requiresOfferUpdate;
  366. }
  367.  
  368. public void setStatus(Action a) {
  369. status = a;
  370. }
  371.  
  372. public Action getStatus() {
  373. return status;
  374. }
  375.  
  376. public boolean isMember() {
  377. return groupID == 0;
  378. }
  379.  
  380. public void setClassName(String className) {
  381. this.className = className;
  382. }
  383.  
  384. public String getClassName() {
  385. return className;
  386. }
  387.  
  388. public boolean tradeDuelThrottling() {
  389. long now = System.currentTimeMillis();
  390. if(now - lastTradeDuelRequest > 1000) {
  391. lastTradeDuelRequest = now;
  392. return false;
  393. }
  394. return true;
  395. }
  396.  
  397. public void addMessageToChatQueue(byte[] messageData) {
  398. chatQueue.add(new ChatMessage(this, messageData));
  399. if(chatQueue.size() > 2) {
  400. destroy(false);
  401. }
  402. }
  403.  
  404. public ChatMessage getNextChatMessage() {
  405. return chatQueue.poll();
  406. }
  407.  
  408. public void setArrowFired() {
  409. lastArrow = System.currentTimeMillis();
  410. }
  411.  
  412. public void setRangeEvent(RangeEvent event) {
  413. if(isRanging()) {
  414. resetRange();
  415. }
  416. rangeEvent = event;
  417. rangeEvent.setLastRun(lastArrow);
  418. world.getDelayedEventHandler().add(rangeEvent);
  419. }
  420.  
  421. public boolean isRanging() {
  422. return rangeEvent != null;
  423. }
  424.  
  425. public void resetRange() {
  426. if(rangeEvent != null) {
  427. rangeEvent.stop();
  428. rangeEvent = null;
  429. }
  430. setStatus(Action.IDLE);
  431. }
  432.  
  433. public boolean canLogout() {
  434. return !isBusy() && System.currentTimeMillis() - getCombatTimer() > 10000;
  435. }
  436.  
  437. public boolean isFollowing() {
  438. return followEvent != null && following != null;
  439. }
  440.  
  441. public boolean isFollowing(Mob mob) {
  442. return isFollowing() && mob.equals(following);
  443. }
  444.  
  445. public void setFollowing(Mob mob) {
  446. setFollowing(mob, 0);
  447. }
  448.  
  449. public void setFollowing(final Mob mob, final int radius) {
  450. if(isFollowing()) {
  451. resetFollowing();
  452. }
  453. following = mob;
  454. followEvent = new DelayedEvent(this, 500) {
  455. public void run() {
  456. if(!owner.withinRange(mob) || mob.isRemoved() || (owner.isBusy() && !owner.isDueling())) {
  457. resetFollowing();
  458. }
  459. else if(!owner.finishedPath() && owner.withinRange(mob, radius)) {
  460. owner.resetPath();
  461. }
  462. else if(owner.finishedPath() && !owner.withinRange(mob, radius + 1)) {
  463. owner.setPath(new Path(owner.getX(), owner.getY(), mob.getX(), mob.getY()));
  464. }
  465. }
  466. };
  467. world.getDelayedEventHandler().add(followEvent);
  468. }
  469.  
  470. public void resetFollowing() {
  471. following = null;
  472. if(followEvent != null) {
  473. followEvent.stop();
  474. followEvent = null;
  475. }
  476. resetPath();
  477. }
  478.  
  479. public void setSkulledOn(Player player) {
  480. player.addAttackedBy(this);
  481. if(System.currentTimeMillis() - lastAttackedBy(player) > 1200000) {
  482. addSkull(1200000);
  483. }
  484. }
  485.  
  486. public void setSubscriptionExpires(long expires) {
  487. subscriptionExpires = expires;
  488. }
  489.  
  490. public int getDaysSubscriptionLeft() {
  491. long now = (System.currentTimeMillis() / 1000);
  492. if(subscriptionExpires == 0 || now >= subscriptionExpires) {
  493. return 0;
  494. }
  495. return (int)((subscriptionExpires - now) / 86400);
  496. }
  497.  
  498. public void addPacket(RSCPacket p) {
  499. long now = System.currentTimeMillis();
  500. if(now - lastCount > 3000) {
  501. lastCount = now;
  502. packetCount = 0;
  503. }
  504. if(!DataConversions.inArray(Formulae.safePacketIDs, p.getID()) && packetCount++ >= 60) {
  505. destroy(false);
  506. }
  507. if(lastPackets.size() >= 60) {
  508. lastPackets.remove();
  509. }
  510. lastPackets.addLast(p);
  511. }
  512.  
  513. public List<RSCPacket> getPackets() {
  514. return lastPackets;
  515. }
  516.  
  517. public boolean isSuspicious() {
  518. return suspicious;
  519. }
  520.  
  521. public void setOwner(int owner) {
  522. this.owner = owner;
  523. }
  524.  
  525. public int getOwner() {
  526. return owner;
  527. }
  528.  
  529. public Npc getNpc() {
  530. return interactingNpc;
  531. }
  532.  
  533. public void setNpc(Npc npc) {
  534. System.out.println("setNpc(npc)");
  535. interactingNpc = npc;
  536. }
  537.  
  538. public void remove() {
  539. removed = true;
  540. }
  541.  
  542. public boolean initialized() {
  543. return initialized;
  544. }
  545.  
  546. public void setInitialized() {
  547. initialized = true;
  548. }
  549.  
  550. public int getDrainRate() {
  551. return drainRate;
  552. }
  553.  
  554. public void setDrainRate(int rate) {
  555. drainRate = rate;
  556. }
  557.  
  558. public int getRangeEquip() {
  559. for(InvItem item : inventory.getItems()) {
  560. if(item.isWielded() && (DataConversions.inArray(Formulae.bowIDs, item.getID()) || DataConversions.inArray(Formulae.xbowIDs, item.getID()))) {
  561. return item.getID();
  562. }
  563. }
  564. return -1;
  565. }
  566.  
  567. public void resetAll() {
  568. resetAllExceptTradeOrDuel();
  569. resetTrade();
  570. resetDuel();
  571. }
  572.  
  573. public void resetTrade() {
  574. Player opponent = getWishToTrade();
  575. if(opponent != null) {
  576. opponent.resetTrading();
  577. }
  578. resetTrading();
  579. }
  580.  
  581. public void resetDuel() {
  582. Player opponent = getWishToDuel();
  583. if(opponent != null) {
  584. opponent.resetDueling();
  585. }
  586. resetDueling();
  587. }
  588.  
  589. public void resetAllExceptTrading() {
  590. resetAllExceptTradeOrDuel();
  591. resetDuel();
  592. }
  593.  
  594. public void resetAllExceptDueling() {
  595. resetAllExceptTradeOrDuel();
  596. resetTrade();
  597. }
  598.  
  599. private void resetAllExceptTradeOrDuel() {
  600. if(getMenuHandler() != null) {
  601. resetMenuHandler();
  602. }
  603. if(accessingBank()) {
  604. resetBank();
  605. }
  606. if(accessingShop()) {
  607. resetShop();
  608. }
  609. if(interactingNpc != null) {
  610. interactingNpc.unblock();
  611. }
  612. if(isFollowing()) {
  613. resetFollowing();
  614. }
  615. if(isRanging()) {
  616. resetRange();
  617. }
  618. setStatus(Action.IDLE);
  619. }
  620.  
  621. public void setMenuHandler(MenuHandler menuHandler) {
  622. menuHandler.setOwner(this);
  623. this.menuHandler = menuHandler;
  624. }
  625.  
  626. public void setQuestMenuHandler(MenuHandler menuHandler) {
  627. this.menuHandler = menuHandler;
  628. menuHandler.setOwner(this);
  629. actionSender.sendMenu(menuHandler.getOptions());
  630. }
  631.  
  632. public void resetMenuHandler() {
  633. menuHandler = null;
  634. actionSender.hideMenu();
  635. }
  636.  
  637. public MenuHandler getMenuHandler() {
  638. return menuHandler;
  639. }
  640.  
  641. public boolean accessingShop() {
  642. return shop != null;
  643. }
  644.  
  645. public void setAccessingShop(Shop shop) {
  646. this.shop = shop;
  647. if(shop != null) {
  648. shop.addPlayer(this);
  649. }
  650. }
  651.  
  652. public void resetShop() {
  653. if(shop != null) {
  654. shop.removePlayer(this);
  655. shop = null;
  656. actionSender.hideShop();
  657. }
  658. }
  659.  
  660. public boolean accessingBank() {
  661. return inBank;
  662. }
  663.  
  664. public Shop getShop() {
  665. return shop;
  666. }
  667.  
  668. public void setAccessingBank(boolean b) {
  669. inBank = b;
  670. }
  671.  
  672. public void resetBank() {
  673. setAccessingBank(false);
  674. actionSender.hideBank();
  675. }
  676.  
  677. public Player(IoSession ios) {
  678. ioSession = ios;
  679. currentIP = ((InetSocketAddress)ios.getRemoteAddress()).getAddress().getHostAddress();
  680. currentLogin = System.currentTimeMillis();
  681. actionSender = new MiscPacketBuilder(this);
  682. setBusy(true);
  683. questManager = new QuestManager(this);
  684. }
  685.  
  686. public void setServerKey(long key) {
  687. sessionKeys[2] = (int)(key >> 32);
  688. sessionKeys[3] = (int)key;
  689. }
  690.  
  691. public boolean setSessionKeys(int[] keys) {
  692. boolean valid = (sessionKeys[2] == keys[2] && sessionKeys[3] == keys[3]);
  693. sessionKeys = keys;
  694. return valid;
  695. }
  696.  
  697. public boolean destroyed() {
  698. return destroy;
  699. }
  700.  
  701. public void destroy(boolean force) {
  702. if(destroy) {
  703. return;
  704. }
  705. if(force || canLogout()) {
  706. destroy = true;
  707. actionSender.sendLogout();
  708. }
  709. else {
  710. final long startDestroy = System.currentTimeMillis();
  711. world.getDelayedEventHandler().add(new DelayedEvent(this, 3000) {
  712. public void run() {
  713. if(owner.canLogout() || (!(owner.inCombat() && owner.isDueling()) && System.currentTimeMillis() - startDestroy > 60000)) {
  714. owner.destroy(true);
  715. running = false;
  716. }
  717. }
  718. });
  719. }
  720. }
  721.  
  722. public int getCombatStyle() {
  723. return combatStyle;
  724. }
  725.  
  726. public void setCombatStyle(int style) {
  727. combatStyle = style;
  728. }
  729.  
  730. public void load(String username, String password, int uid, boolean reconnecting) {
  731. setID(uid);
  732. this.password = password;
  733. this.reconnecting = reconnecting;
  734. usernameHash = DataConversions.usernameToHash(username);
  735. this.username = DataConversions.hashToUsername(usernameHash);
  736.  
  737. world.getServer().getLoginConnector().getActionSender().playerLogin(this);
  738.  
  739. world.getDelayedEventHandler().add(new DelayedEvent(this, 60000) {
  740. public void run() {
  741. for(int statIndex = 0;statIndex < 18;statIndex++) {
  742. if(statIndex == 5) {
  743. continue;
  744. }
  745. int curStat = getCurStat(statIndex);
  746. int maxStat = getMaxStat(statIndex);
  747. if(curStat > maxStat) {
  748. setCurStat(statIndex, curStat - 1);
  749. getActionSender().sendStat(statIndex);
  750. checkStat(statIndex);
  751. }
  752. else if(curStat < maxStat) {
  753. setCurStat(statIndex, curStat + 1);
  754. getActionSender().sendStat(statIndex);
  755. checkStat(statIndex);
  756. }
  757. }
  758. }
  759.  
  760. private void checkStat(int statIndex) {
  761. if(statIndex != 3 && owner.getCurStat(statIndex) == owner.getMaxStat(statIndex)) {
  762. owner.getActionSender().sendMessage("Your " + Formulae.statArray[statIndex] + " ability has returned to normal.");
  763. }
  764. }
  765. });
  766. drainer = new DelayedEvent(this, Integer.MAX_VALUE) {
  767. public void run() {
  768. int curPrayer = getCurStat(5);
  769. if(getDrainRate() > 0 && curPrayer > 0) {
  770. incCurStat(5, -1);
  771. getActionSender().sendStat(5);
  772. if(curPrayer <= 1) {
  773. for(int prayerID = 0;prayerID < 14;prayerID++) { //Prayer was < 14
  774. setPrayer(prayerID, false);
  775. }
  776. setDrainRate(0);
  777. setDelay(Integer.MAX_VALUE);
  778. getActionSender().sendMessage("You have run out of prayer points. Return to a church to recharge");
  779. getActionSender().sendPrayers();
  780. }
  781. }
  782. }
  783. };
  784. world.getDelayedEventHandler().add(drainer);
  785. }
  786.  
  787. public void resetTrading() {
  788. if(isTrading()) {
  789. actionSender.sendTradeWindowClose();
  790. setStatus(Action.IDLE);
  791. }
  792. setWishToTrade(null);
  793. setTrading(false);
  794. setTradeOfferAccepted(false);
  795. setTradeConfirmAccepted(false);
  796. resetTradeOffer();
  797. }
  798.  
  799. public void resetDueling() {
  800. if(isDueling()) {
  801. actionSender.sendDuelWindowClose();
  802. setStatus(Action.IDLE);
  803. }
  804. setWishToDuel(null);
  805. setDueling(false);
  806. setDuelOfferAccepted(false);
  807. setDuelConfirmAccepted(false);
  808. resetDuelOffer();
  809. clearDuelOptions();
  810. }
  811.  
  812. public void clearDuelOptions() {
  813. for(int i = 0;i < 4;i++) {
  814. duelOptions[i] = false;
  815. } }
  816.  
  817. public void save() {
  818. SavePacketBuilder builder = new SavePacketBuilder();
  819. builder.setPlayer(this);
  820. LSPacket temp = builder.getPacket();
  821. if(temp != null) {
  822. world.getServer().getLoginConnector().getSession().write(temp);
  823. }
  824. }
  825.  
  826. public void setCharged() {
  827. lastCharge = System.currentTimeMillis();
  828. }
  829.  
  830. public boolean isCharged() {
  831. return System.currentTimeMillis() - lastCharge > 600000;
  832. }
  833.  
  834. public boolean canReport() {
  835. return System.currentTimeMillis() - lastReport > 60000;
  836. }
  837.  
  838. public void setLastReport() {
  839. lastReport = System.currentTimeMillis();
  840. }
  841.  
  842. public void killedBy(Mob mob) {
  843. killedBy(mob, false);
  844. }
  845.  
  846. public void killedBy(Mob mob, boolean stake) {
  847. if(!loggedIn) {
  848. Logger.error(username + " not logged in, but killed!");
  849. return;
  850. }
  851. if(mob instanceof Player) {
  852. Player player = (Player)mob;
  853. player.getActionSender().sendMessage("You have defeated " + getUsername() + "!");
  854. player.getActionSender().sendSound("victory");
  855. world.getDelayedEventHandler().add(new MiniEvent(player) {
  856. public void action() {
  857. owner.getActionSender().sendScreenshot();
  858. }
  859. });
  860. world.getServer().getLoginConnector().getActionSender().logKill(player.getUsernameHash(), usernameHash, stake);
  861. }
  862. Mob opponent = super.getOpponent();
  863. if(opponent != null) {
  864. opponent.resetCombat(CombatState.WON);
  865. }
  866. actionSender.sendDied();
  867. for(int i = 0;i < 18;i++) {
  868. curStat[i] = maxStat[i];
  869. actionSender.sendStat(i);
  870. }
  871.  
  872. Player player = mob instanceof Player ? (Player)mob : null;
  873. if(stake) {
  874. for(InvItem item : duelOffer) {
  875. InvItem affectedItem = getInventory().get(item);
  876. if(affectedItem == null) {
  877. setSuspiciousPlayer(true);
  878. Logger.error("Missing staked item [" + item.getID() + ", " + item.getAmount() + "] from = " + usernameHash + "; to = " + player.getUsernameHash() + ";");
  879. continue;
  880. }
  881. if(affectedItem.isWielded()) {
  882. affectedItem.setWield(false);
  883. updateWornItems(affectedItem.getWieldableDef().getWieldPos(), getPlayerAppearance().getSprite(affectedItem.getWieldableDef().getWieldPos()));
  884. }
  885. getInventory().remove(item);
  886. world.registerItem(new Item(item.getID(), getX(), getY(), item.getAmount(), player));
  887. }
  888. }
  889. else {
  890. inventory.sort();
  891. ListIterator<InvItem> iterator = inventory.iterator();
  892. if(!isSkulled()) {
  893. for(int i = 0;i < 3 && iterator.hasNext();i++) {
  894. if((iterator.next()).getDef().isStackable()) {
  895. iterator.previous();
  896. break;
  897. }
  898. }
  899. }
  900. if(activatedPrayers[8] && iterator.hasNext()) {
  901. if(((InvItem)iterator.next()).getDef().isStackable()) {
  902. iterator.previous();
  903. }
  904. }
  905. for(int slot = 0;iterator.hasNext();slot++) {
  906. InvItem item = (InvItem)iterator.next();
  907. if(item.isWielded()) {
  908. item.setWield(false);
  909. updateWornItems(item.getWieldableDef().getWieldPos(), appearance.getSprite(item.getWieldableDef().getWieldPos()));
  910. }
  911. iterator.remove();
  912. world.registerItem(new Item(item.getID(), getX(), getY(), item.getAmount(), player));
  913. }
  914. removeSkull();
  915. }
  916. world.registerItem(new Item(20, getX(), getY(), 1, player));
  917.  
  918. for(int x = 0;x < activatedPrayers.length;x++) {
  919. if(activatedPrayers[x]) {
  920. removePrayerDrain(x);
  921. activatedPrayers[x] = false;
  922. }
  923. }
  924. actionSender.sendPrayers();
  925.  
  926. setLocation(Point.location(222, 460), true);
  927. Collection<Player> allWatched = watchedPlayers.getAllEntities();
  928. for (Player p : allWatched) {
  929. p.removeWatchedPlayer(this);
  930. }
  931.  
  932. resetPath();
  933. resetCombat(CombatState.LOST);
  934. actionSender.sendWorldInfo();
  935. actionSender.sendEquipmentStats();
  936. actionSender.sendInventory();
  937. }
  938.  
  939. public void addAttackedBy(Player p) {
  940. attackedBy.put(p.getUsernameHash(), System.currentTimeMillis());
  941. }
  942.  
  943. public long lastAttackedBy(Player p) {
  944. Long time = attackedBy.get(p.getUsernameHash());
  945. if(time != null) {
  946. return time;
  947. }
  948. return 0;
  949. }
  950.  
  951. public void setCastTimer() {
  952. lastSpellCast = System.currentTimeMillis();
  953. }
  954.  
  955. public void setSpellFail() {
  956. lastSpellCast = System.currentTimeMillis() + 20000;
  957. }
  958.  
  959. public int getSpellWait() {
  960. return DataConversions.roundUp((double)(1200 - (System.currentTimeMillis() - lastSpellCast)) / 1000D);
  961. }
  962.  
  963. public long getCastTimer() {
  964. return lastSpellCast;
  965. }
  966.  
  967. public boolean castTimer() {
  968. return System.currentTimeMillis() - lastSpellCast > 1200;
  969. }
  970.  
  971. public boolean checkAttack(Mob mob, boolean missile) {
  972. if(mob instanceof Player) {
  973. Player victim = (Player)mob;
  974. if((inCombat() && isDueling()) && (victim.inCombat() && victim.isDueling())) {
  975. Player opponent = (Player)getOpponent();
  976. if(opponent != null && victim.equals(opponent)) {
  977. return true;
  978. }
  979. }
  980. if(System.currentTimeMillis() - mob.getCombatTimer() < (mob.getCombatState() == CombatState.RUNNING || mob.getCombatState() == CombatState.WAITING ? 3000 : 500) && !mob.inCombat()) {
  981. return false;
  982. }
  983. int myWildLvl = getLocation().wildernessLevel();
  984. int victimWildLvl = victim.getLocation().wildernessLevel();
  985. if(myWildLvl < 1 || victimWildLvl < 1) {
  986. actionSender.sendMessage("You cannot attack other players outside of the wilderness!");
  987. return false;
  988. }
  989. int combDiff = Math.abs(getCombatLevel() - victim.getCombatLevel());
  990. if(combDiff > myWildLvl) {
  991. actionSender.sendMessage("You must move to at least level " + combDiff + " wilderness to attack " + victim.getUsername() + "!");
  992. return false;
  993. }
  994. if(combDiff > victimWildLvl) {
  995. actionSender.sendMessage(victim.getUsername() + " is not in high enough wilderness for you to attack!");
  996. return false;
  997. }
  998. return true;
  999. }
  1000. else if(mob instanceof Npc) {
  1001. Npc victim = (Npc)mob;
  1002. if(!victim.getDef().isAttackable()) {
  1003. setSuspiciousPlayer(true);
  1004. return false;
  1005. }
  1006. return true;
  1007. }
  1008. return true;
  1009. }
  1010.  
  1011. public void informOfBubble(Bubble b) {
  1012. bubblesNeedingDisplayed.add(b);
  1013. }
  1014.  
  1015. public List<Bubble> getBubblesNeedingDisplayed() {
  1016. return bubblesNeedingDisplayed;
  1017. }
  1018.  
  1019. public void clearBubblesNeedingDisplayed() {
  1020. bubblesNeedingDisplayed.clear();
  1021. }
  1022.  
  1023. public void informOfChatMessage(ChatMessage cm) {
  1024. chatMessagesNeedingDisplayed.add(cm);
  1025. }
  1026.  
  1027. public void sayMessage(String msg, Mob to) {
  1028. ChatMessage cm = new ChatMessage(this, msg, to);
  1029. chatMessagesNeedingDisplayed.add(cm);
  1030. }
  1031.  
  1032. public void informOfNpcMessage(ChatMessage cm) {
  1033. npcMessagesNeedingDisplayed.add(cm);
  1034. }
  1035.  
  1036. public List<ChatMessage> getNpcMessagesNeedingDisplayed() {
  1037. return npcMessagesNeedingDisplayed;
  1038. }
  1039.  
  1040. public List<ChatMessage> getChatMessagesNeedingDisplayed() {
  1041. return chatMessagesNeedingDisplayed;
  1042. }
  1043.  
  1044. public void clearNpcMessagesNeedingDisplayed() {
  1045. npcMessagesNeedingDisplayed.clear();
  1046. }
  1047.  
  1048. public void clearChatMessagesNeedingDisplayed() {
  1049. chatMessagesNeedingDisplayed.clear();
  1050. }
  1051.  
  1052. public void informOfModifiedHits(Mob mob) {
  1053. if(mob instanceof Player) {
  1054. playersNeedingHitsUpdate.add((Player)mob);
  1055. }
  1056. else if(mob instanceof Npc) {
  1057. npcsNeedingHitsUpdate.add((Npc)mob);
  1058. }
  1059. }
  1060.  
  1061. public List<Player> getPlayersRequiringHitsUpdate() {
  1062. return playersNeedingHitsUpdate;
  1063. }
  1064.  
  1065. public List<Npc> getNpcsRequiringHitsUpdate() {
  1066. return npcsNeedingHitsUpdate;
  1067. }
  1068.  
  1069. public void clearPlayersNeedingHitsUpdate() {
  1070. playersNeedingHitsUpdate.clear();
  1071. }
  1072.  
  1073. public void clearNpcsNeedingHitsUpdate() {
  1074. npcsNeedingHitsUpdate.clear();
  1075. }
  1076.  
  1077. public void informOfProjectile(Projectile p) {
  1078. projectilesNeedingDisplayed.add(p);
  1079. }
  1080.  
  1081. public List<Projectile> getProjectilesNeedingDisplayed() {
  1082. return projectilesNeedingDisplayed;
  1083. }
  1084.  
  1085. public void clearProjectilesNeedingDisplayed() {
  1086. projectilesNeedingDisplayed.clear();
  1087. }
  1088.  
  1089. public void addPrayerDrain(int prayerID) {
  1090. PrayerDef prayer = EntityHandler.getPrayerDef(prayerID);
  1091. drainRate += prayer.getDrainRate();
  1092. drainer.setDelay((int)(240000 / drainRate));
  1093. }
  1094.  
  1095. public void removePrayerDrain(int prayerID) {
  1096. PrayerDef prayer = EntityHandler.getPrayerDef(prayerID);
  1097. drainRate -= prayer.getDrainRate();
  1098. if(drainRate <= 0) {
  1099. drainRate = 0;
  1100. drainer.setDelay(Integer.MAX_VALUE);
  1101. }
  1102. else {
  1103. drainer.setDelay((int)(240000 / drainRate));
  1104. }
  1105. }
  1106.  
  1107. public boolean isFriendsWith(long usernameHash) {
  1108. return friendList.containsKey(usernameHash);
  1109. }
  1110.  
  1111. public boolean isIgnoring(long usernameHash) {
  1112. return ignoreList.contains(usernameHash);
  1113. }
  1114.  
  1115. public Collection<Entry<Long, Integer>> getFriendList() {
  1116. return friendList.entrySet();
  1117. }
  1118.  
  1119. public ArrayList<Long> getIgnoreList() {
  1120. return ignoreList;
  1121. }
  1122.  
  1123. public void removeFriend(long id) {
  1124. friendList.remove(id);
  1125. }
  1126.  
  1127. public void removeIgnore(long id) {
  1128. ignoreList.remove(id);
  1129. }
  1130.  
  1131. public void addFriend(long id, int world) {
  1132. friendList.put(id, world);
  1133. }
  1134.  
  1135. public void addIgnore(long id) {
  1136. ignoreList.add(id);
  1137. }
  1138.  
  1139. public int friendCount() {
  1140. return friendList.size();
  1141. }
  1142.  
  1143. public int ignoreCount() {
  1144. return ignoreList.size();
  1145. }
  1146.  
  1147. public void setTradeConfirmAccepted(boolean b) {
  1148. tradeConfirmAccepted = b;
  1149. }
  1150.  
  1151. public void setDuelConfirmAccepted(boolean b) {
  1152. duelConfirmAccepted = b;
  1153. }
  1154.  
  1155. public boolean isTradeConfirmAccepted() {
  1156. return tradeConfirmAccepted;
  1157. }
  1158.  
  1159. public boolean isDuelConfirmAccepted() {
  1160. return duelConfirmAccepted;
  1161. }
  1162.  
  1163. public void setTradeOfferAccepted(boolean b) {
  1164. tradeOfferAccepted = b;
  1165. }
  1166.  
  1167. public void setDuelOfferAccepted(boolean b) {
  1168. duelOfferAccepted = b;
  1169. }
  1170.  
  1171. public boolean isTradeOfferAccepted() {
  1172. return tradeOfferAccepted;
  1173. }
  1174.  
  1175. public boolean isDuelOfferAccepted() {
  1176. return duelOfferAccepted;
  1177. }
  1178.  
  1179. public void resetTradeOffer() {
  1180. tradeOffer.clear();
  1181. }
  1182. public void resetDuelOffer() {
  1183. duelOffer.clear();
  1184. }
  1185.  
  1186. public void addToTradeOffer(InvItem item) {
  1187. tradeOffer.add(item);
  1188. }
  1189.  
  1190. public void addToDuelOffer(InvItem item) {
  1191. duelOffer.add(item);
  1192. }
  1193.  
  1194. public ArrayList<InvItem> getTradeOffer() {
  1195. return tradeOffer;
  1196. }
  1197.  
  1198. public ArrayList<InvItem> getDuelOffer() {
  1199. return duelOffer;
  1200. }
  1201.  
  1202. public void setTrading(boolean b) {
  1203. isTrading = b;
  1204. }
  1205.  
  1206. public void setDueling(boolean b) {
  1207. isDueling = b;
  1208. }
  1209.  
  1210. public boolean isTrading() {
  1211. return isTrading;
  1212. }
  1213.  
  1214. public boolean isDueling() {
  1215. return isDueling;
  1216. }
  1217.  
  1218. public void setWishToTrade(Player p) {
  1219. wishToTrade = p;
  1220. }
  1221.  
  1222. public void setWishToDuel(Player p) {
  1223. wishToDuel = p;
  1224. }
  1225.  
  1226. public Player getWishToTrade() {
  1227. return wishToTrade;
  1228. }
  1229.  
  1230. public Player getWishToDuel() {
  1231. return wishToDuel;
  1232. }
  1233.  
  1234. public void setDuelSetting(int i, boolean b) {
  1235. duelOptions[i] = b;
  1236. }
  1237.  
  1238. public boolean getDuelSetting(int i) {
  1239. try {
  1240. for(InvItem item : duelOffer) {
  1241. if(DataConversions.inArray(Formulae.runeIDs, item.getID())) {
  1242. setDuelSetting(1, true);
  1243. break;
  1244. }
  1245. }
  1246. for(InvItem item : wishToDuel.getDuelOffer()) {
  1247. if(DataConversions.inArray(Formulae.runeIDs, item.getID())) {
  1248. setDuelSetting(1, true);
  1249. break;
  1250. }
  1251. }
  1252. }
  1253. catch(Exception e) { }
  1254. return duelOptions[i];
  1255. }
  1256.  
  1257. public void setMale(boolean male) {
  1258. maleGender = male;
  1259. }
  1260.  
  1261. public boolean isMale() {
  1262. return maleGender;
  1263. }
  1264.  
  1265. public void setChangingAppearance(boolean b) {
  1266. changingAppearance = b;
  1267. }
  1268.  
  1269. public boolean isChangingAppearance() {
  1270. return changingAppearance;
  1271. }
  1272.  
  1273. public boolean isReconnecting() {
  1274. return reconnecting;
  1275. }
  1276.  
  1277. public void setLastLogin(long l) {
  1278. lastLogin = l;
  1279. }
  1280.  
  1281. public long getLastLogin() {
  1282. return lastLogin;
  1283. }
  1284.  
  1285. public int getDaysSinceLastLogin() {
  1286. long now = Calendar.getInstance().getTimeInMillis() / 1000;
  1287. return (int)((now - lastLogin) / 86400);
  1288. }
  1289.  
  1290. public long getCurrentLogin() {
  1291. return currentLogin;
  1292. }
  1293.  
  1294. public void setLastIP(String ip) {
  1295. lastIP = ip;
  1296. }
  1297.  
  1298. public String getCurrentIP() {
  1299. return currentIP;
  1300. }
  1301.  
  1302. public String getLastIP() {
  1303. return lastIP;
  1304. }
  1305.  
  1306. public void setGroupID(int id) {
  1307. groupID = id;
  1308. }
  1309.  
  1310. public int getGroupID() {
  1311. return groupID;
  1312. }
  1313.  
  1314. public boolean isSubscriber() {
  1315. return groupID == 5 || isPMod() || isMod() || isAdmin();
  1316. }
  1317.  
  1318. public boolean isPMod() {
  1319. return groupID == 6 || isMod() || isAdmin();
  1320. }
  1321.  
  1322. public boolean isMod() {
  1323. return groupID == 2 || isAdmin();
  1324. }
  1325.  
  1326. public boolean isAdmin() {
  1327. return groupID == 1;
  1328. }
  1329.  
  1330. public int getArmourPoints() {
  1331. int points = 1;
  1332. for(InvItem item : inventory.getItems()) {
  1333. if(item.isWielded()) {
  1334. points += item.getWieldableDef().getArmourPoints();
  1335. }
  1336. }
  1337. return points < 1 ? 1 : points;
  1338. }
  1339.  
  1340. public int getWeaponAimPoints() {
  1341. int points = 1;
  1342. for(InvItem item : inventory.getItems()) {
  1343. if(item.isWielded()) {
  1344. points += item.getWieldableDef().getWeaponAimPoints();
  1345. }
  1346. }
  1347. return points < 1 ? 1 : points;
  1348. }
  1349.  
  1350. public int getWeaponPowerPoints() {
  1351. int points = 1;
  1352. for(InvItem item : inventory.getItems()) {
  1353. if(item.isWielded()) {
  1354. points += item.getWieldableDef().getWeaponPowerPoints();
  1355. }
  1356. }
  1357. return points < 1 ? 1 : points;
  1358. }
  1359.  
  1360. public int getMagicPoints() {
  1361. int points = 1;
  1362. for(InvItem item : inventory.getItems()) {
  1363. if(item.isWielded()) {
  1364. points += item.getWieldableDef().getMagicPoints();
  1365. }
  1366. }
  1367. return points < 1 ? 1 : points;
  1368. }
  1369.  
  1370. public int getPrayerPoints() {
  1371. int points = 1;
  1372. for(InvItem item : inventory.getItems()) {
  1373. if(item.isWielded()) {
  1374. points += item.getWieldableDef().getPrayerPoints();
  1375. }
  1376. }
  1377. return points < 1 ? 1 : points;
  1378. }
  1379.  
  1380. public int getRangePoints() {
  1381. int points = 1;
  1382. for(InvItem item : inventory.getItems()) {
  1383. if(item.isWielded()) {
  1384. points += item.getWieldableDef().getRangePoints();
  1385. }
  1386. }
  1387. return points < 1 ? 1 : points;
  1388. }
  1389.  
  1390. public MiscPacketBuilder getActionSender() {
  1391. return actionSender;
  1392. }
  1393.  
  1394. public int[] getWornItems() {
  1395. return wornItems;
  1396. }
  1397.  
  1398. public void updateWornItems(int index, int id) {
  1399. wornItems[index] = id;
  1400. super.ourAppearanceChanged = true;
  1401. }
  1402.  
  1403. public void setWornItems(int[] worn) {
  1404. wornItems = worn;
  1405. super.ourAppearanceChanged = true;
  1406. }
  1407.  
  1408. public Inventory getInventory() {
  1409. return inventory;
  1410. }
  1411.  
  1412. public void setInventory(Inventory i) {
  1413. inventory = i;
  1414. }
  1415.  
  1416. public Bank getBank() {
  1417. return bank;
  1418. }
  1419.  
  1420. public void setBank(Bank b) {
  1421. bank = b;
  1422. }
  1423.  
  1424. public void setGameSetting(int i, boolean b) {
  1425. gameSettings[i] = b;
  1426. }
  1427.  
  1428. public boolean getGameSetting(int i) {
  1429. return gameSettings[i];
  1430. }
  1431.  
  1432. public void setPrivacySetting(int i, boolean b) {
  1433. privacySettings[i] = b;
  1434. }
  1435.  
  1436. public boolean getPrivacySetting(int i) {
  1437. return privacySettings[i];
  1438. }
  1439.  
  1440. public long getLastPing() {
  1441. return lastPing;
  1442. }
  1443.  
  1444. public IoSession getSession() {
  1445. return ioSession;
  1446. }
  1447.  
  1448. public boolean loggedIn() {
  1449. return loggedIn;
  1450. }
  1451.  
  1452. public void setLoggedIn(boolean loggedIn) {
  1453. if(loggedIn) {
  1454. currentLogin = System.currentTimeMillis();
  1455. }
  1456. this.loggedIn = loggedIn;
  1457. }
  1458.  
  1459. public String getUsername() {
  1460. return username;
  1461. }
  1462.  
  1463. public long getUsernameHash() {
  1464. return usernameHash;
  1465. }
  1466.  
  1467. public String getPassword() {
  1468. return password;
  1469. }
  1470.  
  1471. public void ping() {
  1472. lastPing = System.currentTimeMillis();
  1473. }
  1474.  
  1475. public boolean isSkulled() {
  1476. return skullEvent != null;
  1477. }
  1478.  
  1479. public PlayerAppearance getPlayerAppearance() {
  1480. return appearance;
  1481. }
  1482.  
  1483. public void setAppearance(PlayerAppearance appearance) {
  1484. this.appearance = appearance;
  1485. }
  1486.  
  1487. public int getSkullTime() {
  1488. if(isSkulled()) {
  1489. return skullEvent.timeTillNextRun();
  1490. }
  1491. return 0;
  1492. }
  1493.  
  1494. public void addSkull(long timeLeft) {
  1495. if(!isSkulled()) {
  1496. skullEvent = new DelayedEvent(this, 1200000) {
  1497. public void run() {
  1498. removeSkull();
  1499. }
  1500. };
  1501. world.getDelayedEventHandler().add(skullEvent);
  1502. super.setAppearnceChanged(true);
  1503. }
  1504. skullEvent.setLastRun(System.currentTimeMillis() - (1200000 - timeLeft));
  1505. }
  1506.  
  1507. public void removeSkull() {
  1508. if(!isSkulled()) {
  1509. return;
  1510. }
  1511. super.setAppearnceChanged(true);
  1512. skullEvent.stop();
  1513. skullEvent = null;
  1514. }
  1515.  
  1516. public void setSuspiciousPlayer(boolean suspicious) {
  1517. this.suspicious = suspicious;
  1518. }
  1519.  
  1520. public void addPlayersAppearanceIDs(int[] indicies, int[] appearanceIDs) {
  1521. for (int x = 0; x < indicies.length; x++) {
  1522. knownPlayersAppearanceIDs.put(indicies[x], appearanceIDs[x]);
  1523. }
  1524. }
  1525.  
  1526. public List<Player> getPlayersRequiringAppearanceUpdate() {
  1527. List<Player> needingUpdates = new ArrayList<Player>();
  1528. needingUpdates.addAll(watchedPlayers.getNewEntities());
  1529. if (super.ourAppearanceChanged) {
  1530. needingUpdates.add(this);
  1531. }
  1532. for (Player p : watchedPlayers.getKnownEntities()) {
  1533. if (needsAppearanceUpdateFor(p)) {
  1534. needingUpdates.add(p);
  1535. }
  1536. }
  1537. return needingUpdates;
  1538. }
  1539.  
  1540. private boolean needsAppearanceUpdateFor(Player p) {
  1541. int playerServerIndex = p.getIndex();
  1542. if (knownPlayersAppearanceIDs.containsKey(playerServerIndex)) {
  1543. int knownPlayerAppearanceID = knownPlayersAppearanceIDs.get(playerServerIndex);
  1544. if(knownPlayerAppearanceID != p.getAppearanceID()) {
  1545. return true;
  1546. }
  1547. }
  1548. else {
  1549. return true;
  1550. }
  1551. return false;
  1552. }
  1553.  
  1554. public void updateViewedPlayers() {
  1555. List<Player> playersInView = viewArea.getPlayersInView();
  1556. for (Player p : playersInView) {
  1557. if (p.getIndex() != getIndex() && p.loggedIn()) {
  1558. this.informOfPlayer(p);
  1559. p.informOfPlayer(this);
  1560. }
  1561. }
  1562. }
  1563.  
  1564. public void updateViewedObjects() {
  1565. List<GameObject> objectsInView = viewArea.getGameObjectsInView();
  1566. for (GameObject o : objectsInView) {
  1567. if (!watchedObjects.contains(o) && !o.isRemoved() && withinRange(o)) {
  1568. watchedObjects.add(o);
  1569. }
  1570. }
  1571. }
  1572.  
  1573. public void updateViewedItems() {
  1574. List<Item> itemsInView = viewArea.getItemsInView();
  1575. for (Item i : itemsInView) {
  1576. if (!watchedItems.contains(i) && !i.isRemoved() && withinRange(i) && i.visibleTo(this)) {
  1577. watchedItems.add(i);
  1578. }
  1579. }
  1580. }
  1581.  
  1582. public void updateViewedNpcs() {
  1583. List<Npc> npcsInView = viewArea.getNpcsInView();
  1584. for (Npc n : npcsInView) {
  1585. if ((!watchedNpcs.contains(n) || watchedNpcs.isRemoving(n)) && withinRange(n)) {
  1586. watchedNpcs.add(n);
  1587. }
  1588. }
  1589. }
  1590.  
  1591. public void teleport(int x, int y, boolean bubble) {
  1592. Mob opponent = super.getOpponent();
  1593. if(inCombat()) {
  1594. resetCombat(CombatState.ERROR);
  1595. }
  1596. if(opponent != null) {
  1597. opponent.resetCombat(CombatState.ERROR);
  1598. }
  1599. for (Object o : getWatchedPlayers().getAllEntities()) {
  1600. Player p = ((Player)o);
  1601. if(bubble) {
  1602. p.getActionSender().sendTeleBubble(getX(), getY(), false);
  1603. }
  1604. p.removeWatchedPlayer(this);
  1605. }
  1606. if(bubble) {
  1607. actionSender.sendTeleBubble(getX(), getY(), false);
  1608. }
  1609. setLocation(Point.location(x, y), true);
  1610. resetPath();
  1611. actionSender.sendWorldInfo();
  1612. }
  1613.  
  1614. /**
  1615. * This is a 'another player has tapped us on the shoulder' method.
  1616. *
  1617. * If we are in another players viewArea, they should in theory be in ours.
  1618. * So they will call this method to let the player know they should probably
  1619. * be informed of them.
  1620. */
  1621. public void informOfPlayer(Player p) {
  1622. if ((!watchedPlayers.contains(p) || watchedPlayers.isRemoving(p)) && withinRange(p)) {
  1623. watchedPlayers.add(p);
  1624. }
  1625. }
  1626.  
  1627. public StatefulEntityCollection<Player> getWatchedPlayers() {
  1628. return watchedPlayers;
  1629. }
  1630.  
  1631. public StatefulEntityCollection<GameObject> getWatchedObjects() {
  1632. return watchedObjects;
  1633. }
  1634.  
  1635. public StatefulEntityCollection<Item> getWatchedItems() {
  1636. return watchedItems;
  1637. }
  1638.  
  1639. public StatefulEntityCollection<Npc> getWatchedNpcs() {
  1640. return watchedNpcs;
  1641. }
  1642.  
  1643. public void removeWatchedNpc(Npc n) {
  1644. watchedNpcs.remove(n);
  1645. }
  1646.  
  1647. public void removeWatchedPlayer(Player p) {
  1648. watchedPlayers.remove(p);
  1649. }
  1650.  
  1651. public void revalidateWatchedPlayers() {
  1652. for (Player p : watchedPlayers.getKnownEntities()) {
  1653. if (!withinRange(p) || !p.loggedIn()) {
  1654. watchedPlayers.remove(p);
  1655. knownPlayersAppearanceIDs.remove(p.getIndex());
  1656. }
  1657. }
  1658. }
  1659.  
  1660. public void revalidateWatchedObjects() {
  1661. for (GameObject o : watchedObjects.getKnownEntities()) {
  1662. if (!withinRange(o) || o.isRemoved()) {
  1663. watchedObjects.remove(o);
  1664. }
  1665. }
  1666. }
  1667.  
  1668. public void revalidateWatchedItems() {
  1669. for (Item i : watchedItems.getKnownEntities()) {
  1670. if (!withinRange(i) || i.isRemoved() || !i.visibleTo(this)) {
  1671. watchedItems.remove(i);
  1672. }
  1673. }
  1674. }
  1675.  
  1676. public void revalidateWatchedNpcs() {
  1677. for (Npc n : watchedNpcs.getKnownEntities()) {
  1678. if (!withinRange(n) || n.isRemoved()) {
  1679. watchedNpcs.remove(n);
  1680. }
  1681. }
  1682. }
  1683.  
  1684. public boolean withinRange(Entity e) {
  1685. int xDiff = location.getX() - e.getLocation().getX();
  1686. int yDiff = location.getY() - e.getLocation().getY();
  1687. return xDiff <= 16 && xDiff >= -15 && yDiff <= 16 && yDiff >= -15;
  1688. }
  1689.  
  1690. public int[] getCurStats() {
  1691. return curStat;
  1692. }
  1693.  
  1694. public int getCurStat(int id) {
  1695. return curStat[id];
  1696. }
  1697.  
  1698. public int getHits() {
  1699. return getCurStat(3);
  1700. }
  1701.  
  1702. public int getAttack() {
  1703. return getCurStat(0);
  1704. }
  1705.  
  1706. public int getDefense() {
  1707. return getCurStat(1);
  1708. }
  1709.  
  1710. public int getStrength() {
  1711. return getCurStat(2);
  1712. }
  1713.  
  1714. public void setHits(int lvl) {
  1715. setCurStat(3, lvl);
  1716. }
  1717.  
  1718. public void setCurStat(int id, int lvl) {
  1719. if(lvl <= 0) {
  1720. lvl = 0;
  1721. }
  1722. curStat[id] = lvl;
  1723. }
  1724.  
  1725. public int getMaxStat(int id) {
  1726. return maxStat[id];
  1727. }
  1728.  
  1729. public void setMaxStat(int id, int lvl) {
  1730. if(lvl < 0) {
  1731. lvl = 0;
  1732. }
  1733. maxStat[id] = lvl;
  1734. }
  1735.  
  1736. public int[] getMaxStats() {
  1737. return maxStat;
  1738. }
  1739.  
  1740. public int getSkillTotal() {
  1741. int total = 0;
  1742. for(int stat : maxStat) {
  1743. total += stat;
  1744. }
  1745. return total;
  1746. }
  1747.  
  1748. public void incCurStat(int i, int amount) {
  1749. curStat[i] += amount;
  1750. if(curStat[i] < 0) {
  1751. curStat[i] = 0;
  1752. }
  1753. }
  1754.  
  1755. public void incMaxStat(int i, int amount) {
  1756. maxStat[i] += amount;
  1757. if(maxStat[i] < 0) {
  1758. maxStat[i] = 0;
  1759. }
  1760. }
  1761.  
  1762. public void setFatigue(int fatigue) {
  1763. this.fatigue = fatigue;
  1764. }
  1765.  
  1766. public int getFatigue() {
  1767. return fatigue;
  1768. }
  1769.  
  1770. public void incExp(int i, int amount, boolean useFatigue) {
  1771. useFatigue = true;
  1772. if(useFatigue) {
  1773. if(fatigue >= 100) {
  1774. actionSender.sendMessage("@gre@You are too tired to gain experience, get some rest!");
  1775. return;
  1776. }
  1777. if(fatigue >= 96) {
  1778. actionSender.sendMessage("@gre@You start to feel tired, maybe you should rest soon.");
  1779. }
  1780. fatigue++;
  1781. actionSender.sendFatigue();
  1782. }
  1783. if(isSubscriber()) {
  1784. amount *= 1.5D;
  1785. if(isSubscriber() && getLocation().wildernessLevel() > 0) {
  1786. amount *= 1.33333D; // 2.5x more exp as subscriber and in the wilderness
  1787. }
  1788. if(isMember()) {
  1789. amount *= 1D; // 0.5x more exp if they are normal member
  1790. }
  1791. if(isMember() && getLocation().wildernessLevel() > 0) {
  1792. amount *= 1.5D; // 1.5x more exp if they are normal member and in the wilderness
  1793. }
  1794. }
  1795. exp[i] += amount;
  1796. if(exp[i] < 0) {
  1797. exp[i] = 0;
  1798. }
  1799. int level = Formulae.experienceToLevel(exp[i]);
  1800. if(level != maxStat[i]) {
  1801. int advanced = level - maxStat[i];
  1802. incCurStat(i, advanced);
  1803. incMaxStat(i, advanced);
  1804. actionSender.sendStat(i);
  1805. actionSender.sendMessage("@gre@You just advanced " + advanced + " " + Formulae.statArray[i] + " level!");
  1806. actionSender.sendSound("advance");
  1807. world.getDelayedEventHandler().add(new MiniEvent(this) {
  1808. public void action() {
  1809. owner.getActionSender().sendScreenshot();
  1810. }
  1811. });
  1812. int comb = Formulae.getCombatlevel(maxStat);
  1813. if(comb != getCombatLevel()) {
  1814. setCombatLevel(comb);
  1815. }
  1816. }
  1817. }
  1818.  
  1819. public int[] getExps() {
  1820. return exp;
  1821. }
  1822.  
  1823. public int getExp(int id) {
  1824. return exp[id];
  1825. }
  1826.  
  1827. public void setExp(int id, int lvl) {
  1828. if(lvl < 0) {
  1829. lvl = 0;
  1830. }
  1831. exp[id] = lvl;
  1832. }
  1833.  
  1834. public void setExp(int[] lvls) {
  1835. exp = lvls;
  1836. }
  1837.  
  1838. public boolean equals(Object o) {
  1839. if (o instanceof Player) {
  1840. Player p = (Player)o;
  1841. return usernameHash == p.getUsernameHash();
  1842. }
  1843. return false;
  1844. }
  1845.  
  1846. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement