Example #1
0
bool TeleportCommand::execute(CommandSender *sender, std::string &label, std::vector<std::string> &args)
{
	if (!testPermission(sender)) return true;
	if ((int)args.size() < 1 || (int)args.size() > 4)
	{
		sender->sendTranslation(ChatColor::RED + "%commands.generic.usage", { usageMessage });
		return false;
	}

	SMPlayer *player;

	if ((int)args.size() == 1 || (int)args.size() == 3)
		player = (SMPlayer *)sender;
	else
		player = ServerManager::getPlayer(args[0]);

	if (!player)
	{
		sender->sendTranslation(ChatColor::RED + "%commands.generic.player.notFound", {});
		return true;
	}

	if ((int)args.size() < 3)
	{
		SMPlayer *target = ServerManager::getPlayer(args[args.size() - 1]);
		if (!target)
		{
			sender->sendTranslation(ChatColor::RED + "%commands.generic.player.notFound", {});
			return true;
		}
		player->SMEntity::teleport(target, PlayerTeleportEvent::COMMAND);
		Command::broadcastCommandTranslation(sender, "commands.tp.success", { player->getDisplayName(), target->getDisplayName() });
	}
	else if (player->getRegion())
	{
		Location playerLocation = player->getLocation();
		double x = getCoordinate(sender, playerLocation.getX(), args[args.size() - 3]);
		double y = getCoordinate(sender, playerLocation.getY(), args[args.size() - 2], 0, 0);
		double z = getCoordinate(sender, playerLocation.getZ(), args[args.size() - 1]);

		playerLocation.setX(x);
		playerLocation.setY(y);
		playerLocation.setZ(z);

		player->teleport(playerLocation, PlayerTeleportEvent::COMMAND);

		Command::broadcastCommandTranslation(sender, "commands.tp.success.coordinates", { player->getDisplayName(),
				SMUtil::format("%.2f", x), SMUtil::format("%.2f", y), SMUtil::format("%.2f", z) });
	}
	return true;
}
void CustomServerNetworkHandler::handleMovePlayer(ServerNetworkHandler *real, const RakNet::RakNetGUID &guid, MovePlayerPacket *packet)
{
	Player *player = real->_getPlayer(guid, packet->uniqueID);
	if (!player || player->i1 != 0 || player->b4)
		return;

	SMPlayer *smPlayer = ServerManager::getServer()->getPlayer(player);

	Location from(smPlayer->getRegion(), player->lastPos, player->lastRotation);
	Location to = smPlayer->getLocation();

	to.setPos(packet->pos);
	to.setRotation(packet->rot);

	PlayerMoveEvent event(smPlayer, from, to);
	ServerManager::getPluginManager()->callEvent(event);

	if (event.isCancelled())
	{
		from.setY(from.getY() + 1.62f);

		MovePlayerPacket pk;
		pk.uniqueID = player->getUniqueID();
		pk.pos = from.getPos();
		pk.rot = from.getRotation();
		pk.yaw = from.getRotation().y;
		pk.mode = MovePlayerPacket::RESET;
		pk.onGround = false;
		real->sender->send(player->guid, pk);
		return;
	}

	if (!to.equals(event.getTo()) && !event.isCancelled())
	{
		smPlayer->teleport(event.getTo(), PlayerTeleportEvent::UNKNOWN);
		return;
	}

	if (!from.equals(smPlayer->getLocation()) && smPlayer->justTeleported)
	{
		smPlayer->justTeleported = false;
		return;
	}

	if (!player->vehicle)
	{
		if (packet->pos.y < -128.0f)
			packet->pos.y = -128.0f;
		else if (packet->pos.y > 512)
			packet->pos.y = 512.0f;

		player->motion.x = 0;
		player->motion.y = 0;
		player->motion.z = 0;

		if (packet->mode != MovePlayerPacket::RESET || (packet->mode == MovePlayerPacket::RESET && player->isAlive()))
		{
			player->_checkMovementStatistiscs(packet->pos - player->pos);
			if (player->b3 || !player->onGround)
				player->lerpTo(packet->pos, packet->rot, 3);
			else
				player->lerpTo(packet->pos, packet->rot, 1);
		}
		player->checkBlockCollisions();
		player->yaw = packet->yaw;
	}
	else
	{
		player->vehicle->positionRider(*player);
		player->normalTick();
		player->pos4.y = 0;
		player->moveTo(player->pos, packet->rot);
		player->yaw = packet->yaw;

		if (player->vehicle)
			player->vehicle->positionRider(*player);

		player->tick(*player->getRegion());
	}
	player->getRegion()->getDimension()->sendBroadcast(*packet, player);
}