public static bool leaveCrypt(Player player, int stairs, int x, int y) {
		    if (stairs != 6707 && stairs != 6703 && stairs != 6702 && stairs != 6704 && stairs != 6705 && stairs != 6706) {
			    return false;
		    }
		    Player p = player;
		    Location stairLocation;
		    Location moundLocation;
		    int cryptIndex = getCryptIndex(p);
		    if (cryptIndex == -1) {
			    return false;
		    }
            stairLocation = new Location(STAIR_COORDS[cryptIndex][0], STAIR_COORDS[cryptIndex][1], 3);
		    moundLocation = new Location(MOUND_COORDS[cryptIndex][0] + misc.random(3), MOUND_COORDS[cryptIndex][1] + misc.random(3), 0);
		    if (p.getLocation().Equals(stairLocation)) {
			    p.setFaceLocation(new Location(x, y, 3));
			    p.teleport(moundLocation);
			    return true;
		    }
            CoordinateEvent teleportMoundCoordinateEvent = new CoordinateEvent(p, stairLocation);
            teleportMoundCoordinateEvent.setAction(() => {
				    p.teleport(moundLocation);
		    });
            Server.registerCoordinateEvent(teleportMoundCoordinateEvent);
		    return true;
	    }
	    public static bool useAnimator(Player p, int itemId, int objectId, int objectX, int objectY) {
		    if (objectId != ANIMATOR_ID) {
			    return false;
		    }
		    int animatorIndex = -1;
		    for (int i = 0; i < ANIMATOR_LOCATIONS.Length; i++) {
			    if (objectX == ANIMATOR_LOCATIONS[i][0] && objectY == ANIMATOR_LOCATIONS[i][1]) {
				    animatorIndex = i;
				    break;
			    }
		    }
		    if (animatorIndex == -1) {
			    return false;
		    }
		    p.setTemporaryAttribute("warriorGuildAnimator", animatorIndex);
            CoordinateEvent createAnimatedArmorCoordinateEvent = new CoordinateEvent(p, new Location(ANIMATOR_LOCATIONS[animatorIndex][0], (ANIMATOR_LOCATIONS[animatorIndex][1] + 1), 0));
            createAnimatedArmorCoordinateEvent.setAction(() => {
				int armourIndex = hasArmour(p, itemId);
				if (armourIndex != -1) {
					createdAnimatedArmour(p, armourIndex);
				}
		    });
            Server.registerCoordinateEvent(createAnimatedArmorCoordinateEvent);
		    return true;
	    }
	    public static void handleLever(Player p, int id, Location loc) {
		    if ((p.getTemporaryAttribute("teleporting") != null )) {
			    return;
		    }
		    for (int i = 0 ; i < LEVER_COORDINATES.Length; i++) {
			    Location loc1 = new Location(LEVER_COORDINATES[i][0], LEVER_COORDINATES[i][1], LEVER_COORDINATES[i][2]);
			    if (loc.Equals(loc1)) {
				    int index = i;
				    Location loc2 = new Location(LEVER_FACE_COORDINATES[i][0], LEVER_FACE_COORDINATES[i][1], LEVER_FACE_COORDINATES[i][2]);
                    CoordinateEvent wildernessHandleLeverCoordinateEvent = new CoordinateEvent(p, loc);
                    wildernessHandleLeverCoordinateEvent.setAction(() => {
						p.setFaceLocation(loc2);
						displayWildernessLeverOptions(p, index);
				    });
                    Server.registerCoordinateEvent(wildernessHandleLeverCoordinateEvent);
				    return;
			    }
		    }
		    LaddersAndStairs.useLever(p, id, loc); // Used for default levers/levers with no options
	    }
 public static bool enterRift(Player p, int objectId, int x, int y)
 {
     for (int i = 0; i < ABYSS_DOORWAYS.Length; i++) {
         if (objectId == ABYSS_DOORWAYS[i]) {
             if (i == 13) {
                 return true;
             }
             int j = i;
             CoordinateEvent enterRiftCoordinateEvent = new CoordinateEvent(p, new Location(x, y, 0));
             enterRiftCoordinateEvent.setAction(() => {
                 if (j == 12) {
                     p.getPackets().sendMessage("This altar is currently unavailable due to mapdata issues.");
                     return;
                 }
                 p.teleport(new Location(ALTAR_COORDS[j][0], (ALTAR_COORDS[j][1] + 3), 0));
                 faceAltar(p, j);
             });
             Server.registerCoordinateEvent(enterRiftCoordinateEvent);
             return true;
         }
     }
     return false;
 }
	    public void useDoor(Player p, int doorId) {
		    bool running =  p.getWalkingQueue().isRunToggled();
		    if (doorId == 9369) { // main entrance
			    int y = p.getLocation().getY() >= 5177 ? 5177 : 5175;
                CoordinateEvent mainEntranceCoordinateEvent = new CoordinateEvent(p, new Location(2399, y, 0));
                mainEntranceCoordinateEvent.setAction(() => {
					p.getWalkingQueue().setRunToggled(false);
                    p.getWalkingQueue().resetWalkingQueue();
					p.getPackets().clearMapFlag();
					p.setTemporaryAttribute("unmovable", true);
					p.getWalkingQueue().forceWalk(0, y == 5177 ? -2 : +2);
                    Event useMainDoorEvent = new Event(1000);
                    useMainDoorEvent.setAction(() => {
                        useMainDoorEvent.stop();
						p.removeTemporaryAttribute("unmovable");
						p.getWalkingQueue().setRunToggled(running);
					});
                    Server.registerEvent(useMainDoorEvent);
			    });
                Server.registerCoordinateEvent(mainEntranceCoordinateEvent);
            }
            else if (doorId == 9368)
            { // game door
                int y = p.getLocation().getY() >= 5169 ? 5169 : 5167;
                CoordinateEvent gameDoorCoordinateEvent = new CoordinateEvent(p, new Location(2399, y, 0));
                gameDoorCoordinateEvent.setAction(() =>
                {
                    if (playersPlaying.Count == 1)
                    {
                        sendNPCMessage(p, "Here is some TokKul, as a reward.");
                        p.getInventory().addItemOrGround(6529, tokkulReward);
                    }
                    removePlayingPlayer(p);
                    p.getWalkingQueue().setRunToggled(false);
                    p.getWalkingQueue().resetWalkingQueue();
                    p.getPackets().clearMapFlag();
                    if (y == 5167)
                    {
                        p.getWalkingQueue().forceWalk(0, +2);
                    }
                    else
                    {
                        p.getPackets().sendMessage("You are unable to bypass the hot barrier.");
                        return;
                    }
                    p.setTemporaryAttribute("unmovable", true);
                    Event gameDoorEvent = new Event(1000);
                    gameDoorEvent.setAction(() =>
                    {
                        gameDoorEvent.stop();
                        p.removeTemporaryAttribute("unmovable");
                        p.getWalkingQueue().setRunToggled(running);
                        p.getPackets().sendMessage("You leave the fight pit.");
                        resetVariables(p);
                    });
                    Server.registerEvent(gameDoorEvent);
                });
                Server.registerCoordinateEvent(gameDoorCoordinateEvent);
            }
	    }
	    public static void enterArena(Player p, int objectX, int objectY) {
            CoordinateEvent enterArenaCoordinateEvent = new CoordinateEvent(p, new Location(2809, 3193, 0));
            enterArenaCoordinateEvent.setAction(() => {
				if (!p.hasPaidAgilityArena()) {
					p.getPackets().sendMessage("You must pay Cap'n Izzy the entrance fee before you can enter the Agility Arena.");
					return;
				}
				p.setLastAnimation(new Animation(827));
                Event teleportArenaEvent = new Event(1000);
                teleportArenaEvent.setAction(() => {
					p.setPaidAgilityArena(false);
					p.teleport(new Location(2805, 9589, 3));
					teleportArenaEvent.stop();
				});
                Server.registerEvent(teleportArenaEvent);
		    });
            Server.registerCoordinateEvent(enterArenaCoordinateEvent);
	    }
	    public static void exitArena(Player p, int objectX, int objectY) {
            CoordinateEvent exitArenaCoordinateEvent = new CoordinateEvent(p, new Location(2805, 9589, 3));
            exitArenaCoordinateEvent.setAction(() => {
				p.setLastAnimation(new Animation(828));
                Event exitArenaEvent = new Event(1000);
                exitArenaEvent.setAction(() => {
					p.teleport(new Location(2809, 3193, 0));
					exitArenaEvent.stop();
				});
                Server.registerEvent(exitArenaEvent);
		    });
            Server.registerCoordinateEvent(exitArenaCoordinateEvent);
	    }
        public static void doCourse(Player p, int objectX, int objectY, object[] objectArray)
        {
            if (p.getTemporaryAttribute("unmovable") != null) {
                return;
            }
            int agilityStage = (int) (p.getTemporaryAttribute("agilityStage") == null ? 0 : p.getTemporaryAttribute("agilityStage"));
            switch((int)objectArray[0]) {
                case 2295: // Log
                    CoordinateEvent doLogCoordinateEvent = new CoordinateEvent(p, new Location((int)objectArray[3], (int)objectArray[4], 0));
                    doLogCoordinateEvent.setAction(() => {
                        shoutNPCs[0].setForceText(SHOUT_MESSAGES[0]);
                        p.getPackets().sendMessage("You walk carefully across the slippery log...");
                        bool running = p.getWalkingQueue().isRunToggled();
                        p.getWalkingQueue().setRunToggled(false);
                        p.getWalkingQueue().resetWalkingQueue();
                        p.getPackets().clearMapFlag();
                        p.setTemporaryAttribute("unmovable", true);
                        p.getAppearance().setWalkAnimation(155);
                        p.getUpdateFlags().setAppearanceUpdateRequired(true);
                        p.getWalkingQueue().forceWalk(0, -7);
                        Event doLogEvent = new Event(4300);
                            doLogEvent.setAction(() => {
                                doLogEvent.stop();
                                p.getPackets().sendMessage("...and make it safely to the other side.");
                                p.removeTemporaryAttribute("unmovable");
                                p.getAppearance().setWalkAnimation(-1);
                                p.getUpdateFlags().setAppearanceUpdateRequired(true);
                                p.getSkills().addXp(Skills.SKILL.AGILITY, (double)objectArray[7]);
                                p.getWalkingQueue().setRunToggled(running);
                        });
                        Server.registerEvent(doLogEvent);
                    });
                    Server.registerCoordinateEvent(doLogCoordinateEvent);
                    break;

                case 2285: // Net #1
                    AreaEvent doNetOneAreaEvent = new AreaEvent(p, 2471, 3426, 2476, 3426);
                    doNetOneAreaEvent.setAction(() => {
                        shoutNPCs[1].setForceText(SHOUT_MESSAGES[1]);
                        p.getPackets().sendMessage("You climb the netting...");
                        p.setLastAnimation(new Animation(828));
                        p.getWalkingQueue().resetWalkingQueue();
                        p.getPackets().clearMapFlag();
                        p.setFaceLocation(new Location(p.getLocation().getX(), p.getLocation().getY()-1, 0));
                        p.setTemporaryAttribute("unmovable", true);
                        Event doNetOneEvent = new Event(1000);
                        doNetOneEvent.setAction(() => {
                            doNetOneEvent.stop();
                            p.removeTemporaryAttribute("unmovable");
                            p.teleport(new Location(2473, p.getLocation().getY()-2, 1));
                            p.getSkills().addXp(Skills.SKILL.AGILITY, (double)objectArray[7]);
                        });
                        Server.registerEvent(doNetOneEvent);
                    });
                    Server.registerCoordinateEvent(doNetOneAreaEvent);
                    break;

                case 35970: // Tree climb
                    AreaEvent treeClimbAreaEvent = new AreaEvent(p, 2472, 3422, 2474, 3423);
                    treeClimbAreaEvent.setAction(() => {
                        shoutNPCs[2].setForceText(SHOUT_MESSAGES[2]);
                        p.getPackets().sendMessage("You climb the tree...");
                        p.setLastAnimation(new Animation(828));
                        p.getWalkingQueue().resetWalkingQueue();
                        p.getPackets().clearMapFlag();
                        p.setFaceLocation(new Location(2473, 3422, 1));
                        p.setTemporaryAttribute("unmovable", true);
                        Event treeClimbEvent = new Event(1000);
                        treeClimbEvent.setAction(() => {
                            treeClimbEvent.stop();
                            p.getPackets().sendMessage("...to the platform above.");
                            p.removeTemporaryAttribute("unmovable");
                            p.teleport(new Location(2473, 3420, 2));
                            p.getSkills().addXp(Skills.SKILL.AGILITY, (double)objectArray[7]);
                        });
                        Server.registerEvent(treeClimbEvent);
                    });
                    Server.registerCoordinateEvent(treeClimbAreaEvent);
                    break;

                case 2312: // Rope balance
                    CoordinateEvent ropeBalanceCoordinateEvent = new CoordinateEvent(p, new Location((int)objectArray[3], (int)objectArray[4], 2));
                    ropeBalanceCoordinateEvent.setAction(() => {
                        shoutNPCs[3].setForceText(SHOUT_MESSAGES[3]);
                        p.getPackets().sendMessage("You carefully cross the tightrope.");
                        bool running = p.getWalkingQueue().isRunToggled();
                        p.getWalkingQueue().setRunToggled(false);
                        p.getWalkingQueue().resetWalkingQueue();
                        p.getPackets().clearMapFlag();
                        p.setTemporaryAttribute("unmovable", true);
                        p.getAppearance().setWalkAnimation(155);
                        p.getUpdateFlags().setAppearanceUpdateRequired(true);
                        p.getWalkingQueue().forceWalk(+6, 0);
                        Event ropeBalanceEvent = new Event(3700);
                        ropeBalanceEvent.setAction(() => {
                                ropeBalanceEvent.stop();
                                p.removeTemporaryAttribute("unmovable");
                                p.getAppearance().setWalkAnimation(-1);
                                p.getUpdateFlags().setAppearanceUpdateRequired(true);
                                p.getSkills().addXp(Skills.SKILL.AGILITY, (double)objectArray[7]);
                                p.getWalkingQueue().setRunToggled(running);
                        });
                        Server.registerEvent(ropeBalanceEvent);

                    });
                    Server.registerCoordinateEvent(ropeBalanceCoordinateEvent);
                    break;

                case 2314: // Tree climb #2
                    AreaEvent treeclimbTwoAreaEvent = new AreaEvent(p, 2485, 3418, 2486, 3420);
                    treeclimbTwoAreaEvent.setAction(() => {
                        p.getPackets().sendMessage("You climb down the tree...");
                        p.setLastAnimation(new Animation(828));
                        p.getWalkingQueue().resetWalkingQueue();
                        p.getPackets().clearMapFlag();
                        p.setFaceLocation(new Location(2486, 3419, 2));
                        p.setTemporaryAttribute("unmovable", true);
                        Event treeClimbTwoEvent = new Event(1000);
                        treeClimbTwoEvent.setAction(() => {
                            treeClimbTwoEvent.stop();
                            p.getPackets().sendMessage("You land on the ground.");
                            p.removeTemporaryAttribute("unmovable");
                            p.teleport(new Location(2486, 3420, 0));
                            p.getSkills().addXp(Skills.SKILL.AGILITY, (double)objectArray[7]);
                        });
                        Server.registerEvent(treeClimbTwoEvent);
                    });
                    Server.registerCoordinateEvent(treeclimbTwoAreaEvent);
                    break;

                case 2286: // Net #2
                    AreaEvent doNetTwoAreaEvent = new AreaEvent(p, 2483, 3425, 2488, 3425);
                    doNetTwoAreaEvent.setAction(() => {
                        shoutNPCs[4].setForceText(SHOUT_MESSAGES[4]);
                        p.getPackets().sendMessage("You climb the netting...");
                        p.setLastAnimation(new Animation(828));
                        p.getWalkingQueue().resetWalkingQueue();
                        p.getPackets().clearMapFlag();
                        p.setFaceLocation(new Location(p.getLocation().getX(), p.getLocation().getY()+1, 0));
                        p.setTemporaryAttribute("unmovable", true);
                        Event doNetTwoEvent = new Event(1000);
                        doNetTwoEvent.setAction(() => {
                            doNetTwoEvent.stop();
                            p.removeTemporaryAttribute("unmovable");
                            p.teleport(new Location(p.getLocation().getX(), p.getLocation().getY()+2, 0));
                            p.getSkills().addXp(Skills.SKILL.AGILITY, (double)objectArray[7]);
                        });
                        Server.registerEvent(doNetTwoEvent);
                    });
                    Server.registerCoordinateEvent(doNetTwoAreaEvent);
                    break;

                case 4058: // Right obstacle pipe
                    AreaEvent passRightObstraclePipeAreaEvent = new AreaEvent(p, 2486, 3430, 2488, 3431);
                    passRightObstraclePipeAreaEvent.setAction(() => {
                        int newMove = 0;
                        int pX = p.getLocation().getX();
                        int pY = p.getLocation().getY();
                        if (pX == objectX + 1 && pY == objectY)
                            newMove = 1;
                        else if (pX == objectX - 1 && pY == objectY)
                            newMove = 2;
                        if (newMove > 0) {
                            int path = newMove;
                            int doCourseCounter = 0;
                            Event doCourseEvent = new Event(500);
                            doCourseEvent.setAction(() => {
                                if (doCourseCounter == 0)
                                {
                                    p.getWalkingQueue().forceWalk(0, -1);
                                }
                                else if (doCourseCounter == 1)
                                {
                                    p.getWalkingQueue().forceWalk(path == 1 ? -1 : +1, 0);
                                } else {
                                    doCourse(p, objectX, objectY, objectArray);
                                    doCourseEvent.stop();
                                }
                                doCourseCounter++;
                            });
                            Server.registerEvent(doCourseEvent);
                            return;
                        }
                        Event squeezeIntoRightPipeEvent = new Event(0);
                        squeezeIntoRightPipeEvent.setAction(() => {
                            squeezeIntoRightPipeEvent.stop();
                            p.getPackets().sendMessage("You squeeze into the pipe...");
                            int regionX = p.getUpdateFlags().getLastRegion().getRegionX();
                            int regionY = p.getUpdateFlags().getLastRegion().getRegionY();
                            int lX = (p.getLocation().getX() - ((regionX - 6) * 8));
                            int lY = (p.getLocation().getY() - ((regionY - 6) * 8));
                            p.setForceMovement(new ForceMovement(lX, lY, lX, lY + 4, 10, 60, 0));
                            p.setFaceLocation(new Location(p.getLocation().getX(), p.getLocation().getY() + 1, 0));
                            p.setLastAnimation(new Animation(10578));
                            bool running = p.getWalkingQueue().isRunToggled();
                            p.getWalkingQueue().setRunToggled(false);
                            p.getWalkingQueue().resetWalkingQueue();
                            p.getPackets().clearMapFlag();
                            p.setTemporaryAttribute("unmovable", true);
                            int squeezeOutOfRightPipeCounter = 0;
                            Event squeezeOutOfRightPipeEvent = new Event(1150);
                            squeezeOutOfRightPipeEvent.setAction(() => {
                                if (squeezeOutOfRightPipeCounter == 0)
                                {
                                    p.teleport(new Location(p.getLocation().getX(), p.getLocation().getY() + 3, 0));
                                    squeezeOutOfRightPipeEvent.setTick(450);
                                    squeezeOutOfRightPipeCounter++;
                                    return;
                                }
                                else if (squeezeOutOfRightPipeCounter == 1)
                                {
                                    p.setForceMovement(new ForceMovement(lX, lY + 3, lX, lY + 6, 10, 60, 0));
                                    squeezeOutOfRightPipeEvent.setTick(900);
                                    squeezeOutOfRightPipeCounter++;
                                }
                                else if (squeezeOutOfRightPipeCounter == 2)
                                {
                                    squeezeOutOfRightPipeEvent.setTick(500);
                                    p.setLastAnimation(new Animation(10579));
                                    p.setForceMovement(new ForceMovement(lX, lY + 6, lX, lY + 7, 10, 40, 0));
                                    squeezeOutOfRightPipeCounter++;
                                } else {
                                    p.getPackets().sendMessage("...You squeeze out of the pipe.");
                                    squeezeOutOfRightPipeEvent.stop();
                                    p.removeTemporaryAttribute("unmovable");
                                    p.getSkills().addXp(Skills.SKILL.AGILITY, (double)objectArray[7]);
                                    p.getWalkingQueue().setRunToggled(running);
                                    p.teleport(new Location(p.getLocation().getX(), p.getLocation().getY() + 5, 0));
                                }
                            });
                            Server.registerEvent(squeezeOutOfRightPipeEvent);
                        });
                        Server.registerEvent(squeezeIntoRightPipeEvent);
                    });
                    Server.registerCoordinateEvent(passRightObstraclePipeAreaEvent);
                    break;

                case 154: // Left obstacle pipe
                    AreaEvent passLeftObstraclePipeAreaEvent = new AreaEvent(p, 2483, 3430, 2485, 3431);
                    passLeftObstraclePipeAreaEvent.setAction(() => {
                        int newMove = 0;
                        int pX = p.getLocation().getX();
                        int pY = p.getLocation().getY();
                        if (pX == objectX + 1 && pY == objectY)
                            newMove = 1;
                        else if (pX == objectX - 1 && pY == objectY)
                            newMove = 2;
                        else if (pX == 2483)
                            newMove = 3;
                        else if (pX == 2485)
                            newMove = 4;
                        if (newMove > 0) {
                            int path = newMove;
                            int doCourseCounter = 0;
                            Event doCourseEvent = new Event(500);
                            doCourseEvent.setAction(() => {
                                if (path == 3 || path == 4) {
                                    p.getWalkingQueue().forceWalk(path == 3 ? +1 : -1, 0);
                                    doCourseCounter = 2;
                                }
                                if (doCourseCounter == 0)
                                {
                                    p.getWalkingQueue().forceWalk(0, -1);
                                }
                                else if (doCourseCounter == 1)
                                {
                                    p.getWalkingQueue().forceWalk(path == 1 ? -1 : +1, 0);
                                } else {
                                    doCourse(p, objectX, objectY, objectArray);
                                    doCourseEvent.stop();
                                }
                                doCourseCounter++;
                            });
                            Server.registerEvent(doCourseEvent);
                            return;
                        }
                        Event squeezeIntoLeftPipeEvent = new Event(0);
                        squeezeIntoLeftPipeEvent.setAction(() => {
                            squeezeIntoLeftPipeEvent.stop();
                            p.getPackets().sendMessage("You squeeze into the pipe...");
                            int regionX = p.getUpdateFlags().getLastRegion().getRegionX();
                            int regionY = p.getUpdateFlags().getLastRegion().getRegionY();
                            int lX = (p.getLocation().getX() - ((regionX - 6) * 8));
                            int lY = (p.getLocation().getY() - ((regionY - 6) * 8));
                            p.setForceMovement(new ForceMovement(lX, lY, lX, lY + 3, 10, 60, 0));
                            p.setFaceLocation(new Location(p.getLocation().getX(), p.getLocation().getY() + 1, 0));
                            p.setLastAnimation(new Animation(10578));
                            bool running = p.getWalkingQueue().isRunToggled();
                            p.getWalkingQueue().setRunToggled(false);
                            p.getWalkingQueue().resetWalkingQueue();
                            p.getPackets().clearMapFlag();
                            p.setTemporaryAttribute("unmovable", true);
                            int squeezeOutOfLeftPipeCounter = 0;
                            Event squeezeOutOfLeftPipeEvent = new Event(1150);
                            squeezeOutOfLeftPipeEvent.setAction(() => {
                                if (squeezeOutOfLeftPipeCounter == 0)
                                {
                                    p.teleport(new Location(p.getLocation().getX(), p.getLocation().getY() + 3, 0));
                                    squeezeOutOfLeftPipeEvent.setTick(450);
                                    squeezeOutOfLeftPipeCounter++;
                                    return;
                                }
                                else if (squeezeOutOfLeftPipeCounter == 1)
                                {
                                    p.setForceMovement(new ForceMovement(lX, lY + 3, lX, lY + 6, 10, 60, 0));
                                    squeezeOutOfLeftPipeEvent.setTick(900);
                                    squeezeOutOfLeftPipeCounter++;
                                }
                                else if (squeezeOutOfLeftPipeCounter == 2)
                                {
                                    squeezeOutOfLeftPipeEvent.setTick(500);
                                    p.setLastAnimation(new Animation(10579));
                                    p.setForceMovement(new ForceMovement(lX, lY + 6, lX, lY + 7, 10, 40, 0));
                                    squeezeOutOfLeftPipeCounter++;
                                } else {
                                    p.getPackets().sendMessage("...You squeeze out of the pipe.");
                                    squeezeOutOfLeftPipeEvent.stop();
                                    p.removeTemporaryAttribute("unmovable");
                                    p.getSkills().addXp(Skills.SKILL.AGILITY, (double)objectArray[7]);
                                    p.getWalkingQueue().setRunToggled(running);
                                    p.teleport(new Location(p.getLocation().getX(), p.getLocation().getY() + 5, 0));
                                }
                            });
                            Server.registerEvent(squeezeOutOfLeftPipeEvent);
                        });
                        Server.registerEvent(squeezeIntoLeftPipeEvent);
                    });
                    Server.registerCoordinateEvent(passLeftObstraclePipeAreaEvent);
                    break;
            }
        }
	    public static void doObstacle(Player p, int index) {
		    if (p.getTemporaryAttribute("unmovable") != null) {
			    return;
		    }
            switch (index)
            {
                case 0: // 3 planks, northern (east side)
                case 1: // 3 planks, middle (east side)
                case 2: // 3 planks, southern (east side)
                case 3: // 3 planks, northern (west side)
                case 4: // 3 planks, middle (west side)
                case 5: // 3 planks, southern (west side)
                    int logXCoord = index <= 2 ? (int)AGILITY_ARENA_OBJECTS[index][1] + 1 : (int)AGILITY_ARENA_OBJECTS[index][1] - 1;
                    int logDirectionX = index <= 2 ? -7 : +7;
                    CoordinateEvent plankObstaclesCoordinateEvent = new CoordinateEvent(p, new Location(logXCoord, (int)AGILITY_ARENA_OBJECTS[index][2], 3));
                    plankObstaclesCoordinateEvent.setAction(() =>
                    {
                        bool running = p.getWalkingQueue().isRunToggled();
                        p.getWalkingQueue().setRunToggled(false);
                        p.getWalkingQueue().resetWalkingQueue();
                        p.getPackets().clearMapFlag();
                        p.setTemporaryAttribute("unmovable", true);
                        p.getAppearance().setWalkAnimation(155);
                        p.getUpdateFlags().setAppearanceUpdateRequired(true);
                        p.getWalkingQueue().forceWalk(logDirectionX, 0);
                        Event plankObstaclesEvent = new Event(4300);
                        plankObstaclesEvent.setAction(() =>
                        {
                            plankObstaclesEvent.stop();
                            p.removeTemporaryAttribute("unmovable");
                            p.getAppearance().setWalkAnimation(-1);
                            p.getUpdateFlags().setAppearanceUpdateRequired(true);
                            p.getSkills().addXp(Skills.SKILL.AGILITY, (double)AGILITY_ARENA_OBJECTS[index][3]);
                            p.getWalkingQueue().setRunToggled(running);
                        });
                        Server.registerEvent(plankObstaclesEvent);
                    });
                    Server.registerCoordinateEvent(plankObstaclesCoordinateEvent);
                    break;

                case 6: //Handholds obstacle east of planks.
                case 7: //Handholds obstacle west of planks.
                    int handHoldsDirectionX = 1;
                    int handHoldsDirectionY = 1;
                    if (index == 6) {
                        handHoldsDirectionX = -1;
                        handHoldsDirectionY = +1;
                    } else if (index == 7) {
                        handHoldsDirectionX = +1;
                        handHoldsDirectionY = -1;
                    }
                    CoordinateEvent handholdsObstacleCoordinateEvent = new CoordinateEvent(p, new Location((int)AGILITY_ARENA_OBJECTS[index][1], (int)AGILITY_ARENA_OBJECTS[index][2], 3));
                    handholdsObstacleCoordinateEvent.setAction(() =>
                    {
                        bool running = p.getWalkingQueue().isRunToggled();
                        p.getWalkingQueue().setRunToggled(false);
                        p.getWalkingQueue().resetWalkingQueue();
                        p.getPackets().clearMapFlag();
                        p.setTemporaryAttribute("unmovable", true);
                        p.setLastAnimation(new Animation(1121));
                        p.setFaceLocation(new Location(p.getLocation().getX(), p.getLocation().getY() + handHoldsDirectionY, 3));
                        Event handholdsObstaclesEvent = new Event(700);
                        int handholdsObstaclesCounter = 0;
                        handholdsObstaclesEvent.setAction(() =>
                        {
                            p.setLastAnimation(new Animation(1122));
                            int regionX = p.getUpdateFlags().getLastRegion().getRegionX();
                            int regionY = p.getUpdateFlags().getLastRegion().getRegionY();
                            int lX = (p.getLocation().getX() - ((regionX - 6) * 8));
                            int lY = (p.getLocation().getY() - ((regionY - 6) * 8));
                            p.setForceMovement(new ForceMovement(lX, lY, lX + handHoldsDirectionX, lY, 0, 5, 0));
                            if (handholdsObstaclesCounter++ >= 7)
                            {
                                p.setLastAnimation(new Animation(65535));
                                handholdsObstaclesEvent.stop();
                                p.removeTemporaryAttribute("unmovable");
                                p.getSkills().addXp(Skills.SKILL.AGILITY, (double)AGILITY_ARENA_OBJECTS[index][3]);
                                p.getWalkingQueue().setRunToggled(running);
                                return;
                            }
                            Event teleportEvent = new Event(500);
                            teleportEvent.setAction(() =>
                            {
                                teleportEvent.stop();
                                p.teleport(new Location(p.getLocation().getX() + handHoldsDirectionX, p.getLocation().getY(), 3));
                            });
                            Server.registerEvent(teleportEvent);
                        });
                        Server.registerEvent(handholdsObstaclesEvent);
                    });
                    Server.registerCoordinateEvent(handholdsObstacleCoordinateEvent);
                    break;
            }
	    }
        public static void doCourse(Player p, int objectX, int objectY, object[] objectArray)
        {
            if (p.getTemporaryAttribute("unmovable") != null) {
                return;
            }
            switch((int)objectArray[0]) {
                case 2309: //Entrance log
                    CoordinateEvent startEntranceLogCoordinateEvent = new CoordinateEvent(p, new Location((int)objectArray[1], (int)objectArray[2], 0));
                    startEntranceLogCoordinateEvent.setAction(() => {
                        bool running = p.getWalkingQueue().isRunToggled();
                        Event comeToLogEvent = new Event(500);
                        comeToLogEvent.setAction(() => {
                            p.getWalkingQueue().setRunToggled(false);
                            p.getWalkingQueue().resetWalkingQueue();
                            p.getPackets().clearMapFlag();
                            p.setTemporaryAttribute("unmovable", true);
                            p.getWalkingQueue().forceWalk(0, 1); //go past gate, no animation yet.
                            comeToLogEvent.stop();
                        });
                        Server.registerEvent(comeToLogEvent);
                        int doLogWalkCounter = 0;
                        Event doLogWalkEvent = new Event(800);
                        doLogWalkEvent.setAction(() => {
                            if (doLogWalkCounter == 0) { //start the animation
                                p.getAppearance().setWalkAnimation(155);
                                p.getUpdateFlags().setAppearanceUpdateRequired(true);
                                doLogWalkEvent.setTick(500); //500 milliseconds required to make animations realistic.
                            } else if(doLogWalkCounter < 16) { //15 steps foward, 1 step is just quickfix TODO: Add gate opener.
                                p.getWalkingQueue().forceWalk(0, 1);
                            } else if(doLogWalkCounter == 17) { //stop the animation add the xp.
                                doLogWalkEvent.stop();
                                p.getAppearance().setWalkAnimation(-1);
                                p.getUpdateFlags().setAppearanceUpdateRequired(true);
                                p.removeTemporaryAttribute("unmovable");
                                p.getSkills().addXp(Skills.SKILL.AGILITY, (double)objectArray[7]);
                                p.getWalkingQueue().setRunToggled(running);
                            }
                            doLogWalkCounter++;
                        });
                        Server.registerEvent(doLogWalkEvent);
                    });
                    Server.registerCoordinateEvent(startEntranceLogCoordinateEvent);
                    break;
                case 2288: // Tunnel
                    AreaEvent startTunnelAreaEvent = new AreaEvent(p, 3003, 3937, 3005, 3938);
                    startTunnelAreaEvent.setAction(() => {
                        int newMove = 0;
                        int pX = p.getLocation().getX();
                        int pY = p.getLocation().getY();
                        if (pX == objectX + 1 && pY == objectY) // right side
                            newMove = 1;
                        else if (pX == objectX - 1 && pY == objectY) // left side
                            newMove = 2;
                        if (newMove > 0) {
                            int walkTunnelCounter = 0;
                            Event walkTunnelEvent = new Event(500);
                            walkTunnelEvent.setAction(() => {
                                if (walkTunnelCounter == 0) {
                                    p.getWalkingQueue().forceWalk(0, -1);
                                } else if (walkTunnelCounter == 1) {
                                    p.getWalkingQueue().forceWalk(newMove == 1 ? -1 : +1, 0);
                                } else {
                                    doCourse(p, objectX, objectY, objectArray);
                                    walkTunnelEvent.stop();
                                }
                                walkTunnelCounter++;
                            });
                            Server.registerEvent(walkTunnelEvent);
                            return;
                        }
                        Event squeezeIntoPipeEvent = new Event(0);
                        squeezeIntoPipeEvent.setAction(() => {
                            squeezeIntoPipeEvent.stop();
                            p.getPackets().sendMessage("You squeeze into the pipe...");
                            int regionX = p.getUpdateFlags().getLastRegion().getRegionX();
                            int regionY = p.getUpdateFlags().getLastRegion().getRegionY();
                            int lX = (p.getLocation().getX() - ((regionX - 6) * 8));
                            int lY = (p.getLocation().getY() - ((regionY - 6) * 8));
                            p.setForceMovement(new ForceMovement(lX, lY, lX, lY + 3, 10, 60, 0));
                            p.setFaceLocation(new Location(p.getLocation().getX(), p.getLocation().getY() + 1, 0));
                            p.setLastAnimation(new Animation(10578));
                            bool running = p.getWalkingQueue().isRunToggled();
                            p.getWalkingQueue().setRunToggled(false);
                            p.getWalkingQueue().resetWalkingQueue();
                            p.getPackets().clearMapFlag();
                            p.setTemporaryAttribute("unmovable", true);
                            Event squeezeOutOfPipeEvent = new Event(1000);
                            int squeezeOutOfPipeCounter = 0;
                            squeezeOutOfPipeEvent.setAction(() => {
                                if (squeezeOutOfPipeCounter == 0) {
                                    p.teleport(new Location(p.getLocation().getX(), p.getLocation().getY() + 9, 0));
                                    squeezeOutOfPipeEvent.setTick(850);
                                } else if (squeezeOutOfPipeCounter == 1) {
                                    ForceMovement movement = new ForceMovement(lX, lY + 9, lX, lY + 12, 10, 90, 0);
                                    p.setForceMovement(movement);
                                    squeezeOutOfPipeEvent.setTick(1100);
                                } else if (squeezeOutOfPipeCounter == 2) {
                                    squeezeOutOfPipeEvent.setTick(500);
                                    p.setLastAnimation(new Animation(10579));
                                    p.setForceMovement(new ForceMovement(lX, lY + 12, lX, lY + 13, 10, 40, 0));
                                } else {
                                    p.getPackets().sendMessage("...You squeeze out of the pipe.");
                                    squeezeOutOfPipeEvent.stop();
                                    p.removeTemporaryAttribute("unmovable");
                                    p.getSkills().addXp(Skills.SKILL.AGILITY, (double)objectArray[7]);
                                    p.getWalkingQueue().setRunToggled(running);
                                    p.teleport(new Location(3004, 3950, 0));
                                }
                                squeezeOutOfPipeCounter++;
                            });
                            Server.registerEvent(squeezeOutOfPipeEvent);
                        });
                        Server.registerEvent(squeezeIntoPipeEvent);
                    });
                    Server.registerCoordinateEvent(startTunnelAreaEvent);
                    break;

                case 2283: // Rope swing
                    AreaEvent startRopeSwingAreaEvent = new AreaEvent(p, 3004, 3951, 3006, 3953);
                    startRopeSwingAreaEvent.setAction(() => {
                        int newMove = 0;
                        int pX = p.getLocation().getX();
                        int pY = p.getLocation().getY();
                        if (pX == objectX - 1 && pY == objectY + 1) // front left
                            newMove = 1;
                        else if (pX == objectX + 1 && pY == objectY + 1) // front right
                            newMove = 2;
                        else if (pX == objectX - 1 && pY == objectY) // back left
                            newMove = 3;
                        else if (pX == objectX + 1 && pY == objectY) // back right
                            newMove = 4;
                        else if (pX == objectX && pY == objectY - 1) // very back middle
                            newMove = 5;
                        else if (pX == objectX && pY == objectY) //  middle
                            newMove = 6;
                        if (newMove > 0) {
                            int path = newMove;
                            int walkRopeSwingCounter = 0;
                            Event walkRopeSwingEvent = new Event(500);
                            walkRopeSwingEvent.setAction(() => {
                                p.setFaceLocation(new Location(3005, 3958, 0));
                                if (path == 1 || path == 2) {
                                    if (walkRopeSwingCounter == 0)
                                    {
                                        p.getWalkingQueue().forceWalk(path == 1 ? +1 : -1, 0);
                                    }
                                    else if (walkRopeSwingCounter >= 1)
                                    {
                                        walkRopeSwingEvent.stop();
                                        doCourse(p, objectX, objectY, objectArray);
                                    }
                                } else if (path == 3 || path == 4) {
                                    if (walkRopeSwingCounter == 0)
                                    {
                                        p.getWalkingQueue().forceWalk(path == 3 ? +1 : -1, +1);
                                    }
                                    else if (walkRopeSwingCounter >= 1)
                                    {
                                        walkRopeSwingEvent.stop();
                                        doCourse(p, objectX, objectY, objectArray);
                                    }
                                } else if (path == 5 || path == 6) {
                                    if (walkRopeSwingCounter == 0)
                                    {
                                        p.getWalkingQueue().forceWalk(0, path == 5 ? +2 : +1);
                                    }
                                    else if (walkRopeSwingCounter >= 1)
                                    {
                                        walkRopeSwingEvent.stop();
                                        doCourse(p, objectX, objectY, objectArray);
                                    }
                                }
                                walkRopeSwingCounter++;
                            });
                            Server.registerEvent(walkRopeSwingEvent);
                            return;
                        }
                        Event setupRopeSwingEvent = new Event(0);
                        setupRopeSwingEvent.setAction(() => {
                            setupRopeSwingEvent.stop();
                            bool running = p.getWalkingQueue().isRunToggled();
                            int regionX = p.getUpdateFlags().getLastRegion().getRegionX();
                            int regionY = p.getUpdateFlags().getLastRegion().getRegionY();
                            int lX = (p.getLocation().getX() - ((regionX - 6) * 8));
                            int lY = (p.getLocation().getY() - ((regionY - 6) * 8));
                            int newY = p.getLocation().getY() + 5;
                            int dir = 4;
                            p.setLastAnimation(new Animation(751));
                            p.setForceMovement(new ForceMovement(lX, lY, lX, lY+5, 25, 57, dir));
                            p.getWalkingQueue().resetWalkingQueue();
                            p.getPackets().clearMapFlag();
                            p.setTemporaryAttribute("unmovable", true);
                            Location objectLocation = new Location(3005, 3952, 0);
                            Event doRopeSwingAnimationEvent = new Event(30);
                            doRopeSwingAnimationEvent.setAction(() => {
                                doRopeSwingAnimationEvent.stop();
                                foreach(Player nearbyPlayers in Server.getPlayerList()) {
                                    if (nearbyPlayers != null) {
                                        nearbyPlayers.getPackets().newObjectAnimation(objectLocation, 497);
                                    }
                                }
                            });
                            Server.registerEvent(doRopeSwingAnimationEvent);

                            Event finishRopeSwingEvent = new Event(1300);
                            finishRopeSwingEvent.setAction(() => {
                                finishRopeSwingEvent.stop();
                                p.getAppearance().setWalkAnimation(-1);
                                p.getUpdateFlags().setAppearanceUpdateRequired(true);
                                p.removeTemporaryAttribute("unmovable");
                                p.teleport(new Location(p.getLocation().getX(), newY, 0));
                                p.getSkills().addXp(Skills.SKILL.AGILITY, (double)objectArray[7]);
                                p.getWalkingQueue().setRunToggled(running);
                            });
                            Server.registerEvent(finishRopeSwingEvent);
                        });
                        Server.registerEvent(setupRopeSwingEvent);
                    });
                    Server.registerCoordinateEvent(startRopeSwingAreaEvent);
                    break;

                case 37704: // Stepping stones
                    CoordinateEvent startSteppingStonesCoordinateEvent = new CoordinateEvent(p, new Location(3002, 3960, 0));
                    startSteppingStonesCoordinateEvent.setAction(() => {
                        bool running = p.getWalkingQueue().isRunToggled();
                        int dir = 4;
                        p.getWalkingQueue().setRunToggled(false);
                        p.getWalkingQueue().resetWalkingQueue();
                        p.getPackets().clearMapFlag();
                        p.setTemporaryAttribute("unmovable", true);
                        int stepStoneCounter = 0;
                        Event stepStoneEvent = new Event(600);
                        stepStoneEvent.setAction(() => {
                            if (stepStoneCounter >= 6)
                            {
                                stepStoneEvent.stop();
                                p.removeTemporaryAttribute("unmovable");
                                p.getSkills().addXp(Skills.SKILL.AGILITY, (double)objectArray[7]);
                                p.getWalkingQueue().setRunToggled(running);
                            } else {
                                int regionX = p.getUpdateFlags().getLastRegion().getRegionX();
                                int regionY = p.getUpdateFlags().getLastRegion().getRegionY();
                                int lX = (p.getLocation().getX() - ((regionX - 6) * 8));
                                int lY = (p.getLocation().getY() - ((regionY - 6) * 8));
                                p.setLastAnimation(new Animation(741));
                                p.setForceMovement(new ForceMovement(lX, lY, lX - 1, lY, 5, 35, dir));
                                Event teleportStepStoneEvent = new Event(550);
                                teleportStepStoneEvent.setAction(() => {
                                    teleportStepStoneEvent.stop();
                                    p.teleport(new Location(p.getLocation().getX()-1, p.getLocation().getY(), 0));
                                });
                                Server.registerEvent(teleportStepStoneEvent);
                                stepStoneEvent.setTick(stepStoneCounter == 6 ? 300 : 1100);
                                stepStoneCounter++;
                                p.setFaceLocation(new Location(2995, 3960, 0));
                            }
                        });
                        Server.registerEvent(stepStoneEvent);
                    });
                    Server.registerCoordinateEvent(startSteppingStonesCoordinateEvent);
                    break;

                case 2297: // Log
                    AreaEvent startLogAreaEvent = new AreaEvent(p, 3001, 3944, 3002, 3946);
                    startLogAreaEvent.setAction(() => {
                        int distanceToWalk = p.getLocation().getX() == 3001 ? -7 : -8;
                        int newMove = 0;
                        int pX = p.getLocation().getX();
                        int pY = p.getLocation().getY();
                        if (pX == objectX && pY == objectY - 1)
                            newMove = 1;
                        else if (pX == objectX && pY == objectY + 1)
                            newMove = 2;
                        else if (pX == objectX + 1 && pY == objectY + 1)
                            newMove = 3;
                        else if (pX == objectX + 1 && pY == objectY - 1)
                            newMove = 4;
                        if (newMove > 0) {
                            int path = newMove;
                            int doLogWalkCounter = 0;
                            Event doLogWalkEvent = new Event(500);
                            doLogWalkEvent.setAction(() => {
                                if (doLogWalkCounter == 0)
                                {
                                    if (path == 1 || path == 2) {
                                        p.getWalkingQueue().forceWalk(0, path == 1 ? +1 : -1);
                                    } else if (path == 3 || path == 4) {
                                        p.getWalkingQueue().forceWalk(0, path == 4 ? +1 : -1);
                                    }
                                } else {
                                    doCourse(p, objectX, objectY, objectArray);
                                    doLogWalkEvent.stop();
                                }
                                doLogWalkCounter++;
                            });
                            Server.registerEvent(doLogWalkEvent);
                            return;
                        }
                        Event doLogAnimationEvent = new Event(0);
                        doLogAnimationEvent.setAction(() => {
                            doLogAnimationEvent.stop();
                            bool running = p.getWalkingQueue().isRunToggled();
                            p.getWalkingQueue().setRunToggled(false);
                            p.getWalkingQueue().resetWalkingQueue();
                            p.getPackets().clearMapFlag();
                            p.setTemporaryAttribute("unmovable", true);
                            p.getAppearance().setWalkAnimation(155);
                            p.getUpdateFlags().setAppearanceUpdateRequired(true);
                            p.getWalkingQueue().forceWalk(distanceToWalk, 0);
                            int delay = distanceToWalk == -7 ? 4100 : 4600;
                            Event doLogFinishEvent = new Event(delay);
                            doLogFinishEvent.setAction(() => {
                                doLogFinishEvent.stop();
                                p.getAppearance().setWalkAnimation(-1);
                                p.getUpdateFlags().setAppearanceUpdateRequired(true);
                                p.removeTemporaryAttribute("unmovable");
                                p.getSkills().addXp(Skills.SKILL.AGILITY, (double)objectArray[7]);
                                p.getWalkingQueue().setRunToggled(running);
                            });
                            Server.registerEvent(doLogFinishEvent);
                        });
                        Server.registerEvent(doLogAnimationEvent);
                    });
                    Server.registerCoordinateEvent(startLogAreaEvent);
                    break;

                case 2328: // Rocks
                    AreaEvent startRocksAreaEvent = new AreaEvent(p, 2993, 3937, 2995, 3937);
                    startRocksAreaEvent.setAction(() => {
                        int doRocksAnimationCounter = 0;
                        Event doRocksAnimationEvent = new Event(0);
                        doRocksAnimationEvent.setAction(() => {
                            if (doRocksAnimationCounter == 0)
                            {
                                p.setFaceLocation(new Location(p.getLocation().getX(), p.getLocation().getY() - 5, 0));
                                doRocksAnimationCounter++;
                                doRocksAnimationEvent.setTick(500);
                                return;
                            }
                            doRocksAnimationEvent.stop();
                            p.setLastAnimation(new Animation(740));
                            p.getWalkingQueue().resetWalkingQueue();
                            p.getPackets().clearMapFlag();
                            p.getAppearance().setWalkAnimation(0);
                            p.getUpdateFlags().setAppearanceUpdateRequired(true);
                            p.setTemporaryAttribute("unmovable", true);
                            int regionX = p.getUpdateFlags().getLastRegion().getRegionX();
                            int regionY = p.getUpdateFlags().getLastRegion().getRegionY();
                            int lX = (p.getLocation().getX() - ((regionX - 6) * 8));
                            int lY = (p.getLocation().getY() - ((regionY - 6) * 8));
                            p.setForceMovement(new ForceMovement(lX, lY, lX, lY - 4, 5, 80, 4));
                            Event finishRocksEvent = new Event(1500);
                            finishRocksEvent.setAction(() => {
                                finishRocksEvent.stop();
                                p.teleport(new Location(p.getLocation().getX(), p.getLocation().getY() - 4, 0));
                                p.setLastAnimation(new Animation(65535));
                                p.removeTemporaryAttribute("unmovable");
                                p.getSkills().addXp(Skills.SKILL.AGILITY, (double)objectArray[7]);
                            });
                            Server.registerEvent(finishRocksEvent);
                        });
                        Server.registerEvent(doRocksAnimationEvent);
                    });
                    Server.registerCoordinateEvent(startRocksAreaEvent);
                    break;
            }
        }
 public static void useLadder(Player p)
 {
     CoordinateEvent useLadderCoordinateEvent = new CoordinateEvent(p, new Location(2532, 3546, 1));
     useLadderCoordinateEvent.setAction(() => {
         p.setLastAnimation(new Animation(828));
         p.setTemporaryAttribute("unmovable", true);
         Event useLadderEvent = new Event(1000);
         useLadderEvent.setAction(() => {
             useLadderEvent.stop();
             p.teleport(new Location(p.getLocation().getX(), p.getLocation().getY(), 0));
             p.removeTemporaryAttribute("unmovable");
         });
         Server.registerEvent(useLadderEvent);
     });
     Server.registerCoordinateEvent(useLadderCoordinateEvent);
 }
        public static void doCourse(Player p, int objectX, int objectY, object[] objectArray)
        {
            if (p.getTemporaryAttribute("unmovable") != null)
                return;

            switch((int)objectArray[0]) {
                case 20210: // Entrance tunnel
                    AreaEvent entranceTunnelAreaEvent = new AreaEvent(p, 2551, 3558, 2553, 3561);
                    entranceTunnelAreaEvent.setAction(() => {
                        entranceTunnelAreaEvent.stop();
                        int newMove = 0;
                        int pX = p.getLocation().getX();
                        int pY = p.getLocation().getY();
                        if (pX == objectX + 1 && pY == objectY)
                            newMove = 1;
                        else if (pX == objectX - 1 && pY == objectY)
                            newMove = 2;
                        else if (pX == objectX - 1 && pY == objectY + 1)
                            newMove = 3;
                        else if (pX == objectX + 1 && pY == objectY + 1)
                            newMove = 4;
                        if (newMove > 0) {
                            int path = newMove;
                            int doCourseCounter = 0;
                            Event doCourseEvent = new Event(500);
                            doCourseEvent.setAction(() => {
                                if (doCourseCounter == 0)
                                {
                                    p.getWalkingQueue().forceWalk(0, (path == 1 || path == 2) ? -1 : (path == 3 || path == 4) ? +1 : 0);
                                }
                                else if (doCourseCounter == 1)
                                {
                                    p.getWalkingQueue().forceWalk((path == 1 || path == 4) ? -1 : (path == 2 || path == 3) ? +1 : 0, 0);
                                } else {
                                    doCourse(p, objectX, objectY, objectArray);
                                    doCourseEvent.stop();
                                }
                                doCourseCounter++;
                            });
                            Server.registerEvent(doCourseEvent);
                            return;
                        }
                        int startEnterTunnelCounter = 0;
                        Event startEnterTunnelEvent = new Event(0);
                        startEnterTunnelEvent.setAction(() => {
                            if (startEnterTunnelCounter == 0)
                            {
                                p.setFaceLocation(new Location(p.getLocation().getX(), p.getLocation().getY() <= 3558 ? 3561 : 3558, 0));
                                startEnterTunnelEvent.setTick(500);
                                startEnterTunnelCounter++;
                            } else {
                                startEnterTunnelEvent.stop();
                                bool running = p.getWalkingQueue().isRunToggled();
                                int regionX = p.getUpdateFlags().getLastRegion().getRegionX();
                                int regionY = p.getUpdateFlags().getLastRegion().getRegionY();
                                int lX = (p.getLocation().getX() - ((regionX - 6) * 8));
                                int lY = (p.getLocation().getY() - ((regionY - 6) * 8));
                                int newLocalY = p.getLocation().getY() == 3558 ? lY + 3 : lY - 3;
                                int newY = newLocalY > lY ? p.getLocation().getY() + 3 : p.getLocation().getY() - 3;
                                int dir = newLocalY > lY ? 0 : 4;
                                p.setForceMovement(new ForceMovement(lX, lY, lX, newLocalY, 10, 60, dir));
                                p.setLastAnimation(new Animation(10580));
                                p.getWalkingQueue().resetWalkingQueue();
                                p.getPackets().clearMapFlag();
                                p.setTemporaryAttribute("unmovable", true);
                                Event enterTunnelEvent = new Event(1500);
                                enterTunnelEvent.setAction(() => {
                                    enterTunnelEvent.stop();
                                    p.removeTemporaryAttribute("unmovable");
                                    p.teleport(new Location(p.getLocation().getX(), newY, 0));
                                    p.getSkills().addXp(Skills.SKILL.AGILITY, (double)objectArray[7]);
                                    p.getWalkingQueue().setRunToggled(running);
                                });
                                Server.registerEvent(enterTunnelEvent);
                            }
                        });
                        Server.registerEvent(startEnterTunnelEvent);
                    });
                    Server.registerCoordinateEvent(entranceTunnelAreaEvent);
                    break;

                case 2282: // Swing
                    AreaEvent swingAreaEvent = new AreaEvent(p, 2550, 3554, 2552, 3555);
                    swingAreaEvent.setAction(() => {
                        int newMove = 0;
                        int pX = p.getLocation().getX();
                        int pY = p.getLocation().getY();
                        if (pX == objectX - 1 && pY == objectY + 4) // front left
                            newMove = 1;
                        else if (pX == objectX + 1 && pY == objectY + 4) // front right
                            newMove = 2;
                        else if (pX == objectX - 1 && pY == objectY + 5) // back left
                            newMove = 3;
                        else if (pX == objectX + 1 && pY == objectY + 5) // back right
                            newMove = 4;
                        else if (pX == objectX && pY == objectY + 5) // back middle
                            newMove = 5;
                        if (newMove > 0) {
                            int path = newMove;
                            int doCourseCounter = 0;
                            Event doCourseEvent = new Event(500);
                            doCourseEvent.setAction(() => {
                                p.setFaceLocation(new Location(2551, 3549, 0));
                                if (path == 1 || path == 2) {
                                    if (doCourseCounter == 0)
                                    {
                                        p.getWalkingQueue().forceWalk(path == 1 ? +1 : -1, 0);
                                    }
                                    else if (doCourseCounter >= 1)
                                    {
                                        doCourseEvent.stop();
                                        doCourse(p, objectX, objectY, objectArray);
                                    }
                                } else if (path == 3 || path == 4) {
                                    if (doCourseCounter == 0)
                                    {
                                        p.getWalkingQueue().forceWalk(path == 3 ? +1 : -1, -1);
                                    }
                                    else if (doCourseCounter >= 1)
                                    {
                                        doCourseEvent.stop();
                                        doCourse(p, objectX, objectY, objectArray);
                                    }
                                } else if (path == 5) {
                                    if (doCourseCounter == 0)
                                    {
                                        p.getWalkingQueue().forceWalk(0, -1);
                                    }
                                    else if (doCourseCounter >= 1)
                                    {
                                        doCourseEvent.stop();
                                        doCourse(p, objectX, objectY, objectArray);
                                    }
                                }
                                doCourseCounter++;
                            });
                            Server.registerEvent(doCourseEvent);
                            return;
                        }

                        Event doSwingEvent = new Event(0);
                        doSwingEvent.setAction(() => {
                            doSwingEvent.stop();
                            bool running = p.getWalkingQueue().isRunToggled();
                            int regionX = p.getUpdateFlags().getLastRegion().getRegionX();
                            int regionY = p.getUpdateFlags().getLastRegion().getRegionY();
                            int lX = (p.getLocation().getX() - ((regionX - 6) * 8));
                            int lY = (p.getLocation().getY() - ((regionY - 6) * 8));
                            int newY = p.getLocation().getY() - 5;
                            int dir = 4;
                            p.setLastAnimation(new Animation(751));
                            p.setForceMovement(new ForceMovement(lX, lY, lX, lY-5, 25, 57, dir));
                            p.getWalkingQueue().resetWalkingQueue();
                            p.getPackets().clearMapFlag();
                            p.setTemporaryAttribute("unmovable", true);
                            Location objectLocation = new Location(2551, 3550, 0);
                            Event showObjectAnimationEvent = new Event(30);
                            showObjectAnimationEvent.setAction(() => {
                                showObjectAnimationEvent.stop();
                                foreach(Player nearbyPlayers in Server.getPlayerList()) {
                                    if (nearbyPlayers != null) {
                                        nearbyPlayers.getPackets().newObjectAnimation(objectLocation, 497);
                                    }
                                }
                            });
                            Server.registerEvent(showObjectAnimationEvent);
                            Event finishSwingEvent = new Event(1300);
                            finishSwingEvent.setAction(() => {
                                finishSwingEvent.stop();
                                p.getAppearance().setWalkAnimation(-1);
                                p.getUpdateFlags().setAppearanceUpdateRequired(true);
                                p.removeTemporaryAttribute("unmovable");
                                p.teleport(new Location(p.getLocation().getX(), newY, 0));
                                p.getSkills().addXp(Skills.SKILL.AGILITY, (double)objectArray[7]);
                                p.getWalkingQueue().setRunToggled(running);
                            });
                            Server.registerEvent(finishSwingEvent);
                        });
                        Server.registerEvent(doSwingEvent);
                    });
                    Server.registerCoordinateEvent(swingAreaEvent);
                    break;

                case 2294: // Log
                    AreaEvent logAreaEvent = new AreaEvent(p, 2550, 3545, 2551, 3547);
                    logAreaEvent.setAction(() => {
                        int distanceToWalk = p.getLocation().getX() == 2550 ? -9 : -10;
                        int newMove = 0;
                        int pX = p.getLocation().getX();
                        int pY = p.getLocation().getY();
                        if (pX == objectX && pY == objectY - 1)
                            newMove = 1;
                        else if (pX == objectX&& pY == objectY + 1)
                            newMove = 2;
                        if (newMove > 0) {
                            int path = newMove;
                            int doLogCounter = 0;
                            Event doLogEvent = new Event(500);
                            doLogEvent.setAction(() => {
                                if (doLogCounter++ == 0)
                                {
                                    p.getWalkingQueue().forceWalk(0, path == 1 ? +1 : -1);
                                } else {
                                    doCourse(p, objectX, objectY, objectArray);
                                    doLogEvent.stop();
                                }
                            });
                            Server.registerEvent(doLogEvent);
                            return;
                        }
                        Event startLogEvent = new Event(0);
                        startLogEvent.setAction(() => {
                            startLogEvent.stop();
                            bool running = p.getWalkingQueue().isRunToggled();
                            p.getWalkingQueue().setRunToggled(false);
                            p.getWalkingQueue().resetWalkingQueue();
                            p.getPackets().clearMapFlag();
                            p.setTemporaryAttribute("unmovable", true);
                            p.getAppearance().setWalkAnimation(155);
                            p.getUpdateFlags().setAppearanceUpdateRequired(true);
                            p.getWalkingQueue().forceWalk(distanceToWalk, 0);
                            int delay = distanceToWalk == -9 ? 5400 : 5900;
                            Event endLogEvent = new Event(delay);
                            endLogEvent.setAction(() => {
                                endLogEvent.stop();
                                p.getAppearance().setWalkAnimation(-1);
                                p.getUpdateFlags().setAppearanceUpdateRequired(true);
                                p.removeTemporaryAttribute("unmovable");
                                p.getSkills().addXp(Skills.SKILL.AGILITY, (double)objectArray[7]);
                                p.getWalkingQueue().setRunToggled(running);
                            });
                            Server.registerEvent(endLogEvent);
                        });
                        Server.registerEvent(startLogEvent);
                    });
                    Server.registerCoordinateEvent(logAreaEvent);
                    break;

                case 20211: // Net
                    AreaEvent netAreaEvent = new AreaEvent(p, 2539, 3545, 2539, 3546);
                    netAreaEvent.setAction(() => {
                        p.setLastAnimation(new Animation(828));
                        p.getWalkingQueue().resetWalkingQueue();
                        p.getPackets().clearMapFlag();
                        p.setFaceLocation(new Location(p.getLocation().getX()-1, p.getLocation().getY(), 0));
                        p.setTemporaryAttribute("unmovable", true);
                        Event netEvent = new Event(1000);
                        netEvent.setAction(() => {
                            netEvent.stop();
                            p.removeTemporaryAttribute("unmovable");
                            p.teleport(new Location(p.getLocation().getX()-2, p.getLocation().getY(), 1));
                            p.getSkills().addXp(Skills.SKILL.AGILITY, (double)objectArray[7]);
                        });
                        Server.registerEvent(netEvent);
                    });
                    Server.registerCoordinateEvent(netAreaEvent);
                    break;

                case 2302: // Balance beam
                    CoordinateEvent balanceBeamCoordinateEvent = new CoordinateEvent(p, new Location((int)objectArray[3], (int)objectArray[4], 1));
                    balanceBeamCoordinateEvent.setAction(() => {
                        bool running = p.getWalkingQueue().isRunToggled();
                        p.getWalkingQueue().setRunToggled(false);
                        p.getWalkingQueue().resetWalkingQueue();
                        p.getPackets().clearMapFlag();
                        p.setTemporaryAttribute("unmovable", true);
                        p.getAppearance().setWalkAnimation(157);
                        p.getUpdateFlags().setAppearanceUpdateRequired(true);
                        p.getWalkingQueue().forceWalk(-4, 0);
                        Event balanceBeamEvent = new Event(2450);
                        balanceBeamEvent.setAction(() => {
                            balanceBeamEvent.stop();
                            p.removeTemporaryAttribute("unmovable");
                            p.getAppearance().setWalkAnimation(-1);
                            p.getUpdateFlags().setAppearanceUpdateRequired(true);
                            p.getSkills().addXp(Skills.SKILL.AGILITY, (double)objectArray[7]);
                            p.getWalkingQueue().setRunToggled(running);
                        });
                        Server.registerEvent(balanceBeamEvent);
                    });
                    Server.registerCoordinateEvent(balanceBeamCoordinateEvent);
                    break;

                case 1948: // Crumbling wall
                    AreaEvent crumblingWallAreaEvent = new AreaEvent(p, objectX - 1, objectY - 1, objectX, objectY + 1);
                    crumblingWallAreaEvent.setAction(() => {
                        int newMove = 0;
                        int pX = p.getLocation().getX();
                        int pY = p.getLocation().getY();
                        if (pX == objectX && pY == objectY - 1)
                            newMove = 1;
                        else if (pX == objectX && pY == objectY + 1)
                            newMove = 2;
                        if (newMove > 0) {
                            int path = newMove;
                            Event startCrumblingWallEvent = new Event(500);
                            int startCrumblingWallCounter = 0;
                            startCrumblingWallEvent.setAction(() => {
                                if (startCrumblingWallCounter == 0)
                                {
                                    p.getWalkingQueue().forceWalk(-1, 0);
                                }
                                else if (startCrumblingWallCounter == 1)
                                {
                                    p.getWalkingQueue().forceWalk(0, path == 1 ? +1 : -1);
                                } else {
                                    doCourse(p, objectX, objectY, objectArray);
                                    startCrumblingWallEvent.stop();
                                }
                                startCrumblingWallCounter++;
                            });
                            Server.registerEvent(startCrumblingWallEvent);
                            return;
                        }
                        int regionX = p.getUpdateFlags().getLastRegion().getRegionX();
                        int regionY = p.getUpdateFlags().getLastRegion().getRegionY();
                        int lX = (p.getLocation().getX() - ((regionX - 6) * 8));
                        int lY = (p.getLocation().getY() - ((regionY - 6) * 8));
                        p.setForceMovement(new ForceMovement(lX, lY, lX+2, lY, 10, 60, 1));
                        p.setLastAnimation(new Animation(4853));
                        p.getWalkingQueue().resetWalkingQueue();
                        p.getPackets().clearMapFlag();
                        p.setTemporaryAttribute("unmovable", true);
                        Event finishCrumblingWallEvent = new Event(1250);
                        finishCrumblingWallEvent.setAction(() => {
                            finishCrumblingWallEvent.stop();
                            p.setFaceLocation(new Location(p.getLocation().getX()+1, p.getLocation().getY(), 0));
                            p.removeTemporaryAttribute("unmovable");
                            p.teleport(new Location(p.getLocation().getX()+2, p.getLocation().getY(), 0));
                            p.getSkills().addXp(Skills.SKILL.AGILITY, (double)objectArray[7]);
                        });
                        Server.registerEvent(finishCrumblingWallEvent);
                    });
                    Server.registerCoordinateEvent(crumblingWallAreaEvent);
                    break;
            }
        }
	    private void handlePickupItem(Player player, Packet packet) {
		    int x = packet.readLEShort();
		    int id  = packet.readUShort();
		    int y  = packet.readLEShortA();
		    Location l = new Location(x, y, player.getLocation().getZ());
		    SkillHandler.resetAllSkills(player);
		    if (x < 1000 || y < 1000 | id < 0 || player.isDead() || player.getTemporaryAttribute("cantDoAnything") != null) {
			    return;
		    }
		    player.getPackets().closeInterfaces();
		    if(player.getLocation().Equals(l)) {
			    Server.getGroundItems().pickupItem(player, id, player.getLocation());
			    return;
		    }
            CoordinateEvent pickupItemCoordinateEvent = new CoordinateEvent(player, l);
		    pickupItemCoordinateEvent.setAction(() => {
				Server.getGroundItems().pickupItem(player, id, player.getLocation());
		    });
            Server.registerCoordinateEvent(pickupItemCoordinateEvent);
	    }
        /**
	     * Registers a 'coordiante' event.
	     * @param event
	     */
	    public static void registerCoordinateEvent(CoordinateEvent coordinateEvent) {
            Event newCoordinateEvent = new Event(0);
            newCoordinateEvent.setAction(() => {
				bool standingStill = coordinateEvent.getPlayer().getSprites().getPrimarySprite() == -1 && coordinateEvent.getPlayer().getSprites().getSecondarySprite() == -1;
				if(coordinateEvent.getPlayer().getDistanceEvent() == null || !coordinateEvent.getPlayer().getDistanceEvent().Equals(coordinateEvent)) {
					newCoordinateEvent.stop();
					return;
				}
				if (standingStill) {
					if((coordinateEvent.getPlayer().getLocation().Equals(coordinateEvent.getTargetLocation()) && coordinateEvent.getPlayer().getLocation().Equals(coordinateEvent.getOldLocation())) || coordinateEvent.getFailedAttempts() >= 15) {
						if(newCoordinateEvent.getTick() == 0) {
							coordinateEvent.run();
							newCoordinateEvent.stop();
							coordinateEvent.setPlayerNull();
						} else {
							if(!coordinateEvent.hasReached()) {
								coordinateEvent.setReached(true);
							} else {
								coordinateEvent.run();
								newCoordinateEvent.stop();
								coordinateEvent.setPlayerNull();
							}
						}
					}
				} else {
					if(!coordinateEvent.getPlayer().getLocation().Equals(coordinateEvent.getOldLocation())) {
						coordinateEvent.setOldLocation(coordinateEvent.getPlayer().getLocation());
					} else {
						coordinateEvent.incrementFailedAttempts();
					}
				}
				newCoordinateEvent.setTick(200);
		    });
            registerEvent(newCoordinateEvent);
	    }
 public static void useLever(Player p, int id, Location leverLocation)
 {
     if (p.getTemporaryAttribute("teleporting") != null)
     {
         return;
     }
     foreach (LoadedLaddersAndStairs.Lever lever in LoadedLaddersAndStairs.levers)
     {
         if (lever.getId() == id)
         {
             if (lever.getLeverLocation().Equals(leverLocation))
             {
                 LoadedLaddersAndStairs.Lever l = lever;
                 //TODO when in use it cant be used (in use = lever is facing down)
                 CoordinateEvent useLeverCoordinateEvent = new CoordinateEvent(p, l.getLeverLocation());
                 useLeverCoordinateEvent.setAction(() =>
                 {
                     p.setFaceLocation(l.getFaceLocation());
                     if (p.getTemporaryAttribute("teleblocked") != null)
                     {
                         p.getPackets().sendMessage("A magical force prevents you from teleporting!");
                         return;
                     }
                     else if ((p.getTemporaryAttribute("teleporting") != null))
                     {
                         return;
                     }
                     p.setLastAnimation(new Animation(2140));
                     p.getPackets().closeInterfaces();
                     p.setTemporaryAttribute("teleporting", true);
                     p.getWalkingQueue().resetWalkingQueue();
                     p.getPackets().clearMapFlag();
                     SkillHandler.resetAllSkills(p);
                     l.setInUse(true);
                     Event useLeverEvent = new Event(700);
                     useLeverEvent.setAction(() =>
                     {
                         useLeverEvent.stop();
                         p.setLastAnimation(new Animation(8939, 0));
                         p.setLastGraphics(new Graphics(1576, 0));
                         l.setInUse(false);
                         Event setLeverTeleportEvent = new Event(1800);
                         setLeverTeleportEvent.setAction(() =>
                         {
                             setLeverTeleportEvent.stop();
                             p.teleport(l.getTeleLocation());
                             p.setLastAnimation(new Animation(8941, 0));
                             p.setLastGraphics(new Graphics(1577, 0));
                             Teleport.resetTeleport(p);
                         });
                         Server.registerEvent(setLeverTeleportEvent);
                     });
                     Server.registerEvent(useLeverEvent);
                 });
                 Server.registerCoordinateEvent(useLeverCoordinateEvent);
                 break;
             }
         }
     }
 }
 public static bool useObject(Player p, int id, Location location, int option)
 {
     foreach (LoadedLaddersAndStairs.HeightObject heightObject in LoadedLaddersAndStairs.objects)
     {
         if (heightObject.getId() == id)
         {
             if (heightObject.getLocation().Equals(location) && heightObject.getOption() == option)
             {
                 LoadedLaddersAndStairs.HeightObject obj = heightObject;
                 if (heightObject.getType() == LoadedLaddersAndStairs.Event_Type.COORDINATE_POSITION)
                 {
                     CoordinateEvent useObjectCoordinateEvent = new CoordinateEvent(p, heightObject.getStandLocation());
                     useObjectCoordinateEvent.setAction(() =>
                     {
                         LaddersAndStairs.teleport(p, obj);
                     });
                     Server.registerCoordinateEvent(useObjectCoordinateEvent);
                 }
                 else if (heightObject.getType() == LoadedLaddersAndStairs.Event_Type.AREA_POSITION)
                 {
                     AreaEvent useObjectAreaEvent = new AreaEvent(p, heightObject.getMinCoords().getX(), heightObject.getMinCoords().getY(), heightObject.getMaxCoords().getX(), heightObject.getMaxCoords().getY());
                     useObjectAreaEvent.setAction(() =>
                     {
                         LaddersAndStairs.teleport(p, obj);
                     });
                     Server.registerCoordinateEvent(useObjectAreaEvent);
                 }
                 return true;
             }
         }
     }
     return false;
 }