Arcturus-Community/src/main/java/com/eu/habbo/habbohotel/items/interactions/InteractionTeleport.java

236 lines
7.1 KiB
Java
Raw Normal View History

2018-07-06 15:30:00 +02:00
package com.eu.habbo.habbohotel.items.interactions;
import com.eu.habbo.Emulator;
import com.eu.habbo.habbohotel.gameclients.GameClient;
import com.eu.habbo.habbohotel.items.Item;
import com.eu.habbo.habbohotel.rooms.Room;
import com.eu.habbo.habbohotel.rooms.RoomTile;
import com.eu.habbo.habbohotel.rooms.RoomUnit;
import com.eu.habbo.habbohotel.users.Habbo;
import com.eu.habbo.habbohotel.users.HabboItem;
import com.eu.habbo.messages.ServerMessage;
import com.eu.habbo.threading.runnables.RoomUnitWalkToLocation;
2018-07-06 15:30:00 +02:00
import com.eu.habbo.threading.runnables.teleport.TeleportActionOne;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
2018-07-06 15:30:00 +02:00
2019-05-26 20:14:53 +02:00
public class InteractionTeleport extends HabboItem {
2018-07-06 15:30:00 +02:00
private int targetId;
private int targetRoomId;
private int roomUnitID = -1;
2019-12-26 17:53:10 +01:00
private boolean walkable;
2018-07-06 15:30:00 +02:00
2019-05-26 20:14:53 +02:00
public InteractionTeleport(ResultSet set, Item baseItem) throws SQLException {
2018-07-06 15:30:00 +02:00
super(set, baseItem);
walkable = baseItem.allowWalk();
this.setExtradata("0");
2018-07-06 15:30:00 +02:00
}
2019-05-26 20:14:53 +02:00
public InteractionTeleport(int id, int userId, Item item, String extradata, int limitedStack, int limitedSells) {
2018-07-06 15:30:00 +02:00
super(id, userId, item, extradata, limitedStack, limitedSells);
walkable = item.allowWalk();
this.setExtradata("0");
2018-07-06 15:30:00 +02:00
}
@Override
2019-05-26 20:14:53 +02:00
public void serializeExtradata(ServerMessage serverMessage) {
2018-07-06 15:30:00 +02:00
serverMessage.appendInt((this.isLimited() ? 256 : 0));
serverMessage.appendString(this.getExtradata());
super.serializeExtradata(serverMessage);
}
@Override
2019-05-26 20:14:53 +02:00
public boolean canWalkOn(RoomUnit roomUnit, Room room, Object[] objects) {
2019-03-18 02:22:00 +01:00
return this.getBaseItem().allowWalk() || roomUnit.getId() == this.roomUnitID;
2018-07-06 15:30:00 +02:00
}
@Override
public boolean isWalkable() {
return walkable;
2018-07-06 15:30:00 +02:00
}
private void tryTeleport(GameClient client, Room room) {
/*
if user is on item, startTeleport
else if user is on infront, set state 1 and walk on item
else move to infront and interact
*/
2018-07-06 15:30:00 +02:00
Habbo habbo = client.getHabbo();
2018-07-06 15:30:00 +02:00
2019-05-26 20:14:53 +02:00
if (habbo == null)
return;
RoomUnit unit = habbo.getRoomUnit();
2019-05-26 20:14:53 +02:00
if (unit == null)
return;
RoomTile currentLocation = room.getLayout().getTile(this.getX(), this.getY());
2019-05-26 20:14:53 +02:00
if (currentLocation == null)
return;
RoomTile infrontTile = room.getLayout().getTileInFront(currentLocation, this.getRotation());
2019-05-26 20:14:53 +02:00
if (!canUseTeleport(client, room))
return;
2019-05-26 20:14:53 +02:00
if (this.roomUnitID == unit.getId() && unit.getCurrentLocation().equals(currentLocation)) {
startTeleport(room, habbo);
2019-12-26 17:53:10 +01:00
walkable = true;
2020-01-27 21:38:41 +01:00
try {
super.onClick(client, room, new Object[]{"TOGGLE_OVERRIDE"});
} catch (Exception e) {
e.printStackTrace();
}
2019-05-26 20:14:53 +02:00
} else if (unit.getCurrentLocation().equals(currentLocation) || unit.getCurrentLocation().equals(infrontTile)) {
// set state 1 and walk on item
this.roomUnitID = unit.getId();
this.setExtradata("1");
room.updateItemState(this);
unit.setGoalLocation(infrontTile);
2018-07-06 15:30:00 +02:00
List<Runnable> onSuccess = new ArrayList<Runnable>();
List<Runnable> onFail = new ArrayList<Runnable>();
onSuccess.add(() -> {
room.updateTile(currentLocation);
tryTeleport(client, room);
unit.removeOverrideTile(currentLocation);
unit.setCanLeaveRoomByDoor(true);
2019-12-26 17:53:10 +01:00
walkable = this.getBaseItem().allowWalk();
});
onFail.add(() -> {
walkable = this.getBaseItem().allowWalk();
room.updateTile(currentLocation);
this.setExtradata("0");
room.updateItemState(this);
this.roomUnitID = -1;
unit.removeOverrideTile(currentLocation);
unit.setCanLeaveRoomByDoor(true);
});
walkable = true;
room.updateTile(currentLocation);
unit.addOverrideTile(currentLocation);
unit.setGoalLocation(currentLocation);
unit.setCanLeaveRoomByDoor(false);
Emulator.getThreading().run(new RoomUnitWalkToLocation(unit, currentLocation, room, onSuccess, onFail));
2019-05-26 20:14:53 +02:00
} else {
// walk to teleport and interact
List<Runnable> onSuccess = new ArrayList<Runnable>();
List<Runnable> onFail = new ArrayList<Runnable>();
onSuccess.add(() -> {
tryTeleport(client, room);
});
unit.setGoalLocation(infrontTile);
Emulator.getThreading().run(new RoomUnitWalkToLocation(unit, infrontTile, room, onSuccess, onFail));
}
2018-07-06 15:30:00 +02:00
}
@Override
2019-05-26 20:14:53 +02:00
public void onClick(GameClient client, Room room, Object[] objects) throws Exception {
2020-01-27 21:38:41 +01:00
if (room != null && client != null && objects != null && objects.length <= 1) {
tryTeleport(client, room);
2018-07-06 15:30:00 +02:00
}
}
@Override
public void onWalk(RoomUnit roomUnit, Room room, Object[] objects) throws Exception {
2018-07-06 15:30:00 +02:00
}
@Override
2019-05-26 20:14:53 +02:00
public void run() {
if (!this.getExtradata().equals("0")) {
2018-07-06 15:30:00 +02:00
this.setExtradata("0");
Room room = Emulator.getGameEnvironment().getRoomManager().getRoom(this.getRoomId());
2019-05-26 20:14:53 +02:00
if (room != null) {
2018-07-06 15:30:00 +02:00
room.updateItem(this);
}
}
super.run();
}
@Override
2019-05-26 20:14:53 +02:00
public void onPickUp(Room room) {
2018-07-06 15:30:00 +02:00
this.targetId = 0;
this.targetRoomId = 0;
this.roomUnitID = -1;
2018-07-06 15:30:00 +02:00
this.setExtradata("0");
}
2019-05-26 20:14:53 +02:00
public int getTargetId() {
2018-07-06 15:30:00 +02:00
return this.targetId;
}
2019-05-26 20:14:53 +02:00
public void setTargetId(int targetId) {
2018-07-06 15:30:00 +02:00
this.targetId = targetId;
}
2019-05-26 20:14:53 +02:00
public int getTargetRoomId() {
2018-07-06 15:30:00 +02:00
return this.targetRoomId;
}
2019-05-26 20:14:53 +02:00
public void setTargetRoomId(int targetRoomId) {
2018-07-06 15:30:00 +02:00
this.targetRoomId = targetRoomId;
}
@Override
2019-05-26 20:14:53 +02:00
public boolean allowWiredResetState() {
2018-07-06 15:30:00 +02:00
return false;
}
public boolean canUseTeleport(GameClient client, Room room) {
Habbo habbo = client.getHabbo();
2019-05-26 20:14:53 +02:00
if (habbo == null)
return false;
RoomUnit unit = habbo.getRoomUnit();
2019-05-26 20:14:53 +02:00
if (unit == null)
return false;
2019-05-26 20:14:53 +02:00
if (habbo.getHabboInfo().getRiding() != null)
return false;
return true;
}
2019-05-26 20:14:53 +02:00
public void startTeleport(Room room, Habbo habbo) {
2019-05-30 18:26:42 +02:00
this.startTeleport(room, habbo, 500);
}
public void startTeleport(Room room, Habbo habbo, int delay) {
2019-12-26 17:53:10 +01:00
if (habbo.getRoomUnit().isTeleporting) {
walkable = this.getBaseItem().allowWalk();
return;
2019-12-26 17:53:10 +01:00
}
this.roomUnitID = -1;
habbo.getRoomUnit().isTeleporting = true;
2019-05-30 18:26:42 +02:00
Emulator.getThreading().run(new TeleportActionOne(this, room, habbo.getClient()), delay);
2018-07-06 15:30:00 +02:00
}
2018-10-07 00:28:00 +02:00
@Override
2019-05-26 20:14:53 +02:00
public boolean isUsable() {
2018-10-07 00:28:00 +02:00
return true;
}
@Override
public boolean invalidatesToRoomKick() {
return true;
}
2018-07-06 15:30:00 +02:00
}