RuneScape 2 Development > Server Tutorials

Project Insanity Teleport handler

Pages: (1/2) >>>

Special Air Service:

Awhile ago i made a teleport handler that wasn't the best. But im back with a better teleport handler.

First off add this class Fix the imports and the packaging

--- Code: ---package org.rs2server.entity.players.teleport;

import org.rs2server.Config;
import org.rs2server.entity.players.Client;
import org.rs2server.entity.players.PlayerHandler;
import org.rs2server.event.CycleEvent;
import org.rs2server.event.CycleEventContainer;
import org.rs2server.event.CycleEventHandler;

/**
 * This class will handle all everything to do with the teleporting
 * it will check if the player is eligible to teleport and
 * handle all the animations and gfxs.
 * @author Zack/Optimum
 *
 */
public class Teleport {

/**
* The player teleporting
*/
private Client player;

/**
* The targeted x, y and h coordinates
*/
private int targetX, targetY, targetH;

/**
* Constructor for teleporting
* @param player - the player
*/
public Teleport(Client player){
this.player = player;
}

/**
* Submits a new teleport tab type
* @param tarX - the new x coordinates
* @param tarY - the new y coordinates
*/
public void submitTab(final int tarX, final int tarY){
this.targetX = tarX;
this.targetY = tarY;
startTeleport(TeleportType.TELETAB_TELEPORT);
}

/**
* Submits a new ancient teleport type
* @param tarX - the new x coordinates
* @param tarY - the new y coordinates
*/
public void submitAncient(final int tarX, final int tarY){
this.targetX = tarX;
this.targetY = tarY;
startTeleport(TeleportType.ANICENT_TELEPORT);
}

/**
* Submits a new ancient teleport type
* @param tarX - the new x coordinates
* @param tarY - the new y coordinates
* @param tarH - the new height
*/
public void submitAncient(final int tarX, final int tarY, final int tarH){
this.targetX = tarX;
this.targetY = tarY;
this.targetH = tarH;
startTeleport(TeleportType.ANICENT_TELEPORT);
}

/**
* Submits a new modern teleport
* @param tarX - the new x coordinates
* @param tarY - the new y coordinates
*/
public void submitModern(final int tarX, final int tarY){
this.targetX = tarX;
this.targetY = tarY;
startTeleport(TeleportType.MODERN_TELEPORT);
}

/**
* Submits a new modern teleport
* @param tarX - the new x coordinates
* @param tarY - the new y coordinates
* @param tarH - the new height
*/
public void submitModern(final int tarX, final int tarY, final int tarH){
this.targetX = tarX;
this.targetY = tarY;
this.targetH = tarH;
startTeleport(TeleportType.MODERN_TELEPORT);
}


/**
* Handles the teleport event in 3 stages
*/
public void startTeleport(final TeleportType teleType){
if(!canTeleport(player))
return;
final int startStage = teleType.getTickTimer1Start();
final int middleStage = teleType.getTickTimer2Start();
final int stopStage = teleType.getStopTimer();
cancelTradeAndDuel(player);
player.stopMovement();
player.isTeleporting = true;
player.startAnimation(teleType.getStartGfxAnim()[0]);
if(teleType.getStartGfxAnim()[2] == 100)
player.gfx100(teleType.getStartGfxAnim()[1]);
else
player.gfx0(teleType.getStartGfxAnim()[1]);
CycleEventHandler.getSingleton().addEvent(player, new CycleEvent() {
int stage = 0;
@Override
public void execute(CycleEventContainer container) {
if (stage == startStage) {
player.startAnimation(teleType.getTimer1GfxAnim()[0]);
if(teleType.getTimer1GfxAnim()[2] == 100)
player.gfx100(teleType.getTimer1GfxAnim()[1]);
else
player.gfx0(teleType.getTimer1GfxAnim()[1]);
}
if (stage == middleStage){
player.startAnimation(teleType.getTimer2GfxAnim()[0]);
if(teleType.getTimer2GfxAnim()[2] == 100)
player.gfx100(teleType.getTimer2GfxAnim()[1]);
else
player.gfx0(teleType.getTimer2GfxAnim()[1]);
player.getPA().movePlayer(targetX, targetY, targetH);
}
if(stage == stopStage){
player.startAnimation(65535);
player.resetWalkingQueue();
container.stop();
}
stage++;
System.out.println("Teleport Stage: " + stage);
}
@Override
public void stop() {
player.isTeleporting = false;
}
}, 1);
}

/**
* Checks to see if the player is eligible for teleporting
* @param player - The target player
* @return true if the player can teleport
*/
public boolean canTeleport(Client player){
if(player == null)
return false;
if(player.isDead)
return false;
if (isDueling(player))
return false;
if(isTeleBlocked(player))
return false;
if(player.isTeleporting)
return false;
if(wildyLevelCheck(player))
return false;
return true;
}

/**
* Checks to see if the player is in a duel
* @param player - The target player
* @return true if the player is in a duel
*/
public boolean isDueling(Client player){
if (player.duelStatus == 5) {
player.sendMessage("You can't teleport during a duel!");
return true;
} else
return false;
}

/**
* Checks if the player is teleblocked
* @param player - The target player
* @return true if player is teleblocked
*/
public boolean isTeleBlocked(Client player){
if (System.currentTimeMillis() - player.teleBlockDelay < player.teleBlockLength) {
player.sendMessage("You are teleblocked and can't teleport.");
return true;
} else
return false;
}

/**
* Cancels the trade and duel for {@link player} and
* the opponent he is dueling / trading with
* @param player - the target player
*/
public void cancelTradeAndDuel(Client player){
if (player.duelStatus >= 1 && player.duelStatus <= 4) {
Client o = (Client) PlayerHandler.players[player.duelingWith];
player.duelStatus = 0;
o.duelStatus = 0;
o.getTradeAndDuel().declineDuel();
player.getTradeAndDuel().declineDuel();
}
}

/**
* Checks the current player wilderness level and returns true
* if the player is above the max teleport wilderness level
* @param player - The target player
* @return if player is in wildy > 20
*/
public boolean wildyLevelCheck(Client player){
if (player.inWild() && player.wildLevel > Config.NO_TELEPORT_WILD_LEVEL) {
player.sendMessage("You can't teleport above level "
+ Config.NO_TELEPORT_WILD_LEVEL + " in the wilderness.");
return true;
} else
return false;
}

}


--- End code ---

Now add this class inside the same package

--- Code: ---package org.rs2server.entity.players.teleport;

/**
 * Contains all the teleport types and all their data
 * @author Zack/Optimum
 *
 */

public enum TeleportType {

MODERN_TELEPORT(
new int[] {714, -1, 0}, 1, new int[] {-1, 308, 100},
3, new int[] {715, -1, 0}, 6),
ANICENT_TELEPORT(
new int[] {9599, 1681, 0}, 5, new int[] {-1, -1, 0},
5, new int[] {-1, -1, 0}, 5),
TELETAB_TELEPORT(
new int[] {9597, 1680, 0}, 2, new int[] {4731, -1, 0},
3, new int[] {9598, -1, 0}, 4);


int[] startGfxAnim;
int tickTimer1Start;
int[] timer1GfxAnim;
int tickTimer2Start;
int[] timer2GfxAnim;
int stopTimer;

TeleportType(int[] startGfxAnim, int tickTimer1Start, int[] timer1GfxAnim, int tickTimer2Start, int[] timer2GfxAnim, int stopTimer){
this.startGfxAnim = startGfxAnim;
this.tickTimer1Start = tickTimer1Start;
this.timer1GfxAnim = timer1GfxAnim;
this.tickTimer2Start = tickTimer2Start;
this.timer2GfxAnim = timer2GfxAnim;
this.stopTimer = stopTimer;
}

public int[] getStartGfxAnim(){
return startGfxAnim;
}

public int getTickTimer1Start(){
return tickTimer1Start;
}

public int[] getTimer1GfxAnim(){
return timer1GfxAnim;
}

public int getTickTimer2Start(){
return tickTimer2Start;
}

public int[] getTimer2GfxAnim(){
return timer2GfxAnim;
}

public int getStopTimer(){
return stopTimer;
}
}

--- End code ---

now go to Client.java and add these:

--- Code: --- private Teleport teleport = new Teleport(this);
public Teleport getTeleportHandler(){
return teleport;
}
--- End code ---

this is an example of how to use this:
--- Code: --- case 4171:
case 117048:
case 75010:
case 50056:
case 84237:
if(c.playerMagicBook != 1) {
c.getTeleportHandler().submitModern(Config.RESPAWN_X, Config.RESPAWN_Y);
} else {
c.getTeleportHandler().submitAncient(Config.RESPAWN_X, Config.RESPAWN_Y);
}
break;
--- End code ---


If i have missed anything please tell me.

lare69:

please don't handle all of that teleport logic in the constructor :(

jummppy:

aren't constructors only meant for holding and assigning data? Not checking it Lare69? OT: I would like to see it like this.
--- Code: ---new Teleport(c, Config.LUMBY_X, Config.LUMBY_Y, 0).startTeleport();
--- End code ---
But that's just me. They way I would of done the start of the class is this.

--- Code: ---Public class Teleport

private final int teleportX, teleportY, teleportZ;

public Teleport(final Client client, int teleportX, int teleportY, int teleportZ) {

 this.teleportX = teleportX;
 .......

}

--- End code ---

If I'm doing anything wrong or bad. Please someone correct me.

sk8rdude461:

That... doesn't look pretty...
IMO you should have a single boolean to check if the player can/can't teleport..

--- Code: Java ---public boolean canTeleport(Client client) {        if(client.isDueling) {                client.sendMessage("You can't teleport in a duel");                return false;        }        if(client.inTrade) {                client.sendMessage("Please finish what you're doing first");                return false;        }// Etc}
You do not need to make everything final in the constructor (for the CycleEvent) if the values act as final (Meaning the values do not get modified).

Also, why not handle all the teleport stages in one event, instead of a bunch of separate events....

--- Code: Java ---CycleEventHandler.getSingleton().addEvent(player, new CycleEvent() {        int stage = 0;        @Override        public void execute(CycleEventContainer container) {                switch(stage) {                case 0:// Stage                         player.startAnimation(type.getTimer1GfxAnim()[0]);                        if(type.getTimer1GfxAnim()[2] == 100)                                player.gfx100(type.getTimer1GfxAnim()[1]);                        else                                player.gfx0(type.getTimer1GfxAnim()[1]);                        break;                case 6:// end of teleport stages.                        container.stop();                        break;                default:                        // Do nothing                        break;                }                stage++;        @Override        public void stop() {                player.sendMessage("You teleport to " + destination + ".");        }}, 1);

RandQm:


--- Code: Java ---public boolean canTeleport(Client player){                if(player.isDead)                        return false;                if (isDueling(player))                        return false;                if(isTeleBlocked(player))                        return false;                if(player.isTeleporting)                        return false;                if(wildyLevelCheck(player))                        return false;                return true;        }
Could become:


--- Code: Java ---public boolean canTeleport(Client player){return !wildyLevelCheck(player) && !isTeleporting() && !isTeleblocked...;} 

Pages: (1/2) >>>

Go to full version