aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.Client/PlayerConnection.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'Minecraft.Client/PlayerConnection.cpp')
-rw-r--r--Minecraft.Client/PlayerConnection.cpp330
1 files changed, 191 insertions, 139 deletions
diff --git a/Minecraft.Client/PlayerConnection.cpp b/Minecraft.Client/PlayerConnection.cpp
index ab8ae20b..6319b660 100644
--- a/Minecraft.Client/PlayerConnection.cpp
+++ b/Minecraft.Client/PlayerConnection.cpp
@@ -21,6 +21,8 @@
#include "..\Minecraft.World\AABB.h"
#include "..\Minecraft.World\Pos.h"
#include "..\Minecraft.World\SharedConstants.h"
+#include "..\Minecraft.World\ChatPacket.h"
+#include "..\Minecraft.World\StringHelpers.h"
#include "..\Minecraft.World\Socket.h"
#include "..\Minecraft.World\Achievements.h"
#include "..\Minecraft.World\net.minecraft.h"
@@ -32,9 +34,26 @@
// 4J Added
#include "..\Minecraft.World\net.minecraft.world.item.crafting.h"
#include "Options.h"
+#if defined(_WINDOWS64) && defined(MINECRAFT_SERVER_BUILD)
+#include "..\Minecraft.Server\ServerLogManager.h"
+#endif
+
+namespace
+{
+ // Anti-cheat thresholds. Keep server-side checks authoritative even in host mode.
+ // Base max squared displacement allowed per move packet before speed flags trigger.
+ const double kMoveBaseAllowanceSq = 100.0;
+ // Extra squared displacement allowance derived from current server-side velocity.
+ const double kMoveVelocityAllowanceScale = 100.0;
+ // Max squared distance for interact/attack when the target is visible (normal reach).
+ const double kInteractReachSq = 6.0 * 6.0;
+ // Stricter max squared distance used when LOS is blocked to reduce wall-hit abuse.
+ const double kInteractBlockedReachSq = 3.0 * 3.0;
+}
Random PlayerConnection::random;
+
PlayerConnection::PlayerConnection(MinecraftServer *server, Connection *connection, shared_ptr<ServerPlayer> player)
{
// 4J - added initialisers
@@ -64,6 +83,13 @@ PlayerConnection::PlayerConnection(MinecraftServer *server, Connection *connecti
m_offlineXUID = INVALID_XUID;
m_onlineXUID = INVALID_XUID;
m_bHasClientTickedOnce = false;
+ m_logSmallId = 0;
+
+ // Cache the first valid transport smallId because disconnect teardown can clear it before the server logger runs.
+ if (this->connection != NULL && this->connection->getSocket() != NULL)
+ {
+ m_logSmallId = this->connection->getSocket()->getSmallId();
+ }
setShowOnMaps(app.GetGameHostOption(eGameHostOption_Gamertags)!=0?true:false);
}
@@ -74,6 +100,17 @@ PlayerConnection::~PlayerConnection()
DeleteCriticalSection(&done_cs);
}
+unsigned char PlayerConnection::getLogSmallId()
+{
+ // Fall back to the live socket only while the cached value is still empty.
+ if (m_logSmallId == 0 && connection != NULL && connection->getSocket() != NULL)
+ {
+ m_logSmallId = connection->getSocket()->getSmallId();
+ }
+
+ return m_logSmallId;
+}
+
void PlayerConnection::tick()
{
if( done ) return;
@@ -94,7 +131,7 @@ void PlayerConnection::tick()
lastKeepAliveTick = tickCount;
lastKeepAliveTime = System::nanoTime() / 1000000;
lastKeepAliveId = random.nextInt();
- send( shared_ptr<KeepAlivePacket>( new KeepAlivePacket(lastKeepAliveId) ) );
+ send(std::make_shared<KeepAlivePacket>(lastKeepAliveId));
}
if (chatSpamTickCount > 0)
@@ -116,22 +153,29 @@ void PlayerConnection::disconnect(DisconnectPacket::eDisconnectReason reason)
return;
}
+#if defined(_WINDOWS64) && defined(MINECRAFT_SERVER_BUILD)
+ ServerRuntime::ServerLogManager::OnPlayerDisconnected(
+ getLogSmallId(),
+ (player != NULL) ? player->name : std::wstring(),
+ reason,
+ true);
+#endif
app.DebugPrintf("PlayerConnection disconect reason: %d\n", reason );
player->disconnect();
// 4J Stu - Need to remove the player from the receiving list before their socket is NULLed so that we can find another player on their system
server->getPlayers()->removePlayerFromReceiving( player );
- send( shared_ptr<DisconnectPacket>( new DisconnectPacket(reason) ));
+ send(std::make_shared<DisconnectPacket>(reason));
connection->sendAndQuit();
// 4J-PB - removed, since it needs to be localised in the language the client is in
//server->players->broadcastAll( shared_ptr<ChatPacket>( new ChatPacket(L"�e" + player->name + L" left the game.") ) );
if(getWasKicked())
{
- server->getPlayers()->broadcastAll( shared_ptr<ChatPacket>( new ChatPacket(player->name, ChatPacket::e_ChatPlayerKickedFromGame) ) );
+ server->getPlayers()->broadcastAll(std::make_shared<ChatPacket>(player->name, ChatPacket::e_ChatPlayerKickedFromGame));
}
else
{
- server->getPlayers()->broadcastAll( shared_ptr<ChatPacket>( new ChatPacket(player->name, ChatPacket::e_ChatPlayerLeftGame) ) );
+ server->getPlayers()->broadcastAll(std::make_shared<ChatPacket>(player->name, ChatPacket::e_ChatPlayerLeftGame));
}
server->getPlayers()->remove(player);
@@ -164,7 +208,7 @@ void PlayerConnection::handleMovePlayer(shared_ptr<MovePlayerPacket> packet)
if (synched)
{
- if (player->riding != NULL)
+ if (player->riding != nullptr)
{
float yRotT = player->yRot;
@@ -185,7 +229,7 @@ void PlayerConnection::handleMovePlayer(shared_ptr<MovePlayerPacket> packet)
player->doTick(false);
player->ySlideOffset = 0;
player->absMoveTo(xt, yt, zt, yRotT, xRotT);
- if (player->riding != NULL) player->riding->positionRider();
+ if (player->riding != nullptr) player->riding->positionRider();
server->getPlayers()->move(player);
// player may have been kicked off the mount during the tick, so
@@ -195,7 +239,7 @@ void PlayerConnection::handleMovePlayer(shared_ptr<MovePlayerPacket> packet)
yLastOk = player->y;
zLastOk = player->z;
}
- ((Level *)level)->tick(player);
+ static_cast<Level *>(level)->tick(player);
return;
}
@@ -204,7 +248,7 @@ void PlayerConnection::handleMovePlayer(shared_ptr<MovePlayerPacket> packet)
{
player->doTick(false);
player->absMoveTo(xLastOk, yLastOk, zLastOk, player->yRot, player->xRot);
- ((Level *)level)->tick(player);
+ static_cast<Level *>(level)->tick(player);
return;
}
@@ -269,16 +313,19 @@ void PlayerConnection::handleMovePlayer(shared_ptr<MovePlayerPacket> packet)
double dist = xDist * xDist + yDist * yDist + zDist * zDist;
- // 4J-PB - removing this one for now
- /*if (dist > 100.0f)
+ // Anti-cheat: reject movement packets that exceed server-authoritative bounds.
+ double velocitySq = player->xd * player->xd + player->yd * player->yd + player->zd * player->zd;
+ double maxAllowedSq = kMoveBaseAllowanceSq + (velocitySq * kMoveVelocityAllowanceScale);
+ if (player->isAllowedToFly() || player->gameMode->isCreative())
{
- // logger.warning(player->name + " moved too quickly!");
- disconnect(DisconnectPacket::eDisconnect_MovedTooQuickly);
- // System.out.println("Moved too quickly at " + xt + ", " + yt + ", " + zt);
- // teleport(player->x, player->y, player->z, player->yRot, player->xRot);
- return;
+ // Creative / flight-allowed players can move farther legitimately per tick.
+ maxAllowedSq *= 1.5;
+ }
+ if (dist > maxAllowedSq)
+ {
+ disconnect(DisconnectPacket::eDisconnect_MovedTooQuickly);
+ return;
}
- */
float r = 1 / 16.0f;
bool oldOk = level->getCubes(player, player->bb->copy()->shrink(r, r, r))->empty();
@@ -306,8 +353,8 @@ void PlayerConnection::handleMovePlayer(shared_ptr<MovePlayerPacket> packet)
xDist = xt - player->x;
yDist = yt - player->y;
- // 4J-PB - line below will always be true!
- if (yDist > -0.5 || yDist < 0.5)
+ // Clamp tiny Y drift noise to reduce false positives.
+ if (yDist > -0.5 && yDist < 0.5)
{
yDist = 0;
}
@@ -376,7 +423,7 @@ void PlayerConnection::teleport(double x, double y, double z, float yRot, float
player->absMoveTo(x, y, z, yRot, xRot);
// 4J - note that 1.62 is added to the height here as the client connection that receives this will presume it represents y + heightOffset at that end
// This is different to the way that height is sent back to the server, where it represents the bottom of the player bounding volume
- if(sendPacket) player->connection->send( shared_ptr<MovePlayerPacket>( new MovePlayerPacket::PosRot(x, y + 1.62f, y, z, yRot, xRot, false, false) ) );
+ if(sendPacket) player->connection->send(std::make_shared<MovePlayerPacket::PosRot>(x, y + 1.62f, y, z, yRot, xRot, false, false));
}
void PlayerConnection::handlePlayerAction(shared_ptr<PlayerActionPacket> packet)
@@ -428,20 +475,21 @@ void PlayerConnection::handlePlayerAction(shared_ptr<PlayerActionPacket> packet)
if (packet->action == PlayerActionPacket::START_DESTROY_BLOCK)
{
- if (true) player->gameMode->startDestroyBlock(x, y, z, packet->face); // 4J - condition was !server->isUnderSpawnProtection(level, x, y, z, player) (from Java 1.6.4) but putting back to old behaviour
- else player->connection->send( shared_ptr<TileUpdatePacket>( new TileUpdatePacket(x, y, z, level) ) );
+ // Anti-cheat: validate spawn protection on the server for mining start.
+ if (!server->isUnderSpawnProtection(level, x, y, z, player)) player->gameMode->startDestroyBlock(x, y, z, packet->face);
+ else player->connection->send(std::make_shared<TileUpdatePacket>(x, y, z, level));
}
else if (packet->action == PlayerActionPacket::STOP_DESTROY_BLOCK)
{
player->gameMode->stopDestroyBlock(x, y, z);
server->getPlayers()->prioritiseTileChanges(x, y, z, level->dimension->id); // 4J added - make sure that the update packets for this get prioritised over other general world updates
- if (level->getTile(x, y, z) != 0) player->connection->send( shared_ptr<TileUpdatePacket>( new TileUpdatePacket(x, y, z, level) ) );
+ if (level->getTile(x, y, z) != 0) player->connection->send(std::make_shared<TileUpdatePacket>(x, y, z, level));
}
else if (packet->action == PlayerActionPacket::ABORT_DESTROY_BLOCK)
{
player->gameMode->abortDestroyBlock(x, y, z);
- if (level->getTile(x, y, z) != 0) player->connection->send(shared_ptr<TileUpdatePacket>( new TileUpdatePacket(x, y, z, level)));
+ if (level->getTile(x, y, z) != 0) player->connection->send(std::make_shared<TileUpdatePacket>(x, y, z, level));
}
}
@@ -456,18 +504,17 @@ void PlayerConnection::handleUseItem(shared_ptr<UseItemPacket> packet)
int face = packet->getFace();
player->resetLastActionTime();
- // 4J Stu - We don't have ops, so just use the levels setting
- bool canEditSpawn = level->canEditSpawn; // = level->dimension->id != 0 || server->players->isOp(player->name);
if (packet->getFace() == 255)
{
- if (item == NULL) return;
+ if (item == nullptr) return;
player->gameMode->useItem(player, level, item);
}
else if ((packet->getY() < server->getMaxBuildHeight() - 1) || (packet->getFace() != Facing::UP && packet->getY() < server->getMaxBuildHeight()))
{
if (synched && player->distanceToSqr(x + 0.5, y + 0.5, z + 0.5) < 8 * 8)
{
- if (true) // 4J - condition was !server->isUnderSpawnProtection(level, x, y, z, player) (from java 1.6.4) but putting back to old behaviour
+ // Anti-cheat: block placement/use must pass server-side spawn protection.
+ if (!server->isUnderSpawnProtection(level, x, y, z, player))
{
player->gameMode->useItemOn(player, level, item, x, y, z, face, packet->getClickX(), packet->getClickY(), packet->getClickZ());
}
@@ -484,7 +531,7 @@ void PlayerConnection::handleUseItem(shared_ptr<UseItemPacket> packet)
if (informClient)
{
- player->connection->send( shared_ptr<TileUpdatePacket>( new TileUpdatePacket(x, y, z, level) ) );
+ player->connection->send(std::make_shared<TileUpdatePacket>(x, y, z, level));
if (face == 0) y--;
if (face == 1) y++;
@@ -500,7 +547,7 @@ void PlayerConnection::handleUseItem(shared_ptr<UseItemPacket> packet)
// isn't what it is expecting.
if( level->getTile(x,y,z) != Tile::pistonMovingPiece_Id )
{
- player->connection->send( shared_ptr<TileUpdatePacket>( new TileUpdatePacket(x, y, z, level) ) );
+ player->connection->send(std::make_shared<TileUpdatePacket>(x, y, z, level));
}
}
@@ -508,17 +555,17 @@ void PlayerConnection::handleUseItem(shared_ptr<UseItemPacket> packet)
item = player->inventory->getSelected();
bool forceClientUpdate = false;
- if(item != NULL && packet->getItem() == NULL)
+ if(item != nullptr && packet->getItem() == nullptr)
{
forceClientUpdate = true;
}
- if (item != NULL && item->count == 0)
+ if (item != nullptr && item->count == 0)
{
player->inventory->items[player->inventory->selected] = nullptr;
item = nullptr;
}
- if (item == NULL || item->getUseDuration() == 0)
+ if (item == nullptr || item->getUseDuration() == 0)
{
player->ignoreSlotUpdateHack = true;
player->inventory->items[player->inventory->selected] = ItemInstance::clone(player->inventory->items[player->inventory->selected]);
@@ -528,7 +575,7 @@ void PlayerConnection::handleUseItem(shared_ptr<UseItemPacket> packet)
if (forceClientUpdate || !ItemInstance::matches(player->inventory->getSelected(), packet->getItem()))
{
- send( shared_ptr<ContainerSetSlotPacket>( new ContainerSetSlotPacket(player->containerMenu->containerId, s->index, player->inventory->getSelected()) ) );
+ send(std::make_shared<ContainerSetSlotPacket>(player->containerMenu->containerId, s->index, player->inventory->getSelected()));
}
}
}
@@ -536,17 +583,28 @@ void PlayerConnection::handleUseItem(shared_ptr<UseItemPacket> packet)
void PlayerConnection::onDisconnect(DisconnectPacket::eDisconnectReason reason, void *reasonObjects)
{
EnterCriticalSection(&done_cs);
- if( done ) return;
+ if( done )
+ {
+ LeaveCriticalSection(&done_cs);
+ return;
+ }
+#if defined(_WINDOWS64) && defined(MINECRAFT_SERVER_BUILD)
+ ServerRuntime::ServerLogManager::OnPlayerDisconnected(
+ getLogSmallId(),
+ (player != NULL) ? player->name : std::wstring(),
+ reason,
+ false);
+#endif
// logger.info(player.name + " lost connection: " + reason);
// 4J-PB - removed, since it needs to be localised in the language the client is in
//server->players->broadcastAll( shared_ptr<ChatPacket>( new ChatPacket(L"�e" + player->name + L" left the game.") ) );
if(getWasKicked())
{
- server->getPlayers()->broadcastAll( shared_ptr<ChatPacket>( new ChatPacket(player->name, ChatPacket::e_ChatPlayerKickedFromGame) ) );
+ server->getPlayers()->broadcastAll(std::make_shared<ChatPacket>(player->name, ChatPacket::e_ChatPlayerKickedFromGame));
}
else
{
- server->getPlayers()->broadcastAll( shared_ptr<ChatPacket>( new ChatPacket(player->name, ChatPacket::e_ChatPlayerLeftGame) ) );
+ server->getPlayers()->broadcastAll(std::make_shared<ChatPacket>(player->name, ChatPacket::e_ChatPlayerLeftGame));
}
server->getPlayers()->remove(player);
done = true;
@@ -561,7 +619,7 @@ void PlayerConnection::onUnhandledPacket(shared_ptr<Packet> packet)
void PlayerConnection::send(shared_ptr<Packet> packet)
{
- if( connection->getSocket() != NULL )
+ if( connection->getSocket() != nullptr )
{
if( !server->getPlayers()->canReceiveAllPackets( player ) )
{
@@ -579,7 +637,7 @@ void PlayerConnection::send(shared_ptr<Packet> packet)
// 4J Added
void PlayerConnection::queueSend(shared_ptr<Packet> packet)
{
- if( connection->getSocket() != NULL )
+ if( connection->getSocket() != nullptr )
{
if( !server->getPlayers()->canReceiveAllPackets( player ) )
{
@@ -607,38 +665,26 @@ void PlayerConnection::handleSetCarriedItem(shared_ptr<SetCarriedItemPacket> pac
void PlayerConnection::handleChat(shared_ptr<ChatPacket> packet)
{
- // 4J - TODO
-#if 0
- wstring message = packet->message;
+ if (packet->m_stringArgs.empty()) return;
+ wstring message = trimString(packet->m_stringArgs[0]);
if (message.length() > SharedConstants::maxChatLength)
{
- disconnect(L"Chat message too long");
+ disconnect(DisconnectPacket::eDisconnect_None); // or a specific reason
return;
}
- message = message.trim();
- for (int i = 0; i < message.length(); i++)
- {
- if (SharedConstants.acceptableLetters.indexOf(message.charAt(i)) < 0 && (int) message.charAt(i) < 32)
- {
- disconnect(L"Illegal characters in chat");
- return;
- }
- }
-
- if (message.startsWith("/"))
+ // Optional: validate characters (acceptableLetters)
+ if (message.length() > 0 && message[0] == L'/')
{
handleCommand(message);
- } else {
- message = "<" + player.name + "> " + message;
- logger.info(message);
- server.players.broadcastAll(new ChatPacket(message));
+ return;
}
+ wstring formatted = L"<" + player->name + L"> " + message;
+ server->getPlayers()->broadcastAll(shared_ptr<ChatPacket>(new ChatPacket(formatted)));
chatSpamTickCount += SharedConstants::TICKS_PER_SECOND;
if (chatSpamTickCount > SharedConstants::TICKS_PER_SECOND * 10)
{
- disconnect("disconnect.spam");
+ disconnect(DisconnectPacket::eDisconnect_None); // spam
}
-#endif
}
void PlayerConnection::handleCommand(const wstring& message)
@@ -685,7 +731,7 @@ void PlayerConnection::handlePlayerCommand(shared_ptr<PlayerCommandPacket> packe
else if (packet->action == PlayerCommandPacket::RIDING_JUMP)
{
// currently only supported by horses...
- if ( (player->riding != NULL) && player->riding->GetType() == eTYPE_HORSE)
+ if ( (player->riding != nullptr) && player->riding->GetType() == eTYPE_HORSE)
{
dynamic_pointer_cast<EntityHorse>(player->riding)->onPlayerJump(packet->data);
}
@@ -693,7 +739,7 @@ void PlayerConnection::handlePlayerCommand(shared_ptr<PlayerCommandPacket> packe
else if (packet->action == PlayerCommandPacket::OPEN_INVENTORY)
{
// also only supported by horses...
- if ( (player->riding != NULL) && player->riding->instanceof(eTYPE_HORSE) )
+ if ( (player->riding != nullptr) && player->riding->instanceof(eTYPE_HORSE) )
{
dynamic_pointer_cast<EntityHorse>(player->riding)->openInventory(player);
}
@@ -752,17 +798,16 @@ void PlayerConnection::handleInteract(shared_ptr<InteractPacket> packet)
// 4J Stu - If the client says that we hit something, then agree with it. The canSee can fail here as it checks
// a ray from head->head, but we may actually be looking at a different part of the entity that can be seen
// even though the ray is blocked.
- if (target != NULL) // && player->canSee(target) && player->distanceToSqr(target) < 6 * 6)
+ if (target != nullptr)
{
- //boole canSee = player->canSee(target);
- //double maxDist = 6 * 6;
- //if (!canSee)
- //{
- // maxDist = 3 * 3;
- //}
+ // Anti-cheat: enforce reach and LOS on the server to reject forged hits.
+ bool canSeeTarget = player->canSee(target);
+ double maxDistSq = canSeeTarget ? kInteractReachSq : kInteractBlockedReachSq;
+ if (player->distanceToSqr(target) > maxDistSq)
+ {
+ return;
+ }
- //if (player->distanceToSqr(target) < maxDist)
- //{
if (packet->action == InteractPacket::INTERACT)
{
player->interact(target);
@@ -777,7 +822,6 @@ void PlayerConnection::handleInteract(shared_ptr<InteractPacket> packet)
}
player->attack(target);
}
- //}
}
}
@@ -797,13 +841,13 @@ void PlayerConnection::handleTexture(shared_ptr<TexturePacket> packet)
#ifndef _CONTENT_PACKAGE
wprintf(L"Server received request for custom texture %ls\n",packet->textureName.c_str());
#endif
- PBYTE pbData=NULL;
+ PBYTE pbData=nullptr;
DWORD dwBytes=0;
app.GetMemFileDetails(packet->textureName,&pbData,&dwBytes);
if(dwBytes!=0)
{
- send( shared_ptr<TexturePacket>( new TexturePacket(packet->textureName,pbData,dwBytes) ) );
+ send(std::make_shared<TexturePacket>(packet->textureName, pbData, dwBytes));
}
else
{
@@ -831,7 +875,7 @@ void PlayerConnection::handleTextureAndGeometry(shared_ptr<TextureAndGeometryPac
#ifndef _CONTENT_PACKAGE
wprintf(L"Server received request for custom texture %ls\n",packet->textureName.c_str());
#endif
- PBYTE pbData=NULL;
+ PBYTE pbData=nullptr;
DWORD dwTextureBytes=0;
app.GetMemFileDetails(packet->textureName,&pbData,&dwTextureBytes);
DLCSkinFile *pDLCSkinFile = app.m_dlcManager.getSkinFile(packet->textureName);
@@ -843,11 +887,11 @@ void PlayerConnection::handleTextureAndGeometry(shared_ptr<TextureAndGeometryPac
{
if(pDLCSkinFile->getAdditionalBoxesCount()!=0)
{
- send( shared_ptr<TextureAndGeometryPacket>( new TextureAndGeometryPacket(packet->textureName,pbData,dwTextureBytes,pDLCSkinFile) ) );
+ send(std::make_shared<TextureAndGeometryPacket>(packet->textureName, pbData, dwTextureBytes, pDLCSkinFile));
}
else
{
- send( shared_ptr<TextureAndGeometryPacket>( new TextureAndGeometryPacket(packet->textureName,pbData,dwTextureBytes) ) );
+ send(std::make_shared<TextureAndGeometryPacket>(packet->textureName, pbData, dwTextureBytes));
}
}
else
@@ -856,7 +900,7 @@ void PlayerConnection::handleTextureAndGeometry(shared_ptr<TextureAndGeometryPac
vector<SKIN_BOX *> *pvSkinBoxes = app.GetAdditionalSkinBoxes(packet->dwSkinID);
unsigned int uiAnimOverrideBitmask= app.GetAnimOverrideBitmask(packet->dwSkinID);
- send( shared_ptr<TextureAndGeometryPacket>( new TextureAndGeometryPacket(packet->textureName,pbData,dwTextureBytes,pvSkinBoxes,uiAnimOverrideBitmask) ) );
+ send(std::make_shared<TextureAndGeometryPacket>(packet->textureName, pbData, dwTextureBytes, pvSkinBoxes, uiAnimOverrideBitmask));
}
}
else
@@ -895,13 +939,13 @@ void PlayerConnection::handleTextureReceived(const wstring &textureName)
auto it = find(m_texturesRequested.begin(), m_texturesRequested.end(), textureName);
if( it != m_texturesRequested.end() )
{
- PBYTE pbData=NULL;
+ PBYTE pbData=nullptr;
DWORD dwBytes=0;
app.GetMemFileDetails(textureName,&pbData,&dwBytes);
if(dwBytes!=0)
{
- send( shared_ptr<TexturePacket>( new TexturePacket(textureName,pbData,dwBytes) ) );
+ send(std::make_shared<TexturePacket>(textureName, pbData, dwBytes));
m_texturesRequested.erase(it);
}
}
@@ -913,7 +957,7 @@ void PlayerConnection::handleTextureAndGeometryReceived(const wstring &textureNa
auto it = find(m_texturesRequested.begin(), m_texturesRequested.end(), textureName);
if( it != m_texturesRequested.end() )
{
- PBYTE pbData=NULL;
+ PBYTE pbData=nullptr;
DWORD dwTextureBytes=0;
app.GetMemFileDetails(textureName,&pbData,&dwTextureBytes);
DLCSkinFile *pDLCSkinFile=app.m_dlcManager.getSkinFile(textureName);
@@ -922,7 +966,7 @@ void PlayerConnection::handleTextureAndGeometryReceived(const wstring &textureNa
{
if(pDLCSkinFile && (pDLCSkinFile->getAdditionalBoxesCount()!=0))
{
- send( shared_ptr<TextureAndGeometryPacket>( new TextureAndGeometryPacket(textureName,pbData,dwTextureBytes,pDLCSkinFile) ) );
+ send(std::make_shared<TextureAndGeometryPacket>(textureName, pbData, dwTextureBytes, pDLCSkinFile));
}
else
{
@@ -931,7 +975,7 @@ void PlayerConnection::handleTextureAndGeometryReceived(const wstring &textureNa
vector<SKIN_BOX *> *pvSkinBoxes = app.GetAdditionalSkinBoxes(dwSkinID);
unsigned int uiAnimOverrideBitmask= app.GetAnimOverrideBitmask(dwSkinID);
- send( shared_ptr<TextureAndGeometryPacket>( new TextureAndGeometryPacket(textureName,pbData,dwTextureBytes, pvSkinBoxes, uiAnimOverrideBitmask) ) );
+ send(std::make_shared<TextureAndGeometryPacket>(textureName, pbData, dwTextureBytes, pvSkinBoxes, uiAnimOverrideBitmask));
}
m_texturesRequested.erase(it);
}
@@ -958,20 +1002,25 @@ void PlayerConnection::handleTextureChange(shared_ptr<TextureChangePacket> packe
}
if(!packet->path.empty() && packet->path.substr(0,3).compare(L"def") != 0 && !app.IsFileInMemoryTextures(packet->path))
{
- if( server->connection->addPendingTextureRequest(packet->path))
- {
+ if (server->connection->addPendingTextureRequest(packet->path))
+ {
#ifndef _CONTENT_PACKAGE
- wprintf(L"Sending texture packet to get custom skin %ls from player %ls\n",packet->path.c_str(), player->name.c_str());
+ wprintf(L"Sending texture packet to get custom skin %ls from player %ls\n", packet->path.c_str(), player->name.c_str());
#endif
- send(shared_ptr<TexturePacket>( new TexturePacket(packet->path,NULL,0) ) );
- }
- }
+ send(std::make_shared<TexturePacket>(
+ packet->path,
+ nullptr,
+ static_cast<DWORD>(0)
+ ));
+
+ }
+ }
else if(!packet->path.empty() && app.IsFileInMemoryTextures(packet->path))
{
// Update the ref count on the memory texture data
- app.AddMemoryTextureFile(packet->path,NULL,0);
+ app.AddMemoryTextureFile(packet->path,nullptr,0);
}
- server->getPlayers()->broadcastAll( shared_ptr<TextureChangePacket>( new TextureChangePacket(player,packet->action,packet->path) ), player->dimension );
+ server->getPlayers()->broadcastAll(std::make_shared<TextureChangePacket>(player, packet->action, packet->path), player->dimension );
}
void PlayerConnection::handleTextureAndGeometryChange(shared_ptr<TextureAndGeometryChangePacket> packet)
@@ -990,13 +1039,16 @@ void PlayerConnection::handleTextureAndGeometryChange(shared_ptr<TextureAndGeome
#ifndef _CONTENT_PACKAGE
wprintf(L"Sending texture packet to get custom skin %ls from player %ls\n",packet->path.c_str(), player->name.c_str());
#endif
- send(shared_ptr<TextureAndGeometryPacket>( new TextureAndGeometryPacket(packet->path,NULL,0) ) );
+ send(std::make_shared<TextureAndGeometryPacket>(
+ packet->path,
+ nullptr,
+ static_cast<DWORD>(0)));
}
}
else if(!packet->path.empty() && app.IsFileInMemoryTextures(packet->path))
{
// Update the ref count on the memory texture data
- app.AddMemoryTextureFile(packet->path,NULL,0);
+ app.AddMemoryTextureFile(packet->path,nullptr,0);
player->setCustomSkin(packet->dwSkinID);
@@ -1004,7 +1056,7 @@ void PlayerConnection::handleTextureAndGeometryChange(shared_ptr<TextureAndGeome
//app.SetAdditionalSkinBoxes(packet->dwSkinID,)
//DebugBreak();
}
- server->getPlayers()->broadcastAll( shared_ptr<TextureAndGeometryChangePacket>( new TextureAndGeometryChangePacket(player,packet->path) ), player->dimension );
+ server->getPlayers()->broadcastAll(std::make_shared<TextureAndGeometryChangePacket>(player, packet->path), player->dimension );
}
void PlayerConnection::handleServerSettingsChanged(shared_ptr<ServerSettingsChangedPacket> packet)
@@ -1014,7 +1066,7 @@ void PlayerConnection::handleServerSettingsChanged(shared_ptr<ServerSettingsChan
// Need to check that this player has permission to change each individual setting?
INetworkPlayer *networkPlayer = getNetworkPlayer();
- if( (networkPlayer != NULL && networkPlayer->IsHost()) || player->isModerator())
+ if( (networkPlayer != nullptr && networkPlayer->IsHost()) || player->isModerator())
{
app.SetGameHostOption(eGameHostOption_FireSpreads, app.GetGameHostOption(packet->data,eGameHostOption_FireSpreads));
app.SetGameHostOption(eGameHostOption_TNT, app.GetGameHostOption(packet->data,eGameHostOption_TNT));
@@ -1026,7 +1078,7 @@ void PlayerConnection::handleServerSettingsChanged(shared_ptr<ServerSettingsChan
app.SetGameHostOption(eGameHostOption_DoDaylightCycle, app.GetGameHostOption(packet->data, eGameHostOption_DoDaylightCycle));
app.SetGameHostOption(eGameHostOption_NaturalRegeneration, app.GetGameHostOption(packet->data, eGameHostOption_NaturalRegeneration));
- server->getPlayers()->broadcastAll( shared_ptr<ServerSettingsChangedPacket>( new ServerSettingsChangedPacket( ServerSettingsChangedPacket::HOST_IN_GAME_SETTINGS,app.GetGameHostOption(eGameHostOption_All) ) ) );
+ server->getPlayers()->broadcastAll(std::make_shared<ServerSettingsChangedPacket>(ServerSettingsChangedPacket::HOST_IN_GAME_SETTINGS, app.GetGameHostOption(eGameHostOption_All)));
// Update the QoS data
g_NetworkManager.UpdateAndSetGameSessionData();
@@ -1037,7 +1089,7 @@ void PlayerConnection::handleServerSettingsChanged(shared_ptr<ServerSettingsChan
void PlayerConnection::handleKickPlayer(shared_ptr<KickPlayerPacket> packet)
{
INetworkPlayer *networkPlayer = getNetworkPlayer();
- if( (networkPlayer != NULL && networkPlayer->IsHost()) || player->isModerator())
+ if( (networkPlayer != nullptr && networkPlayer->IsHost()) || player->isModerator())
{
server->getPlayers()->kickPlayerByShortId(packet->m_networkSmallId);
}
@@ -1106,9 +1158,9 @@ void PlayerConnection::handleContainerSetSlot(shared_ptr<ContainerSetSlotPacket>
if (packet->containerId == AbstractContainerMenu::CONTAINER_ID_INVENTORY && packet->slot >= 36 && packet->slot < 36 + 9)
{
shared_ptr<ItemInstance> lastItem = player->inventoryMenu->getSlot(packet->slot)->getItem();
- if (packet->item != NULL)
+ if (packet->item != nullptr)
{
- if (lastItem == NULL || lastItem->count < packet->item->count)
+ if (lastItem == nullptr || lastItem->count < packet->item->count)
{
packet->item->popTime = Inventory::POP_TIME_DURATION;
}
@@ -1141,7 +1193,7 @@ void PlayerConnection::handleContainerClick(shared_ptr<ContainerClickPacket> pac
if (ItemInstance::matches(packet->item, clicked))
{
// Yep, you sure did click what you claimed to click!
- player->connection->send( shared_ptr<ContainerAckPacket>( new ContainerAckPacket(packet->containerId, packet->uid, true) ) );
+ player->connection->send(std::make_shared<ContainerAckPacket>(packet->containerId, packet->uid, true));
player->ignoreSlotUpdateHack = true;
player->containerMenu->broadcastChanges();
player->broadcastCarriedItem();
@@ -1151,7 +1203,7 @@ void PlayerConnection::handleContainerClick(shared_ptr<ContainerClickPacket> pac
{
// No, you clicked the wrong thing!
expectedAcks[player->containerMenu->containerId] = packet->uid;
- player->connection->send( shared_ptr<ContainerAckPacket>( new ContainerAckPacket(packet->containerId, packet->uid, false) ) );
+ player->connection->send(std::make_shared<ContainerAckPacket>(packet->containerId, packet->uid, false));
player->containerMenu->setSynched(player, false);
vector<shared_ptr<ItemInstance> > items;
@@ -1184,13 +1236,13 @@ void PlayerConnection::handleSetCreativeModeSlot(shared_ptr<SetCreativeModeSlotP
bool drop = packet->slotNum < 0;
shared_ptr<ItemInstance> item = packet->item;
- if(item != NULL && item->id == Item::map_Id)
+ if(item != nullptr && item->id == Item::map_Id)
{
int mapScale = 3;
#ifdef _LARGE_WORLDS
int scale = MapItemSavedData::MAP_SIZE * 2 * (1 << mapScale);
- int centreXC = (int) (Math::round(player->x / scale) * scale);
- int centreZC = (int) (Math::round(player->z / scale) * scale);
+ int centreXC = static_cast<int>(Math::round(player->x / scale) * scale);
+ int centreZC = static_cast<int>(Math::round(player->z / scale) * scale);
#else
// 4J-PB - for Xbox maps, we'll centre them on the origin of the world, since we can fit the whole world in our map
int centreXC = 0;
@@ -1204,9 +1256,9 @@ void PlayerConnection::handleSetCreativeModeSlot(shared_ptr<SetCreativeModeSlotP
wchar_t buf[64];
swprintf(buf,64,L"map_%d", item->getAuxValue());
std::wstring id = wstring(buf);
- if( data == NULL )
+ if( data == nullptr )
{
- data = shared_ptr<MapItemSavedData>( new MapItemSavedData(id) );
+ data = std::make_shared<MapItemSavedData>(id);
}
player->level->setSavedData(id, (shared_ptr<SavedData> ) data);
@@ -1214,17 +1266,17 @@ void PlayerConnection::handleSetCreativeModeSlot(shared_ptr<SetCreativeModeSlotP
// 4J-PB - for Xbox maps, we'll centre them on the origin of the world, since we can fit the whole world in our map
data->x = centreXC;
data->z = centreZC;
- data->dimension = (byte) player->level->dimension->id;
+ data->dimension = static_cast<byte>(player->level->dimension->id);
data->setDirty();
}
bool validSlot = (packet->slotNum >= InventoryMenu::CRAFT_SLOT_START && packet->slotNum < (InventoryMenu::USE_ROW_SLOT_START + Inventory::getSelectionSize()));
- bool validItem = item == NULL || (item->id < Item::items.length && item->id >= 0 && Item::items[item->id] != NULL);
- bool validData = item == NULL || (item->getAuxValue() >= 0 && item->count > 0 && item->count <= 64);
+ bool validItem = item == nullptr || (item->id < Item::items.length && item->id >= 0 && Item::items[item->id] != nullptr);
+ bool validData = item == nullptr || (item->getAuxValue() >= 0 && item->count > 0 && item->count <= 64);
if (validSlot && validItem && validData)
{
- if (item == NULL)
+ if (item == nullptr)
{
player->inventoryMenu->setItem(packet->slotNum, nullptr);
}
@@ -1242,14 +1294,14 @@ void PlayerConnection::handleSetCreativeModeSlot(shared_ptr<SetCreativeModeSlotP
dropSpamTickCount += SharedConstants::TICKS_PER_SECOND;
// drop item
shared_ptr<ItemEntity> dropped = player->drop(item);
- if (dropped != NULL)
+ if (dropped != nullptr)
{
dropped->setShortLifeTime();
}
}
}
- if( item != NULL && item->id == Item::map_Id )
+ if( item != nullptr && item->id == Item::map_Id )
{
// 4J Stu - Maps need to have their aux value update, so the client should always be assumed to be wrong
// This is how the Java works, as the client also incorrectly predicts the auxvalue of the mapItem
@@ -1283,7 +1335,7 @@ void PlayerConnection::handleSignUpdate(shared_ptr<SignUpdatePacket> packet)
{
shared_ptr<TileEntity> te = level->getTileEntity(packet->x, packet->y, packet->z);
- if (dynamic_pointer_cast<SignTileEntity>(te) != NULL)
+ if (dynamic_pointer_cast<SignTileEntity>(te) != nullptr)
{
shared_ptr<SignTileEntity> ste = dynamic_pointer_cast<SignTileEntity>(te);
if (!ste->isEditable() || ste->getPlayerWhoMayEdit() != player)
@@ -1294,7 +1346,7 @@ void PlayerConnection::handleSignUpdate(shared_ptr<SignUpdatePacket> packet)
}
// 4J-JEV: Changed to allow characters to display as a [].
- if (dynamic_pointer_cast<SignTileEntity>(te) != NULL)
+ if (dynamic_pointer_cast<SignTileEntity>(te) != nullptr)
{
int x = packet->x;
int y = packet->y;
@@ -1317,7 +1369,7 @@ void PlayerConnection::handleKeepAlive(shared_ptr<KeepAlivePacket> packet)
{
if (packet->id == lastKeepAliveId)
{
- int time = (int) (System::nanoTime() / 1000000 - lastKeepAliveTime);
+ int time = static_cast<int>(System::nanoTime() / 1000000 - lastKeepAliveTime);
player->latency = (player->latency * 3 + time) / 4;
}
}
@@ -1327,20 +1379,20 @@ void PlayerConnection::handlePlayerInfo(shared_ptr<PlayerInfoPacket> packet)
// Need to check that this player has permission to change each individual setting?
INetworkPlayer *networkPlayer = getNetworkPlayer();
- if( (networkPlayer != NULL && networkPlayer->IsHost()) || player->isModerator() )
+ if( (networkPlayer != nullptr && networkPlayer->IsHost()) || player->isModerator() )
{
shared_ptr<ServerPlayer> serverPlayer;
// Find the player being edited
for(auto& checkingPlayer : server->getPlayers()->players)
{
- if(checkingPlayer->connection->getNetworkPlayer() != NULL && checkingPlayer->connection->getNetworkPlayer()->GetSmallId() == packet->m_networkSmallId)
+ if(checkingPlayer->connection->getNetworkPlayer() != nullptr && checkingPlayer->connection->getNetworkPlayer()->GetSmallId() == packet->m_networkSmallId)
{
serverPlayer = checkingPlayer;
break;
}
}
- if(serverPlayer != NULL)
+ if(serverPlayer != nullptr)
{
unsigned int origPrivs = serverPlayer->getAllPlayerGamePrivileges();
@@ -1357,7 +1409,7 @@ void PlayerConnection::handlePlayerInfo(shared_ptr<PlayerInfoPacket> packet)
#endif
serverPlayer->setPlayerGamePrivilege(Player::ePlayerGamePrivilege_CreativeMode,Player::getPlayerGamePrivilege(packet->m_playerPrivileges,Player::ePlayerGamePrivilege_CreativeMode) );
serverPlayer->gameMode->setGameModeForPlayer(gameType);
- serverPlayer->connection->send( shared_ptr<GameEventPacket>( new GameEventPacket(GameEventPacket::CHANGE_GAME_MODE, gameType->getId()) ));
+ serverPlayer->connection->send(std::make_shared<GameEventPacket>(GameEventPacket::CHANGE_GAME_MODE, gameType->getId()));
}
else
{
@@ -1409,7 +1461,7 @@ void PlayerConnection::handlePlayerInfo(shared_ptr<PlayerInfoPacket> packet)
}
}
- server->getPlayers()->broadcastAll( shared_ptr<PlayerInfoPacket>( new PlayerInfoPacket( serverPlayer ) ) );
+ server->getPlayers()->broadcastAll(std::make_shared<PlayerInfoPacket>(serverPlayer));
}
}
}
@@ -1493,7 +1545,7 @@ void PlayerConnection::handleCustomPayload(shared_ptr<CustomPayloadPacket> custo
AbstractContainerMenu *menu = player->containerMenu;
if (dynamic_cast<MerchantMenu *>(menu))
{
- ((MerchantMenu *) menu)->setSelectionHint(selection);
+ static_cast<MerchantMenu *>(menu)->setSelectionHint(selection);
}
}
else if (CustomPayloadPacket::SET_ADVENTURE_COMMAND_PACKET.compare(customPayloadPacket->identifier) == 0)
@@ -1514,7 +1566,7 @@ void PlayerConnection::handleCustomPayload(shared_ptr<CustomPayloadPacket> custo
shared_ptr<TileEntity> tileEntity = player->level->getTileEntity(x, y, z);
shared_ptr<CommandBlockEntity> cbe = dynamic_pointer_cast<CommandBlockEntity>(tileEntity);
- if (tileEntity != NULL && cbe != NULL)
+ if (tileEntity != nullptr && cbe != nullptr)
{
cbe->setCommand(command);
player->level->sendTileUpdated(x, y, z);
@@ -1528,14 +1580,14 @@ void PlayerConnection::handleCustomPayload(shared_ptr<CustomPayloadPacket> custo
}
else if (CustomPayloadPacket::SET_BEACON_PACKET.compare(customPayloadPacket->identifier) == 0)
{
- if ( dynamic_cast<BeaconMenu *>( player->containerMenu) != NULL)
+ if ( dynamic_cast<BeaconMenu *>( player->containerMenu) != nullptr)
{
ByteArrayInputStream bais(customPayloadPacket->data);
DataInputStream input(&bais);
int primary = input.readInt();
int secondary = input.readInt();
- BeaconMenu *beaconMenu = (BeaconMenu *) player->containerMenu;
+ BeaconMenu *beaconMenu = static_cast<BeaconMenu *>(player->containerMenu);
Slot *slot = beaconMenu->getSlot(0);
if (slot->hasItem())
{
@@ -1552,7 +1604,7 @@ void PlayerConnection::handleCustomPayload(shared_ptr<CustomPayloadPacket> custo
AnvilMenu *menu = dynamic_cast<AnvilMenu *>( player->containerMenu);
if (menu)
{
- if (customPayloadPacket->data.data == NULL || customPayloadPacket->data.length < 1)
+ if (customPayloadPacket->data.data == nullptr || customPayloadPacket->data.length < 1)
{
menu->setItemName(L"");
}
@@ -1590,13 +1642,13 @@ void PlayerConnection::handleCraftItem(shared_ptr<CraftItemPacket> packet)
if(iRecipe == -1)
return;
+ int recipeCount = (int)Recipes::getInstance()->getRecipies()->size();
+ if(iRecipe < 0 || iRecipe >= recipeCount)
+ return;
+
Recipy::INGREDIENTS_REQUIRED *pRecipeIngredientsRequired=Recipes::getInstance()->getRecipeIngredientsArray();
shared_ptr<ItemInstance> pTempItemInst=pRecipeIngredientsRequired[iRecipe].pRecipy->assemble(nullptr);
- size_t recipeCount = Recipes::getInstance()->getRecipies()->size();
- if (iRecipe < 0 || iRecipe >= (int)recipeCount)
- return;
-
if(app.DebugSettingsOn() && (player->GetDebugOptions()&(1L<<eDebugSetting_CraftAnything)))
{
pTempItemInst->onCraftedBy(player->level, dynamic_pointer_cast<Player>( player->shared_from_this() ), pTempItemInst->count );
@@ -1608,7 +1660,7 @@ void PlayerConnection::handleCraftItem(shared_ptr<CraftItemPacket> packet)
}
else if (pTempItemInst->id == Item::fireworksCharge_Id || pTempItemInst->id == Item::fireworks_Id)
{
- CraftingMenu *menu = (CraftingMenu *)player->containerMenu;
+ CraftingMenu *menu = static_cast<CraftingMenu *>(player->containerMenu);
player->openFireworks(menu->getX(), menu->getY(), menu->getZ() );
}
else
@@ -1616,7 +1668,7 @@ void PlayerConnection::handleCraftItem(shared_ptr<CraftItemPacket> packet)
Recipy::INGREDIENTS_REQUIRED &req = pRecipeIngredientsRequired[iRecipe];
- if (req.iType == RECIPE_TYPE_3x3 && dynamic_cast<CraftingMenu *>(player->containerMenu) == NULL)
+ if (req.iType == RECIPE_TYPE_3x3 && dynamic_cast<CraftingMenu *>(player->containerMenu) == nullptr)
{
server->warn(L"Player " + player->getName() + L" tried to craft a 3x3 recipe without a crafting bench");
return;
@@ -1651,12 +1703,12 @@ void PlayerConnection::handleCraftItem(shared_ptr<CraftItemPacket> packet)
}
// 4J Stu - Fix for #13097 - Bug: Milk Buckets are removed when crafting Cake
- if (ingItemInst != NULL)
+ if (ingItemInst != nullptr)
{
if (ingItemInst->getItem()->hasCraftingRemainingItem())
{
// replace item with remaining result
- player->inventory->add( shared_ptr<ItemInstance>( new ItemInstance(ingItemInst->getItem()->getCraftingRemainingItem()) ) );
+ player->inventory->add(std::make_shared<ItemInstance>(ingItemInst->getItem()->getCraftingRemainingItem()));
}
}
@@ -1717,7 +1769,7 @@ void PlayerConnection::handleTradeItem(shared_ptr<TradeItemPacket> packet)
{
if (player->containerMenu->containerId == packet->containerId)
{
- MerchantMenu *menu = (MerchantMenu *)player->containerMenu;
+ MerchantMenu *menu = static_cast<MerchantMenu *>(player->containerMenu);
MerchantRecipeList *offers = menu->getMerchant()->getOffers(player);
@@ -1735,7 +1787,7 @@ void PlayerConnection::handleTradeItem(shared_ptr<TradeItemPacket> packet)
int buyAMatches = player->inventory->countMatches(buyAItem);
int buyBMatches = player->inventory->countMatches(buyBItem);
- if( (buyAItem != NULL && buyAMatches >= buyAItem->count) && (buyBItem == NULL || buyBMatches >= buyBItem->count) )
+ if( (buyAItem != nullptr && buyAMatches >= buyAItem->count) && (buyBItem == nullptr || buyBMatches >= buyBItem->count) )
{
menu->getMerchant()->notifyTrade(activeRecipe);
@@ -1769,13 +1821,13 @@ void PlayerConnection::handleTradeItem(shared_ptr<TradeItemPacket> packet)
INetworkPlayer *PlayerConnection::getNetworkPlayer()
{
- if( connection != NULL && connection->getSocket() != NULL) return connection->getSocket()->getPlayer();
- else return NULL;
+ if( connection != nullptr && connection->getSocket() != nullptr) return connection->getSocket()->getPlayer();
+ else return nullptr;
}
bool PlayerConnection::isLocal()
{
- if( connection->getSocket() == NULL )
+ if( connection->getSocket() == nullptr )
{
return false;
}
@@ -1788,7 +1840,7 @@ bool PlayerConnection::isLocal()
bool PlayerConnection::isGuest()
{
- if( connection->getSocket() == NULL )
+ if( connection->getSocket() == nullptr )
{
return false;
}
@@ -1796,7 +1848,7 @@ bool PlayerConnection::isGuest()
{
INetworkPlayer *networkPlayer = connection->getSocket()->getPlayer();
bool isGuest = false;
- if(networkPlayer != NULL)
+ if(networkPlayer != nullptr)
{
isGuest = networkPlayer->IsGuest() == TRUE;
}