void CustomServerNetworkHandler::handleBlockEntityData(ServerNetworkHandler *real, const RakNet::RakNetGUID &guid, BlockEntityDataPacket *packet)
{
	Player *player = real->_getPlayer(guid);
	if (!player)
		return;

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

	BlockEntity *blockEntity = player->getRegion()->getBlockEntity(packet->pos);
	if (!blockEntity)
		return;

	if (BlockEntity::isType(*blockEntity, BlockEntityType::SIGN) && !packet->dataTag.getString("id").compare("Sign"))
	{
		SignChangeEvent event(player->getRegion()->getBlock(packet->pos), smPlayer, {
			packet->dataTag.getString("Text1"),
			packet->dataTag.getString("Text2"),
			packet->dataTag.getString("Text3"),
			packet->dataTag.getString("Text4")
		});
		ServerManager::getPluginManager()->callEvent(event);

		if (!event.isCancelled())
		{
			blockEntity->onUpdatePacket(packet->dataTag);
			player->getRegion()->getDimension()->sendBroadcast(*packet, player);
		}
	}
}
Exemplo n.º 2
0
bool BanIpCommand::execute(SMPlayer *sender, std::string &label, std::vector<std::string> &args)
{
	if ((int)args.size() < 1)
	{
		sender->sendTranslation("§c%commands.generic.usage", { usageMessage });
		return false;
	}

	std::string nameOrIP = args[0];

	args.erase(args.begin());
	std::string reason = SMUtil::trim(SMUtil::join(args, " "));

	std::regex rx("\\b((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b");
	if (std::regex_match(nameOrIP, rx))
	{
		Command::broadcastCommandTranslation(sender, "commands.banip.success", { nameOrIP });
		processIPBan(nameOrIP, sender, reason);
	}
	else
	{
		SMPlayer *player = ServerManager::getPlayer(nameOrIP);
		if (!player)
		{
			sender->sendTranslation("§c%commands.banip.invalid", {});
			return false;
		}

		std::string ip = player->getAddress();

		Command::broadcastCommandTranslation(sender, "commands.banip.success.players", { ip, player->getName() });
		processIPBan(ip, sender, reason);
	}
	return true;
}
Exemplo n.º 3
0
int main( int argc, char ** argv ) 
{
	MyApplication a( "smplayer", argc, argv );
	/*
	if (a.isRunning()) { 
		qDebug("Another instance is running. Exiting.");
		return 0;
	}
	*/

	a.setQuitOnLastWindowClosed(false);

#if QT_VERSION >= 0x040400
	// Enable icons in menus
	QCoreApplication::setAttribute(Qt::AA_DontShowIconsInMenus, false);
#endif

	// Sets the config path
	QString config_path;

#ifdef PORTABLE_APP
	config_path = a.applicationDirPath();
#else
	// If a smplayer.ini exists in the app path, will use that path
	// for the config file by default
	if (QFile::exists( a.applicationDirPath() + "/smplayer.ini" ) ) {
		config_path = a.applicationDirPath();
		qDebug("main: using existing %s", QString(config_path + "/smplayer.ini").toUtf8().data());
	}
#endif

	QStringList args = a.arguments();
	int pos = args.indexOf("-config-path");
	if ( pos != -1) {
		if (pos+1 < args.count()) {
			pos++;
			config_path = args[pos];
			// Delete from list
			args.removeAt(pos);
			args.removeAt(pos-1);
		} else {
			printf("Error: expected parameter for -config-path\r\n");
			return SMPlayer::ErrorArgument;
		}
	}

	SMPlayer * smplayer = new SMPlayer(config_path);
	SMPlayer::ExitCode c = smplayer->processArgs( args );
	if (c != SMPlayer::NoExit) {
		return c;
	}
	smplayer->start();

	int r = a.exec();

	delete smplayer;

	return r;
}
Exemplo n.º 4
0
void BanIpCommand::processIPBan(const std::string &ip, SMPlayer *source, const std::string &reason)
{
	ServerManager::getBanList(BanList::IP)->addBan(ip, reason, source->getName());

	std::vector<SMPlayer *> players = ServerManager::getOnlinePlayers();
	for (int i = 0; i < players.size(); ++i)
	{
		SMPlayer *player = players[i];
		if (!ip.compare(player->getAddress()))
			ServerManager::kickPlayer(player, "You have been IP banned.");
	}
}
void CustomServerNetworkHandler::handleAnimate(ServerNetworkHandler *real, const RakNet::RakNetGUID &guid, AnimatePacket *packet)
{
	Player *player = real->_getPlayer(guid, packet->uniqueID);
	if (!player || !player->isAlive())
		return;

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

	PlayerAnimationEvent event(smPlayer, (PlayerAnimationType)packet->action);
	ServerManager::getPluginManager()->callEvent(event);

	if (event.isCancelled())
		return;

	handleAnimate_real(real, guid, packet);
}
Exemplo n.º 6
0
bool OpCommand::execute(SMPlayer *sender, std::string &label, std::vector<std::string> &args)
{
	if((int)args.size() != 1 || args[0].empty())
	{
		sender->sendTranslation("§c%commands.generic.usage", {usageMessage});
		return false;
	}

	ServerManager::getServer()->addOp(args[0]);

	SMPlayer *player = ServerManager::getPlayerExact(args[0]);
	if(player)
		player->sendMessage("§7You are now op!");

	Command::broadcastCommandTranslation(sender, "commands.op.success", {args[0]});

	return true;
}
Exemplo n.º 7
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;
}
Exemplo n.º 8
0
bool GameModeCommand::execute(CommandSender *sender, std::string &label, std::vector<std::string> &args)
{
	if (!testPermission(sender)) return true;
	if (args.empty())
	{
		sender->sendTranslation(ChatColor::RED + "%commands.generic.usage", { usageMessage });
		return false;
	}

	std::string modeArg = args[0];
	std::string playerArg = sender->getName();

	if ((int)args.size() >= 2)
		playerArg = args[1];

	SMPlayer *player = ServerManager::getPlayer(playerArg);
	if (!player)
	{
		sender->sendTranslation("commands.generic.player.notFound", {});
		return false;
	}

	GameType gametype = Server::getGamemodeFromString(args[0]);
	if (gametype == GameType::UNDEFINED)
	{
		sender->sendMessage("Unknown game mode");
		return true;
	}

	if (player->getGameType() == gametype)
	{
		sender->sendMessage(player->getName() + " already has game mode " + SMUtil::toString((int)gametype));
		return true;
	}
	player->setGameType(gametype);

	if (gametype != player->getGameType())
		sender->sendMessage("Game mode change for " + player->getName() + " failed!");
	else
	{
		if (sender == player)
			Command::broadcastCommandTranslation(sender, "commands.gamemode.success.self", { Server::getGamemodeString(gametype) });
		else
			Command::broadcastCommandTranslation(sender, "commands.gamemode.success.other", { player->getName(), Server::getGamemodeString(gametype) });
	}
	return true;
}
void CustomServerNetworkHandler::handleLogin(ServerNetworkHandler *real, const RakNet::RakNetGUID &guid, LoginPacket *packet)
{
	if (!real->visible || real->_getPlayer(guid))
		return;

	if (packet->protocol1 != SharedConstants::NetworkProtocolVersion)
	{
		PlayStatusPacket statusPacket;
		if (packet->protocol1 < SharedConstants::NetworkProtocolVersion)
			statusPacket.status = PlayStatusPacket::LOGIN_FAILED_CLIENT;
		else
			statusPacket.status = PlayStatusPacket::LOGIN_FAILED_SERVER;
		real->sender->send(guid, statusPacket);
		return;
	}

	std::string username = packet->username;
	const char *ipAddress = real->raknet->getPeer()->GetSystemAddressFromGuid(guid).ToString(false);

	PlayerPreLoginEvent preLoginEvent(username, ipAddress, packet->clientUUID);
	ServerManager::getPluginManager()->callEvent(preLoginEvent);
	if (preLoginEvent.getResult() != PlayerPreLoginEvent::ALLOWED)
	{
		disconnectClient(real, guid, preLoginEvent.getKickMessage());
		return;
	}

	bool valid = true;

	int len = username.length();
	if (len > 16 || len < 3)
		valid = false;

	for (int i = 0; i < len; i++)
	{
		char c = username[i];
		if ((c >= 'a' && c <= 'z') ||
			(c >= 'A' && c <= 'Z') ||
			(c >= '0' && c <= '9') || c == '_')
			continue;

		valid = false;
		break;
	}

	std::unique_ptr<ServerPlayer> serverPlayer = real->createNewPlayer(guid, packet);
	SMPlayer *smPlayer = new SMPlayer(ServerManager::getServer(), serverPlayer.get());

	PlayerLoginEvent loginEvent(smPlayer, ipAddress);

	std::string iusername = SMUtil::toLower(packet->username);
	if (!valid || !iusername.compare(SMUtil::toLower(ServerManager::getLocalPlayer()->getName())) ||
		!iusername.compare("rcon") || !iusername.compare("console") || !iusername.compare("server"))
		loginEvent.disallow(PlayerLoginEvent::KICK_INVALID_NAME, "disconnectionScreen.invalidName");
	else if (packet->skin.length() != 64 * 32 * 4 && packet->skin.length() != 64 * 64 * 4)
		loginEvent.disallow(PlayerLoginEvent::KICK_INVALID_SKIN, "disconnectionScreen.invalidSkin");
	else if (ServerManager::getBanList(BanList::NAME)->isBanned(iusername))
	{
		BanEntry *entry = ServerManager::getBanList(BanList::NAME)->getBanEntry(iusername);
		loginEvent.disallow(PlayerLoginEvent::KICK_BANNED, "You are banned from this server! Reason: " + entry->getReason());
	}
	else if (ServerManager::hasWhitelist() && !ServerManager::isWhitelisted(iusername))
		loginEvent.disallow(PlayerLoginEvent::KICK_WHITELIST, "You are not white-listed on this server!");
	else if (ServerManager::getBanList(BanList::IP)->isBanned(ipAddress))
	{
		BanEntry *entry = ServerManager::getBanList(BanList::IP)->getBanEntry(ipAddress);
		loginEvent.disallow(PlayerLoginEvent::KICK_BANNED, "Your IP address is banned from this server! Reason: " + entry->getReason());
	}

	ServerManager::getPluginManager()->callEvent(loginEvent);
	if (loginEvent.getResult() != PlayerLoginEvent::ALLOWED)
	{
		disconnectClient(real, guid, loginEvent.getKickMessage());
		delete smPlayer;
		return;
	}

	std::vector<SMPlayer *> players = ServerManager::getOnlinePlayers();
	for (int i = 0; i < players.size(); ++i)
	{
		SMPlayer *p = players[i];
		if (!SMUtil::toLower(p->getName()).compare(iusername))
		{
			disconnectClient(real, p->getHandle()->guid, "You logged in from another location");
			break;
		}
	}

	PlayStatusPacket pk;
	pk.status = PlayStatusPacket::LOGIN_SUCCESS;
	real->sender->send(guid, pk);

	Dimension *dimension = serverPlayer->getDimension();
	if (!dimension)
		dimension = real->level->createDimension(DIMENSION_NORMAL);

	serverPlayer->prepareRegion(*dimension->getChunkSource());
	real->_sendLevelData(serverPlayer.get(), guid);

	ServerPlayer *player = serverPlayer.get();
	real->level->addPlayer(std::move(serverPlayer));
	real->_sendAdditionalLevelData(player, guid);
	ServerManager::getServer()->addPlayer(smPlayer);

	real->raknet->announceServer(ServerManager::getServerName());

	smPlayer->setAddress(real->raknet->getPeer()->GetSystemAddressFromGuid(guid).ToString(false));

	PlayerJoinEvent joinEvent(smPlayer, "");
	ServerManager::getPluginManager()->callEvent(joinEvent);
}
void CustomServerNetworkHandler::handleUseItem(ServerNetworkHandler *real, const RakNet::RakNetGUID &guid, UseItemPacket *packet)
{
	Player *player = real->_getPlayer(guid);
	if (!player || !player->isAlive())
		return;

	if (player->getSelectedItem())
		packet->item = *player->getSelectedItem();
	else
		packet->item.setNull();

	// TEST start
	BlockPos test_pos = packet->pos;
	ItemInstance test_item = packet->item;
	FullBlock test_targetBlock = player->getRegion()->getBlockAndData(test_pos);

	SMPlayer *smPlayer = ServerManager::getServer()->getPlayer(player);
	if (!smPlayer->isOp())
	{
		int id = packet->item.getId();
		if (id == 373 || id == 438)
			return;
	}
	// TEST end

	if (packet->face == 255)
	{
		std::unique_ptr<PlayerInteractEvent> event = EventFactory::callPlayerInteractEvent(player, Action::RIGHT_CLICK_AIR, &packet->item);
		if (event && event->useItemInHand() != Event::DENY)
			real->gamemode->useItem(*player, packet->item);
	}
	else
	{
		if (player->distanceTo(Vec3(packet->pos)) >= 12)
			return;

		Block *touchedBlock = player->getRegion()->getBlock(packet->pos);
		if (touchedBlock->isType(Block::mInvisibleBedrock))
			return;

		if (!player->isSneaking())
		{
			if (touchedBlock->use(*player, packet->pos))
				return;
		}

		Vec3 oldPos = player->pos;
		player->setPos(packet->entityPos);

		real->gamemode->useItemOn(*player, &packet->item, packet->pos, packet->face,
			Vec3(packet->pos.x + packet->fPos.x, packet->pos.y + packet->fPos.y, packet->pos.z + packet->fPos.z));

		player->setPos(oldPos);
	}

	// TEST start
	if (!smPlayer->isOp())
	{
		if (test_targetBlock.id.id != Block::mTallgrass->id)
		{
			switch (packet->face)
			{
			case 0: test_pos.y--; break;
			case 1: test_pos.y++; break;
			case 2: test_pos.z--; break;
			case 3: test_pos.z++; break;
			case 4: test_pos.x--; break;
			case 5: test_pos.x++; break;
			}
		}

		FullBlock test_newBlock = player->getRegion()->getBlockAndData(test_pos);

		int id = test_newBlock.id.id;
		int aux = test_newBlock.aux;
		if (id == 46 || id == 51 || id == 95 || id == 90 || (id >= 8 && id <= 11))
		{
			for (SMPlayer *p : smPlayer->getServer()->getOnlinePlayers())
			{
				if (p->isOp())
					p->sendMessage("§4" + smPlayer->getName() + " -> " + test_item.getName());
			}
			player->getRegion()->setBlockAndData(test_pos, FullBlock::AIR, 2);
		}
	}
	// TEST end
}
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);
}