Skip to content

Instantly share code, notes, and snippets.

@NeatMonster
Created January 22, 2015 14:36
Show Gist options
  • Save NeatMonster/bfbecec7b402245fb40d to your computer and use it in GitHub Desktop.
Save NeatMonster/bfbecec7b402245fb40d to your computer and use it in GitHub Desktop.
handlerMovement/sendMovement
void PacketHandler::handleMovement(double_t x, double_t y, double_t z, float_t yaw, float_t pitch, bool onGround) {
EntityPlayer *player = connect->player;
double_t deltaX = x - lastPosX;
double_t deltaY = y - lastPosY;
double_t deltaZ = z - lastPosZ;
double_t delta = deltaX * deltaX + deltaY * deltaY + deltaZ * deltaZ;
std::cout << delta << std::endl;
if (delta < 0.25) {
lastPosX = player->posX;
lastPosY = player->posY;
lastPosZ = player->posZ;
if (MathUtils::abs(x) > 3.0e7 || MathUtils::abs(z) > 3.0e7) {
player->disconnect("Position illégale");
return;
}
if (yaw != player->rotYaw || pitch != player->rotPitch)
player->setRotation(yaw, pitch);
deltaX = x - player->posX;
deltaY = y - player->posY;
deltaZ = z - player->posZ;
double_t speedX = MathUtils::min(MathUtils::abs(deltaX), MathUtils::abs(player->motX));
double_t speedY = MathUtils::min(MathUtils::abs(deltaY), MathUtils::abs(player->motY));
double_t speedZ = MathUtils::min(MathUtils::abs(deltaZ), MathUtils::abs(player->motZ));
double_t speed = speedX * speedX + speedY * speedY + speedZ * speedZ;
if (speed > 100) {
Logger(LogLevel::WARNING) << player->getName() << " s'est déplacé trop vite !" << std::endl;
sendMovement(lastPosX, lastPosY, lastPosZ, player->rotYaw, player->rotPitch);
return;
}
bool free = player->world->getColliding(player,
player->getBoundingBox().clone().expand(0.0625, 0.0625, 0.0625)).empty();
if (player->onGround && deltaY > 0)
player->jump();
player->move(deltaX, deltaY, deltaZ);
player->onGround = onGround;
double_t moveX = x - player->posX;
double_t moveY = y - player->posY;
double_t moveZ = z - player->posZ;
if (moveY > -0.5 || moveY < 0.5)
moveY = 0;
double_t move = moveX * moveX + moveY * moveY + moveZ * moveZ;
bool movedWrongly = false;
if (move > 0.0625 && player->getGameMode() != EntityPlayer::GameMode::CREATIVE) {
movedWrongly = true;
Logger(LogLevel::WARNING) << player->getName() << " s'est déplacé incorrectement !" << std::endl;
}
player->setPosition(x, y, z);
bool stuck = player->world->getColliding(player,
player->getBoundingBox().clone().contract(0.0625, 0.0625, 0.0625)).empty();
if (free && (movedWrongly || !stuck)) {
sendMovement(lastPosX, lastPosY, lastPosZ, yaw, pitch);
return;
}
if (player->getGameMode() != EntityPlayer::GameMode::CREATIVE && !player->world->checkBlockCollision(player->getBoundingBox())) {
if (deltaY >= -0.03125) {
floatingTicks++;
if (floatingTicks > 80) {
Logger(LogLevel::WARNING) << player->getName() << " a été kické pour avoir flotté trop longtemps !" << std::endl;
connect->disconnect("Voler n'est pas autorisé sur ce serveur");
return;
}
}
} else
floatingTicks = 0;
} else
sendMovement(lastPosX, lastPosY, lastPosZ, player->rotYaw, player->rotPitch);
}
void PacketHandler::sendMovement(double_t x, double_t y, double_t z, float_t yaw, float_t pitch) {
connect->player->setPosition(x, y, z);
connect->player->setRotation(yaw, pitch);
connect->player->setHeadRotation(yaw);
PacketPlayerPositionLook *packet = new PacketPlayerPositionLook();
packet->x = x;
packet->y = y;
packet->z = z;
packet->yaw = yaw;
packet->pitch = pitch;
packet->flags = 0;
connect->sendPacket(packet);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment