示例#1
0
func (this *PacketServerHandshakeCodec) Encode(writer io.Writer, util []byte, encode packet.Packet) (err error) {
	packetServerHandshake := encode.(*PacketServerHandshake)
	err = packet.WriteVarInt(writer, util, packetServerHandshake.ProtocolVersion)
	if err != nil {
		return
	}
	err = packet.WriteString(writer, util, packetServerHandshake.ServerAddress)
	if err != nil {
		return
	}
	err = packet.WriteUint16(writer, util, packetServerHandshake.ServerPort)
	if err != nil {
		return
	}
	err = packet.WriteVarInt(writer, util, packetServerHandshake.State)
	return
}
func (this *packetServerLoginEncryptResponseCodec) Encode(writer io.Writer, util []byte, encode packet.Packet) (err error) {
	packetServerLoginEncryptResponse := encode.(*PacketServerLoginEncryptResponse)
	err = packet.WriteVarInt(writer, util, len(packetServerLoginEncryptResponse.SharedSecret))
	if err != nil {
		return
	}
	_, err = writer.Write(packetServerLoginEncryptResponse.SharedSecret)
	if err != nil {
		return
	}
	err = packet.WriteVarInt(writer, util, len(packetServerLoginEncryptResponse.VerifyToken))
	if err != nil {
		return
	}
	_, err = writer.Write(packetServerLoginEncryptResponse.VerifyToken)
	return
}
func (this *packetClientLoginEncryptRequestCodec) Encode(writer io.Writer, encode packet.Packet) (err error) {
	packetClientLoginEncryptRequest := encode.(*PacketClientLoginEncryptRequest)
	err = packet.WriteString(writer, packetClientLoginEncryptRequest.ServerId)
	if err != nil {
		return
	}
	err = packet.WriteVarInt(writer, len(packetClientLoginEncryptRequest.PublicKey))
	if err != nil {
		return
	}
	_, err = writer.Write(packetClientLoginEncryptRequest.PublicKey)
	if err != nil {
		return
	}
	err = packet.WriteVarInt(writer, len(packetClientLoginEncryptRequest.VerifyToken))
	if err != nil {
		return
	}
	_, err = writer.Write(packetClientLoginEncryptRequest.VerifyToken)
	return
}
func (this *packetServerPluginMessageCodec) Encode(writer io.Writer, util []byte, encode packet.Packet) (err error) {
	packetServerPluginMessage := encode.(*PacketServerPluginMessage)
	err = packet.WriteString(writer, util, packetServerPluginMessage.Channel)
	if err != nil {
		return
	}
	err = packet.WriteVarInt(writer, util, len(packetServerPluginMessage.Data))
	if err != nil {
		return
	}
	_, err = writer.Write(packetServerPluginMessage.Data)
	return
}
func (this *PacketGeneric) swapEntitiesVarInt(a int32, b int32, clientServer bool, protocol17 bool) {
	var positions []bool
	if clientServer {
		if protocol17 {
			positions = PlayPacketClientEntityVarIntPositions17
		} else {
			positions = PlayPacketClientEntityVarIntPositions
		}
	} else {
		if protocol17 {
			positions = PlayPacketServerEntityVarIntPositions17
		} else {
			positions = PlayPacketServerEntityVarIntPositions
		}
	}
	if this.id < 0 {
		return
	}
	if this.id >= len(positions) {
		return
	}
	if positions[this.id] == false {
		return
	}
	this.Decompress()
	// Read the old Id
	buffer := bytes.NewBuffer(this.Bytes)
	bufferUtil := make([]byte, packet.UTIL_BUFFER_LENGTH)
	id, err := packet.ReadVarInt(buffer, bufferUtil)
	if err != nil {
		return
	}
	// Check the Id
	var newId int
	if id == int(a) {
		newId = int(b)
	} else if id == int(b) {
		newId = int(a)
	} else {
		return
	}
	// Apply the new Id
	newBuffer := new(bytes.Buffer)
	err = packet.WriteVarInt(newBuffer, bufferUtil, newId)
	if err != nil {
		return
	}
	buffer.WriteTo(newBuffer)
	this.Bytes = newBuffer.Bytes()
}
func (this *packetClientTeamsCodec) Encode(writer io.Writer, util []byte, encode packet.Packet) (err error) {
	packetClientTeams := encode.(*PacketClientTeams)
	err = packet.WriteString(writer, util, packetClientTeams.Name)
	if err != nil {
		return
	}
	err = packet.WriteInt8(writer, util, packetClientTeams.Action)
	if packetClientTeams.Action == 0 || packetClientTeams.Action == 2 {
		if err != nil {
			return
		}
		err = packet.WriteString(writer, util, packetClientTeams.DisplayName)
		if err != nil {
			return
		}
		err = packet.WriteString(writer, util, packetClientTeams.Prefix)
		if err != nil {
			return
		}
		err = packet.WriteString(writer, util, packetClientTeams.Suffix)
		if err != nil {
			return
		}
		err = packet.WriteInt8(writer, util, packetClientTeams.FriendlyFire)
		if err != nil {
			return
		}
		err = packet.WriteString(writer, util, packetClientTeams.NameTagVisibility)
		if err != nil {
			return
		}
		err = packet.WriteInt8(writer, util, packetClientTeams.Color)
	}
	if packetClientTeams.Action == 0 || packetClientTeams.Action == 3 || packetClientTeams.Action == 4 {
		if err != nil {
			return
		}
		err = packet.WriteVarInt(writer, util, len(packetClientTeams.Players))
		for i := 0; i < len(packetClientTeams.Players); i++ {
			if err != nil {
				return
			}
			err = packet.WriteString(writer, util, packetClientTeams.Players[i])
		}
	}
	return
}
func (this *packetClientSetCompressionCodec) Encode(writer io.Writer, encode packet.Packet) (err error) {
	packetClientSetCompression := encode.(*PacketClientSetCompression)
	err = packet.WriteVarInt(writer, packetClientSetCompression.Threshold)
	return
}
func (this *packetClientPlayerListCodec) Encode(writer io.Writer, encode packet.Packet) (err error) {
	packetClientPlayerList := encode.(*PacketClientPlayerList)
	err = packet.WriteVarInt(writer, packetClientPlayerList.Action)
	if err != nil {
		return
	}
	err = packet.WriteVarInt(writer, len(packetClientPlayerList.Items))
	if err != nil {
		return
	}
	for _, item := range packetClientPlayerList.Items {
		err = packet.WriteUUID(writer, item.UUID)
		if err != nil {
			return
		}
		switch packetClientPlayerList.Action {
		case PACKET_CLIENT_PLAYER_LIST_ACTION_ADD:
			addPlayer := item.Info.(PacketClientPlayerListAddPlayer)
			err = packet.WriteString(writer, addPlayer.Name)
			if err != nil {
				return
			}
			err = packet.WriteVarInt(writer, len(addPlayer.Properties))
			if err != nil {
				return
			}
			for _, property := range addPlayer.Properties {
				err = packet.WriteString(writer, property.Name)
				if err != nil {
					return
				}
				err = packet.WriteString(writer, property.Value)
				if err != nil {
					return
				}
				if property.Signature == "" {
					err = packet.WriteBool(writer, false)
					if err != nil {
						return
					}
				} else {
					err = packet.WriteBool(writer, true)
					if err != nil {
						return
					}
					err = packet.WriteString(writer, property.Signature)
					if err != nil {
						return
					}
				}
			}
			err = packet.WriteVarInt(writer, addPlayer.Gamemode)
			if err != nil {
				return
			}
			err = packet.WriteVarInt(writer, addPlayer.Latency)
			if err != nil {
				return
			}
			if addPlayer.DisplayName == "" {
				err = packet.WriteBool(writer, false)
				if err != nil {
					return
				}
			} else {
				err = packet.WriteBool(writer, true)
				if err != nil {
					return
				}
				err = packet.WriteString(writer, addPlayer.DisplayName)
				if err != nil {
					return
				}
			}
		case PACKET_CLIENT_PLAYER_LIST_ACTION_UPDATE_GAMEMODE:
			updateGamemode := item.Info.(PacketClientPlayerListUpdateGamemode)
			err = packet.WriteVarInt(writer, updateGamemode.Gamemode)
			if err != nil {
				return
			}
		case PACKET_CLIENT_PLAYER_LIST_ACTION_UPDATE_LATENCY:
			updateLatency := item.Info.(PacketClientPlayerListUpdateLatency)
			err = packet.WriteVarInt(writer, updateLatency.Latency)
			if err != nil {
				return
			}
		case PACKET_CLIENT_PLAYER_LIST_ACTION_UPDATE_DISPLAY_NAME:
			updateDisplayName := item.Info.(PacketClientPlayerListUpdateDisplayName)
			if updateDisplayName.DisplayName == "" {
				err = packet.WriteBool(writer, false)
				if err != nil {
					return
				}
			} else {
				err = packet.WriteBool(writer, true)
				if err != nil {
					return
				}
				err = packet.WriteString(writer, updateDisplayName.DisplayName)
				if err != nil {
					return
				}
			}
		case PACKET_CLIENT_PLAYER_LIST_ACTION_REMOVE:
			// no payload
		default:
			err = errors.New(fmt.Sprintf("Encode, PacketClientPlayerList action is not valid: %d", packetClientPlayerList.Action))
		}
	}
	return
}