diff --git a/Minecraft.World/Network/Connection.cpp b/Minecraft.World/Network/Connection.cpp index 3529fadf1..eed63cd65 100644 --- a/Minecraft.World/Network/Connection.cpp +++ b/Minecraft.World/Network/Connection.cpp @@ -32,6 +32,9 @@ void Connection::_init() { fakeLag = 0; slowWriteDelay = 50; + saqThreadID = 0; + closeThreadID = 0; + tickCount = 0; } @@ -46,9 +49,11 @@ Connection::~Connection() { // waiting on a read readThread->WaitForCompletion(INFINITE); writeThread->WaitForCompletion(INFINITE); + DeleteCriticalSection(&writeLock); DeleteCriticalSection(&threadCounterLock); DeleteCriticalSection(&incoming_cs); + delete m_hWakeReadThread; delete m_hWakeWriteThread; @@ -132,7 +137,7 @@ Connection::Connection(Socket* socket, const std::wstring& id, writeThread->Run(); /* 4J JEV, java: - new Thread(std::wstring(id).append(L" read thread")) { + new Thread(wstring(id).append(L" read thread")) { }; @@ -174,8 +179,10 @@ void Connection::send(std::shared_ptr packet) { void Connection::queueSend(std::shared_ptr packet) { if (quitting) return; + EnterCriticalSection(&writeLock); estimatedRemaining += packet->getEstimatedSize() + 1; outgoing_slow.push(packet); + LeaveCriticalSection(&writeLock); } bool Connection::writeTick() { @@ -208,7 +215,17 @@ bool Connection::writeTick() { #ifndef _CONTENT_PACKAGE // 4J Added for debugging - if (!socket->isLocal()) Packet::recordOutgoingPacket(packet); + int playerId = 0; + if (!socket->isLocal()) { + Socket* socket = getSocket(); + if (socket) { + INetworkPlayer* player = socket->getPlayer(); + if (player) { + playerId = player->GetSmallId(); + } + } + Packet::recordOutgoingPacket(packet, playerId); + } #endif // 4J Stu - Changed this so that rather than writing to the network @@ -260,17 +277,32 @@ bool Connection::writeTick() { // "game" packets to QNet, rather than amalgamated chunks of data // that may include many packets, and partial packets b) To be able // to change the priority and queue of a packet if required +#ifdef _XBOX int flags = QNET_SENDDATA_LOW_PRIORITY | QNET_SENDDATA_SECONDARY; +#else + int flags = NON_QNET_SENDDATA_ACK_REQUIRED; +#endif sos->writeWithFlags(baos->buf, 0, baos->size(), flags); baos->reset(); } else { Packet::writePacket(packet, bufferedDos); } -#endif #ifndef _CONTENT_PACKAGE // 4J Added for debugging - if (!socket->isLocal()) Packet::recordOutgoingPacket(packet); + if (!socket->isLocal()) { + int playerId = 0; + if (!socket->isLocal()) { + Socket* socket = getSocket(); + if (socket) { + INetworkPlayer* player = socket->getPlayer(); + if (player) { + playerId = player->GetSmallId(); + } + } + Packet::recordOutgoingPacket(packet, playerId); + } + } #endif writeSizes[packet->getId()] += packet->getEstimatedSize() + 1; @@ -341,16 +373,29 @@ e.printStackTrace(); close("disconnect.genericReason", "Internal exception: " + e.toString()); }*/ -void Connection::close(DisconnectPacket::eDisconnectReason reason) { - fprintf(stderr, "[CONN] close called with reason=%d on connection=%p\n", - reason, (void*)this); +void Connection::close(DisconnectPacket::eDisconnectReason reason, ...) { + // printf("Con:0x%x close\n",this); if (!running) return; - fprintf(stderr, "[CONN] close proceeding (was running) on connection=%p\n", - (void*)this); + // printf("Con:0x%x close doing something\n",this); disconnected = true; - disconnectReason = reason; - disconnectReasonObjects = NULL; + va_list input; + va_start(input, reason); + + disconnectReason = reason; // va_arg( input, const wstring ); + + std::vector objs = std::vector(); + void* i = NULL; + while (i != NULL) { + i = va_arg(input, void*); + objs.push_back(i); + } + + if (objs.size()) { + disconnectReasonObjects = &objs[0]; + } else { + disconnectReasonObjects = NULL; + } // int count = 0, sum = 0, i = first; // va_list marker; @@ -365,7 +410,7 @@ void Connection::close(DisconnectPacket::eDisconnectReason reason) { // va_end( marker ); // return( sum ? (sum / count) : 0 ); - // CreateThread(NULL, 0, runClose, this, 0, NULL); + // CreateThread(NULL, 0, runClose, this, 0, &closeThreadID); running = false; @@ -510,7 +555,7 @@ void Connection::sendAndQuit() { close(DisconnectPacket::eDisconnect_Closed); } #else - CreateThread(NULL, 0, runSendAndQuit, this, 0, NULL); + CreateThread(NULL, 0, runSendAndQuit, this, 0, &saqThreadID); #endif } @@ -589,7 +634,7 @@ int Connection::runWrite(void* lpParam) { unsigned int waitResult = WAIT_TIMEOUT; while ( - (con->running || waitResult == 0) && + (con->running || waitResult == WAIT_OBJECT_0) && ShutdownManager::ShouldRun(ShutdownManager::eConnectionWriteThreads)) { while (con->writeTick()); diff --git a/Minecraft.World/Network/Connection.h b/Minecraft.World/Network/Connection.h index 068e7fe46..4c6bf7311 100644 --- a/Minecraft.World/Network/Connection.h +++ b/Minecraft.World/Network/Connection.h @@ -17,6 +17,11 @@ class ByteArrayOutputStream; class Connection { + friend DWORD WINAPI runRead(LPVOID lpParam); + friend DWORD WINAPI runWrite(LPVOID lpParam); + friend DWORD WINAPI runSendAndQuit(LPVOID lpParam); + friend DWORD WINAPI runClose(LPVOID lpParam); + private: static const int SEND_BUFFER_SIZE = 1024 * 5; @@ -67,6 +72,8 @@ private: C4JThread::Event* m_hWakeReadThread; C4JThread::Event* m_hWakeWriteThread; + DWORD saqThreadID, closeThreadID; + bool disconnected; DisconnectPacket::eDisconnectReason disconnectReason; void** disconnectReasonObjects; // 4J a pointer to an array. @@ -121,7 +128,7 @@ private: }*/ public: - void close(DisconnectPacket::eDisconnectReason reason); + void close(DisconnectPacket::eDisconnectReason reason, ...); void tick(); diff --git a/Minecraft.World/Network/Packets/AddEntityPacket.h b/Minecraft.World/Network/Packets/AddEntityPacket.h index abbc99c3b..afe9c2eaf 100644 --- a/Minecraft.World/Network/Packets/AddEntityPacket.h +++ b/Minecraft.World/Network/Packets/AddEntityPacket.h @@ -7,9 +7,7 @@ class AddEntityPacket : public Packet, public: static const int BOAT = 1; static const int ITEM = 2; - static const int MINECART_RIDEABLE = 10; - static const int MINECART_CHEST = 11; - static const int MINECART_FURNACE = 12; + static const int MINECART = 10; static const int PRIMED_TNT = 50; static const int ENDER_CRYSTAL = 51; static const int ARROW = 60; @@ -18,14 +16,15 @@ public: static const int FIREBALL = 63; static const int SMALL_FIREBALL = 64; static const int THROWN_ENDERPEARL = 65; - + static const int WITHER_SKULL = 66; static const int FALLING = 70; static const int ITEM_FRAME = 71; static const int EYEOFENDERSIGNAL = 72; static const int THROWN_POTION = 73; static const int FALLING_EGG = 74; static const int THROWN_EXPBOTTLE = 75; - + static const int FIREWORKS = 76; + static const int LEASH_KNOT = 77; static const int FISH_HOOK = 90; // 4J Added TU9 diff --git a/Minecraft.World/Network/Packets/AddGlobalEntityPacket.cpp b/Minecraft.World/Network/Packets/AddGlobalEntityPacket.cpp index 41eb3c0b0..724dbb1f9 100644 --- a/Minecraft.World/Network/Packets/AddGlobalEntityPacket.cpp +++ b/Minecraft.World/Network/Packets/AddGlobalEntityPacket.cpp @@ -22,10 +22,10 @@ AddGlobalEntityPacket::AddGlobalEntityPacket(std::shared_ptr e) { x = Mth::floor(e->x * 32); y = Mth::floor(e->y * 32); z = Mth::floor(e->z * 32); - if (std::dynamic_pointer_cast(e) != NULL) { - this->type = LIGHTNING; + if (e->instanceof(eTYPE_LIGHTNINGBOLT)) { + type = LIGHTNING; } else { - this->type = 0; + type = 0; } } diff --git a/Minecraft.World/Network/Packets/AddMobPacket.cpp b/Minecraft.World/Network/Packets/AddMobPacket.cpp index d70fafea9..cc407caca 100644 --- a/Minecraft.World/Network/Packets/AddMobPacket.cpp +++ b/Minecraft.World/Network/Packets/AddMobPacket.cpp @@ -19,8 +19,8 @@ AddMobPacket::AddMobPacket() { AddMobPacket::~AddMobPacket() { delete unpack; } -AddMobPacket::AddMobPacket(std::shared_ptr mob, int yRotp, int xRotp, - int xp, int yp, int zp, int yHeadRotp) { +AddMobPacket::AddMobPacket(std::shared_ptr mob, int yRotp, + int xRotp, int xp, int yp, int zp, int yHeadRotp) { id = mob->entityId; type = EntityIO::getId(mob); diff --git a/Minecraft.World/Network/Packets/AddMobPacket.h b/Minecraft.World/Network/Packets/AddMobPacket.h index 2d56acf08..adde6dcde 100644 --- a/Minecraft.World/Network/Packets/AddMobPacket.h +++ b/Minecraft.World/Network/Packets/AddMobPacket.h @@ -3,7 +3,7 @@ #include "Packet.h" #include "../../Entities/SyncedEntityData.h" -class Mob; +class LivingEntity; class AddMobPacket : public Packet, public std::enable_shared_from_this { @@ -21,8 +21,8 @@ private: public: AddMobPacket(); ~AddMobPacket(); - AddMobPacket(std::shared_ptr mob, int yRotp, int xRotp, int xp, int yp, - int zp, int yHeadRotp); + AddMobPacket(std::shared_ptr mob, int yRotp, int xRotp, + int xp, int yp, int zp, int yHeadRotp); virtual void read(DataInputStream* dis); virtual void write(DataOutputStream* dos); diff --git a/Minecraft.World/Network/Packets/AddPlayerPacket.cpp b/Minecraft.World/Network/Packets/AddPlayerPacket.cpp index e55a54382..0a83a7293 100644 --- a/Minecraft.World/Network/Packets/AddPlayerPacket.cpp +++ b/Minecraft.World/Network/Packets/AddPlayerPacket.cpp @@ -33,7 +33,7 @@ AddPlayerPacket::AddPlayerPacket(std::shared_ptr player, PlayerUID xuid, PlayerUID OnlineXuid, int xp, int yp, int zp, int yRotp, int xRotp, int yHeadRotp) { id = player->entityId; - name = player->name; + name = player->getName(); // 4J Stu - Send "previously sent" value of position as well so that we stay // in sync diff --git a/Minecraft.World/Network/Packets/BlockRegionUpdatePacket.cpp b/Minecraft.World/Network/Packets/BlockRegionUpdatePacket.cpp index 258bb410c..c6ca7f954 100644 --- a/Minecraft.World/Network/Packets/BlockRegionUpdatePacket.cpp +++ b/Minecraft.World/Network/Packets/BlockRegionUpdatePacket.cpp @@ -10,6 +10,11 @@ #include "../../Level/Storage/DataLayer.h" #include "../../Level/Dimensions/Dimension.h" +#define BLOCK_REGION_UPDATE_FULLCHUNK 0x01 +#define BLOCK_REGION_UPDATE_ZEROHEIGHT \ + 0x02 // added so we can still send a byte for ys, which really needs the + // range 0-256 + BlockRegionUpdatePacket::~BlockRegionUpdatePacket() { delete[] buffer.data; } BlockRegionUpdatePacket::BlockRegionUpdatePacket() { @@ -81,7 +86,7 @@ BlockRegionUpdatePacket::BlockRegionUpdatePacket(int x, int y, int z, int xs, void BlockRegionUpdatePacket::read(DataInputStream* dis) // throws IOException { - bIsFullChunk = dis->readBoolean(); + uint8_t chunkFlags = dis->readByte(); x = dis->readInt(); y = dis->readShort(); z = dis->readInt(); @@ -89,6 +94,9 @@ void BlockRegionUpdatePacket::read(DataInputStream* dis) // throws IOException ys = dis->read() + 1; zs = dis->read() + 1; + bIsFullChunk = (chunkFlags & BLOCK_REGION_UPDATE_FULLCHUNK) ? true : false; + if (chunkFlags & BLOCK_REGION_UPDATE_ZEROHEIGHT) ys = 0; + size = dis->readInt(); levelIdx = (size >> 30) & 3; size &= 0x3fffffff; @@ -124,7 +132,11 @@ void BlockRegionUpdatePacket::read(DataInputStream* dis) // throws IOException void BlockRegionUpdatePacket::write( DataOutputStream* dos) // throws IOException { - dos->writeBoolean(bIsFullChunk); + uint8_t chunkFlags = 0; + if (bIsFullChunk) chunkFlags |= BLOCK_REGION_UPDATE_FULLCHUNK; + if (ys == 0) chunkFlags |= BLOCK_REGION_UPDATE_ZEROHEIGHT; + + dos->writeByte(chunkFlags); dos->writeInt(x); dos->writeShort(y); dos->writeInt(z); diff --git a/Minecraft.World/Network/Packets/ChatPacket.cpp b/Minecraft.World/Network/Packets/ChatPacket.cpp index a19b42857..4f8ee4b0e 100644 --- a/Minecraft.World/Network/Packets/ChatPacket.cpp +++ b/Minecraft.World/Network/Packets/ChatPacket.cpp @@ -11,17 +11,33 @@ const unsigned int ChatPacket::MAX_LENGTH = ChatPacket::ChatPacket() { m_messageType = e_ChatCustom; } -// Old chat packet constructor, adds message, custom data and additional message -// to arg vectors ChatPacket::ChatPacket(const std::wstring& message, EChatPacketMessage type /*= e_ChatCustom*/, - int customData /*= -1*/, - const std::wstring& additionalMessage /*= L""*/) { + int customData /*= -1*/) { m_messageType = type; if (customData != -1) m_intArgs.push_back(customData); - if (message != L"" || additionalMessage != L"") - m_stringArgs.push_back(message); - if (additionalMessage != L"") m_stringArgs.push_back(additionalMessage); + + m_stringArgs.push_back(message); +} + +ChatPacket::ChatPacket(const std::wstring& message, EChatPacketMessage type, + int sourceEntityType, const std::wstring& sourceName) { + m_messageType = type; + if (sourceEntityType != -1) m_intArgs.push_back(sourceEntityType); + + m_stringArgs.push_back(message); + m_stringArgs.push_back(sourceName); +} + +ChatPacket::ChatPacket(const std::wstring& message, EChatPacketMessage type, + int sourceEntityType, const std::wstring& sourceName, + const std::wstring& itemName) { + m_messageType = type; + if (sourceEntityType != -1) m_intArgs.push_back(sourceEntityType); + + m_stringArgs.push_back(message); + m_stringArgs.push_back(sourceName); + m_stringArgs.push_back(itemName); } // Read chat packet (throws IOException) diff --git a/Minecraft.World/Network/Packets/ChatPacket.h b/Minecraft.World/Network/Packets/ChatPacket.h index 80885bbce..7a9d1f446 100644 --- a/Minecraft.World/Network/Packets/ChatPacket.h +++ b/Minecraft.World/Network/Packets/ChatPacket.h @@ -4,17 +4,17 @@ class ChatPacket : public Packet, public std::enable_shared_from_this { - // longest allowed string is "<" + name + "> " + message + // longest allowed std::string is "<" + name + "> " + message private: static const unsigned int MAX_LENGTH; public: // 4J - We want to be able to localise the messages sent. The enum also // allows for the posibility that there may be different versions playing - // the game, so the enum should map to a string id which may be different on - // different versions + // the game, so the enum should map to a std::string id which may be + // different on different versions enum EChatPacketMessage { - e_ChatCustom = 0, // No localised string, only the text passed in + e_ChatCustom = 0, // No localised std::string, only the text passed in e_ChatBedOccupied, e_ChatBedNoSleep, e_ChatBedNotValid, @@ -45,11 +45,32 @@ public: e_ChatDeathThrown, e_ChatDeathIndirectMagic, e_ChatDeathDragonBreath, - e_ChatDeathWither, e_ChatDeathAnvil, e_ChatDeathFallingBlock, e_ChatDeathThorns, + e_ChatDeathFellAccidentLadder, + e_ChatDeathFellAccidentVines, + e_ChatDeathFellAccidentWater, + e_ChatDeathFellAccidentGeneric, + e_ChatDeathFellKiller, + e_ChatDeathFellAssist, + e_ChatDeathFellAssistItem, + e_ChatDeathFellFinish, + e_ChatDeathFellFinishItem, + e_ChatDeathInFirePlayer, + e_ChatDeathOnFirePlayer, + e_ChatDeathLavaPlayer, + e_ChatDeathDrownPlayer, + e_ChatDeathCactusPlayer, + e_ChatDeathExplosionPlayer, + e_ChatDeathWither, + e_ChatDeathPlayerItem, + e_ChatDeathArrowItem, + e_ChatDeathFireballItem, + e_ChatDeathThrownItem, + e_ChatDeathIndirectMagicItem, + e_ChatPlayerEnteredEnd, e_ChatPlayerLeftEnd, @@ -89,6 +110,7 @@ public: e_ChatPlayerCantShearMooshroom, // Tell the player they can't shear // because the limits have been reached e_ChatPlayerMaxBoats, + e_ChatPlayerMaxBats, e_ChatCommandTeleportSuccess, e_ChatCommandTeleportMe, @@ -102,9 +124,17 @@ public: EChatPacketMessage m_messageType; ChatPacket(); + + // 4J: Seperated the one convoluted ctor into three more readable ctors. The + // last two ctors are only used for death messages and I'd really like to + // consolodate them and/or the logic that uses them at some point. ChatPacket(const std::wstring& message, - EChatPacketMessage type = e_ChatCustom, int customData = -1, - const std::wstring& additionalMessage = L""); + EChatPacketMessage type = e_ChatCustom, int customData = -1); + ChatPacket(const std::wstring& message, EChatPacketMessage type, + int sourceEntityType, const std::wstring& sourceName); + ChatPacket(const std::wstring& message, EChatPacketMessage type, + int sourceEntityType, const std::wstring& sourceName, + const std::wstring& itemName); virtual void read(DataInputStream* dis); virtual void write(DataOutputStream* dos); diff --git a/Minecraft.World/Network/Packets/ClientInformationPacket.h b/Minecraft.World/Network/Packets/ClientInformationPacket.h index 3701384ee..01714a846 100644 --- a/Minecraft.World/Network/Packets/ClientInformationPacket.h +++ b/Minecraft.World/Network/Packets/ClientInformationPacket.h @@ -13,12 +13,13 @@ class ClientInformationPacket : public Packet { public ClientInformationPacket() { } - public ClientInformationPacket(String language, int viewDistance, int chatVisibility, boolean chatColors, int difficulty) { + public ClientInformationPacket(String language, int viewDistance, int chatVisibility, boolean chatColors, int difficulty, boolean showCape) { this.language = language; this.viewDistance = viewDistance; this.chatVisibility = chatVisibility; this.chatColors = chatColors; this.difficulty = difficulty; + this.showCape = showCape; } @Override @@ -31,6 +32,7 @@ class ClientInformationPacket : public Packet { chatColors = (chat & 0x8) == 0x8; difficulty = dis.readByte(); + showCape = dis.readBoolean(); } @Override @@ -39,6 +41,7 @@ class ClientInformationPacket : public Packet { dos.writeByte(viewDistance); dos.writeByte(chatVisibility | (chatColors ? 1 : 0) << 3); dos.writeByte(difficulty); + dos.writeBoolean(showCape); } @Override @@ -48,7 +51,7 @@ class ClientInformationPacket : public Packet { @Override public int getEstimatedSize() { - return 0; + return 7; } public String getLanguage() { @@ -71,6 +74,10 @@ class ClientInformationPacket : public Packet { return difficulty; } + public boolean getShowCape() { + return showCape; + } + public void setDifficulty(int difficulty) { this.difficulty = difficulty; } diff --git a/Minecraft.World/Network/Packets/ComplexItemDataPacket.cpp b/Minecraft.World/Network/Packets/ComplexItemDataPacket.cpp index a5b108de2..a6767cc65 100644 --- a/Minecraft.World/Network/Packets/ComplexItemDataPacket.cpp +++ b/Minecraft.World/Network/Packets/ComplexItemDataPacket.cpp @@ -27,7 +27,7 @@ void ComplexItemDataPacket::read(DataInputStream* dis) // throws IOException itemType = dis->readShort(); itemId = dis->readShort(); - data = charArray(dis->readShort() & 0xffff); + data = charArray(dis->readUnsignedShort() & 0xffff); dis->readFully(data); } @@ -35,7 +35,7 @@ void ComplexItemDataPacket::write(DataOutputStream* dos) // throws IOException { dos->writeShort(itemType); dos->writeShort(itemId); - dos->writeShort(data.length); + dos->writeUnsignedShort(data.length); byteArray ba((uint8_t*)data.data, data.length); dos->write(ba); diff --git a/Minecraft.World/Network/Packets/ContainerClickPacket.cpp b/Minecraft.World/Network/Packets/ContainerClickPacket.cpp index ef55b0818..0c853d50f 100644 --- a/Minecraft.World/Network/Packets/ContainerClickPacket.cpp +++ b/Minecraft.World/Network/Packets/ContainerClickPacket.cpp @@ -13,18 +13,18 @@ ContainerClickPacket::ContainerClickPacket() { buttonNum = 0; uid = 0; item = nullptr; - quickKey = false; + clickType = 0; } ContainerClickPacket::ContainerClickPacket(int containerId, int slotNum, - int buttonNum, bool quickKey, + int buttonNum, int clickType, std::shared_ptr item, short uid) { this->containerId = containerId; this->slotNum = slotNum; this->buttonNum = buttonNum; this->uid = uid; - this->quickKey = quickKey; + this->clickType = clickType; // 4J - make a copy of the relevant bits of this item, as we want our // packets to have full ownership of any data they reference this->item = item ? item->copy() : nullptr; @@ -36,11 +36,11 @@ void ContainerClickPacket::handle(PacketListener* listener) { void ContainerClickPacket::read(DataInputStream* dis) // throws IOException { - containerId = (int)dis->readByte(); + containerId = dis->readByte(); slotNum = dis->readShort(); - buttonNum = (int)dis->readByte(); + buttonNum = dis->readByte(); uid = dis->readShort(); - quickKey = dis->readBoolean(); + clickType = dis->readByte(); item = readItem(dis); } diff --git a/Minecraft.World/Network/Packets/ContainerClickPacket.h b/Minecraft.World/Network/Packets/ContainerClickPacket.h index 29ac23c11..a36c95a3d 100644 --- a/Minecraft.World/Network/Packets/ContainerClickPacket.h +++ b/Minecraft.World/Network/Packets/ContainerClickPacket.h @@ -11,12 +11,12 @@ public: int buttonNum; short uid; std::shared_ptr item; - bool quickKey; + int clickType; ContainerClickPacket(); ~ContainerClickPacket(); ContainerClickPacket(int containerId, int slotNum, int buttonNum, - bool quickKey, std::shared_ptr item, + int clickType, std::shared_ptr item, short uid); virtual void handle(PacketListener* listener); diff --git a/Minecraft.World/Network/Packets/ContainerOpenPacket.cpp b/Minecraft.World/Network/Packets/ContainerOpenPacket.cpp index 1395b5b58..c70949f71 100644 --- a/Minecraft.World/Network/Packets/ContainerOpenPacket.cpp +++ b/Minecraft.World/Network/Packets/ContainerOpenPacket.cpp @@ -4,19 +4,29 @@ #include "PacketListener.h" #include "ContainerOpenPacket.h" -ContainerOpenPacket::ContainerOpenPacket() { - containerId = 0; - type = 0; - title = 0; - size = 0; -} - -ContainerOpenPacket::ContainerOpenPacket(int containerId, int type, int title, - int size) { +void ContainerOpenPacket::_init(int containerId, int type, + const std::wstring& title, int size, + bool customName, int entityId) { this->containerId = containerId; this->type = type; this->title = title; this->size = size; + this->customName = customName; + this->entityId = entityId; +} + +ContainerOpenPacket::ContainerOpenPacket() { _init(0, 0, L"", 0, false, 0); } + +ContainerOpenPacket::ContainerOpenPacket(int containerId, int type, + const std::wstring& title, int size, + bool customName) { + _init(containerId, type, title, size, customName, 0); +} + +ContainerOpenPacket::ContainerOpenPacket(int containerId, int type, + const std::wstring& title, int size, + bool customName, int entityId) { + _init(containerId, type, title, size, customName, entityId); } void ContainerOpenPacket::handle(PacketListener* listener) { @@ -25,18 +35,35 @@ void ContainerOpenPacket::handle(PacketListener* listener) { void ContainerOpenPacket::read(DataInputStream* dis) // throws IOException { - containerId = (int)(dis->readByte() & (uint8_t)0xff); - type = (int)(dis->readByte() & (uint8_t)0xff); - title = dis->readShort(); - size = (int)(dis->readByte() & (uint8_t)0xff); + containerId = dis->readByte() & 0xff; + type = dis->readByte() & 0xff; + size = dis->readByte() & 0xff; + customName = dis->readBoolean(); + if (type == HORSE) { + entityId = dis->readInt(); + } + if (customName) { + title = readUtf(dis, 64); + } } void ContainerOpenPacket::write(DataOutputStream* dos) // throws IOException { - dos->writeByte((uint8_t)containerId & (uint8_t)0xff); - dos->writeByte((uint8_t)type & (uint8_t)0xff); - dos->writeShort(title & 0xffff); - dos->writeByte((uint8_t)size & (uint8_t)0xff); + dos->writeByte(containerId & 0xff); + dos->writeByte(type & 0xff); + dos->writeByte(size & 0xff); + dos->writeBoolean(customName); + if (type == HORSE) { + dos->writeInt(entityId); + } + if (customName) { + writeUtf(title, dos); + } } -int ContainerOpenPacket::getEstimatedSize() { return 5; } +int ContainerOpenPacket::getEstimatedSize() { + if (type == HORSE) { + return 10; + } + return 6; +} diff --git a/Minecraft.World/Network/Packets/ContainerOpenPacket.h b/Minecraft.World/Network/Packets/ContainerOpenPacket.h index 772b4f1a2..ab0329351 100644 --- a/Minecraft.World/Network/Packets/ContainerOpenPacket.h +++ b/Minecraft.World/Network/Packets/ContainerOpenPacket.h @@ -15,14 +15,33 @@ public: static const int TRADER_NPC = 6; static const int BEACON = 7; static const int REPAIR_TABLE = 8; + static const int HOPPER = 9; + static const int DROPPER = 10; + static const int HORSE = 11; + static const int FIREWORKS = 12; // 4J Added + static const int BONUS_CHEST = 13; // 4J Added + static const int LARGE_CHEST = 14; // 4J Added + static const int ENDER_CHEST = 15; // 4J Added + static const int MINECART_CHEST = 16; // 4J Added + static const int MINECART_HOPPER = 17; // 4J Added int containerId; int type; - int title; // 4J Stu - Changed from string int size; + bool customName; + std::wstring title; + int entityId; +private: + void _init(int containerId, int type, const std::wstring& title, int size, + bool customName, int entityId); + +public: ContainerOpenPacket(); - ContainerOpenPacket(int containerId, int type, int title, int size); + ContainerOpenPacket(int containerId, int type, const std::wstring& title, + int size, bool customName); + ContainerOpenPacket(int containerId, int type, const std::wstring& title, + int size, bool customName, int entityId); virtual void handle(PacketListener* listener); virtual void read(DataInputStream* dis); diff --git a/Minecraft.World/Network/Packets/CustomPayloadPacket.cpp b/Minecraft.World/Network/Packets/CustomPayloadPacket.cpp index 21e2baa65..bbc1d275c 100644 --- a/Minecraft.World/Network/Packets/CustomPayloadPacket.cpp +++ b/Minecraft.World/Network/Packets/CustomPayloadPacket.cpp @@ -63,4 +63,4 @@ void CustomPayloadPacket::handle(PacketListener* listener) { int CustomPayloadPacket::getEstimatedSize() { return 2 + identifier.length() * 2 + 2 + length; -} +} \ No newline at end of file diff --git a/Minecraft.World/Network/Packets/GameCommandPacket.cpp b/Minecraft.World/Network/Packets/GameCommandPacket.cpp index be93ef5c8..6cd44bdee 100644 --- a/Minecraft.World/Network/Packets/GameCommandPacket.cpp +++ b/Minecraft.World/Network/Packets/GameCommandPacket.cpp @@ -52,4 +52,4 @@ void GameCommandPacket::handle(PacketListener* listener) { listener->handleGameCommand(shared_from_this()); } -int GameCommandPacket::getEstimatedSize() { return 2 + 2 + length; } +int GameCommandPacket::getEstimatedSize() { return 2 + 2 + length; } \ No newline at end of file diff --git a/Minecraft.World/Network/Packets/LevelEventPacket.h b/Minecraft.World/Network/Packets/LevelEventPacket.h index ef9ea470c..58298c990 100644 --- a/Minecraft.World/Network/Packets/LevelEventPacket.h +++ b/Minecraft.World/Network/Packets/LevelEventPacket.h @@ -8,14 +8,16 @@ public: int type; int data; int x, y, z; + bool globalEvent; LevelEventPacket(); - LevelEventPacket(int type, int x, int y, int z, int data); + LevelEventPacket(int type, int x, int y, int z, int data, bool globalEvent); virtual void read(DataInputStream* dis); virtual void write(DataOutputStream* dos); virtual void handle(PacketListener* listener); virtual int getEstimatedSize(); + bool isGlobalEvent(); public: static std::shared_ptr create() { diff --git a/Minecraft.World/Network/Packets/LevelParticlesPacket.cpp b/Minecraft.World/Network/Packets/LevelParticlesPacket.cpp new file mode 100644 index 000000000..a844f3a02 --- /dev/null +++ b/Minecraft.World/Network/Packets/LevelParticlesPacket.cpp @@ -0,0 +1,78 @@ +#include "../../Platform/stdafx.h" +#include "PacketListener.h" +#include "LevelParticlesPacket.h" + +LevelParticlesPacket::LevelParticlesPacket() { + this->name = L""; + this->x = 0.0f; + this->y = 0.0f; + this->z = 0.0f; + this->xDist = 0.0f; + this->yDist = 0.0f; + this->zDist = 0.0f; + this->maxSpeed = 0.0f; + this->count = 0; +} + +LevelParticlesPacket::LevelParticlesPacket(const std::wstring& name, float x, + float y, float z, float xDist, + float yDist, float zDist, + float maxSpeed, int count) { + this->name = name; + this->x = x; + this->y = y; + this->z = z; + this->xDist = xDist; + this->yDist = yDist; + this->zDist = zDist; + this->maxSpeed = maxSpeed; + this->count = count; +} + +void LevelParticlesPacket::read(DataInputStream* dis) { + name = readUtf(dis, 64); + x = dis->readFloat(); + y = dis->readFloat(); + z = dis->readFloat(); + xDist = dis->readFloat(); + yDist = dis->readFloat(); + zDist = dis->readFloat(); + maxSpeed = dis->readFloat(); + count = dis->readInt(); +} + +void LevelParticlesPacket::write(DataOutputStream* dos) { + writeUtf(name, dos); + dos->writeFloat(x); + dos->writeFloat(y); + dos->writeFloat(z); + dos->writeFloat(xDist); + dos->writeFloat(yDist); + dos->writeFloat(zDist); + dos->writeFloat(maxSpeed); + dos->writeInt(count); +} + +std::wstring LevelParticlesPacket::getName() { return name; } + +double LevelParticlesPacket::getX() { return x; } + +double LevelParticlesPacket::getY() { return y; } + +double LevelParticlesPacket::getZ() { return z; } + +float LevelParticlesPacket::getXDist() { return xDist; } + +float LevelParticlesPacket::getYDist() { return yDist; } + +float LevelParticlesPacket::getZDist() { return zDist; } + +float LevelParticlesPacket::getMaxSpeed() { return maxSpeed; } + +int LevelParticlesPacket::getCount() { return count; } + +void LevelParticlesPacket::handle(PacketListener* listener) { + listener->handleParticleEvent(shared_from_this()); +} + +int LevelParticlesPacket::getEstimatedSize() { return 4 * 2 + 7 * 8; } \ No newline at end of file diff --git a/Minecraft.World/Network/Packets/LevelParticlesPacket.h b/Minecraft.World/Network/Packets/LevelParticlesPacket.h new file mode 100644 index 000000000..fd07783bd --- /dev/null +++ b/Minecraft.World/Network/Packets/LevelParticlesPacket.h @@ -0,0 +1,44 @@ +#pragma once + +#include "Packet.h" + +class LevelParticlesPacket + : public Packet, + public std::enable_shared_from_this { +private: + std::wstring name; + float x; + float y; + float z; + float xDist; + float yDist; + float zDist; + float maxSpeed; + int count; + +public: + LevelParticlesPacket(); + LevelParticlesPacket(const std::wstring& name, float x, float y, float z, + float xDist, float yDist, float zDist, float maxSpeed, + int count); + + void read(DataInputStream* dis); + void write(DataOutputStream* dos); + std::wstring getName(); + double getX(); + double getY(); + double getZ(); + float getXDist(); + float getYDist(); + float getZDist(); + float getMaxSpeed(); + int getCount(); + void handle(PacketListener* listener); + int getEstimatedSize(); + +public: + static std::shared_ptr create() { + return std::shared_ptr(new LevelParticlesPacket()); + } + virtual int getId() { return 63; } +}; \ No newline at end of file diff --git a/Minecraft.World/Network/Packets/Packet.cpp b/Minecraft.World/Network/Packets/Packet.cpp index 9db3b3676..d6e540a99 100644 --- a/Minecraft.World/Network/Packets/Packet.cpp +++ b/Minecraft.World/Network/Packets/Packet.cpp @@ -52,7 +52,7 @@ void Packet::staticCtor() { PlayerActionPacket::create); map(15, false, true, false, false, typeid(UseItemPacket), UseItemPacket::create); - map(16, false, true, false, false, typeid(SetCarriedItemPacket), + map(16, true, true, true, false, typeid(SetCarriedItemPacket), SetCarriedItemPacket::create); // 4J-PB - we need to send to any client for the sleep in bed // map(17, true, false, false, false, EntityActionAtPositionPacket)); @@ -78,9 +78,10 @@ void Packet::staticCtor() { map(26, true, false, false, false, typeid(AddExperienceOrbPacket), AddExperienceOrbPacket::create); // TODO New for 1.8.2 - Needs // sendToAny? - // map(27, false, true, false, false, PlayerInputPacket)); - // 4J-PB - needs to go to any player, due to the knockback effect when a - // played is hit + map(27, false, true, false, false, typeid(PlayerInputPacket), + PlayerInputPacket::create); + // 4J-PB - needs to go to any player, due to the knockback effect when a + // played is hit map(28, true, false, true, true, typeid(SetEntityMotionPacket), SetEntityMotionPacket::create); map(29, true, false, false, true, typeid(RemoveEntitiesPacket), @@ -103,8 +104,8 @@ void Packet::staticCtor() { // hit map(38, true, false, true, true, typeid(EntityEventPacket), EntityEventPacket::create); - map(39, true, false, true, false, typeid(SetRidingPacket), - SetRidingPacket::create); + map(39, true, false, true, false, typeid(SetEntityLinkPacket), + SetEntityLinkPacket::create); map(40, true, false, true, true, typeid(SetEntityDataPacket), SetEntityDataPacket::create); map(41, true, false, true, false, typeid(UpdateMobEffectPacket), @@ -113,6 +114,8 @@ void Packet::staticCtor() { RemoveMobEffectPacket::create); map(43, true, false, true, false, typeid(SetExperiencePacket), SetExperiencePacket::create); + map(44, true, false, true, false, typeid(UpdateAttributesPacket), + UpdateAttributesPacket::create); map(50, true, false, true, true, typeid(ChunkVisibilityPacket), ChunkVisibilityPacket::create); @@ -135,8 +138,8 @@ void Packet::staticCtor() { // 4J-PB - don't see the need for this, we can use 61 map(62, true, false, true, false, typeid(LevelSoundPacket), LevelSoundPacket::create); - // map(62, true, false, true, false, typeid(LevelSoundPacket), - // LevelSoundPacket::create); + map(63, true, false, true, false, typeid(LevelParticlesPacket), + LevelParticlesPacket::create); map(70, true, false, false, false, typeid(GameEventPacket), GameEventPacket::create); @@ -176,6 +179,8 @@ void Packet::staticCtor() { ComplexItemDataPacket::create); map(132, true, false, false, false, typeid(TileEntityDataPacket), TileEntityDataPacket::create); + map(133, true, false, true, false, typeid(TileEditorOpenPacket), + TileEditorOpenPacket::create); // 4J Added map(150, false, true, false, false, typeid(CraftItemPacket), @@ -226,6 +231,16 @@ void Packet::staticCtor() { // map(204, false, true, true, false, ClientInformationPacket.class); map(205, false, true, true, false, typeid(ClientCommandPacket), ClientCommandPacket::create); + + map(206, true, false, true, false, typeid(SetObjectivePacket), + SetObjectivePacket::create); + map(207, true, false, true, false, typeid(SetScorePacket), + SetScorePacket::create); + map(208, true, false, true, false, typeid(SetDisplayObjectivePacket), + SetDisplayObjectivePacket::create); + map(209, true, false, true, false, typeid(SetPlayerTeamPacket), + SetPlayerTeamPacket::create); + map(250, true, true, true, false, typeid(CustomPayloadPacket), CustomPayloadPacket::create); // 4J Stu - These added 1.3.2, but don't think we need them @@ -246,8 +261,6 @@ IOException::IOException(const std::wstring& information) { this->information = information; } -RuntimeException::RuntimeException(const std::wstring& /*information*/) {} - Packet::Packet() : createTime(System::currentTimeMillis()) { shouldDelay = false; } @@ -304,15 +317,23 @@ void Packet::map(int id, bool receiveOnClient, bool receiveOnServer, } // 4J Added to record data for outgoing packets -void Packet::recordOutgoingPacket(std::shared_ptr packet) { +void Packet::recordOutgoingPacket(std::shared_ptr packet, + int playerIndex) { #ifndef _CONTENT_PACKAGE #if PACKET_ENABLE_STAT_TRACKING - AUTO_VAR(it, outgoingStatistics.find(packet->getId())); +#if 0 + int idx = packet->getId(); +#else + int idx = playerIndex; + if (packet->getId() != 51) { + idx = 100; + } +#endif + AUTO_VAR(it, outgoingStatistics.find(idx)); if (it == outgoingStatistics.end()) { - Packet::PacketStatistics* packetStatistics = - new PacketStatistics(packet->getId()); - outgoingStatistics[packet->getId()] = packetStatistics; + Packet::PacketStatistics* packetStatistics = new PacketStatistics(idx); + outgoingStatistics[idx] = packetStatistics; packetStatistics->addPacket(packet->getEstimatedSize()); } else { it->second->addPacket(packet->getEstimatedSize()); @@ -321,74 +342,29 @@ void Packet::recordOutgoingPacket(std::shared_ptr packet) { #endif } -void Packet::renderPacketStats(int id) { - AUTO_VAR(it, outgoingStatistics.find(id)); - - if (it != outgoingStatistics.end()) { - it->second->renderStats(); - } -} - -void Packet::renderAllPacketStats() { +void Packet::updatePacketStatsPIX() { #ifndef _CONTENT_PACKAGE #if PACKET_ENABLE_STAT_TRACKING - Minecraft* pMinecraft = Minecraft::GetInstance(); - pMinecraft->gui->renderStackedGraph(Packet::renderPos, 512, - renderableStats.size(), - &Packet::getIndexedStatValue); - renderAllPacketStatsKey(); - - Packet::renderPos++; - Packet::renderPos %= 511; -#endif -#endif -} - -void Packet::renderAllPacketStatsKey() { -#ifndef _CONTENT_PACKAGE -#if PACKET_ENABLE_STAT_TRACKING - Minecraft* pMinecraft = Minecraft::GetInstance(); - int total = Packet::renderableStats.size(); - for (unsigned int i = 0; i < total; ++i) { - Packet::PacketStatistics* stat = Packet::renderableStats[i]; - float vary = (float)i / total; - int fColour = floor(vary * 0xffffff); - - int colour = 0xff000000 + fColour; - pMinecraft->gui->drawString(pMinecraft->font, stat->getLegendString(), - 900, 30 + (10 * i), colour); + for (AUTO_VAR(it, outgoingStatistics.begin()); + it != outgoingStatistics.end(); it++) { + Packet::PacketStatistics* stat = it->second; + int64_t count = stat->getRunningCount(); + wchar_t pixName[256]; + swprintf_s(pixName, L"Packet count %d", stat->id); + // PIXReportCounter(pixName,(float)count); + int64_t total = stat->getRunningTotal(); + swprintf_s(pixName, L"Packet bytes %d", stat->id); + PIXReportCounter(pixName, (float)total); + stat->IncrementPos(); } #endif #endif } -int64_t Packet::getIndexedStatValue(unsigned int samplePos, - unsigned int renderableId) { - int64_t val = 0; - -#ifndef _CONTENT_PACKAGE -#if PACKET_ENABLE_STAT_TRACKING - val = renderableStats[renderableId]->getCountSample(samplePos); -#endif -#endif - - return val; -} - std::shared_ptr Packet::getPacket(int id) { - // 4J - removed try/catch - // try - // { + // 4J: Removed try/catch return idToCreateMap[id](); - // } - // catch (exception e) - // { - // // TODO 4J JEV print stack trace, newInstance doesnt throw an - //exception in c++ yet. - // printf("Skipping packet with id %d" , id); - // return NULL; - // } } void Packet::writeBytes(DataOutputStream* dataoutputstream, byteArray bytes) { @@ -408,7 +384,7 @@ byteArray Packet::readBytes(DataInputStream* datainputstream) { } byteArray bytes(size); - datainputstream->read(bytes); + datainputstream->readFully(bytes); return bytes; } @@ -436,14 +412,6 @@ std::shared_ptr Packet::readPacket( DataInputStream* dis, bool isServer) // throws IOException TODO 4J JEV, // should this declare a throws? { - // N packet ID - static int s_clientPktHistory[64]; - static int s_clientPktIdx = 0; - static int s_serverPktHistory[64]; - static int s_serverPktIdx = 0; - static bool s_clientDesyncLogged = false; - static bool s_serverDesyncLogged = false; - int id = 0; std::shared_ptr packet = nullptr; @@ -457,47 +425,20 @@ std::shared_ptr Packet::readPacket( serverReceivedPackets.find(id) == serverReceivedPackets.end()) || (!isServer && clientReceivedPackets.find(id) == clientReceivedPackets.end())) { - int* history = isServer ? s_serverPktHistory : s_clientPktHistory; - int idx = isServer ? s_serverPktIdx : s_clientPktIdx; - bool& logged = isServer ? s_serverDesyncLogged : s_clientDesyncLogged; - - fprintf(stderr, "[PKT] Bad packet id %d (0x%x) isServer=%d\n", id, id, - isServer); - if (!logged) { - logged = true; - fprintf(stderr, - "[PKT] === PACKET HISTORY (last %d, newest last) ===\n", - 64); - for (int i = 0; i < 64; i++) { - int h = history[(idx + i) % 64]; - if (h != 0) fprintf(stderr, "[PKT] pkt %d (0x%x)\n", h, h); - } - fprintf(stderr, "[PKT] === END HISTORY ===\n"); - } + // app.DebugPrintf("Bad packet id %d\n", id); __debugbreak(); - // assert(false); - // Close the stream to prevent further reads on a desynced stream - dis->close(); - return nullptr; - // throw new IOException(std::wstring(L"Bad packet id ") + + assert(false); + // throw new IOException(wstring(L"Bad packet id ") + // _toString(id)); } - // Record successfully read packet ID - if (isServer) { - s_serverPktHistory[s_serverPktIdx % 64] = id; - s_serverPktIdx++; - } else { - s_clientPktHistory[s_clientPktIdx % 64] = id; - s_clientPktIdx++; - } - packet = getPacket(id); - if (packet == NULL) { - fprintf(stderr, "[PKT] getPacket(%d) returned NULL\n", id); - return nullptr; - } + if (packet == NULL) + assert(false); // throw new IOException(wstring(L"Bad packet id ") + + // _toString(id)); + // app.DebugPrintf("%s reading packet %d\n", isServer ? "Server" : "Client", + // packet->getId()); packet->read(dis); // } // catch (EOFException e) @@ -558,16 +499,16 @@ std::wstring Packet::readUtf(DataInputStream* dis, { short stringLength = dis->readShort(); if (stringLength > maxLength) { - fprintf(stderr, - "[PKT] readUtf: string length %d > max %d (stream desync?)\n", - stringLength, maxLength); - return L""; + wstringstream stream; + stream << L"Received string length longer than maximum allowed (" + << stringLength << " > " << maxLength << ")"; + assert(false); + // throw new IOException( stream.str() ); } if (stringLength < 0) { - fprintf(stderr, - "[PKT] readUtf: negative string length %d (stream desync?)\n", - stringLength); - return L""; + assert(false); + // throw new IOException(L"Received string length is less than + // zero! Weird string!"); } std::wstring builder = L""; @@ -579,16 +520,18 @@ std::wstring Packet::readUtf(DataInputStream* dis, return builder; } -void Packet::PacketStatistics::addPacket(int bytes) { - if (count == 0) { - firstSampleTime = System::currentTimeMillis(); - } - count++; - totalSize += bytes; +Packet::PacketStatistics::PacketStatistics(int id) + : id(id), count(0), totalSize(0), samplesPos(0) { + memset(countSamples, 0, sizeof(countSamples)); + memset(sizeSamples, 0, sizeof(sizeSamples)); +} - // 4J Added - countSamples[samplesPos & (512 - 1)]++; - sizeSamples[samplesPos & (512 - 1)] += (unsigned int)bytes; +void Packet::PacketStatistics::addPacket(int bytes) { + countSamples[samplesPos]++; + sizeSamples[samplesPos] += bytes; + timeSamples[samplesPos] = System::currentTimeMillis(); + totalSize += bytes; + count++; } int Packet::PacketStatistics::getCount() { return count; } @@ -600,44 +543,35 @@ double Packet::PacketStatistics::getAverageSize() { return (double)totalSize / count; } -void Packet::PacketStatistics::renderStats() { -#ifndef _CONTENT_PACKAGE -#if PACKET_ENABLE_STAT_TRACKING - samplesPos++; +int Packet::PacketStatistics::getTotalSize() { return totalSize; } - countSamples[samplesPos & (512 - 1)] = 0; - sizeSamples[samplesPos & (512 - 1)] = 0; - - Minecraft* pMinecraft = Minecraft::GetInstance(); - pMinecraft->gui->renderGraph(512, samplesPos, countSamples, 1, 10, - sizeSamples, 1, 50); -#endif -#endif +int64_t Packet::PacketStatistics::getRunningTotal() { + int64_t total = 0; + int64_t currentTime = System::currentTimeMillis(); + for (int i = 0; i < TOTAL_TICKS; i++) { + if (currentTime - timeSamples[i] <= 1000) { + total += sizeSamples[i]; + } + } + return total; } -int64_t Packet::PacketStatistics::getCountSample(int samplePos) { - if (samplePos == 511) { - samplesPos++; - countSamples[samplesPos & (512 - 1)] = 0; - sizeSamples[samplesPos & (512 - 1)] = 0; +int64_t Packet::PacketStatistics::getRunningCount() { + int64_t total = 0; + int64_t currentTime = System::currentTimeMillis(); + for (int i = 0; i < TOTAL_TICKS; i++) { + if (currentTime - timeSamples[i] <= 1000) { + total += countSamples[i]; + } } - - return countSamples[samplePos] * 10; + return total; } -std::wstring Packet::PacketStatistics::getLegendString() { - static wchar_t string[128]; - double bps = 0.0; - if (firstSampleTime > 0) { - float timeDiff = - ((System::currentTimeMillis() - firstSampleTime) / 1000); - if (timeDiff > 0) bps = totalSize / timeDiff; - } - swprintf(string, 128, - L"id: %d , packets: %d , total: %d , bytes: %d, total: %d, %f Bps", - id, countSamples[(samplesPos - 1) & (512 - 1)], count, - sizeSamples[(samplesPos - 1) & (512 - 1)], totalSize, bps); - return string; +void Packet::PacketStatistics::IncrementPos() { + samplesPos = (samplesPos + 1) % TOTAL_TICKS; + countSamples[samplesPos] = 0; + sizeSamples[samplesPos] = 0; + timeSamples[samplesPos] = 0; } bool Packet::canBeInvalidated() { return false; } diff --git a/Minecraft.World/Network/Packets/Packet.h b/Minecraft.World/Network/Packets/Packet.h index cd1ef3c3d..c52fbf239 100644 --- a/Minecraft.World/Network/Packets/Packet.h +++ b/Minecraft.World/Network/Packets/Packet.h @@ -1,5 +1,4 @@ #pragma once -// class Packet; class PacketListener; @@ -19,33 +18,26 @@ public: int count; int totalSize; + static const int TOTAL_TICKS = 100; + // 4J Added - int64_t countSamples[512]; - int64_t sizeSamples[512]; + int64_t countSamples[TOTAL_TICKS]; + int64_t sizeSamples[TOTAL_TICKS]; + int64_t timeSamples[TOTAL_TICKS]; int samplesPos; - int64_t firstSampleTime; public: const int id; public: - PacketStatistics(int id) - : count(0), - totalSize(0), - samplesPos(0), - firstSampleTime(0), - id(id) { - countSamples[0] = 0; - sizeSamples[0] = 0; - } + PacketStatistics(int id); void addPacket(int bytes); int getCount(); + int getTotalSize(); double getAverageSize(); - - // 4J Added - void renderStats(); - int64_t getCountSample(int samplePos); - std::wstring getLegendString(); + int64_t getRunningTotal(); + int64_t getRunningCount(); + void IncrementPos(); }; // 4J JEV, replaces the static blocks. @@ -71,7 +63,6 @@ public: const int64_t createTime; Packet(); - virtual ~Packet() {} static std::shared_ptr getPacket(int id); @@ -93,12 +84,9 @@ private: static int renderPos; public: - static void recordOutgoingPacket(std::shared_ptr packet); - static void renderPacketStats(int id); - static void renderAllPacketStats(); - static void renderAllPacketStatsKey(); - static int64_t getIndexedStatValue(unsigned int samplePos, - unsigned int renderableId); + static void recordOutgoingPacket(std::shared_ptr packet, + int playerIndex); + static void updatePacketStatsPIX(); private: static std::unordered_map statistics; @@ -131,4 +119,4 @@ public: protected: static void writeNbt(CompoundTag* tag, DataOutputStream* dos); -}; +}; \ No newline at end of file diff --git a/Minecraft.World/Network/Packets/PacketListener.cpp b/Minecraft.World/Network/Packets/PacketListener.cpp index a6dd4edc7..db1045e03 100644 --- a/Minecraft.World/Network/Packets/PacketListener.cpp +++ b/Minecraft.World/Network/Packets/PacketListener.cpp @@ -126,7 +126,8 @@ void PacketListener::handleSetEntityData( onUnhandledPacket((std::shared_ptr)packet); } -void PacketListener::handleRidePacket(std::shared_ptr packet) { +void PacketListener::handleEntityLinkPacket( + std::shared_ptr packet) { onUnhandledPacket((std::shared_ptr)packet); } @@ -320,7 +321,7 @@ void PacketListener::handleServerAuthData( onUnhandledPacket(packet); } -// void PacketListener::handleSharedKey(std::shared_ptr packet) +// void PacketListener::handleSharedKey(shared_ptr packet) //{ // onUnhandledPacket(packet); // } @@ -353,14 +354,48 @@ void PacketListener::handleTileDestruction( void PacketListener::handleClientCommand( std::shared_ptr packet) {} -// void PacketListener::handleLevelChunks(std::shared_ptr -// packet) +// void PacketListener::handleLevelChunks(shared_ptr packet) //{ // onUnhandledPacket(packet); // } bool PacketListener::canHandleAsyncPackets() { return false; } +// 1.6.4 +void PacketListener::handleAddObjective( + std::shared_ptr packet) { + onUnhandledPacket(packet); +} + +void PacketListener::handleSetScore(std::shared_ptr packet) { + onUnhandledPacket(packet); +} + +void PacketListener::handleSetDisplayObjective( + std::shared_ptr packet) { + onUnhandledPacket(packet); +} + +void PacketListener::handleSetPlayerTeamPacket( + std::shared_ptr packet) { + onUnhandledPacket(packet); +} + +void PacketListener::handleParticleEvent( + std::shared_ptr packet) { + onUnhandledPacket(packet); +} + +void PacketListener::handleUpdateAttributes( + std::shared_ptr packet) { + onUnhandledPacket(packet); +} + +void PacketListener::handleTileEditorOpen( + std::shared_ptr tileEditorOpenPacket) {} + +bool PacketListener::isDisconnected() { return false; } + // 4J Added void PacketListener::handleCraftItem(std::shared_ptr packet) { diff --git a/Minecraft.World/Network/Packets/PacketListener.h b/Minecraft.World/Network/Packets/PacketListener.h index ab00ce43d..504cccde4 100644 --- a/Minecraft.World/Network/Packets/PacketListener.h +++ b/Minecraft.World/Network/Packets/PacketListener.h @@ -44,7 +44,7 @@ class SetEntityDataPacket; class SetEntityMotionPacket; class SetEquippedItemPacket; class SetHealthPacket; -class SetRidingPacket; +class SetEntityLinkPacket; class SetSpawnPositionPacket; class SetTimePacket; class SignUpdatePacket; @@ -85,6 +85,15 @@ class TileDestructionPacket; class ClientCommandPacket; class LevelChunksPacket; +// 1.6.4 +class SetObjectivePacket; +class SetScorePacket; +class SetDisplayObjectivePacket; +class SetPlayerTeamPacket; +class LevelParticlesPacket; +class UpdateAttributesPacket; +class TileEditorOpenPacket; + // 4J Added class CraftItemPacket; class TradeItemPacket; @@ -104,7 +113,6 @@ class GameCommandPacket; class PacketListener { public: - virtual ~PacketListener() {} virtual bool isServerPacketListener() = 0; virtual void handleBlockRegionUpdate( std::shared_ptr packet); @@ -146,7 +154,8 @@ public: std::shared_ptr packet); virtual void handleSetEntityData( std::shared_ptr packet); - virtual void handleRidePacket(std::shared_ptr packet); + virtual void handleEntityLinkPacket( + std::shared_ptr packet); virtual void handleInteract(std::shared_ptr packet); virtual void handleEntityEvent(std::shared_ptr packet); virtual void handleSetHealth(std::shared_ptr packet); @@ -231,6 +240,21 @@ public: // packet); virtual bool canHandleAsyncPackets(); + // 1.6.4 + virtual void handleAddObjective(std::shared_ptr packet); + virtual void handleSetScore(std::shared_ptr packet); + virtual void handleSetDisplayObjective( + std::shared_ptr packet); + virtual void handleSetPlayerTeamPacket( + std::shared_ptr packet); + virtual void handleParticleEvent( + std::shared_ptr packet); + virtual void handleUpdateAttributes( + std::shared_ptr packet); + virtual void handleTileEditorOpen( + std::shared_ptr tileEditorOpenPacket); + virtual bool isDisconnected(); + // 4J Added virtual void handleCraftItem(std::shared_ptr packet); virtual void handleTradeItem(std::shared_ptr packet); diff --git a/Minecraft.World/Network/Packets/PlayerAbilitiesPacket.cpp b/Minecraft.World/Network/Packets/PlayerAbilitiesPacket.cpp index b3da7e67a..52b977607 100644 --- a/Minecraft.World/Network/Packets/PlayerAbilitiesPacket.cpp +++ b/Minecraft.World/Network/Packets/PlayerAbilitiesPacket.cpp @@ -3,8 +3,6 @@ #include "../../Headers/net.minecraft.network.packet.h" #include "PlayerAbilitiesPacket.h" -const float PlayerAbilitiesPacket::SPEED_ACCURACY = 255.0f; - PlayerAbilitiesPacket::PlayerAbilitiesPacket() { invulnerable = false; _isFlying = false; @@ -15,23 +13,23 @@ PlayerAbilitiesPacket::PlayerAbilitiesPacket() { } PlayerAbilitiesPacket::PlayerAbilitiesPacket(Abilities* abilities) { - this->setInvulnerable(abilities->invulnerable); - this->setFlying(abilities->flying); - this->setCanFly(abilities->mayfly); - this->setInstabuild(abilities->instabuild); - this->setFlyingSpeed(abilities->getFlyingSpeed()); - this->setWalkingSpeed(abilities->getWalkingSpeed()); + setInvulnerable(abilities->invulnerable); + setFlying(abilities->flying); + setCanFly(abilities->mayfly); + setInstabuild(abilities->instabuild); + setFlyingSpeed(abilities->getFlyingSpeed()); + setWalkingSpeed(abilities->getWalkingSpeed()); } void PlayerAbilitiesPacket::read(DataInputStream* dis) { uint8_t bitfield = dis->readByte(); - this->setInvulnerable((bitfield & FLAG_INVULNERABLE) > 0); - this->setFlying((bitfield & FLAG_FLYING) > 0); - this->setCanFly((bitfield & FLAG_CAN_FLY) > 0); - this->setInstabuild((bitfield & FLAG_INSTABUILD) > 0); - this->setFlyingSpeed(dis->readByte() / SPEED_ACCURACY); - this->setWalkingSpeed(dis->readByte() / SPEED_ACCURACY); + setInvulnerable((bitfield & FLAG_INVULNERABLE) > 0); + setFlying((bitfield & FLAG_FLYING) > 0); + setCanFly((bitfield & FLAG_CAN_FLY) > 0); + setInstabuild((bitfield & FLAG_INSTABUILD) > 0); + setFlyingSpeed(dis->readFloat()); + setWalkingSpeed(dis->readFloat()); } void PlayerAbilitiesPacket::write(DataOutputStream* dos) { @@ -43,8 +41,8 @@ void PlayerAbilitiesPacket::write(DataOutputStream* dos) { if (canInstabuild()) bitfield |= FLAG_INSTABUILD; dos->writeByte(bitfield); - dos->writeByte((int)(flyingSpeed * SPEED_ACCURACY)); - dos->writeByte((int)(walkingSpeed * SPEED_ACCURACY)); + dos->writeFloat(flyingSpeed); + dos->writeFloat(walkingSpeed); } void PlayerAbilitiesPacket::handle(PacketListener* listener) { @@ -53,11 +51,11 @@ void PlayerAbilitiesPacket::handle(PacketListener* listener) { int PlayerAbilitiesPacket::getEstimatedSize() { return 2; } -// std::wstring getDebugInfo() +// wstring getDebugInfo() //{ // return String.format("invuln=%b, flying=%b, canfly=%b, instabuild=%b, -//flyspeed=%.4f, walkspped=%.4f", isInvulnerable(), isFlying(), canFly(), -//canInstabuild(), getFlyingSpeed(), getWalkingSpeed()); +// flyspeed=%.4f, walkspped=%.4f", isInvulnerable(), isFlying(), canFly(), +// canInstabuild(), getFlyingSpeed(), getWalkingSpeed()); // } bool PlayerAbilitiesPacket::isInvulnerable() { return invulnerable; } @@ -83,7 +81,7 @@ void PlayerAbilitiesPacket::setInstabuild(bool instabuild) { float PlayerAbilitiesPacket::getFlyingSpeed() { return flyingSpeed; } void PlayerAbilitiesPacket::setFlyingSpeed(float flySpeed) { - this->flyingSpeed = flySpeed; + flyingSpeed = flySpeed; } float PlayerAbilitiesPacket::getWalkingSpeed() { return walkingSpeed; } diff --git a/Minecraft.World/Network/Packets/PlayerAbilitiesPacket.h b/Minecraft.World/Network/Packets/PlayerAbilitiesPacket.h index 7627cd916..f4ca84d7d 100644 --- a/Minecraft.World/Network/Packets/PlayerAbilitiesPacket.h +++ b/Minecraft.World/Network/Packets/PlayerAbilitiesPacket.h @@ -12,7 +12,6 @@ private: static const int FLAG_FLYING = 1 << 1; static const int FLAG_CAN_FLY = 1 << 2; static const int FLAG_INSTABUILD = 1 << 3; - static const float SPEED_ACCURACY; bool invulnerable; bool _isFlying; diff --git a/Minecraft.World/Network/Packets/PlayerActionPacket.cpp b/Minecraft.World/Network/Packets/PlayerActionPacket.cpp index 2436ecc6e..b1f585297 100644 --- a/Minecraft.World/Network/Packets/PlayerActionPacket.cpp +++ b/Minecraft.World/Network/Packets/PlayerActionPacket.cpp @@ -7,7 +7,7 @@ const int PlayerActionPacket::START_DESTROY_BLOCK = 0; const int PlayerActionPacket::ABORT_DESTROY_BLOCK = 1; const int PlayerActionPacket::STOP_DESTROY_BLOCK = 2; -const int PlayerActionPacket::GET_UPDATED_BLOCK = 3; +const int PlayerActionPacket::DROP_ALL_ITEMS = 3; const int PlayerActionPacket::DROP_ITEM = 4; const int PlayerActionPacket::RELEASE_USE_ITEM = 5; @@ -30,11 +30,11 @@ PlayerActionPacket::PlayerActionPacket(int action, int x, int y, int z, void PlayerActionPacket::read(DataInputStream* dis) // throws IOException { - action = dis->read(); + action = dis->readUnsignedByte(); x = dis->readInt(); - y = dis->read(); + y = dis->readUnsignedByte(); z = dis->readInt(); - face = dis->read(); + face = dis->readUnsignedByte(); } void PlayerActionPacket::write(DataOutputStream* dos) // throws IOException diff --git a/Minecraft.World/Network/Packets/PlayerActionPacket.h b/Minecraft.World/Network/Packets/PlayerActionPacket.h index d73ff9211..42ec5fdf2 100644 --- a/Minecraft.World/Network/Packets/PlayerActionPacket.h +++ b/Minecraft.World/Network/Packets/PlayerActionPacket.h @@ -9,7 +9,7 @@ public: static const int START_DESTROY_BLOCK; static const int ABORT_DESTROY_BLOCK; static const int STOP_DESTROY_BLOCK; - static const int GET_UPDATED_BLOCK; + static const int DROP_ALL_ITEMS; static const int DROP_ITEM; static const int RELEASE_USE_ITEM; diff --git a/Minecraft.World/Network/Packets/PlayerCommandPacket.cpp b/Minecraft.World/Network/Packets/PlayerCommandPacket.cpp index d8129e640..2f0996811 100644 --- a/Minecraft.World/Network/Packets/PlayerCommandPacket.cpp +++ b/Minecraft.World/Network/Packets/PlayerCommandPacket.cpp @@ -12,32 +12,45 @@ const int PlayerCommandPacket::START_SPRINTING = 4; const int PlayerCommandPacket::STOP_SPRINTING = 5; const int PlayerCommandPacket::START_IDLEANIM = 6; const int PlayerCommandPacket::STOP_IDLEANIM = 7; +const int PlayerCommandPacket::RIDING_JUMP = 8; +const int PlayerCommandPacket::OPEN_INVENTORY = 9; PlayerCommandPacket::PlayerCommandPacket() { id = -1; action = 0; + data = 0; } PlayerCommandPacket::PlayerCommandPacket(std::shared_ptr e, int action) { id = e->entityId; this->action = action; + this->data = 0; +} + +PlayerCommandPacket::PlayerCommandPacket(std::shared_ptr e, int action, + int data) { + id = e->entityId; + this->action = action; + this->data = data; } void PlayerCommandPacket::read(DataInputStream* dis) // throws IOException { id = dis->readInt(); action = dis->readByte(); + data = dis->readInt(); } void PlayerCommandPacket::write(DataOutputStream* dos) // throws IOException { dos->writeInt(id); dos->writeByte(action); + dos->writeInt(data); } void PlayerCommandPacket::handle(PacketListener* listener) { listener->handlePlayerCommand(shared_from_this()); } -int PlayerCommandPacket::getEstimatedSize() { return 5; } +int PlayerCommandPacket::getEstimatedSize() { return 9; } diff --git a/Minecraft.World/Network/Packets/PlayerCommandPacket.h b/Minecraft.World/Network/Packets/PlayerCommandPacket.h index 08c0c1251..29b5ed6da 100644 --- a/Minecraft.World/Network/Packets/PlayerCommandPacket.h +++ b/Minecraft.World/Network/Packets/PlayerCommandPacket.h @@ -13,6 +13,8 @@ public: static const int STOP_SPRINTING; static const int START_IDLEANIM; static const int STOP_IDLEANIM; + static const int RIDING_JUMP; + static const int OPEN_INVENTORY; // 4J Added // 4J-PB - Making this host only setting @@ -23,9 +25,11 @@ public: int id; int action; + int data; PlayerCommandPacket(); PlayerCommandPacket(std::shared_ptr e, int action); + PlayerCommandPacket(std::shared_ptr e, int action, int data); virtual void read(DataInputStream* dis); virtual void write(DataOutputStream* dos); diff --git a/Minecraft.World/Network/Packets/PlayerInfoPacket.h b/Minecraft.World/Network/Packets/PlayerInfoPacket.h index cf556646c..cf91c5c6d 100644 --- a/Minecraft.World/Network/Packets/PlayerInfoPacket.h +++ b/Minecraft.World/Network/Packets/PlayerInfoPacket.h @@ -1,5 +1,4 @@ #pragma once -#include #include "Packet.h" @@ -12,7 +11,7 @@ public: // std::wstring name; // bool add; // int latency; - std::uint8_t m_networkSmallId; + short m_networkSmallId; short m_playerColourIndex; unsigned int m_playerPrivileges; int m_entityId; @@ -33,4 +32,4 @@ public: return std::shared_ptr(new PlayerInfoPacket()); } virtual int getId() { return 201; } -}; +}; \ No newline at end of file diff --git a/Minecraft.World/Network/Packets/PlayerInputPacket.cpp b/Minecraft.World/Network/Packets/PlayerInputPacket.cpp index 0f5461e15..8f1bad5b2 100644 --- a/Minecraft.World/Network/Packets/PlayerInputPacket.cpp +++ b/Minecraft.World/Network/Packets/PlayerInputPacket.cpp @@ -5,41 +5,32 @@ #include "PlayerInputPacket.h" PlayerInputPacket::PlayerInputPacket() { - xa = 0.0f; - ya = 0.0f; + xxa = 0.0f; + yya = 0.0f; isJumpingVar = false; isSneakingVar = false; - xRot = 0.0f; - yRot = 0.0f; } -PlayerInputPacket::PlayerInputPacket(float xa, float ya, bool isJumpingVar, - bool isSneakingVar, float xRot, - float yRot) { - this->xa = xa; - this->ya = ya; +PlayerInputPacket::PlayerInputPacket(float xxa, float yya, bool isJumpingVar, + bool isSneakingVar) { + this->xxa = xxa; + this->yya = yya; this->isJumpingVar = isJumpingVar; this->isSneakingVar = isSneakingVar; - this->xRot = xRot; - this->yRot = yRot; } void PlayerInputPacket::read(DataInputStream* dis) // throws IOException { - xa = dis->readFloat(); - ya = dis->readFloat(); - xRot = dis->readFloat(); - yRot = dis->readFloat(); + xxa = dis->readFloat(); + yya = dis->readFloat(); isJumpingVar = dis->readBoolean(); isSneakingVar = dis->readBoolean(); } void PlayerInputPacket::write(DataOutputStream* dos) // throws IOException { - dos->writeFloat(xa); - dos->writeFloat(ya); - dos->writeFloat(xRot); - dos->writeFloat(yRot); + dos->writeFloat(xxa); + dos->writeFloat(yya); dos->writeBoolean(isJumpingVar); dos->writeBoolean(isSneakingVar); } @@ -48,15 +39,11 @@ void PlayerInputPacket::handle(PacketListener* listener) { listener->handlePlayerInput(shared_from_this()); } -int PlayerInputPacket::getEstimatedSize() { return 18; } +int PlayerInputPacket::getEstimatedSize() { return 10; } -float PlayerInputPacket::getXa() { return xa; } +float PlayerInputPacket::getXxa() { return xxa; } -float PlayerInputPacket::getXRot() { return xRot; } - -float PlayerInputPacket::getYa() { return ya; } - -float PlayerInputPacket::getYRot() { return yRot; } +float PlayerInputPacket::getYya() { return yya; } bool PlayerInputPacket::isJumping() { return isJumpingVar; } diff --git a/Minecraft.World/Network/Packets/PlayerInputPacket.h b/Minecraft.World/Network/Packets/PlayerInputPacket.h index 54b14ca71..1abb5ac92 100644 --- a/Minecraft.World/Network/Packets/PlayerInputPacket.h +++ b/Minecraft.World/Network/Packets/PlayerInputPacket.h @@ -6,27 +6,23 @@ class PlayerInputPacket : public Packet, public std::enable_shared_from_this { private: - float xa; - float ya; + float xxa; + float yya; bool isJumpingVar; bool isSneakingVar; - float xRot; - float yRot; public: PlayerInputPacket(); - PlayerInputPacket(float xa, float ya, bool isJumpingVar, bool isSneakingVar, - float xRot, float yRot); + PlayerInputPacket(float xxa, float yya, bool isJumpingVar, + bool isSneakingVar); virtual void read(DataInputStream* dis); virtual void write(DataOutputStream* dos); virtual void handle(PacketListener* listener); virtual int getEstimatedSize(); - float getXa(); - float getXRot(); - float getYa(); - float getYRot(); + float getXxa(); + float getYya(); bool isJumping(); bool isSneaking(); diff --git a/Minecraft.World/Network/Packets/ServerAuthDataPacket.h b/Minecraft.World/Network/Packets/ServerAuthDataPacket.h index 8fbc0f733..55dd64e70 100644 --- a/Minecraft.World/Network/Packets/ServerAuthDataPacket.h +++ b/Minecraft.World/Network/Packets/ServerAuthDataPacket.h @@ -6,13 +6,13 @@ class ServerAuthDataPacket : public Packet { #if 0 private String serverId; private PublicKey publicKey; - private byte[] nonce = new uint8_t[]{}; + private uint8_t[] nonce = new uint8_t[]{}; public ServerAuthDataPacket() { // Needed } - public ServerAuthDataPacket(final String serverId, final PublicKey publicKey, final byte[] nonce) { + public ServerAuthDataPacket(final String serverId, final PublicKey publicKey, final uint8_t[] nonce) { this.serverId = serverId; this.publicKey = publicKey; this.nonce = nonce; @@ -50,7 +50,7 @@ class ServerAuthDataPacket : public Packet { return publicKey; } - public byte[] getNonce() { + public uint8_t[] getNonce() { return nonce; } #endif diff --git a/Minecraft.World/Network/Packets/SetDisplayObjectivePacket.cpp b/Minecraft.World/Network/Packets/SetDisplayObjectivePacket.cpp new file mode 100644 index 000000000..ba62aff85 --- /dev/null +++ b/Minecraft.World/Network/Packets/SetDisplayObjectivePacket.cpp @@ -0,0 +1,38 @@ +#include "../../Platform/stdafx.h" +#include "PacketListener.h" +#include "../../Headers/net.minecraft.world.scores.h" +#include "SetDisplayObjectivePacket.h" + +SetDisplayObjectivePacket::SetDisplayObjectivePacket() { + slot = 0; + objectiveName = L""; +} + +SetDisplayObjectivePacket::SetDisplayObjectivePacket(int slot, + Objective* objective) { + this->slot = slot; + + if (objective == NULL) { + objectiveName = L""; + } else { + objectiveName = objective->getName(); + } +} + +void SetDisplayObjectivePacket::read(DataInputStream* dis) { + slot = dis->readByte(); + objectiveName = readUtf(dis, Objective::MAX_NAME_LENGTH); +} + +void SetDisplayObjectivePacket::write(DataOutputStream* dos) { + dos->writeByte(slot); + writeUtf(objectiveName, dos); +} + +void SetDisplayObjectivePacket::handle(PacketListener* listener) { + listener->handleSetDisplayObjective(shared_from_this()); +} + +int SetDisplayObjectivePacket::getEstimatedSize() { + return 1 + 2 + objectiveName.length(); +} \ No newline at end of file diff --git a/Minecraft.World/Network/Packets/SetDisplayObjectivePacket.h b/Minecraft.World/Network/Packets/SetDisplayObjectivePacket.h new file mode 100644 index 000000000..3157ffbac --- /dev/null +++ b/Minecraft.World/Network/Packets/SetDisplayObjectivePacket.h @@ -0,0 +1,27 @@ +#pragma once + +#include "Packet.h" + +class Objective; + +class SetDisplayObjectivePacket + : public Packet, + public std::enable_shared_from_this { +public: + int slot; + std::wstring objectiveName; + + SetDisplayObjectivePacket(); + SetDisplayObjectivePacket(int slot, Objective* objective); + + void read(DataInputStream* dis); + void write(DataOutputStream* dos); + void handle(PacketListener* listener); + int getEstimatedSize(); + +public: + static std::shared_ptr create() { + return std::shared_ptr(new SetDisplayObjectivePacket()); + } + virtual int getId() { return 208; } +}; \ No newline at end of file diff --git a/Minecraft.World/Network/Packets/SetEntityDataPacket.cpp b/Minecraft.World/Network/Packets/SetEntityDataPacket.cpp index f96148210..c8765b1a5 100644 --- a/Minecraft.World/Network/Packets/SetEntityDataPacket.cpp +++ b/Minecraft.World/Network/Packets/SetEntityDataPacket.cpp @@ -40,8 +40,6 @@ void SetEntityDataPacket::handle(PacketListener* listener) { int SetEntityDataPacket::getEstimatedSize() { return 5; } -bool SetEntityDataPacket::isAync() { return true; } - std::vector >* SetEntityDataPacket::getUnpackedData() { return packedItems; diff --git a/Minecraft.World/Network/Packets/SetEntityDataPacket.h b/Minecraft.World/Network/Packets/SetEntityDataPacket.h index 0833b9a9a..4ffffbc5d 100644 --- a/Minecraft.World/Network/Packets/SetEntityDataPacket.h +++ b/Minecraft.World/Network/Packets/SetEntityDataPacket.h @@ -22,7 +22,6 @@ public: virtual void write(DataOutputStream* dos); virtual void handle(PacketListener* listener); virtual int getEstimatedSize(); - virtual bool isAync(); std::vector >* getUnpackedData(); diff --git a/Minecraft.World/Network/Packets/SetEntityLinkPacket.cpp b/Minecraft.World/Network/Packets/SetEntityLinkPacket.cpp new file mode 100644 index 000000000..6ba2bd235 --- /dev/null +++ b/Minecraft.World/Network/Packets/SetEntityLinkPacket.cpp @@ -0,0 +1,48 @@ +#include "../../Platform/stdafx.h" +#include +#include "../../IO/Streams/InputOutputStream.h" +#include "PacketListener.h" +#include "../../Headers/net.minecraft.world.entity.h" +#include "SetEntityLinkPacket.h" + +SetEntityLinkPacket::SetEntityLinkPacket() { + sourceId = -1; + destId = -1; + type = -1; +} + +SetEntityLinkPacket::SetEntityLinkPacket(int linkType, + std::shared_ptr sourceEntity, + std::shared_ptr destEntity) { + type = linkType; + this->sourceId = sourceEntity->entityId; + this->destId = destEntity != NULL ? destEntity->entityId : -1; +} + +int SetEntityLinkPacket::getEstimatedSize() { return 8; } + +void SetEntityLinkPacket::read(DataInputStream* dis) // throws IOException +{ + sourceId = dis->readInt(); + destId = dis->readInt(); + type = dis->readUnsignedByte(); +} + +void SetEntityLinkPacket::write(DataOutputStream* dos) // throws IOException +{ + dos->writeInt(sourceId); + dos->writeInt(destId); + dos->writeByte(type); +} + +void SetEntityLinkPacket::handle(PacketListener* listener) { + listener->handleEntityLinkPacket(shared_from_this()); +} + +bool SetEntityLinkPacket::canBeInvalidated() { return true; } + +bool SetEntityLinkPacket::isInvalidatedBy(std::shared_ptr packet) { + std::shared_ptr target = + std::dynamic_pointer_cast(packet); + return target->sourceId == sourceId; +} diff --git a/Minecraft.World/Network/Packets/SetEntityLinkPacket.h b/Minecraft.World/Network/Packets/SetEntityLinkPacket.h new file mode 100644 index 000000000..aa25f52b6 --- /dev/null +++ b/Minecraft.World/Network/Packets/SetEntityLinkPacket.h @@ -0,0 +1,31 @@ +#pragma once + +#include "Packet.h" + +class SetEntityLinkPacket + : public Packet, + public std::enable_shared_from_this { +public: + static const int RIDING = 0; + static const int LEASH = 1; + + int type; + int sourceId, destId; + + SetEntityLinkPacket(); + SetEntityLinkPacket(int linkType, std::shared_ptr sourceEntity, + std::shared_ptr destEntity); + + virtual int getEstimatedSize(); + virtual void read(DataInputStream* dis); + virtual void write(DataOutputStream* dos); + virtual void handle(PacketListener* listener); + virtual bool canBeInvalidated(); + virtual bool isInvalidatedBy(std::shared_ptr packet); + +public: + static std::shared_ptr create() { + return std::shared_ptr(new SetEntityLinkPacket()); + } + virtual int getId() { return 39; } +}; \ No newline at end of file diff --git a/Minecraft.World/Network/Packets/SetExperiencePacket.cpp b/Minecraft.World/Network/Packets/SetExperiencePacket.cpp index 01b679498..0966b55e6 100644 --- a/Minecraft.World/Network/Packets/SetExperiencePacket.cpp +++ b/Minecraft.World/Network/Packets/SetExperiencePacket.cpp @@ -39,6 +39,4 @@ bool SetExperiencePacket::canBeInvalidated() { return true; } bool SetExperiencePacket::isInvalidatedBy(std::shared_ptr packet) { return true; -} - -bool SetExperiencePacket::isAync() { return true; } \ No newline at end of file +} \ No newline at end of file diff --git a/Minecraft.World/Network/Packets/SetExperiencePacket.h b/Minecraft.World/Network/Packets/SetExperiencePacket.h index e46898537..8dca39b81 100644 --- a/Minecraft.World/Network/Packets/SetExperiencePacket.h +++ b/Minecraft.World/Network/Packets/SetExperiencePacket.h @@ -20,7 +20,6 @@ public: virtual int getEstimatedSize(); virtual bool canBeInvalidated(); virtual bool isInvalidatedBy(std::shared_ptr packet); - virtual bool isAync(); public: static std::shared_ptr create() { diff --git a/Minecraft.World/Network/Packets/SetHealthPacket.cpp b/Minecraft.World/Network/Packets/SetHealthPacket.cpp index ccd8ac338..ed529ecfe 100644 --- a/Minecraft.World/Network/Packets/SetHealthPacket.cpp +++ b/Minecraft.World/Network/Packets/SetHealthPacket.cpp @@ -3,17 +3,16 @@ #include "../../IO/Streams/InputOutputStream.h" #include "PacketListener.h" #include "SetHealthPacket.h" -#include "../../../Minecraft.Client/Platform/Common/Telemetry/TelemetryManager.h" SetHealthPacket::SetHealthPacket() { - this->health = 0; + this->health = 0.0f; this->food = 0; this->saturation = 0; this->damageSource = eTelemetryChallenges_Unknown; } -SetHealthPacket::SetHealthPacket(int health, int food, float saturation, +SetHealthPacket::SetHealthPacket(float health, int food, float saturation, ETelemetryChallenges damageSource) { this->health = health; this->food = food; @@ -25,7 +24,7 @@ SetHealthPacket::SetHealthPacket(int health, int food, float saturation, void SetHealthPacket::read(DataInputStream* dis) // throws IOException { - health = dis->readShort(); + health = dis->readFloat(); food = dis->readShort(); saturation = dis->readFloat(); // exhaustion = dis.readFloat(); @@ -35,7 +34,7 @@ void SetHealthPacket::read(DataInputStream* dis) // throws IOException void SetHealthPacket::write(DataOutputStream* dos) // throws IOException { - dos->writeShort(health); + dos->writeFloat(health); dos->writeShort(food); dos->writeFloat(saturation); // dos.writeFloat(exhaustion); @@ -47,10 +46,10 @@ void SetHealthPacket::handle(PacketListener* listener) { listener->handleSetHealth(shared_from_this()); } -int SetHealthPacket::getEstimatedSize() { return 9; } +int SetHealthPacket::getEstimatedSize() { return 11; } bool SetHealthPacket::canBeInvalidated() { return true; } bool SetHealthPacket::isInvalidatedBy(std::shared_ptr packet) { return true; -} +} \ No newline at end of file diff --git a/Minecraft.World/Network/Packets/SetHealthPacket.h b/Minecraft.World/Network/Packets/SetHealthPacket.h index 2d4deff41..a23dbf00a 100644 --- a/Minecraft.World/Network/Packets/SetHealthPacket.h +++ b/Minecraft.World/Network/Packets/SetHealthPacket.h @@ -5,15 +5,14 @@ class SetHealthPacket : public Packet, public std::enable_shared_from_this { public: - int health; + float health; int food; float saturation; - // public float exhaustion; // 4J - Original comment ETelemetryChallenges damageSource; // 4J Added SetHealthPacket(); - SetHealthPacket(int health, int food, float saturation, + SetHealthPacket(float health, int food, float saturation, ETelemetryChallenges damageSource); virtual void read(DataInputStream* dis); diff --git a/Minecraft.World/Network/Packets/SetObjectivePacket.cpp b/Minecraft.World/Network/Packets/SetObjectivePacket.cpp new file mode 100644 index 000000000..e2a867a44 --- /dev/null +++ b/Minecraft.World/Network/Packets/SetObjectivePacket.cpp @@ -0,0 +1,36 @@ +#include "../../Platform/stdafx.h" +#include "../../Headers/net.minecraft.world.scores.h" +#include "PacketListener.h" +#include "SetObjectivePacket.h" + +SetObjectivePacket::SetObjectivePacket() { + objectiveName = L""; + displayName = L""; + method = 0; +} + +SetObjectivePacket::SetObjectivePacket(Objective* objective, int method) { + objectiveName = objective->getName(); + displayName = objective->getDisplayName(); + this->method = method; +} + +void SetObjectivePacket::read(DataInputStream* dis) { + objectiveName = readUtf(dis, Objective::MAX_NAME_LENGTH); + displayName = readUtf(dis, Objective::MAX_DISPLAY_NAME_LENGTH); + method = dis->readByte(); +} + +void SetObjectivePacket::write(DataOutputStream* dos) { + writeUtf(objectiveName, dos); + writeUtf(displayName, dos); + dos->writeByte(method); +} + +void SetObjectivePacket::handle(PacketListener* listener) { + listener->handleAddObjective(shared_from_this()); +} + +int SetObjectivePacket::getEstimatedSize() { + return 2 + objectiveName.length() + 2 + displayName.length() + 1; +} \ No newline at end of file diff --git a/Minecraft.World/Network/Packets/SetObjectivePacket.h b/Minecraft.World/Network/Packets/SetObjectivePacket.h new file mode 100644 index 000000000..d7429be03 --- /dev/null +++ b/Minecraft.World/Network/Packets/SetObjectivePacket.h @@ -0,0 +1,31 @@ +#pragma once + +#include "Packet.h" + +class Objective; + +class SetObjectivePacket + : public Packet, + public std::enable_shared_from_this { +public: + static const int METHOD_ADD = 0; + static const int METHOD_REMOVE = 1; + static const int METHOD_CHANGE = 2; + + std::wstring objectiveName; + std::wstring displayName; + int method; + + SetObjectivePacket(); + SetObjectivePacket(Objective* objective, int method); + void read(DataInputStream* dis); + void write(DataOutputStream* dos); + void handle(PacketListener* listener); + int getEstimatedSize(); + +public: + static std::shared_ptr create() { + return std::shared_ptr(new SetObjectivePacket()); + } + virtual int getId() { return 206; } +}; \ No newline at end of file diff --git a/Minecraft.World/Network/Packets/SetPlayerTeamPacket.cpp b/Minecraft.World/Network/Packets/SetPlayerTeamPacket.cpp new file mode 100644 index 000000000..2c7afe20c --- /dev/null +++ b/Minecraft.World/Network/Packets/SetPlayerTeamPacket.cpp @@ -0,0 +1,100 @@ +#include "../../Platform/stdafx.h" +#include "../../Headers/net.minecraft.world.scores.h" +#include "../../Headers/net.minecraft.world.entity.player.h" +#include "PacketListener.h" +#include "SetPlayerTeamPacket.h" + +SetPlayerTeamPacket::SetPlayerTeamPacket() { + name = L""; + displayName = L""; + prefix = L""; + suffix = L""; + method = 0; + options = 0; +} + +SetPlayerTeamPacket::SetPlayerTeamPacket(PlayerTeam* team, int method) { + name = team->getName(); + this->method = method; + + if (method == METHOD_ADD || method == METHOD_CHANGE) { + displayName = team->getDisplayName(); + prefix = team->getPrefix(); + suffix = team->getSuffix(); + options = team->packOptions(); + } + if (method == METHOD_ADD) { + std::unordered_set* playerNames = team->getPlayers(); + players.insert(players.end(), playerNames->begin(), playerNames->end()); + } +} + +SetPlayerTeamPacket::SetPlayerTeamPacket(PlayerTeam* team, + std::vector* playerNames, + int method) { + if (method != METHOD_JOIN && method != METHOD_LEAVE) { + app.DebugPrintf("Method must be join or leave for player constructor"); +#ifndef _CONTENT_PACKAGE + __debugbreak(); +#endif + } + if (playerNames == NULL || playerNames->empty()) { + app.DebugPrintf("Players cannot be null/empty"); +#ifndef _CONTENT_PACKAGE + __debugbreak(); +#endif + } + + this->method = method; + name = team->getName(); + this->players.insert(players.end(), playerNames->begin(), + playerNames->end()); +} + +void SetPlayerTeamPacket::read(DataInputStream* dis) { + name = readUtf(dis, Objective::MAX_NAME_LENGTH); + method = dis->readByte(); + + if (method == METHOD_ADD || method == METHOD_CHANGE) { + displayName = readUtf(dis, PlayerTeam::MAX_DISPLAY_NAME_LENGTH); + prefix = readUtf(dis, PlayerTeam::MAX_PREFIX_LENGTH); + suffix = readUtf(dis, PlayerTeam::MAX_SUFFIX_LENGTH); + options = dis->readByte(); + } + + if (method == METHOD_ADD || method == METHOD_JOIN || + method == METHOD_LEAVE) { + int count = dis->readShort(); + + for (int i = 0; i < count; i++) { + players.push_back(readUtf(dis, Player::MAX_NAME_LENGTH)); + } + } +} + +void SetPlayerTeamPacket::write(DataOutputStream* dos) { + writeUtf(name, dos); + dos->writeByte(method); + + if (method == METHOD_ADD || method == METHOD_CHANGE) { + writeUtf(displayName, dos); + writeUtf(prefix, dos); + writeUtf(suffix, dos); + dos->writeByte(options); + } + + if (method == METHOD_ADD || method == METHOD_JOIN || + method == METHOD_LEAVE) { + dos->writeShort(players.size()); + + for (AUTO_VAR(it, players.begin()); it != players.end(); ++it) { + writeUtf(*it, dos); + } + } +} + +void SetPlayerTeamPacket::handle(PacketListener* listener) { + listener->handleSetPlayerTeamPacket(shared_from_this()); +} + +int SetPlayerTeamPacket::getEstimatedSize() { return 1 + 2 + name.length(); } \ No newline at end of file diff --git a/Minecraft.World/Network/Packets/SetPlayerTeamPacket.h b/Minecraft.World/Network/Packets/SetPlayerTeamPacket.h new file mode 100644 index 000000000..32980f13d --- /dev/null +++ b/Minecraft.World/Network/Packets/SetPlayerTeamPacket.h @@ -0,0 +1,39 @@ +#pragma once + +#include "Packet.h" + +class PlayerTeam; + +class SetPlayerTeamPacket + : public Packet, + public std::enable_shared_from_this { +public: + static const int METHOD_ADD = 0; + static const int METHOD_REMOVE = 1; + static const int METHOD_CHANGE = 2; + static const int METHOD_JOIN = 3; + static const int METHOD_LEAVE = 4; + + std::wstring name; + std::wstring displayName; + std::wstring prefix; + std::wstring suffix; + std::vector players; + int method; + int options; + + SetPlayerTeamPacket(); + SetPlayerTeamPacket(PlayerTeam* team, int method); + SetPlayerTeamPacket(PlayerTeam* team, std::vector* players, + int method); + void read(DataInputStream* dis); + void write(DataOutputStream* dos); + void handle(PacketListener* listener); + int getEstimatedSize(); + +public: + static std::shared_ptr create() { + return std::shared_ptr(new SetPlayerTeamPacket()); + } + virtual int getId() { return 209; } +}; \ No newline at end of file diff --git a/Minecraft.World/Network/Packets/SetRidingPacket.cpp b/Minecraft.World/Network/Packets/SetRidingPacket.cpp deleted file mode 100644 index 1db4914a5..000000000 --- a/Minecraft.World/Network/Packets/SetRidingPacket.cpp +++ /dev/null @@ -1,43 +0,0 @@ -#include "../../Platform/stdafx.h" -#include -#include "../../IO/Streams/InputOutputStream.h" -#include "PacketListener.h" -#include "../../Headers/net.minecraft.world.entity.h" -#include "SetRidingPacket.h" - -SetRidingPacket::SetRidingPacket() { - riderId = -1; - riddenId = -1; -} - -SetRidingPacket::SetRidingPacket(std::shared_ptr rider, - std::shared_ptr riding) { - this->riderId = rider->entityId; - this->riddenId = riding != NULL ? riding->entityId : -1; -} - -int SetRidingPacket::getEstimatedSize() { return 8; } - -void SetRidingPacket::read(DataInputStream* dis) // throws IOException -{ - riderId = dis->readInt(); - riddenId = dis->readInt(); -} - -void SetRidingPacket::write(DataOutputStream* dos) // throws IOException -{ - dos->writeInt(riderId); - dos->writeInt(riddenId); -} - -void SetRidingPacket::handle(PacketListener* listener) { - listener->handleRidePacket(shared_from_this()); -} - -bool SetRidingPacket::canBeInvalidated() { return true; } - -bool SetRidingPacket::isInvalidatedBy(std::shared_ptr packet) { - std::shared_ptr target = - std::dynamic_pointer_cast(packet); - return target->riderId == riderId; -} diff --git a/Minecraft.World/Network/Packets/SetRidingPacket.h b/Minecraft.World/Network/Packets/SetRidingPacket.h deleted file mode 100644 index 74095d260..000000000 --- a/Minecraft.World/Network/Packets/SetRidingPacket.h +++ /dev/null @@ -1,26 +0,0 @@ -#pragma once - -#include "Packet.h" - -class SetRidingPacket : public Packet, - public std::enable_shared_from_this { -public: - int riderId, riddenId; - - SetRidingPacket(); - SetRidingPacket(std::shared_ptr rider, - std::shared_ptr riding); - - virtual int getEstimatedSize(); - virtual void read(DataInputStream* dis); - virtual void write(DataOutputStream* dos); - virtual void handle(PacketListener* listener); - virtual bool canBeInvalidated(); - virtual bool isInvalidatedBy(std::shared_ptr packet); - -public: - static std::shared_ptr create() { - return std::shared_ptr(new SetRidingPacket()); - } - virtual int getId() { return 39; } -}; \ No newline at end of file diff --git a/Minecraft.World/Network/Packets/SetScorePacket.cpp b/Minecraft.World/Network/Packets/SetScorePacket.cpp new file mode 100644 index 000000000..47a0a5e2f --- /dev/null +++ b/Minecraft.World/Network/Packets/SetScorePacket.cpp @@ -0,0 +1,55 @@ +#include "../../Platform/stdafx.h" +#include "../../Headers/net.minecraft.world.entity.player.h" +#include "../../Headers/net.minecraft.world.scores.h" +#include "PacketListener.h" +#include "SetScorePacket.h" + +SetScorePacket::SetScorePacket() { + owner = L""; + objectiveName = L""; + score = 0; + method = 0; +} + +SetScorePacket::SetScorePacket(Score* score, int method) { + owner = score->getOwner(); + objectiveName = score->getObjective()->getName(); + this->score = score->getScore(); + this->method = method; +} + +SetScorePacket::SetScorePacket(const std::wstring& owner) { + this->owner = owner; + objectiveName = L""; + score = 0; + method = METHOD_REMOVE; +} + +void SetScorePacket::read(DataInputStream* dis) { + owner = readUtf(dis, Player::MAX_NAME_LENGTH); + method = dis->readByte(); + + if (method != METHOD_REMOVE) { + objectiveName = readUtf(dis, Objective::MAX_NAME_LENGTH); + score = dis->readInt(); + } +} + +void SetScorePacket::write(DataOutputStream* dos) { + writeUtf(owner, dos); + dos->writeByte(method); + + if (method != METHOD_REMOVE) { + writeUtf(objectiveName, dos); + dos->writeInt(score); + } +} + +void SetScorePacket::handle(PacketListener* listener) { + listener->handleSetScore(shared_from_this()); +} + +int SetScorePacket::getEstimatedSize() { + return 2 + (owner.empty() ? 0 : owner.length()) + 2 + + (objectiveName.empty() ? 0 : objectiveName.length()) + 4 + 1; +} \ No newline at end of file diff --git a/Minecraft.World/Network/Packets/SetScorePacket.h b/Minecraft.World/Network/Packets/SetScorePacket.h new file mode 100644 index 000000000..893ffeef9 --- /dev/null +++ b/Minecraft.World/Network/Packets/SetScorePacket.h @@ -0,0 +1,32 @@ +#pragma once + +#include "Packet.h" + +class Score; + +class SetScorePacket : public Packet, + public std::enable_shared_from_this { +public: + static const int METHOD_CHANGE = 0; + static const int METHOD_REMOVE = 1; + + std::wstring owner; + std::wstring objectiveName; + int score; + int method; + + SetScorePacket(); + SetScorePacket(Score* score, int method); + SetScorePacket(const std::wstring& owner); + + void read(DataInputStream* dis); + void write(DataOutputStream* dos); + void handle(PacketListener* listener); + int getEstimatedSize(); + +public: + static std::shared_ptr create() { + return std::shared_ptr(new SetScorePacket()); + } + virtual int getId() { return 207; } +}; \ No newline at end of file diff --git a/Minecraft.World/Network/Packets/SetTimePacket.cpp b/Minecraft.World/Network/Packets/SetTimePacket.cpp index 5ba8046ff..0bafd9389 100644 --- a/Minecraft.World/Network/Packets/SetTimePacket.cpp +++ b/Minecraft.World/Network/Packets/SetTimePacket.cpp @@ -4,25 +4,44 @@ #include "PacketListener.h" #include "SetTimePacket.h" -SetTimePacket::SetTimePacket() { time = 0; } +SetTimePacket::SetTimePacket() { + gameTime = 0; + dayTime = 0; +} -SetTimePacket::SetTimePacket(int64_t time) { this->time = time; } +SetTimePacket::SetTimePacket(int64_t gameTime, int64_t dayTime, + bool tickDayTime) { + this->gameTime = gameTime; + this->dayTime = dayTime; + + // 4J: We send daylight cycle rule with host options so don't need this + /*if (!tickDayTime) + { + this->dayTime = -this->dayTime; + if (this->dayTime == 0) + { + this->dayTime = -1; + } + }*/ +} void SetTimePacket::read(DataInputStream* dis) // throws IOException { - time = dis->readLong(); + gameTime = dis->readLong(); + dayTime = dis->readLong(); } void SetTimePacket::write(DataOutputStream* dos) // throws IOException { - dos->writeLong(time); + dos->writeLong(gameTime); + dos->writeLong(dayTime); } void SetTimePacket::handle(PacketListener* listener) { listener->handleSetTime(shared_from_this()); } -int SetTimePacket::getEstimatedSize() { return 8; } +int SetTimePacket::getEstimatedSize() { return 16; } bool SetTimePacket::canBeInvalidated() { return true; } diff --git a/Minecraft.World/Network/Packets/SetTimePacket.h b/Minecraft.World/Network/Packets/SetTimePacket.h index fa5d2097b..0e3885955 100644 --- a/Minecraft.World/Network/Packets/SetTimePacket.h +++ b/Minecraft.World/Network/Packets/SetTimePacket.h @@ -5,10 +5,11 @@ class SetTimePacket : public Packet, public std::enable_shared_from_this { public: - int64_t time; + int64_t gameTime; + int64_t dayTime; SetTimePacket(); - SetTimePacket(int64_t time); + SetTimePacket(int64_t gameTime, int64_t dayTime, bool tickDayTime); virtual void read(DataInputStream* dis); virtual void write(DataOutputStream* dos); diff --git a/Minecraft.World/Network/Packets/SharedKeyPacket.h b/Minecraft.World/Network/Packets/SharedKeyPacket.h index f2f7d1441..d56eb7169 100644 --- a/Minecraft.World/Network/Packets/SharedKeyPacket.h +++ b/Minecraft.World/Network/Packets/SharedKeyPacket.h @@ -4,8 +4,8 @@ class SharedKeyPacket : public Packet { #if 0 - private byte[] keybytes = new uint8_t[]{}; - private byte[] nonce = new uint8_t[]{}; + private uint8_t[] keybytes = new uint8_t[]{}; + private uint8_t[] nonce = new uint8_t[]{}; private SecretKey secretKey; @@ -13,7 +13,7 @@ class SharedKeyPacket : public Packet { // Needed } - public SharedKeyPacket(final SecretKey secretKey, final PublicKey publicKey, final byte[] nonce) { + public SharedKeyPacket(final SecretKey secretKey, final PublicKey publicKey, final uint8_t[] nonce) { this.secretKey = secretKey; this.keybytes = Crypt.encryptUsingKey(publicKey, secretKey.getEncoded()); this.nonce = Crypt.encryptUsingKey(publicKey, nonce); @@ -52,7 +52,7 @@ class SharedKeyPacket : public Packet { return getSecretKey(null); } - public byte[] getNonce(PrivateKey privateKey) { + public uint8_t[] getNonce(PrivateKey privateKey) { if (privateKey == null) { return nonce; } diff --git a/Minecraft.World/Network/Packets/TileDestructionPacket.cpp b/Minecraft.World/Network/Packets/TileDestructionPacket.cpp index 859cc031b..81c07581b 100644 --- a/Minecraft.World/Network/Packets/TileDestructionPacket.cpp +++ b/Minecraft.World/Network/Packets/TileDestructionPacket.cpp @@ -24,7 +24,7 @@ void TileDestructionPacket::read(DataInputStream* dis) { x = dis->readInt(); y = dis->readInt(); z = dis->readInt(); - state = dis->read(); + state = dis->readUnsignedByte(); } void TileDestructionPacket::write(DataOutputStream* dos) { diff --git a/Minecraft.World/Network/Packets/TileEditorOpenPacket.cpp b/Minecraft.World/Network/Packets/TileEditorOpenPacket.cpp new file mode 100644 index 000000000..049d37224 --- /dev/null +++ b/Minecraft.World/Network/Packets/TileEditorOpenPacket.cpp @@ -0,0 +1,37 @@ +#include "../../Platform/stdafx.h" + +#include "PacketListener.h" +#include "TileEditorOpenPacket.h" + +TileEditorOpenPacket::TileEditorOpenPacket() { + editorType = 0; + x = y = z = 0; +} + +TileEditorOpenPacket::TileEditorOpenPacket(int editorType, int x, int y, + int z) { + this->editorType = editorType; + this->x = x; + this->y = y; + this->z = z; +} + +void TileEditorOpenPacket::handle(PacketListener* listener) { + listener->handleTileEditorOpen(shared_from_this()); +} + +void TileEditorOpenPacket::read(DataInputStream* dis) { + this->editorType = dis->readByte(); + this->x = dis->readInt(); + this->y = dis->readInt(); + this->z = dis->readInt(); +} + +void TileEditorOpenPacket::write(DataOutputStream* dos) { + dos->writeByte(editorType); + dos->writeInt(x); + dos->writeInt(y); + dos->writeInt(z); +} + +int TileEditorOpenPacket::getEstimatedSize() { return 1 + 3 * 4; } \ No newline at end of file diff --git a/Minecraft.World/Network/Packets/TileEditorOpenPacket.h b/Minecraft.World/Network/Packets/TileEditorOpenPacket.h new file mode 100644 index 000000000..cf8f39239 --- /dev/null +++ b/Minecraft.World/Network/Packets/TileEditorOpenPacket.h @@ -0,0 +1,28 @@ +#pragma once + +#include "Packet.h" + +class TileEditorOpenPacket + : public Packet, + public std::enable_shared_from_this { +public: + static const int SIGN = 0; + static const int COMMAND_BLOCK = 1; + + int editorType; + int x, y, z; + + TileEditorOpenPacket(); + TileEditorOpenPacket(int editorType, int x, int y, int z); + + virtual void handle(PacketListener* listener); + virtual void read(DataInputStream* dis); + virtual void write(DataOutputStream* dos); + virtual int getEstimatedSize(); + +public: + static std::shared_ptr create() { + return std::shared_ptr(new TileEditorOpenPacket()); + } + virtual int getId() { return 133; } +}; \ No newline at end of file diff --git a/Minecraft.World/Network/Packets/TileEventPacket.cpp b/Minecraft.World/Network/Packets/TileEventPacket.cpp index 266535abf..f6f8a023b 100644 --- a/Minecraft.World/Network/Packets/TileEventPacket.cpp +++ b/Minecraft.World/Network/Packets/TileEventPacket.cpp @@ -29,8 +29,8 @@ void TileEventPacket::read(DataInputStream* dis) // throws IOException x = dis->readInt(); y = dis->readShort(); z = dis->readInt(); - b0 = dis->read(); - b1 = dis->read(); + b0 = dis->readUnsignedByte(); + b1 = dis->readUnsignedByte(); tile = dis->readShort() & Tile::TILE_NUM_MASK; } diff --git a/Minecraft.World/Network/Packets/TileUpdatePacket.cpp b/Minecraft.World/Network/Packets/TileUpdatePacket.cpp index b60058ebc..e6a83d150 100644 --- a/Minecraft.World/Network/Packets/TileUpdatePacket.cpp +++ b/Minecraft.World/Network/Packets/TileUpdatePacket.cpp @@ -24,7 +24,7 @@ void TileUpdatePacket::read(DataInputStream* dis) // throws IOException { #ifdef _LARGE_WORLDS x = dis->readInt(); - y = dis->read(); + y = dis->readUnsignedByte(); z = dis->readInt(); block = (int)dis->readShort() & 0xffff; diff --git a/Minecraft.World/Network/Packets/UpdateAttributesPacket.cpp b/Minecraft.World/Network/Packets/UpdateAttributesPacket.cpp new file mode 100644 index 000000000..ce74bc798 --- /dev/null +++ b/Minecraft.World/Network/Packets/UpdateAttributesPacket.cpp @@ -0,0 +1,123 @@ +#include "../../Platform/stdafx.h" + +#include "../../Headers/net.minecraft.world.entity.ai.attributes.h" +#include "PacketListener.h" +#include "UpdateAttributesPacket.h" + +UpdateAttributesPacket::UpdateAttributesPacket() { entityId = 0; } + +UpdateAttributesPacket::UpdateAttributesPacket( + int entityId, std::unordered_set* values) { + this->entityId = entityId; + + for (AUTO_VAR(it, values->begin()); it != values->end(); ++it) { + AttributeInstance* value = *it; + std::unordered_set mods; + value->getModifiers(mods); + attributes.insert(new AttributeSnapshot(value->getAttribute()->getId(), + value->getBaseValue(), &mods)); + } +} + +UpdateAttributesPacket::~UpdateAttributesPacket() { + // Delete modifiers - these are always copies, either on construction or on + // read + for (AUTO_VAR(it, attributes.begin()); it != attributes.end(); ++it) { + delete (*it); + } +} + +void UpdateAttributesPacket::read(DataInputStream* dis) { + entityId = dis->readInt(); + + int attributeCount = dis->readInt(); + for (int i = 0; i < attributeCount; i++) { + eATTRIBUTE_ID id = static_cast(dis->readShort()); + double base = dis->readDouble(); + std::unordered_set modifiers = + std::unordered_set(); + int modifierCount = dis->readShort(); + + for (int j = 0; j < modifierCount; j++) { + eMODIFIER_ID id = static_cast(dis->readInt()); + double amount = dis->readDouble(); + uint8_t operation = dis->readByte(); + modifiers.insert(new AttributeModifier( + id, /*L"Unknown synced attribute modifier",*/ amount, + operation)); + } + + attributes.insert(new AttributeSnapshot(id, base, &modifiers)); + + // modifiers is copied in AttributeSnapshot ctor so delete contents + for (AUTO_VAR(it, modifiers.begin()); it != modifiers.end(); ++it) { + delete *it; + } + } +} + +void UpdateAttributesPacket::write(DataOutputStream* dos) { + dos->writeInt(entityId); + dos->writeInt(attributes.size()); + + for (AUTO_VAR(it, attributes.begin()); it != attributes.end(); ++it) { + AttributeSnapshot* attribute = (*it); + + std::unordered_set* modifiers = + attribute->getModifiers(); + + dos->writeShort(attribute->getId()); + dos->writeDouble(attribute->getBase()); + dos->writeShort(modifiers->size()); + + for (AUTO_VAR(it2, modifiers->begin()); it2 != modifiers->end(); + ++it2) { + AttributeModifier* modifier = (*it2); + dos->writeInt(modifier->getId()); + dos->writeDouble(modifier->getAmount()); + dos->writeByte(modifier->getOperation()); + } + } +} + +void UpdateAttributesPacket::handle(PacketListener* listener) { + listener->handleUpdateAttributes(shared_from_this()); +} + +int UpdateAttributesPacket::getEstimatedSize() { + return 4 + 4 + attributes.size() * (8 + 8 + 8); +} + +int UpdateAttributesPacket::getEntityId() { return entityId; } + +std::unordered_set +UpdateAttributesPacket::getValues() { + return attributes; +} + +UpdateAttributesPacket::AttributeSnapshot::AttributeSnapshot( + eATTRIBUTE_ID id, double base, + std::unordered_set* modifiers) { + this->id = id; + this->base = base; + + for (AUTO_VAR(it, modifiers->begin()); it != modifiers->end(); ++it) { + this->modifiers.insert(new AttributeModifier( + (*it)->getId(), (*it)->getAmount(), (*it)->getOperation())); + } +} + +UpdateAttributesPacket::AttributeSnapshot::~AttributeSnapshot() { + for (AUTO_VAR(it, modifiers.begin()); it != modifiers.end(); ++it) { + delete (*it); + } +} + +eATTRIBUTE_ID UpdateAttributesPacket::AttributeSnapshot::getId() { return id; } + +double UpdateAttributesPacket::AttributeSnapshot::getBase() { return base; } + +std::unordered_set* +UpdateAttributesPacket::AttributeSnapshot::getModifiers() { + return &modifiers; +} \ No newline at end of file diff --git a/Minecraft.World/Network/Packets/UpdateAttributesPacket.h b/Minecraft.World/Network/Packets/UpdateAttributesPacket.h new file mode 100644 index 000000000..ba11a4211 --- /dev/null +++ b/Minecraft.World/Network/Packets/UpdateAttributesPacket.h @@ -0,0 +1,50 @@ +#pragma once + +#include "Packet.h" + +class AttributeModifier; +class AttributeInstance; + +class UpdateAttributesPacket + : public Packet, + public std::enable_shared_from_this { +public: + class AttributeSnapshot { + private: + eATTRIBUTE_ID id; + double base; + std::unordered_set modifiers; + + public: + AttributeSnapshot(eATTRIBUTE_ID id, double base, + std::unordered_set* modifiers); + ~AttributeSnapshot(); + + eATTRIBUTE_ID getId(); + double getBase(); + std::unordered_set* getModifiers(); + }; + +private: + int entityId; + std::unordered_set attributes; + +public: + UpdateAttributesPacket(); + UpdateAttributesPacket(int entityId, + std::unordered_set* values); + ~UpdateAttributesPacket(); + + void read(DataInputStream* dis); + void write(DataOutputStream* dos); + void handle(PacketListener* listener); + int getEstimatedSize(); + int getEntityId(); + std::unordered_set getValues(); + +public: + static std::shared_ptr create() { + return std::shared_ptr(new UpdateAttributesPacket()); + } + virtual int getId() { return 44; } +}; \ No newline at end of file diff --git a/Minecraft.World/Network/Packets/UpdateMobEffectPacket.cpp b/Minecraft.World/Network/Packets/UpdateMobEffectPacket.cpp index 894ddda09..b7590c390 100644 --- a/Minecraft.World/Network/Packets/UpdateMobEffectPacket.cpp +++ b/Minecraft.World/Network/Packets/UpdateMobEffectPacket.cpp @@ -2,21 +2,28 @@ #include "../../Headers/net.minecraft.world.effect.h" #include "../../IO/Streams/InputOutputStream.h" #include "PacketListener.h" +#include "../../WorldGen/Features/BasicTreeFeature.h" +#include "../../Util/BasicTypeContainers.h" #include "UpdateMobEffectPacket.h" UpdateMobEffectPacket::UpdateMobEffectPacket() { - this->entityId = 0; - this->effectId = 0; - this->effectAmplifier = 0; - this->effectDurationTicks = 0; + entityId = 0; + effectId = 0; + effectAmplifier = 0; + effectDurationTicks = 0; } UpdateMobEffectPacket::UpdateMobEffectPacket(int entityId, MobEffectInstance* effect) { this->entityId = entityId; - this->effectId = static_cast(effect->getId() & 0xff); - this->effectAmplifier = (char)(effect->getAmplifier() & 0xff); - this->effectDurationTicks = (short)effect->getDuration(); + effectId = (BYTE)(effect->getId() & 0xff); + effectAmplifier = (char)(effect->getAmplifier() & 0xff); + + if (effect->getDuration() > Short::MAX_VALUE) { + effectDurationTicks = Short::MAX_VALUE; + } else { + effectDurationTicks = (short)effect->getDuration(); + } } void UpdateMobEffectPacket::read(DataInputStream* dis) { @@ -33,6 +40,10 @@ void UpdateMobEffectPacket::write(DataOutputStream* dos) { dos->writeShort(effectDurationTicks); } +bool UpdateMobEffectPacket::isSuperLongDuration() { + return effectDurationTicks == Short::MAX_VALUE; +} + void UpdateMobEffectPacket::handle(PacketListener* listener) { listener->handleUpdateMobEffect(shared_from_this()); } @@ -45,4 +56,4 @@ bool UpdateMobEffectPacket::isInvalidatedBy(std::shared_ptr packet) { std::shared_ptr target = std::dynamic_pointer_cast(packet); return target->entityId == entityId && target->effectId == effectId; -} +} \ No newline at end of file diff --git a/Minecraft.World/Network/Packets/UpdateMobEffectPacket.h b/Minecraft.World/Network/Packets/UpdateMobEffectPacket.h index b732ba1af..326fcd931 100644 --- a/Minecraft.World/Network/Packets/UpdateMobEffectPacket.h +++ b/Minecraft.World/Network/Packets/UpdateMobEffectPacket.h @@ -19,6 +19,7 @@ public: virtual void read(DataInputStream* dis); virtual void write(DataOutputStream* dos); + virtual bool isSuperLongDuration(); virtual void handle(PacketListener* listener); virtual int getEstimatedSize(); virtual bool canBeInvalidated(); @@ -29,4 +30,4 @@ public: return std::shared_ptr(new UpdateMobEffectPacket()); } virtual int getId() { return 41; } -}; +}; \ No newline at end of file diff --git a/Minecraft.World/Network/Packets/UseItemPacket.cpp b/Minecraft.World/Network/Packets/UseItemPacket.cpp index 2a527fd67..de3abc36e 100644 --- a/Minecraft.World/Network/Packets/UseItemPacket.cpp +++ b/Minecraft.World/Network/Packets/UseItemPacket.cpp @@ -38,13 +38,13 @@ UseItemPacket::UseItemPacket(int x, int y, int z, int face, void UseItemPacket::read(DataInputStream* dis) // throws IOException { x = dis->readInt(); - y = dis->read(); + y = dis->readUnsignedByte(); z = dis->readInt(); face = dis->read(); item = readItem(dis); - clickX = dis->read() / CLICK_ACCURACY; - clickY = dis->read() / CLICK_ACCURACY; - clickZ = dis->read() / CLICK_ACCURACY; + clickX = dis->readUnsignedByte() / CLICK_ACCURACY; + clickY = dis->readUnsignedByte() / CLICK_ACCURACY; + clickZ = dis->readUnsignedByte() / CLICK_ACCURACY; } void UseItemPacket::write(DataOutputStream* dos) // throws IOException