the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
at main 105 lines 2.7 kB view raw
1#include "stdafx.h" 2#include <iostream> 3#include "net.minecraft.h" 4#include "net.minecraft.world.entity.player.h" 5#include "PacketListener.h" 6#include "ChatPacket.h" 7 8// longest allowed string is "<" + name + "> " + message 9const unsigned int ChatPacket::MAX_LENGTH = SharedConstants::maxChatLength + Player::MAX_NAME_LENGTH + 3; 10 11ChatPacket::ChatPacket() 12{ 13 m_messageType = e_ChatCustom; 14} 15 16ChatPacket::ChatPacket(const wstring& message, EChatPacketMessage type /*= e_ChatCustom*/, int customData /*= -1*/) 17{ 18 m_messageType = type; 19 if (customData != -1) m_intArgs.push_back(customData); 20 21 m_stringArgs.push_back(message); 22} 23 24ChatPacket::ChatPacket(const wstring& message, EChatPacketMessage type, int sourceEntityType, const wstring& sourceName) 25{ 26 m_messageType = type; 27 if (sourceEntityType != -1) m_intArgs.push_back(sourceEntityType); 28 29 m_stringArgs.push_back(message); 30 m_stringArgs.push_back(sourceName); 31} 32 33ChatPacket::ChatPacket(const wstring& message, EChatPacketMessage type, int sourceEntityType, const wstring& sourceName, const wstring& itemName) 34{ 35 m_messageType = type; 36 if (sourceEntityType != -1) m_intArgs.push_back(sourceEntityType); 37 38 m_stringArgs.push_back(message); 39 m_stringArgs.push_back(sourceName); 40 m_stringArgs.push_back(itemName); 41} 42 43// Read chat packet (throws IOException) 44void ChatPacket::read(DataInputStream *dis) 45{ 46 m_messageType = (EChatPacketMessage) dis->readShort(); 47 48 short packedCounts = dis->readShort(); 49 int stringCount = (packedCounts >> 4) & 0xF; 50 int intCount = (packedCounts >> 0) & 0xF; 51 52 for(int i = 0; i < stringCount; i++) 53 { 54 m_stringArgs.push_back(readUtf(dis, MAX_LENGTH)); 55 } 56 57 for(int i = 0; i < intCount; i++) 58 { 59 m_intArgs.push_back(dis->readInt()); 60 } 61} 62 63// Write chat packet (throws IOException) 64void ChatPacket::write(DataOutputStream *dos) 65{ 66 dos->writeShort(m_messageType); 67 68 short packedCounts = 0; 69 packedCounts |= (m_stringArgs.size() & 0xF) << 4; 70 packedCounts |= (m_intArgs.size() & 0xF) << 0; 71 72 dos->writeShort(packedCounts); 73 74 for(int i = 0; i < m_stringArgs.size(); i++) 75 { 76 writeUtf(m_stringArgs[i], dos); 77 } 78 79 for(int i = 0; i < m_intArgs.size(); i++) 80 { 81 dos->writeInt(m_intArgs[i]); 82 } 83} 84 85// Handle chat packet 86void ChatPacket::handle(PacketListener *listener) 87{ 88 listener->handleChat(shared_from_this()); 89} 90 91// Get an estimated size of the packet 92int ChatPacket::getEstimatedSize() 93{ 94 int stringsSize = 0; 95 for(int i = 0; i < m_stringArgs.size(); i++) 96 { 97 stringsSize += m_stringArgs[i].length(); 98 } 99 100 return 101 sizeof(EChatPacketMessage) + // message type 102 sizeof(short) + // packed arg counts 103 stringsSize + // string args 104 (m_intArgs.size() * sizeof(int)); // int args 105}