Advertisement
Guest User

Untitled

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